
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Tricky Output Questions
Java Tricky Output Questions that are difficult to answer call for more work to be put into them. We will fall short if we attempt to respond to a challenging topic using common sense since such questions need specialized understanding. The majority of challenging java problems are based on perplexing ideas like loops, multi-threading, overloading, overriding, etc.
Even when a question isn't particularly challenging, we occasionally have trouble solving it. Despite the fact that the answer to the question is straightforward, we occasionally write code carelessly.
Even if we don't know the solution, we can still use analytical thinking to get the answer. We will discuss some challenging Java tricky output questions in this article.
Commonly Asked Java Tricky Output Questions
Question
Consider the following output and try to guess the answer
public class A { public static void main(String args[]) { //\u000d System.out.println("hello"); } }
Hello
hello
HELLO
Error
\u000d
Answer
The output will be "hello". The Unicode character \u000d is a carriage return and acts as a new line character. Thus, the commented line is interpreted as a new line of code.
Question
Try to guess the answer of the following output
String s1 = "Java"; String s2 = "Java"; StringBuilder sb1 = new StringBuilder(); sb1.append("Ja").append("va"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); System.out.println(sb1.toString() == s1); System.out.println(sb1.toString().equals(s1));
true is printed out exactly once.
true is printed out exactly twice.
true printed out exactly thrice.
true printed out exactly four times.
Error
Answer
The output will be "true printed out exactly four times". The == operator compares if the two references point to the exact same object, while the equals() method checks if the contents of the two objects are equivalent. String literals are interned in Java, so s1 and s2 refer to the same object. StringBuilder generates a new String object, so sb1.toString() != s1, but its contents are the same as s1.
Question
Answer the following question
public class Demo { public static void main(String args[]) { System.out.print("a"); try { System.out.print("b"); throw new IllegalArgumentException(); } catch (RuntimeException e) { System.out.print("c"); } finally { System.out.print("d"); } System.out.print("e"); } }
abe
abce
abde
abcde
Uncaught exception thrown
Answer
The output will be "abcde". Even though an exception is thrown in the try block, the finally block will always be executed.
Question
Consider the following question to guess the answer:
public class Test { public static void main(String args[]) { int i=20+ +9- -12+ +4- -13+ +19; System.out.println(i); } }
27
33
66
76
77
Answer
The output will be "77". The unary plus and minus operators have higher precedence than the binary plus and minus operators, and unary plus doesn't change the sign of its operand.
Question
Consider the following code to answer
public class Actors { Public static void main(String arg[]) { char[] ca ={0x4e, \u004e, 78}; System.out.println((ca[0] = = ca[1]) + " "+ (ca[0] = = ca[2])); } }
false false
true true
false true
true false
Error
Answer
The output will be "true true". All three elements in the character array represent the same numeric value (78), so the comparisons are true.
Question
What will be the output of the following code
public class _C { private static int $; public static void main(String main[]) { String a_b; System.out.print($); System.out.print(a_b); } }
Compile error at line 4
Compile error at line 2
Compile error at line 1
Compile error at line 5
Compile error at line 8
Answer
The output will be "Compile error at line 5". Local variables need to be initialized before they are used.
Question
What will be the answer of the given code:
switch(x) { case x>70: System.out.println("True"); break; case 65<x<=70: System.out.println("False"); break; }
Assertion error
Stack overflow
Orphaned case
IO error
Output of memory error
Answer
The output will be "Orphaned case". Case labels in switch statements must be constant expressions (typically integers, enums, or string literals), not boolean expressions.
Question
Consider the following code to answer:
public class Demo { static int x=1111; static { x=x-- - --x; } { x=x++ + ++x; } public static void main(String args[]) { System.out.println(x); } }
2
1110
1109
1
11
Answer
The output will be "2". The static block is executed before the instance initializer block, and both blocks change the value of x.
Question
What will be the output of the following code:
import java.lang.Math; public class Example { public static void main(String args[]) { String computerMove; switch ( (int)(3*Math.random()) ) { case 0: computerMove = "Rock"; break; case 1: computerMove = "Scissors"; break; case 2: computerMove = "Paper"; break; } System.out.println("Computer's move is " + computerMove); } }
Paper
Rock
Scissors
Syntax error
Exception
Answer
The output could be "Paper", "Rock", or "Scissors". The switch statement randomly selects one of the three cases.
Question
Consider the following code to give the answer:
public class TestHashMap { public static void main(String[] args) { HashMap<String,String> map = new HashMap<String,String>(){ { put("1", "ONE"); } { put("2", "TWO"); } { put("3", "THREE"); } }; Set<String> keySet = map.keySet(); for (String string : keySet) { System.out.println(string+" ->"+map.get(string)); } } }
The use of double brace initialization is not allowed in Java
The first set of brace creates a new anonymous inner class, and the second set of brace creates an instance initialize like static block in class
The second set of brace creates a new anonymous inner class, and the first set of brace creates an instance initialize like static block in class
Niether first set of braces creates an anonymous inner class, nor the second set of brace creates an instance initialize like static block in class
None of the above
Answer
The correct answer is "The first set of brace creates a new anonymous inner class, and the second set of brace creates an instance initialize like static block in class". This pattern is known as double brace initialization.
Question
What will we get after compiling the following code?
public class ExceptionDemo { public static void main(String args[]) { Object x[] = new String[3]; x[0] = new Integer(0); } }
Negative Array Size Exception
Array store exception
Null pointer exception
Class cast exception
Array index out of bounds exception
Answer
The output will be "Array store exception". The runtime type of the array is String[], so it cannot hold Integer objects.
Question
Consider the situation
If we put System.exit(0) on try to catch block in such a case Will finally block execute or not? Also specify the reason
It skips the final block
It is an invalid situation
The final block may or may not be executed
JVM exit with SecurityException and final block execute
JVM exit without SecurityException and final block will not be executed
Answer
The correct answer is "JVM exit without SecurityException and final block will not be executed". System.exit(0) terminates the JVM immediately, so the finally block does not execute.
Question
Consider the following code to give the correct answer:
import java.util.Arrays; public class SplitString { public static void main(String args[]) { String str="Java|Python|Hadoop"; String[] array = str.split("\|"); System.out.println(Arrays.toString(array)); } }
[Java\Python\Hadoop]
[Java|Python|Hadoop]
[Java\|Python\|Hadoop]
[Java, Python, Hadoop]
Answer
The output will be "[Java, Python, Hadoop]". The split() method uses a regular expression to split the string, and | is a metacharacter that needs to be escaped.
Question
Evaluate the following statement:
long longWithL = 1000*60*60*24*365L; long longWithoutL = 1000*60*60*34*365;
Compile time error
Arithmetic exception
31536000000 and 1726327040
3.1536e10 and 1.72632704e9
31536000000L and 1726327040
Answer
The correct answer is "31536000000 and 1726327040". The L suffix makes the first number a long literal, so the computation doesn't overflow. The second number is computed as an int and overflows.
Question
Evaluate the following code and give the answer carefully:
class Base{ public static void main(String[] args){ System.out.println("Hello"); } } public class Test extends Base{}
Compile time error
Runtime error
Compiles and run with no output
Compiles and runs printing hello
Error at line 6
Answer
The output will be "Compiles and runs printing hello". The main method is inherited from the Base class and executed when the Test class is run.
Conclusion
It is crucial to familiarize yourself with some of the frequent inquiries. You might feel more assured when answering queries or developing test code by practicing. Although they may ask a variety of questions, you might prepare your answers in advance to brush up on your Java expertise.