Output of Java Programs | Set 21 (Type Conversions) - GeeksforGeeks

03 Jun, 2017
Prerequisite – Type Conversions in Java with Examples
1) What is the output of the following program?
public class Test{    public static void main(String[] args)    {        int value = 554;        String var = (String)value;  //line 1        String temp = "123";        int data = (int)temp; //line 2        System.out.println(data + var);    }}
a) 677b) Compilation error due to line 1c) Compilation error due to line 2d) Compilation error due to line 1 and line 2
Ans. (d)Explanation: Converting from int to String as well as converting from String to int is not allowed in java. 2) What is the output of the following program?
public class Test{    public static void main(String[] args)    {        double data = 444.324;        int value = data;        System.out.println(data);    }}
a) 444.324b) 444c) Runtime errord) Compilation error
Ans. (d)Explanation: Converting from a bigger data type to a smaller data type is not allowed in java as it is a lossy conversion. 
3) What is the output of the following program?
public class Test{    public static void main(String[] args)    {        double data = 444.324;        int sum = 9;        float value = 5.1f;        System.out.println(data + sum + value);              }}
a) 444.32495.1b) 456c) 458.42399d) 458.4
Ans. (c)Explanation: If one of the operands is long, double or float, the entire expression is converted to long, double or float respectively. 
4) What is the output of the following program?
public class Test{    public static void main(String[] args)    {        byte var = 1;        var = (byte) var * 0; //line 1        byte data = (byte) (var * 0); //line 2        System.out.println(var);            }}
a) 0b) Compilation error due to line 1c) Compilation error due to line 2d) Compilation error due to line 1 and line 2
Ans. (b)Explanation: When the expressions are evaluated, the data type of the result is implicitly changed to a larger data type and therefore, explicit recasting has to be done as shown in line 2. On the other hand, line 1 shows compilation error because the expression on the right side has data type as int whereas left side it is byte. 
5) What is the output of the following program?
public class Test{    public static void main(String[] args)    {        System.out.println((100/25.0)*Integer.parseInt("5") + 50);    }}
a) Compilation errorb) 70c) 70.0d) Runtime error
Ans. (c)Explanation: If a double value is used in an expression then the output is returned in double format rather than an int.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of C++ programs | Set 34 (File Handling)
Different ways to copy a string in C/C++
Output of Java program | Set 28
Runtime Errors
Output of C++ Program | Set 1
Output of Java Programs | Set 48 (Static keyword)
How to show full column content in a PySpark Dataframe ?
Output of Java program | Set 26
Output of python program | Set 2
Output of C Programs | Set 3