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) Error
Reason: 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 Ouput 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 Ouput of the below code:
public class A { public static void main(String[] args) { int $_ = 5; } } |
Choices:
- a) Nothing
- b) Error
Answer: a) Nothing
Reason: 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.
Recommended Posts:
- Few Tricky Programs in Java
- Interview Questions for Java Professionals
- Java Interview Questions on Constructors
- 10 Most asked Questions from Java Programmers
- Commonly Asked Java Programming Interview Questions | Set 2
- Commonly Asked Java Programming Interview Questions | Set 1
- Frequently asked questions for Java Developer Position from a fresher
- Output of Java programs | Set 29
- Output of Java Programs | Set 37 (If-else)
- Output of Java Program | Set 4
- Output of Java Program | Set 19
- Output of Java Programs | Set 50
- Output of Java Program | Set 2
- Output of Java program | Set 17
- Output of Java program | Set 5
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



