Conditional Loops [Java] - Quiz Explanation
The correct answers are indicated below, along with the text that explains the correct answers.
1.
Branches allow a Java program to:
Please select the best answer.
A.
Repeat a section of code over and over
B.
Alter the flow of execution and run one piece of code instead of another
C.
Simultaneously execute two sections of code at once
The correct answer is B.
Branches allow a Java program to alter the flow of execution and run one piece of code instead of another. Loops are used to repeat a section of code over and over. Threads are used to execute two sections of code at once. Threads are covered later in this series.
2.
How do you execute multiple lines of code based on an
if-else
statement?
Please select the best answer.
A.
Place the code below the branch.
B.
Place the code in parentheses below the branch.
C.
Place the code in a compound statement below the branch.
The correct answer is C.
You execute multiple lines of code based on an
if-else
statement by placing the code in a compound statement below the branch. Curly braces, not parentheses, enclose a compound statement.
3.
What happens when a
switch
statement encounters a
break
statement?
Please select the best answer.
A.
The branch jumps to the next case statement.
B.
The branch is exited.
C.
The branch continues just below the break statement.
The correct answer is B.
When a
switch
statement encounters a
break
statement the branch is exited. Code below a
break
statement in a
switch
statement is never executed.
4.
The code in the default section of a
switch
statement is called:
Please select the best answer.
A.
After executing the code in a
case
match
B.
Whenever there is a
case
match
C.
Whenever there is not a
case
match
The correct answer is C.
The code in the default section of a
switch
statement is called whenever there is not a
case
match. The code in the default section is only executed if there isn't a
case
match.
5.
The
for
loop is primarily used to:
Please select the best answer.
A.
Repeatedly execute a section of code
B.
Conditionally execute a section of code
C.
Branch to another section of code
The correct answer is A.
The
for
loop allows you to repeatedly execute a section of code. Generally an
if
statement allows you to conditionally execute a section of code. Branch statements (
if-else
,
switch
) are used to branch to another section of code.