Java MCQ

Previous Java Multiple Choice Questions Next

JAVA TRICKY input and output MCQ

1

What is the output of: System.out.println(10 + 20 + "Java");

Correct Answer: A) 30Java

Addition happens from left to right: 10+20=30, then 30+"Java"="30Java".

2

What is the output of: System.out.println("Java" + 10 + 20);

Correct Answer: B) Java1020

Once string concatenation starts, everything is treated as string: "Java"+"10"+"20"="Java1020".

3

What is the output of: System.out.println(10 + 20 + "Java" + 10 + 20);

Correct Answer: C) 30Java1020

10+20=30, 30+"Java"="30Java", "30Java"+10="30Java10", "30Java10"+20="30Java1020".

4

What is the output of: System.out.printf("%d %s", "Java", 10);

Correct Answer: C) IllegalFormatConversionException

%d expects integer but gets "Java" (string), causing runtime exception.

5

What is the output of: System.out.println("Java" + null);

Correct Answer: A) Javanull

String concatenation converts null to "null" string.

6

What is the output of: String s = null; System.out.println(s + "Java");

Correct Answer: A) nullJava

String concatenation handles null gracefully, converting it to "null" string.

7

What is the output of: System.out.println('A' + 'B');

Correct Answer: B) 131

char addition uses ASCII values: 'A'(65) + 'B'(66) = 131.

8

What is the output of: System.out.println("A" + "B");

Correct Answer: A) AB

String concatenation: "A" + "B" = "AB".

9

What is the output of: System.out.println(1 + 2 + "3" + 4 + 5);

Correct Answer: A) 3345

1+2=3, 3+"3"="33", "33"+4="334", "334"+5="3345".

10

What is the output of: System.out.println(String.valueOf(null));

Correct Answer: C) NullPointerException

String.valueOf(null) is ambiguous - it could match String.valueOf(char[]) or String.valueOf(Object). The compiler chooses char[] version, but null char array causes NPE.

Previous Java Multiple Choice Questions Next