Menu
Topics Index
...
`

Conversions


If you need explanation Read this topic

If you need Answer Take test on this topic

Conversions
What will be the output of the following program?
public class StringToNumbers {
    public static void main(String[] args) {
        String input = "123";
        int i = Integer.parseInt(input);
        Short s = Short.parseShort(input);
        float f = Float.parseFloat(input);
        long l = Long.parseLong(input);
        System.out.println(i + s + f + l);
    }
}

A. 492.0
B. 123123123.0123
C. Compilation Error
D. Runtime Error
Topic: Converting Numbers to and from Strings In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans A

Posted by Dereje Goshu    2014-11-26 09:30:01


Ans is A as input variable  value 123 is converted and stored into their respective data variables and all the variables are added and  printed the sum value

Posted by Bharath Yelchuri    2014-11-26 09:40:50


Ans: A  -here that addition will be done between the converted number formate.ie. 123+123+123+123= 492

Posted by Sapparapu Pradeep Kumar    2014-11-26 09:46:36


ans is A string is converted to a number
valueOf returns the instance or type of java.lang.integer which is used to convert into number
parseInt parses the given string into  some primitive data format,it can be integer,float or any other.
here string is converted into an number X(say X) i.e X+X+X+X=X'but trick here is we have float type which makes 492.0,,,

Posted by Sai Veerendra    2014-11-26 12:17:01


ans a.                                                                                                                              in this program string objct is initialized with "123".                                                          parsing methods are use to extract or convert  primitive values from object.                  the Integer ,Short,float , and
Long classes also provide the static methods Integer.parseInt() ,Short.parseShort(),Float.parseFloat() and Long.parseLong()
that read a String and return the appropriate type.in this program i=123,s=123,f=123.0,l=123L,                                                                                          sop prints addition of all these value i+s+f+l=492.0 type is implicitily converted into float.                                                                                                   if  the String does not contain a valid representation of a int,short,float,long values thn this type of conversion throws NumberFormatException.                       A NumberFormatException is thrown to indicate that an attempt to parse numeric information in a string.so that type of situation we need to handle try catch mechanism

Posted by Maheshwari Natarajan    2014-11-26 18:11:01


This dose is now closed and the winners are Sai Veerendra,  for 'First Correct Comment', Sai Ram,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-11-27 08:12:33


© meritcampus 2019

All Rights Reserved.

Open In App