wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java If/else/ switch

Total questions: 20

Worksheet time: 15mins

Name
Class
Date
1.

Which of the following is the best description of a "relational operator"?

a)

Comparison symbols used in logical expressions

b)

Mathematical symbols used to calculate numeric results

c)

Links that control the relationships between objects

d)

None of these are true

2.

Given two numbers A and B, which of the following expressions will be true when "A is less than B"?

a)

A < B

b)

A > B

c)

A == B

d)

A != B

3.

Given two numbers A and B, which of the following expressions will be true when "A is equal to B"?

a)

A != B

b)

A == B

c)

A = B

d)

A.eq(B)

4.

Given the following code, what will be displayed when the program is run?

int A = 5; int B = 10;

System.out.print(A != B);

System.out.print(" ");

System.out.print(A < B);

a)

true false

b)

false false

c)

true true

d)

false true

5.

Which of the following expressions (A, B, C) is considered a logical expression?

A) 6 + 4

B) (6 + 4) >= 10

C) 4 < 10

a)

A only

b)

B and C only

c)

A, B, and C

d)

C only

6.

What happens if you do not follow an if() statement with opening and closing curly braces?

a)

All following statements that are indented to the right are part of the if() body

b)

This is a compile-time syntax error

c)

You must follow immediately with another if() statement

d)

The next statement only is part of the if() body

7.

What is the difference between the following three if() statements?

double sodaCost = 2.50;

if (sodaCost > 2.00)

System.out.println("I'll have water instead");

if (sodaCost > 2.00)

System.out.println("I'll have water instead");

if (sodaCost > 2.00)

System.out.println("I'll have water instead");

a)

Only the second one is valid; the other two produce compile-time errors

b)

Impossible to tell without running the program

c)

The second two if() statements are nested inside the first and will only run if the first is true

d)

All three work exactly the same way and produce the same output

8.

Given the following boolean value, how would you write an if() statement that will execute if that value is true?

boolean run_me = true;

a)

All of these will work

b)

if (run_me)

c)

if (run_me == true)

d)

if (run_me != false)

9.

Given the following code, which of these if() statements will produce a compile-time error?

int value = 12;

a)

All of these are compile-time errors

b)

if (value = 13)

c)

if (value + 1)

d)

if (value += 5)

10.

What will be displayed when the following statements are run?

String pet = "cat";

if (pet.equals("dog")) {

System.out.println("WOOF"); }

if (pet.equals("dog") == false) {

System.out.println("MEOW"); }

a)

MEOW

b)

WOOF

c)

WOOF MEOW

d)

MEOW WOOF

11.

Given the if() / else if() code outlined below, when will the statements in the body of the "else if()" be run?

if () {

// body of if()

}

else if () {

// body of else if()

}

a)

When both logical expressions are true

b)

When logical expression 1 is false and logical expression 2 is true

c)

When logical expression 1 is true and logical expression 2 is false

d)

When both logical expressions are false

12.

Given the if() / else if() / else code outlined below, when will the statements in the body of the "else" be run?

if () { // body of if() }

else if () { // body of else if() }

else { // body of else }

a)

When logical expression 1 is false and logical expression 2 is true

b)

When both logical expressions are false

c)

When both logical expressions are true

d)

When logical expression 1 is true and logical expression 2 is false

13.

Given the nested if() code outlined below, when will the statements in the body of the inner "if()" be run?

if () {

if ()

{ // body of inner if() } }

a)

When logical expression 1 is false and logical expression 2 is true

b)

When both logical expressions are false

c)

When logical expression 1 is true and logical expression 2 is false

d)

When both logical expressions are true

14.

Given the code outlined below, how many output lines would be displayed if the program runs? if () { System.out.println("Line A"); return; } else { System.out.println("Line B"); return; } System.out.println("Line C");

a)

2 lines will always appear

b)

1 line will always appear

c)

3 lines will always appear

d)

It depends on the result of the logical expression

15.

Where is it permissible to add a nested if() statement?

a)

Inside the body of an "if()" block

b)

All of these are true

c)

Inside the body of an "else if()" block

d)

Inside the body of an "else" block

16.

When would you be more likely to use a chain of if() / else if() / else statements instead of a switch()?

a)

When your logic needs to test multiple variables

b)

When your logic needs to test multiple variables

c)

Both of these are true

d)

When your logic needs to test multiple variables

17.

In a switch() statement, what comes between the "case" keyword and the colon?

a)

An object reference

b)

A specific number, letter, or quoted-string value

c)

A logical expression

d)

A mathematical expression

18.

When the code below runs, what message will be displayed on the screen?

char grade = 'A';

switch(grade) {

case 'A':

System.out.println("90 - 100");

break;

case 'B':

System.out.println("80 - 89");

break;

case 'C':

System.out.println("70- 79");

break; }

a)

80 - 89

b)

90 - 100

c)

70 - 79

d)

Nothing will appear

19.

What happens in a switch() statement if you leave out the "break" statement between the first and second cases?

switch(value) {

case 1: // statements in first case

case 2: // statements in second case }

a)

Java will report a compile-time syntax error

b)

If the first case is a match, the program will flow through from that case and execute the second case body as well

c)

The program will only execute the statements in the first case

d)

The program will only execute the statements in the second case

20.

What message will appear on the screen when the following code is run?

String sport = "BASKETBALL";

switch (sport) {

case "basketball":

System.out.println("I love to play hoops");

break;

case "baseball":

System.out.println("My preferred position is pitcher");

break;

case "soccer":

System.out.println("I am a goal-scoring machine");

break;

default:

System.out.println("I've never played that sport");

break; }

a)

I've never played that sport

b)

I love to play hoops

c)

My preferred position is pitcher

d)

I am a goal-scoring machine