1. 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. (*)
Incorrect Incorrect. Refer to Section 5 Lesson 2.
2. In a for loop, the counter is automatically incremented after each loop iteration. True or False? Mark for Review
(1) Points
True
False (*)
Incorrect Incorrect. Refer to Section 5 Lesson 2.
3. What is the output of the following code segment?
int num = 7;
while(num >= 0)
{
num -= 3;
}
System.out.println(num); Mark for Review
(1) Points
-2 (*)
2
1
0
Correct Correct
4. What should replace the comment "//your answer here" in the code below if the code is meant to take no action when i % 2 is 0 (in other words when i is even)?
for(int i = 0; i < 10; i++){
if(i%2 == 0)
//your answer here
else
k+=3;
} Mark for Review
(1) Points
k+=1;
continue; (*)
return;
break;
Incorrect Incorrect. Refer to Section 5 Lesson 2.
5. All of the following are essential to initializing a for loop, except which one? Mark for Review
(1) Points
Updating the counter.
Having a conditional statement.
Having an if statement. (*)
Initializing the iterator(i).
Incorrect Incorrect. Refer to Section 5 Lesson 2.
Page 1 of 3 Next Summary
6. What is the function of the word "break" in Java?
Mark for Review
(1) Points
It exits the current loop or case statement. (*)
It continues onto the next line of code.
It stops the program from running.
It does not exist in Java.
Incorrect Incorrect. Refer to Section 5 Lesson 2.
7. Which of the following correctly initializes a for loop that executes 5 times? Mark for Review
(1) Points
for(int i = 1; i < 5; I++)
for(int i = 0; i == 6; i++)
for(int i = 0; i < 5; I++)
for(int i = 1; i < 6; i++) (*)
Incorrect Incorrect. Refer to Section 5 Lesson 2.
8. 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) ? 5 : 10;
numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
Correct Correct
9. Which of the following expressions will evaluate to true when x and y are boolean variables with opposite values?
I. (x || y) && !(x && y)
II. (x && !y) || (!x && y)
III. (x || y) && (!x ||!y) Mark for Review
(1) Points
I only
II only
I and III
II and III
I, II, and III (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
10. Which of the following are relational operators in Java?
Mark for Review
(1) Points
(Choose all correct answers)
< (*)
<= (*)
=
!= (*)
All of the above.
11. In an if-else construct the condition to be evaluated must end with a semi-colon. True or false? Mark for Review
(1) Points
True
False (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
12. Which of the following correctly matches the switch statement keyword to its function? Mark for Review
(1) Points
(Choose all correct answers)
if: records the user's input and sends it to the case statements to find a possible match
switch: tells the compiler the value to compare the input against
case: signals what code is executed if the user input matches the specified element (*)
default: signals what code to execute if the input does not match any of the cases (*)
switch: identifies what element will be compared to the element of the case statements to find a possible match (*)
13. Which of the following correctly initializes an instance of Scanner, called "in", that reads input from the console screen? Mark for Review
(1) Points
Scanner in = Scanner(System.in);
Scanner in = new Scanner("System.in");
System.in in = new Scanner();
Scanner in = new Scanner(System.in); (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
14. The following code fragment properly implements the switch statement. True or false?
default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input"); Mark for Review
(1) Points
True
False (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
15. What will print if the following Java code is executed?
Mark for Review
(1) Points
0
4
3 (*)
5
1) The following code fragment properly implements the switch statement. True or false?
default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input"); Mark for Review
(1) Points
True
False (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
2. Which of the two diagrams below illustrate the correct syntax for variables used in an if-else statement?
Mark for Review
(1) Points
Example A (*)
Example B
3. How would you use the ternary operator to rewrite this if statement?
if (gender == "male")
System.out.print("Mr.");
else
System.out.print("Ms."); Mark for Review
(1) Points
System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
System.out.print( (gender == "male") ? "Ms." : "Mr." );
(gender == "male") ? "Ms." : "Mr." ;
(gender == "male") ? "Mr." : "Ms." ;
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 (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
5. The three logic operators in Java are: Mark for Review
(1) Points
&, |, =
&&, ||, ! (*)
!=, =, ==
&&, !=, =
6. How would you use the ternary operator to rewrite this if statement?
if (gender == "female") System.out.print("Ms.");
else
System.out.print("Mr."); Mark for Review
(1) Points
(gender == "female") ? "Ms." : "Mr." ;
System.out.print( (gender == "female") ? "Mr." : "Ms." );
(gender == "female") ? "Mr." : "Ms." ;
System.out.print( (gender == "female") ? "Ms." : "Mr." ); (*)
Incorrect Incorrect. Refer to Section 5 Lesson 1.
7. The three logic operators in Java are: Mark for Review
(1) Points
&&,!=,=
&,|,=
&&, ||, ! (*)
!=,=,==
Incorrect Incorrect. Refer to Section 5 Lesson 1.
8. How would you use the ternary operator to rewrite this if statement?
if (balance < 500)
fee = 10;
else
fee = 0; Mark for Review
(1) Points
fee = ( balance > 5) ? 10 : 0;
fee= ( balance < 500) ? 10 : 0; (*)
fee = ( balance < 500) ? 0 : 10;
fee = ( balance >= 5) ? 0 : 10;
fee = ( balance >= 500) ? 10 : 0;
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 the code fragment below, the syntax for the for loop's initialization is correct. True or false?
public class ForLoop {
public static void main (String args[])
{
for ((int 1=10) (i<20) (i++))
{
System.out.println ("i: " + i);
}
}
}
Mark for Review
(1) Points
True
False (*)
11. Which of the following is true about a do-while loop? Mark for Review
(1) Points
It is a post-test loop.
It is a modified while loop that allows the program to run through the loop once before testing the boolean condition.
It continues looping until the condition becomes false.
All of the above. (*)
Incorrect Incorrect. Refer to Section 5 Lesson 2.
12. What is the function of the word "break" in Java? Mark for Review
(1) Points
It does not exist in Java.
It stops the program from running.
It exits the current loop or case statement. (*)
It continues onto the next line of code.
Incorrect Incorrect. Refer to Section 5 Lesson 2.
13. In a for loop the counter is not automatically incremented after each loop iteration. Code must be written to increment the counter. True or false? Mark for Review
(1) Points
True (*)
False
Correct Correct
14. One advantage to using a while loop over a for loop is that a while loop always has a counter. True or false? Mark for Review
(1) Points
True
False (*)
Incorrect Incorrect. Refer to Section 5 Lesson 2.
15. Which of the following correctly initializes a for loop that executes 5 times? Mark for Review
(1) Points
for(int i = 1; i < 5; I++)
for(int i = 0; i == 6; i++)
for(int i = 1; i < 6; i++) (*)
for(int i = 0; i < 5; I++)

Tidak ada komentar:
Posting Komentar