What is the output of:
System.out.println(2 + 3 * 4);
A
20 B
14 C
24 D
9
Analysis & Theory
Multiplication has higher precedence, so 3 * 4 = 12, then 2 + 12 = 14.
What is the output of:
System.out.println("Java" + 10 + 20);
A
Java30 B
Java1020 C
30Java D
Java 30
Analysis & Theory
Java + 10 becomes "Java10", then "Java10" + 20 = "Java1020".
What is the output of:
System.out.println(10 + 20 + "Java");
A
Java30 B
1020Java C
30Java D
Java1020
Analysis & Theory
10 + 20 = 30, then 30 + "Java" = "30Java".
What is the output of:
System.out.println("Hello\nWorld");
A
HelloWorld B
Hello World C
Hello
World D
Hello\nWorld
Analysis & Theory
`\n` creates a new line. So output will be on two lines: Hello (newline) World.
What is the output of:
System.out.println('A' + 1);
A
B B
66 C
A1 D
Error
Analysis & Theory
'A' is a char with ASCII value 65. 65 + 1 = 66.
What is the output of:
System.out.println("5" + 3);
A
8 B
53 C
35 D
Error
Analysis & Theory
String + int results in string concatenation: "5" + 3 = "53".
What is the output of:
System.out.println(true + "Java");
A
trueJava B
Javatrue C
Error D
1Java
Analysis & Theory
true + "Java" is valid and results in the string "trueJava".
What is the output of:
System.out.println(10 / 3);
A
3.333 B
3 C
3.0 D
3.33
Analysis & Theory
Both operands are integers, so integer division occurs. Result: 3.
What is the output of:
System.out.println(10.0 / 4);
A
2.5 B
2 C
2.0 D
Error
Analysis & Theory
10.0 is a double, so floating-point division: 10.0 / 4 = 2.5.
What is the output of:
System.out.println((int)5.9);
A
5.9 B
6 C
5 D
Error
Analysis & Theory
Casting a double to int truncates the decimal part. Result: 5.