Section 5 Quiz
(Answer all questions in this
section)
1. Which of the following could be a reason
to use a switch statement in a Java program? Mark for Review
(1) Points
Because it terminates the current loop.
Because it allows the code to be run through until a
certain conditional statement is true.
Because it allows the program to run certain segments of
code and neglect to run others based on the input given. (*)
Because it allows the user to enter an input in the
console screen and prints out a message that the user input was successfully
read in.
2. The three logic operators in Java are: Mark
for Review
(1) Points
!=,=,==
&&,!=,=
&&, ||, ! (*)
&,|,=
3. Which of the two diagrams below
illustrate the correct syntax for variables used in an if-else statement?
(1) Points
Example A (*)
Example B
4. Determine whether this boolean expression
evaluates to true or false:
!(3 < 4 && 5 > 6 || 6 <= 6 && 7
- 1 == 6) Mark for Review
(1) Points
True
False (*)
5. The six relational operators in Java are:
Mark for Review
(1) Points
>,<,=,!=,=<,=>
>,<,==,!=,<=,>= (*)
>,<,=,!=,<=,>=
>,<,=,!,<=,>=
6. How would
you use the ternary operator to rewrite this if statement?
if (skillLevel > 5)
numberOfEnemies = 10;
else
numberOfEnemies = 5;
Mark for Review
(1) Points
numberOfEnemies
= ( skillLevel >= 5) ? 5 : 10;
numberOfEnemies
= ( skillLevel > 5) ? 10 : 5; (*)
numberOfEnemies
= ( skillLevel >= 5) ? 10 : 5;
numberOfEnemies
= ( skillLevel < 5) ? 10 : 5;
numberOfEnemies
= ( skillLevel > 5) ? 5 : 10;
7. What will
print if the following Java code is executed?
if ((5.1 > 4.3 && 6.2 < 8.4) &&
!(7.2 < 3.5 || 1.2 == 2.1 || 2.2 != 2.25))
System.out.print("TRUE");
else
System.out.print("FALSE"); Mark for Review
(1) Points
True
False (*)
8. Determine
whether this boolean expression evaluates to true or false:
!(3 < 4 && 6 > 6 || 6 <= 6 && 7
- 2 == 6) Mark for Review
(1) Points
True (*)
False
9. Why are
loops useful? Mark for Review
(1) Points
They
save programmers from having to rewrite code.
They
allow for repeating code a variable number of times.
They
allow for repeating code until a certain argument is met.
All of
the above. (*)
10. In a for
loop, the counter is automatically incremented after each loop iteration. True
or False? Mark for Review
(1) Points
True
False
(*)
11. What is a
loop? Mark for Review
(1) Points
A
keyword used to skip over the remaining code.
A set of
logic that is repeatedly executed until a certain condition is met. (*)
A
segment of code that may only ever be executed once per call of the program.
None of
the above.
12. Which of
the following best describes a while loop?
Mark for Review
(1) Points
A loop
that contains a counter in parenthesis with the conditional statement.
A loop
that contains a segment of code that is executed before the conditional
statement is tested.
A loop
that is executed repeatedly until the conditional statement is false. (*)
A loop
that executes the code at least one time even if the conditional statement is
false.
13. When the
for loop condition statement is met the construct is exited. True or false? Mark for Review
(1) Points
True
False
(*)
14. Updating
the input of a loop allows you to implement the code with the next element
rather than repeating the code always with the same element. True or false? Mark for Review
(1) Points
True (*)
False
15. What is
one significant difference between a while loop and a do-while loop? Mark for Review
(1) Points
A
DO-WHILE loop will always execute the code at least once, even if the
conditional statement for the WHILE is never true. A WHILE loop is only
executed if the conditional statement is true. (*)
A
DO-WHILE loop includes an int that serves as a counter and a WHILE loop does
not.
There is
no difference between a DO-WHILE loop and a WHILE loop.
A
DO-WHILE loop does not exist in Java and a WHILE loop does.
Komentar
Posting Komentar