Java Tricky Output Questions

16 Aug, 2019
Question 1: What will be the Output of the below code:
public class A {    public static void main(String[] args)    {        if (true)            break;    }}
Choices:
a) Nothing
b) Error
Answer: b) ErrorReason: Break statement can only be used with loop or switch. So, using break with if statement causes “break outside switch or loop” error.
 
Question 2: What will be the Output of the below code:
public class A {    public static void main(String[] args)    {        System.out.println('j' + 'a' + 'v' + 'a');    }}
Choices:
a) java
b) Something else (Other than simple concatenation)
Answer: b) Something else (Other than simple concatenation)Reason: “java” would be printed if String literals (in double quotes) are used, but in the question since character literals has been used, these won’t be concatenated. Therefore After execution of the program, an addition of each equivalent ASCII(Unicode) value of the character will be obtained.Hence the output is 106 + 97 + 118 + 97 = 418
 
Question 3: What will be the Output of the below code:
public class A {    public static void main(String[] args)    {        int $_ = 5;    }}
Choices:
a) Nothing
b) Error
Answer: a) NothingReason: It looks like $ will cause an error, but it won’t. In java, identifier rule says, identifier can start with any alphabet or underscore (“_”) or dollar (“$”). So answer is Nothing.
 
Question 4: What will be the Output of the below code:
public class Demo{    public static void main(String[] arr){        Integer num1 = 100;        Integer num2 = 100;        Integer num3 = 500;        Integer num4 = 500;                  if(num1==num2){            System.out.println("num1 == num2");        }        else{            System.out.println("num1 != num2");        }        if(num3 == num4){            System.out.println("num3 == num4");        }        else{            System.out.println("num3 != num4");        }    }}
Choices:
a) num1 == num2    num3 == num4
b) num1 == num2    num3 != num4
c) num1 != num2    num3 == num4
d) num1 != num2    num3 != num4
Answer: b)num1 == num2                  num3 != num4Reason: We always thought that whenever two object references are compared using “==”, it always evaluates to “false”. But here Integer caching changes the results.Integer class has a caching range of -128 to 127. Whenever a number is between this range and autoboxing is used, it assigns the same reference. That’s why for value 100, both num1 and num2 will have the same reference, but for the value 500 (not in the range of -128 to 127), num3 and num4 will have different reference.
 
Question 5: What will be the Output of the below code:
public class Demo{    public static void main(String[] arr){              }    public static void main(String arr){              }}
Choices:
a) Nothing
b) Error
Answer: a) NothingReason: We can overload main() too. But JVM will always call main() that has String[] argument.
More Java Output Questions
Akanksha_Rai
Vishnu_Radhanpura
Java-Output
Technical Scripter 2018
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
ArrayList in Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Set in Java
Stack Class in Java
Initialize an ArrayList in Java
Introduction to Java