Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.
Question 11Medium
What is the output of the following Java program?
Main.java
public class Main {
public static void main(String[] args) {
int i;
int total = 0;
for (i = 0; i < 5; i++);
{
total += i;
}
System.out.println(i + " " + total);
}
}
A5 5
B5 10
C4 10
D5 0
Correct Answer: Option A
The semicolon is the loop body, so the loop just counts i to 5. The block after it runs once, adding 5.
public class Main {
static int weight(Object o) {
return switch (o) {
case Integer i when i < 0 -> 0;
case Integer i -> i;
case String s when s.length() > 3 -> 10;
case String s -> 1;
case int[] arr -> arr.length;
default -> -1;
};
}
public static void main(String[] args) {
Object[] data = {5, -3, "Java", "Go", new int[]{1, 2, 3}, 2.5};
int sum = 0;
for (Object o : data) sum += weight(o);
System.out.println(sum);
}
}
A18
B19
C15
D16
Correct Answer: Option A
5 + 0 + 10 + 1 + 3 + (-1) = 18. Guarded cases are listed before the unguarded case of the same type.
public class Main {
record Point(int x, int y) {}
static String where(Point p) {
return switch (p) {
case Point(int x, int y) when x == 0 && y == 0 -> "origin";
case Point(int x, int y) when x == 0 -> "y-axis";
case Point(int x, int y) when y == 0 -> "x-axis";
case Point(int x, int y) when x == y -> "diagonal";
case Point(int x, int y) -> "(" + x + "," + y + ")";
};
}
public static void main(String[] args) {
Point[] pts = {new Point(0, 0), new Point(0, 4), new Point(3, 3), new Point(2, 0), new Point(1, 5)};
for (Point p : pts) System.out.print(where(p) + " ");
System.out.println();
}
}
Aorigin y-axis diagonal x-axis (1,5)
Borigin x-axis diagonal y-axis (1,5)
Corigin y-axis (3,3) x-axis (1,5)
Dorigin origin diagonal x-axis (1,5)
Correct Answer: Option A
Record patterns bind the components directly; cases are tried top to bottom, so the order of guards matters.
public class Main {
public static void main(String[] args) {
int a = 5, b = -2;
String r = "start";
if (a > 0)
if (b > 0)
r = "both positive";
else
r = "a positive, b not"; // indented like the first if, but belongs to the second
System.out.println(r);
a = -1;
r = "start";
if (a > 0)
if (b > 0)
r = "both positive";
else
r = "a positive, b not";
System.out.println(r);
}
}
Aa positive, b not
start
Bstart
a positive, b not
Ca positive, b not
a positive, b not
Dboth positive
start
Correct Answer: Option A
The else belongs to if (b > 0). When a is negative, the outer if is false and nothing runs – the indentation lied.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {1, 2, 3};
for (int x : a) x *= 10;
System.out.print(Arrays.toString(a) + " ");
for (int i = 0; i < a.length; i++) a[i] *= 10;
System.out.println(Arrays.toString(a));
}
}
A[1, 2, 3] [10, 20, 30]
B[10, 20, 30] [100, 200, 300]
C[1, 2, 3] [1, 2, 3]
D[10, 20, 30] [10, 20, 30]
Correct Answer: Option A
The for-each variable is a copy of each element; only the index loop changes the array.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = i; j >= 1; j--) System.out.print(j);
System.out.println();
}
}
}
public class Main {
enum Size { S, M, L, XL }
public static void main(String[] args) {
int price = 0;
for (Size s : Size.values()) {
switch (s) {
case S:
case M:
price += 300;
break;
case L:
price += 100; // no break: L also gets the XL surcharge
case XL:
price += 400;
break;
}
}
System.out.println(price);
}
}
A1500
B1100
C1400
D1000
Correct Answer: Option A
S and M share one body (600). L adds 100 and falls into XL's 400 (500); XL adds 400. Total 1500.
public class Main {
public static void main(String[] args) {
int i = 100;
int found = -1;
for (int k = 0; k < 10; k++) {
if (k * k > 30) { found = k; break; }
}
for (i = 0; i < 3; i++) { }
// k is not visible here; i keeps its last value
System.out.println(found + " " + i);
}
}
A6 3
B5 3
C6 100
D6 2
Correct Answer: Option A
6 × 6 = 36 is the first square above 30. The second loop reuses the outer i, which ends at 3.
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·