LearnFrenzy
Sign in Take a free test

Control Flow Statements · Exercise 2 of 4

Control Flow Statements Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

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 11 Medium

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);
    }
}
A 5 5
B 5 10
C 4 10
D 5 0
Question page & discussion
Question 12 Medium

What is the output of the following Java program?

Main.java
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);
    }
}
A 18
B 19
C 15
D 16
Question page & discussion
Question 13 Hard

What is the output of the following Java program?

Main.java
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();
    }
}
A origin y-axis diagonal x-axis (1,5)
B origin x-axis diagonal y-axis (1,5)
C origin y-axis (3,3) x-axis (1,5)
D origin origin diagonal x-axis (1,5)
Question page & discussion
Question 14 Medium

What is the output of the following Java program?

Main.java
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);
    }
}
A a positive, b not start
B start a positive, b not
C a positive, b not a positive, b not
D both positive start
Question page & discussion
Question 15 Medium

What is the output of the following Java program?

Main.java
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]
Question page & discussion
Question 16 Easy

What is the output of the following Java program?

Main.java
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();
        }
    }
}
A 1 21 321 4321
B 1 12 123 1234
C 4321 321 21 1
D 1 22 333 4444
Question page & discussion
Question 17 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int pos = 0, neg = 0, zero = 0;
        for (int n : new int[]{3, -1, 0, 7, -5, 0, 2}) {
            if (n > 0) pos++;
            else if (n < 0) neg++;
            else zero++;
        }
        System.out.println(pos + " " + neg + " " + zero + " " + (pos > neg ? "more positive" : "not more positive"));
    }
}
A 3 2 2 more positive
B 3 2 2 not more positive
C 4 2 1 more positive
D 3 3 1 more positive
Question page & discussion
Question 18 Medium

What is the output of the following Java program?

Main.java
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);
    }
}
A 1500
B 1100
C 1400
D 1000
Question page & discussion
Question 19 Easy

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int n = 27, steps = 0, peak = n;
        while (true) {
            if (n == 1) break;
            n = (n % 2 == 0) ? n / 2 : 3 * n + 1;     // Collatz step
            peak = Math.max(peak, n);
            steps++;
        }
        System.out.println(steps + " " + peak);
    }
}
A 111 9232
B 112 9232
C 111 27
D 27 9232
Question page & discussion
Question 20 Medium

What is the output of the following Java program?

Main.java
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);
    }
}
A 6 3
B 5 3
C 6 100
D 6 2
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links