wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Code Output Quiz

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

What will this code print? int x = 0; if (x == 0) { System.out.println("Zero"); } else if (x > 0) { System.out.println("Positive"); } else { System.out.println("Negative"); }

a)

Zero

b)

Positive

c)

Negative

d)

Nothing

2.

What does this code print? int i = 0; while (i 0; i--) { System.out.print(i + " "); }

a)

1 2 3

b)

3 2 1

c)

3 2

d)

2 1 0

3.

How many times does this loop run? int x = 5; while (x > 0) { x--; } System.out.println(x);

a)

4

b)

5

c)

6

d)

Infinite

4.

What is the output of this code? for (int i = 1; i 1; i--) { if (i == 3) break; System.out.print(i + " "); }

a)

5 4 3

b)

5 4

c)

4 3 2

d)

5 4 2

5.

What is the output of this nested loop? for (int i = 1; i 0) { for (int j = 0; j 1) { System.out.print(x + " "); x--; }

a)

4 3 2

b)

4 3 2 1

c)

3 2 1

d)

4 3

6.

What will this code output? int count = 0; for (int i = 1; i <= 4; i++) { if (i % 3 == 0) count++; } System.out.println(count);

a)

1

b)

2

c)

3

d)

4

7.

What is the output of this code? int i = 0; while (i < 5) { if (i % 2 == 0) { System.out.print(i + " "); } i++; }

a)

0 2 4

b)

1 3 5

c)

0 2

d)

2 4

8.

What is the output of this code? int product = 1; for (int i = 1; i <= 4; i += 2) { product = i; } System.out.println(product);

a)

3

b)

6

c)

12

d)

9

9.

What will this code print?

int x = 0;

while (x < 3) {

    x++;

    if (x == 2) continue;

    System.out.print(x + " ");

}

a)

1 2 4

b)

1 3

c)

2 3

d)

1 2

10.

What is the result of this loop?

for (int i = 0; i < 4; i++) {

    System.out.print(i  2 + " ");

}

a)

0 2 4 6

b)

0 1 2 3

c)

2 4 6 8

d)

0 2 4

11.

What is the result of this loop?

for (int i = 0; i < 4; i++) {

    System.out.print(i  2 + " ");

}

a)

0 2 4 6

b)

0 1 2 3

c)

2 4 6 8

d)

0 2 4

12.

How many times does "Hi" print?

int i = 1;

while (i <= 5) {

    System.out.println("Hi");

    i += 2;

}

a)

2

b)

3

c)

4

d)

5

13.

What will this code output?

for (int i = 5; i > 1; i--) {

    if (i == 3) break;

    System.out.print(i + " ");

}

a)

5 4 3

b)

5 4

c)

4 3 2

d)

5 4 2

14.

What is the output of this nested loop?

for (int i = 1; i <= 2; i++) {

    int j = 0;

    while (j < 2) {

        System.out.print(i + j + " ");

        j++;

    }

}

a)

1 2 2 3

b)

1 1 2 2

c)

1 0 2 1

d)

0 1 1 2

15.

What will this code print?

int sum = 0;

for (int i = 1; i <= 3; i++) {

    sum += i;

    System.out.print(sum + " ");}

a)

1 3 6

b)

1 2 3

c)

3 6 9

d)

0 1 3

16.

What is the output of this code?

int i = 0;

while (i <= 6) {

    System.out.print(i + " ");

    i += 3;

}

a)

0 3 6

b)

0 1 2 3 4 5 6

c)

3 6 9

d)

0 3

17.

What will this loop do?

for (int i = 0; i < 6; i += 2) {

    if (i == 4) break;

    System.out.print(i + " ");

}

a)

0 2

b)

0 2 4

c)

2 4 6

d)

0 2 4 6

18.

What will this loop do?

for (int i = 0; i < 6; i += 2) {

    if (i == 4) break;

    System.out.print(i + " ");

}

a)

0 2

b)

0 2 4

c)

2 4 6

d)

0 2 4 6

19.

How many times does "Run" print?

int i = 0;

for (; i < 5; i++) {

    System.out.println("Run");

}

a)

4

b)

5

c)

6

d)

Infinite

20.

What is the output of this code?

int x = 3;

while (x > 0) {

    for (int j = 0; j < 2; j++) {

        System.out.print("");

    }

    x--;

}

a)

""

b)

"" "" "" ""

c)

3 2 1

d)

Infinite Loop

21.

What will this code print?

for (int i = 0; i < 3; i++) {

    System.out.print(i + " ");

    if (i == 1) break;

}

a)

0 1

b)

0 1 2

c)

1 2

d)

0

22.

What is the result of this loop?

int x = 4;

while (x > 1) {

    System.out.print(x + " ");

    x--;

}

a)

4 3 2

b)

4 3 2 1

c)

3 2 1

d)

4 3

23.

What will this code output?

int count = 0;

for (int i = 1; i <= 4; i++) {

    if (i % 3 == 0) count++;

}

System.out.println(count);

a)

1

b)

2

c)

3

d)

4

24.

What is the output of this code?

int i = 0;

while (i < 5) {

    if (i % 2 == 0) {

        System.out.print(i + " ");

    }

    i++;

}

a)

0 2 4

b)

1 3 5

c)

0 2

d)

2 4

25.

What will this loop print?

for (int i = 1; i <= 2; i++) {

    int j = 0;

    while (j < i) {

        System.out.print("");

        j++;

    }

    System.out.println();

}

a)

"" ""

b)

1

2

c)

1 2

d)

* **

26.

What is the output of this code?

int product = 1;

for (int i = 1; i <= 4; i += 2) {

    product = i;

}

System.out.println(product);

a)

3

b)

6

c)

12

d)

9

27.

What will this code print when executed?

int x = 5;

for (int i = 0; i < x; i++) {

    while (x > 2) {

        x--;

        if (x == 3) break;

        System.out.print(x + " ");

    }

}

a)

4 2

b)

4

c)

4 2 4 2 4

d)

Nothing

28.

What will this code print when executed?

int x = 5;

for (int i = 0; i < x; i++) {

    while (x > 2) {

        x--;

        if (x == 3) break;

        System.out.print(x + " ");

    }

}

a)

4 2

b)

4

c)

4 2 4 2 4

d)

Nothing

29.

How many times will "Loop" be printed?

int count = 0;

for (int i = 1; i <= 4; i++) {

    while (count < i) {

        System.out.println("Loop");

        count++;

    }

}

a)

4

b)

6

c)

10

d)

8

30.

What is the output of this nested loop structure?

int total = 0;

for (int i = 0; i < 3; i++) {

    int j = 0;

    while (j <= i) {

        total += j;

        j++;

    }

}

System.out.println(total);

```

a)

3

b)

4

c)

9

d)

0