NEW
Font size
WorksheetsJava If/else/ switch
Total questions: 20
Worksheet time: 15mins
Which of the following is the best description of a "relational operator"?
Comparison symbols used in logical expressions
Mathematical symbols used to calculate numeric results
Links that control the relationships between objects
None of these are true
Given two numbers A and B, which of the following expressions will be true when "A is less than B"?
A < B
A > B
A == B
A != B
Given two numbers A and B, which of the following expressions will be true when "A is equal to B"?
A != B
A == B
A = B
A.eq(B)
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);
true false
false false
true true
false true
Which of the following expressions (A, B, C) is considered a logical expression?
A) 6 + 4
B) (6 + 4) >= 10
C) 4 < 10
A only
B and C only
A, B, and C
C only
What happens if you do not follow an if() statement with opening and closing curly braces?
All following statements that are indented to the right are part of the if() body
This is a compile-time syntax error
You must follow immediately with another if() statement
The next statement only is part of the if() body
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");
Only the second one is valid; the other two produce compile-time errors
Impossible to tell without running the program
The second two if() statements are nested inside the first and will only run if the first is true
All three work exactly the same way and produce the same output
Given the following boolean value, how would you write an if() statement that will execute if that value is true?
boolean run_me = true;
All of these will work
if (run_me)
if (run_me == true)
if (run_me != false)
Given the following code, which of these if() statements will produce a compile-time error?
int value = 12;
All of these are compile-time errors
if (value = 13)
if (value + 1)
if (value += 5)
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"); }
MEOW
WOOF
WOOF MEOW
MEOW WOOF
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()
}
When both logical expressions are true
When logical expression 1 is false and logical expression 2 is true
When logical expression 1 is true and logical expression 2 is false
When both logical expressions are false
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 }
When logical expression 1 is false and logical expression 2 is true
When both logical expressions are false
When both logical expressions are true
When logical expression 1 is true and logical expression 2 is false
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() } }
When logical expression 1 is false and logical expression 2 is true
When both logical expressions are false
When logical expression 1 is true and logical expression 2 is false
When both logical expressions are true
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");
2 lines will always appear
1 line will always appear
3 lines will always appear
It depends on the result of the logical expression
Where is it permissible to add a nested if() statement?
Inside the body of an "if()" block
All of these are true
Inside the body of an "else if()" block
Inside the body of an "else" block
When would you be more likely to use a chain of if() / else if() / else statements instead of a switch()?
When your logic needs to test multiple variables
When your logic needs to test multiple variables
Both of these are true
When your logic needs to test multiple variables
In a switch() statement, what comes between the "case" keyword and the colon?
An object reference
A specific number, letter, or quoted-string value
A logical expression
A mathematical expression
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; }
80 - 89
90 - 100
70 - 79
Nothing will appear
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 }
Java will report a compile-time syntax error
If the first case is a match, the program will flow through from that case and execute the second case body as well
The program will only execute the statements in the first case
The program will only execute the statements in the second case
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; }
I've never played that sport
I love to play hoops
My preferred position is pitcher
I am a goal-scoring machine
