0% found this document useful (0 votes)
4K views11 pages

Quiz 6

This document contains a 15 question quiz about loops in Java. It covers topics like the different types of loops (for, while, do-while), loop termination using break and continue statements, pre-test and post-test loops, and the general structure and purpose of loops. The questions test understanding of loop syntax and semantics, and which statements are true about each loop type.

Uploaded by

Yulia Kendengis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views11 pages

Quiz 6

This document contains a 15 question quiz about loops in Java. It covers topics like the different types of loops (for, while, do-while), loop termination using break and continue statements, pre-test and post-test loops, and the general structure and purpose of loops. The questions test understanding of loop syntax and semantics, and which statements are true about each loop type.

Uploaded by

Yulia Kendengis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Section 6 Quiz

(Answer all questions in this section)

1. Loops are used in programs for repeated execution of one or more


statements until a terminating condition is reached. Mark for Review
(1) Points

True (*)

False

Correct Correct

2. What is the result?

public static void main(String[] args) {


for (;;) {
System.out.println("Welcome to Java");
}
} Mark for Review
(1) Points

Program prints �Welcome to Java� an infinite number of times. (*)

No error and no output.

Program prints �Welcome to Java� once.

Compilation error as expressions are missing in the for loop.

Correct Correct

3. Which is not a looping statement in Java? Mark for Review


(1) Points

do-while

for

switch (*)

while
Incorrect Incorrect. Refer to Section 6 Lesson 1.

4. The for loop provides a complicated way to iterate over a range


of values. Mark for Review
(1) Points

True

False (*)

Correct Correct

5. Which statement will produce the output: 2, 4, 6, 8, 10? Mark


for Review
(1) Points

for (int i = 0; i < 8; i += 2) {


System.out.print(i + " ");
}

for (int i = 0; i < 10; i += 2) {


System.out.print(i + " ");
}

for (int i = 2; i <= 10; i += 2) {


System.out.print(i + " ");
} (*)

for (int i = 1; i < 10; i += 2) {


System.out.print(i + " ");
}

Correct Correct
6. You want to compute the sum of all the marks of a given subject. Which
approach will you choose? Mark for Review
(1) Points

switch statement

if/else statement

if statement
Looping (*)

Correct Correct

7. Which of the two are pre-test loops? Mark for Review


(1) Points

(Choose all correct answers)

for (*)

do-while

while (*)

forEach

Incorrect Incorrect. Refer to Section 6 Lesson 2.

8. Which statement is false about infinite loop? Mark for Review


(1) Points

An infinite loop is a commonly the result of a syntax error. (*)

The body of a while loop eventually must make the condition false to avoid
infinite loop.

An infinite loop is generally caused by a programming mistake.

An infinite loop is a code which will execute until the user interrupts the
program

Correct Correct

9. Which statement is NOT true about do-while loops? Mark for


Review
(1) Points

Statements in the loop are executed repeatedly until the condition becomes
false.

Statements in the loop are executed once initially, and then the condition is
evaluated.

Statements in the loop are executed once until the condition becomes false.

The number of times a do-while loop is executed is dependent upon the value
of the counter variable. (*)

Incorrect Incorrect. Refer to Section 6 Lesson 2.

10. The while loop continually executes a block of statements while a


particular condition is false. Mark for Review
(1) Points

True

False (*)

Incorrect Incorrect. Refer to Section 6 Lesson 2.


11. Which two statements are true about the while loop. Mark for Review
(1) Points

(Choose all correct answers)

If the condition of a pre-test loop is false, the statements in the loop are
never executed. (*)

The statements of a while loop will execute one or more times.

The statement in a while loop will execute zero or more times. (*)

If the condition of the loop is true initially, the statements are never
executed.

Correct Correct

12. A while loop is often used with Scanner input as you don't know
many times you'll need to re-prompt the user if they type bad data. Mark for
Review
(1) Points
True (*)

False

Correct Correct

13. A continue statement is used to skip the remaining statements in


the body of a loop and continue with the next iteration of the loop. Mark for
Review
(1) Points

True (*)

False

Correct Correct

14. Which two statements are true about the break statement? Mark
for Review
(1) Points

(Choose all correct answers)

When a break statement is executed inside a loop, the loop-statement is


terminated immediately and comes out of the program.

When a break statement is executed inside a loop, the loop-statement is


terminated immediately. (*)

The execution of the program will continue with the statement following the
loop-statement. (*)

The execution of the program will stop at the statement following the loop-
statement.

Incorrect Incorrect. Refer to Section 6 Lesson 3.

15. Which is used to terminate a loop? Mark for Review


(1) Points
switch

continue

catch

break (*)

Correct Correct
Section 6 Quiz
(Answer all questions in this section)

1. What is the result?

public static void main(String[] args) {


for (int var1 = 0; var1 < 2; var1++) {
for (int var2 = 0; var2 < 2; var2++) {
if (var2 == 2) {
continue;
}
System.out.println("var1:" + var1 + ", var2:" + var2);
}
}
}
Mark for Review
(1) Points

var1: 0, var2: 0
var1: 0, var2: 1
var1: 1, var2: 0
var1: 1, var2: 1
var1: 2, var2: 0
var1: 2, var2: 1

var1: 0, var2: 0
var1: 0, var2: 1
var1: 1, var2: 0
var1: 1, var2: 1

(*)

var1: 0, var2: 0
var1: 1, var2: 1
var1: 2, var2: 0

var1: 0, var2: 0
var1: 0, var2: 1
var1: 0, var2: 2
var1: 1, var2: 0
var1: 1, var2: 1
var1: 1, var2: 2
var1: 2, var2: 0
var1: 2, var2: 1
var1: 2, var2: 2

Correct Correct

2. The only way to exit a loop is for the loop condition to evaluate
to false. Mark for Review
(1) Points

True

False (*)

Correct Correct

3. Which two statements are true about the break statement? Mark
for Review
(1) Points

(Choose all correct answers)

When a break statement is executed inside a loop, the loop-statement is


terminated immediately. (*)

The execution of the program will continue with the statement following the
loop-statement. (*)

The execution of the program will stop at the statement following the loop-
statement.

When a break statement is executed inside a loop, the loop-statement is


terminated immediately and comes out of the program.

Correct Correct

4. What is the output?

public static void main(String[] args) {


int num = 1;
while (num >= 200){
System.out.print(num + "" "");
num = num * 5;
}
} Mark for Review
(1) Points

1 5 25 125

No output. (*)

1 5 25 125 175

5 25 125

Correct Correct

5. A do-while will always execute statements contained in the loop


at least once. Mark for Review
(1) Points

True (*)

False

Correct Correct
6. A pre-test loop evaluates the condition prior to execution of the loop.
Mark for Review
(1) Points

True (*)

False

Correct Correct

7. A post-test loop evaluates its condition at the end of the loop


instead of the beginning. Mark for Review
(1) Points

True (*)
False

Correct Correct

8. The while loop continually executes a block of statements while a


particular condition is false. Mark for Review
(1) Points

True

False (*)

Correct Correct

9. Which of the two are pre-test loops? Mark for Review


(1) Points

(Choose all correct answers)

forEach

for (*)

while (*)

do-while

Correct Correct

10. Given:

for(int i = 0; i > 10; i++){ }


What type of variable is i?
Mark for Review
(1) Points

Member

Global
Local (*)

Static

Correct Correct
11. The initialization expression initializes the loop and it is executed only
once, as the loop begins. Mark for Review
(1) Points

True (*)

False

Correct Correct

12. Which statement will produce the output: 2, 4, 6, 8, 10? Mark


for Review
(1) Points

for (int i = 0; i < 10; i += 2) {


System.out.print(i + " ");
}

for (int i = 2; i <= 10; i += 2) {


System.out.print(i + " ");
} (*)

for (int i = 1; i < 10; i += 2) {


System.out.print(i + " ");
}

for (int i = 0; i < 8; i += 2) {


System.out.print(i + " ");
}

Correct Correct

13. In the given syntax of for loop, which part represents the header
section?

for (initialization; condition; update) {


// Code statement(s) } Mark for Review
(1) Points
for (initialization; condition; update) { }

for (initialization; condition; update) { Code statement(s) }

Code statement(s)

for (initialization; condition; update) (*)

Correct Correct

14. Which is not a looping statement in Java? Mark for Review


(1) Points

do-while

for

switch (*)

while

Correct Correct

15. Looping continues as long as the boolean expression in the for


loop is false. Mark for Review
(1) Points

True

False (*)

Correct Correct

You might also like