Deck 6: Loops
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/107
Play
Full screen (f)
Deck 6: Loops
1
What is the output of the code snippet given below?
A)no output
B)abcd
C)abcde
D)bcde

A)no output
B)abcd
C)abcde
D)bcde
bcde
2
How many times does the code snippet given below display "Loop Execution"?
A)infinite times
B)8 times
C)9 times
D)10 times

A)infinite times
B)8 times
C)9 times
D)10 times
9 times
3
How many times will the following loop run?
A)0
B)8
C)9
D)10

A)0
B)8
C)9
D)10
10
4
What is the output of the following code snippet?

A) 1 3 5 7 9 11 13 15 17 19
B) 1 3 5 7 9 11 13 19
C) 1 3 5 7 9 11 13 15 17
D) 1 3 5 7 9 11 13 17 19

A) 1 3 5 7 9 11 13 15 17 19
B) 1 3 5 7 9 11 13 19
C) 1 3 5 7 9 11 13 15 17
D) 1 3 5 7 9 11 13 17 19
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
5
What is the output of the code fragment given below?
A)0
B)62
C)63
D)The code fragment displays no output because it does not compile.

A)0
B)62
C)63
D)The code fragment displays no output because it does not compile.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
6
What is the output of the code snippet given below?
A)no output
B)0 2 4 6 8
C)10 12 14 16 18 ….(infinite loop)
D)0 2 4 6 8 10 12 14 ….(infinite loop)

A)no output
B)0 2 4 6 8
C)10 12 14 16 18 ….(infinite loop)
D)0 2 4 6 8 10 12 14 ….(infinite loop)
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
7
What is the output of the following code snippet?
A)No output because of compilation error.
B)"Inside the while loop" will be displayed 10 times.
C)No output after successful compilation.
D)"Inside the while loop" will be displayed only once.

A)No output because of compilation error.
B)"Inside the while loop" will be displayed 10 times.
C)No output after successful compilation.
D)"Inside the while loop" will be displayed only once.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
8
Which error type does the "off-by-one" error belong to?
A)syntax error
B)compile-time error
C)run-time error
D)infinite loop
A)syntax error
B)compile-time error
C)run-time error
D)infinite loop
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
9
What is the output of the code snippet given below?
A)no output
B)13
C)24
D)2345

A)no output
B)13
C)24
D)2345
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
10
What is the output of the following code snippet?
A)1 3 5
B)1 3 9
C)1 3 5 7 9
D)1 3 5 9

A)1 3 5
B)1 3 9
C)1 3 5 7 9
D)1 3 5 9
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
11
What are the values of i and j after the following code fragment runs?
A)i = 1951, j = 0
B)i = 1951, j = 45
C)i = 65, j = 1
D)i = 65, j = 45

A)i = 1951, j = 0
B)i = 1951, j = 45
C)i = 65, j = 1
D)i = 65, j = 45
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
12
What output does this while loop generate?
A)No output is generated.
B)6, 5, 4, 3, 2, 1
C)6, 5, 4, 3, 2, 1,
D)The output is infinite.

A)No output is generated.
B)6, 5, 4, 3, 2, 1
C)6, 5, 4, 3, 2, 1,
D)The output is infinite.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
13
What is the output of the following code snippet?
A)r = 16.0
B)r = 128.0
C)r = 4096.0
D)r = 65536.0

A)r = 16.0
B)r = 128.0
C)r = 4096.0
D)r = 65536.0
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
14
What are the values of i and j after the following code snippet is run?
A)
B)
C)
D)

A)

B)

C)

D)

Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
15
How many times does the code snippet below display "Hello"?
A)infinite times
B)14 times
C)15 times
D)16 times

A)infinite times
B)14 times
C)15 times
D)16 times
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
16
What is the output of the following code fragment? 
A) The value of sum is 0 .
B) The value of sum is 9 .
C) The value of sum is 12 .
D) The value of sum is 21 .

A) The value of sum is 0 .
B) The value of sum is 9 .
C) The value of sum is 12 .
D) The value of sum is 21 .
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
17
What is the output of the following code fragment?
A)The value of sum is 65.
B)The value of sum is 66.
C)The value of sum is 55.
D)The value of sum is 56.

A)The value of sum is 65.
B)The value of sum is 66.
C)The value of sum is 55.
D)The value of sum is 56.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
18
The code snippet below checks whether a given number is a prime number.What will be the result of executing it?
A)The code snippet will not compile.
B)The code snippet will display the desired result.
C)The code snippet will display an incorrect result if the number is 1.
D)The code snippet will loop forever.

A)The code snippet will not compile.
B)The code snippet will display the desired result.
C)The code snippet will display an incorrect result if the number is 1.
D)The code snippet will loop forever.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
19
How many times does the following code fragment display "Hi"?
A)9 times
B)10 times
C)11 times
D)12 times

A)9 times
B)10 times
C)11 times
D)12 times
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
20
What is the output of the code fragment given below?
A)j=27
B)j=12
C)j=13
D)j=14

A)j=27
B)j=12
C)j=13
D)j=14
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
21
When hand-tracing a portion of code, which statement about Boolean conditions is true?
A)They typically are too complex to be evaluated.
B)They do not need to be monitored because their result usually is not stored in a variable.
C)It is rare to encounter a Boolean condition.
D)They are crucial to evaluate since they determine if-statement conditions and looping control.
A)They typically are too complex to be evaluated.
B)They do not need to be monitored because their result usually is not stored in a variable.
C)It is rare to encounter a Boolean condition.
D)They are crucial to evaluate since they determine if-statement conditions and looping control.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following statements is true about the variable sum in the code snippet below?
A)It contains the value 10 after the loop is finished executing.
B)It contains the value 11 after the loop is finished executing.
C)It is the cause of an infinite loop.
D)It disappears after the loop is done executing.

A)It contains the value 10 after the loop is finished executing.
B)It contains the value 11 after the loop is finished executing.
C)It is the cause of an infinite loop.
D)It disappears after the loop is done executing.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
23
What is the best strategy for avoiding off-by-one errors?
A)Randomly insert +1 or -1 until the program seems to work.
B)Think through a couple of test cases and use the results to come up with a rationale for decisions.
C)Always start a loop at 0.
D)Never use the < operator by itself in a comparison test.
A)Randomly insert +1 or -1 until the program seems to work.
B)Think through a couple of test cases and use the results to come up with a rationale for decisions.
C)Always start a loop at 0.
D)Never use the < operator by itself in a comparison test.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
24
What is the output of the following code snippet? 
A) 0 1 5 7 9 11 13 15 17 19
B) 0 1 1 2 3 5 8 13 21 34 55
C) 0 1 4 6 8 10 12 14 16 18
D) 0 1 6 7 9 12 14 17 19 21

A) 0 1 5 7 9 11 13 15 17 19
B) 0 1 1 2 3 5 8 13 21 34 55
C) 0 1 4 6 8 10 12 14 16 18
D) 0 1 6 7 9 12 14 17 19 21
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following code snippets displays the output exactly 10 times?
A) int i=0;
while (i<10);
{
System. out.println("This is example 1.");
i++;
}
B) int i = 0 ;
while (i < 10)
{ System.out.println("This is example 2.");
i++;
}
C) Int i=0;
while (i<10)
{
System. out.println("This is example 3.");
}
D) int i=1;
while (i<10)
{
System. out.println("This is example 4.");
i++;
}
A) int i=0;
while (i<10);
{
System. out.println("This is example 1.");
i++;
}
B) int i = 0 ;
while (i < 10)
{ System.out.println("This is example 2.");
i++;
}
C) Int i=0;
while (i<10)
{
System. out.println("This is example 3.");
}
D) int i=1;
while (i<10)
{
System. out.println("This is example 4.");
i++;
}
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
26
What changes do you need to make in the following code snippet to display "Let us learn Java" exactly 10 times?
A)while (i < 9)
B)while (i < 11)
C)while (i < 12)
D)int i = 1;

A)while (i < 9)
B)while (i < 11)
C)while (i < 12)
D)int i = 1;
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
27
What is the output of the following code snippet?

A) 1 End
B) 1 End (but the loop is an infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (but the loop is an infinite loop)

A) 1 End
B) 1 End (but the loop is an infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (but the loop is an infinite loop)
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
28
What values does counter variable i take on when this loop executes?
A)20, 14, 8, 2
B)20, 14, 8, 2, -4
C)20, 14, 8
D)14, 8, 2

A)20, 14, 8, 2
B)20, 14, 8, 2, -4
C)20, 14, 8
D)14, 8, 2
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
29
What for loop can be used in the indicated area so the code will print:
A)for (int num = 0; num < 4 - val; num++)
B)for (int num = 0; num < val; num++)
C)for (int num = 4; num < val; num++)
D)for (int num = 4; num > 0; num--)

A)for (int num = 0; num < 4 - val; num++)
B)for (int num = 0; num < val; num++)
C)for (int num = 4; num < val; num++)
D)for (int num = 4; num > 0; num--)
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
30
What is the output of this code snippet?
A)3 4 5
B)3
C)3 3 3 3 3 ...(infinite loop)
D)0 1 2

A)3 4 5
B)3
C)3 3 3 3 3 ...(infinite loop)
D)0 1 2
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
31
When hand tracing, drawing a line through the value stored in a variable means that the
A)value stored there has changed to something new.
B)variable is the wrong data type for the code being executed.
C)expression being evaluated uses that variable.
D)variable must be inside a loop.
A)value stored there has changed to something new.
B)variable is the wrong data type for the code being executed.
C)expression being evaluated uses that variable.
D)variable must be inside a loop.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
32
The process of hand-tracing code is valuable because
A)it is usually faster than just running the code.
B)it is the best way to design an algorithm.
C)you must already have a working program in order to do it.
D)it gives valuable insight that you do not get by running the code.
A)it is usually faster than just running the code.
B)it is the best way to design an algorithm.
C)you must already have a working program in order to do it.
D)it gives valuable insight that you do not get by running the code.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
33
This code snippet is intended to calculate the balance of an account after twenty years of gaining interest.Select the statement that correctly completes the loop.
A)years++;
B)years--;
C)balance++;
D)balance--;

A)years++;
B)years--;
C)balance++;
D)balance--;
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
34
Assuming the variable n is an integer that stores a positive value, which of the following is a loop with a bound that could be problematic?
A)for (i = 1; i != n; i++)
B)for (years = n; years < 0; years--)
C)for (i = 1; i <= n; i++)
D)for (int i = 1; i <= 10; i++)
A)for (i = 1; i != n; i++)
B)for (years = n; years < 0; years--)
C)for (i = 1; i <= n; i++)
D)for (int i = 1; i <= 10; i++)
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
35
How many times does the loop execute in the following code fragment?
A)11
B)12
C)13
D)14

A)11
B)12
C)13
D)14
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
36
What is the output of the code snippet given below?
A)No output
B)0 3 6 9 12 15 18
C)0 1 3 5 7 9
D)0 3 6 9 ….(infinite loop)

A)No output
B)0 3 6 9 12 15 18
C)0 1 3 5 7 9
D)0 3 6 9 ….(infinite loop)
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
37
When hand-tracing the loop in the code snippet below, which variables are important to evaluate?
A)the variables i and j only
B)the variables i and sum
C)the variables i, j, and k
D)Tthe variables j and k only

A)the variables i and j only
B)the variables i and sum
C)the variables i, j, and k
D)Tthe variables j and k only
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
38
What will be the result of running the following code fragment?
A)The code fragment will display the interest calculated for nine years.
B)The code fragment will continue to display the calculated interest forever because the loop will never end.
C)The code fragment will not display the calculated interest and halt abruptly.
D)The code fragment will not display any output because it will not compile.

A)The code fragment will display the interest calculated for nine years.
B)The code fragment will continue to display the calculated interest forever because the loop will never end.
C)The code fragment will not display the calculated interest and halt abruptly.
D)The code fragment will not display any output because it will not compile.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
39
Which of the statements about hand-tracing is true?
A)You need a working program in order to do hand-tracing.
B)Hand-tracing can be done with pseudocode.
C)Hand-tracing will clearly show how long a program will take to execute.
D)The goal of hand-tracing is to double check the answers of the running code.
A)You need a working program in order to do hand-tracing.
B)Hand-tracing can be done with pseudocode.
C)Hand-tracing will clearly show how long a program will take to execute.
D)The goal of hand-tracing is to double check the answers of the running code.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
40
How many times does the following code snippet display "Loop Execution"?
A)ten times
B)The code snippet does not run because of a compile error.
C)infinite loop
D)only one time

A)ten times
B)The code snippet does not run because of a compile error.
C)infinite loop
D)only one time
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
41
What is the output of the following loop?
A)37
B)36
C)25
D)24

A)37
B)36
C)25
D)24
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
42
What is the output of the following code?
A)1
B)2
C)3
D)4

A)1
B)2
C)3
D)4
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
43
Which loop does not check a condition at the beginning of the loop?
I.the do loop
II.the while loop
III.the for loop
A)I and II
B)I and III
C)I only
D)III only
I.the do loop
II.the while loop
III.the for loop
A)I and II
B)I and III
C)I only
D)III only
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following statements expresses why the following code is considered bad form?
for (rate = 5; years-- > 0; System.out.println(balance))
...
I.unrelated expressions in loop header
II.doesn't match expected for loop idiom
III.loop iteration is not clear
A)II and III only
B)I and II only
C)I and III only
D)I, II, and III
for (rate = 5; years-- > 0; System.out.println(balance))
...
I.unrelated expressions in loop header
II.doesn't match expected for loop idiom
III.loop iteration is not clear
A)II and III only
B)I and II only
C)I and III only
D)I, II, and III
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
45
Which of the following loops executes the statements inside the loop before checking the condition?
A)for
B)while
C)do
D)do-for
A)for
B)while
C)do
D)do-for
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
46
Which statement about this code snippet is accurate?
A)The loop will run 50 times.
B)The loop will never stop.
C)The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance.
D)There is a compilation error.

A)The loop will run 50 times.
B)The loop will never stop.
C)The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance.
D)There is a compilation error.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
47
What is the output of the code snippet given below?
A)abcd
B)bcde
C)bcbcd
D)cdef

A)abcd
B)bcde
C)bcbcd
D)cdef
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
48
What is the output of the following code snippet?
A)100
B)49.50
C)60.5
D)10

A)100
B)49.50
C)60.5
D)10
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
49
Given the following code snippet, what should we change to have 26 alphabet characters in the string str?
A)int c = 'A'
B)str = c + str;
C)c <= 'Z'
D)must change to use a do loop

A)int c = 'A'
B)str = c + str;
C)c <= 'Z'
D)must change to use a do loop
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
50
What is the output of the code snippet given below?
A)no output
B)no output (infinite loop)
C)abcde
D)bcde

A)no output
B)no output (infinite loop)
C)abcde
D)bcde
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
51
Is the code snippet written below legal? (lines are numbered for reference)
A)Yes.
B)No; there should be a semicolon at the end of line 2.
C)No; for i = 4, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error.
D)No; line 4 should have no semicolon at the end.

A)Yes.
B)No; there should be a semicolon at the end of line 2.
C)No; for i = 4, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error.
D)No; line 4 should have no semicolon at the end.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
52
The for loop header can contain multiple variables of the same type and multiple update expressions, separated by commas:
for (int i = 0; j = 3; i <= 5; i++, j--)
Which loop below correctly implements the same loop using a single counter in the loop control?
A) int i=0;
for (int j=3 ; j<=5 ; j++ )
{
i++;
}
B) Int i=3;
for (int j=0 ; j<=5 ; j++ )
{
i++;
}
C) int j=3;
for (int i=0 ; i<=5 ; j--)
{
i++;
}
D) Int j=3;
for (int i=0 ; i<=5 ; i++)
{
. . .
j--;
}
for (int i = 0; j = 3; i <= 5; i++, j--)
Which loop below correctly implements the same loop using a single counter in the loop control?
A) int i=0;
for (int j=3 ; j<=5 ; j++ )
{
i++;
}
B) Int i=3;
for (int j=0 ; j<=5 ; j++ )
{
i++;
}
C) int j=3;
for (int i=0 ; i<=5 ; j--)
{
i++;
}
D) Int j=3;
for (int i=0 ; i<=5 ; i++)
{
. . .
j--;
}
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
53
In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended.
A)do
B)while
C)for
D)if
A)do
B)while
C)for
D)if
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
54
What is the output of the code snippet given below?
A)no output
B)no output (infinite loop)
C)12345
D)2345

A)no output
B)no output (infinite loop)
C)12345
D)2345
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
55
Which of the following for loops is illegal?
A)for (int i = 0; ; ) { }
B)for (int i = 0) { }
C)for (int i = 0, k = 1; ; i++) { }
D)for ( ; ; )
A)for (int i = 0; ; ) { }
B)for (int i = 0) { }
C)for (int i = 0, k = 1; ; i++) { }
D)for ( ; ; )
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
56
How many times does the following loop execute?
A)10
B)9
C)8
D)an infinite number of times

A)10
B)9
C)8
D)an infinite number of times
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
57
What does the following code snippet print?
A)10
B)20
C)30
D)40

A)10
B)20
C)30
D)40
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
58
How many times does the following loop run?
A)0 times
B)1 times
C)2 times
D)4 times

A)0 times
B)1 times
C)2 times
D)4 times
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
59
Which of the following loops executes 8 times?
A)for (years = 8; years >= 0; years--)
B)for(i = 0; i <= 7; i++)
C)for(i = 1; i <= 10; i++)
D)for (int i = 19; i >= 11; i--)
A)for (years = 8; years >= 0; years--)
B)for(i = 0; i <= 7; i++)
C)for(i = 1; i <= 10; i++)
D)for (int i = 19; i >= 11; i--)
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
60
Consider the following loop:
int n=1;
double x=0;
double s;
do
{
s=1.0 /(n * n) ;
x=x+5 ;
n++;
}
while (s>0.01);
System. out. println(x);
Which loop below is an equivalent loop?
A) double x=0;
double s=1;
for (int k=1 ; s>0.01 ; k++ )
{
s=1.0 /(k * k) ;
x=x+s ;
}
System. out. println(x);
B) double x=0;
double s=1;
for (int k=1 ; k<100 ; k++ )
{
s=1.0 /(k * k) ;
x=x+5;
}
System. out.println(x);
C) double x=0;
double s=1;
int k=10;
while (s>0.01)
{
s=1.0 /(k * k) ;
x=x+s ;
k++;
}
System.out. println(x);
D) double x=0;
double s=10;
int k=1;
while (s>0.01)
{
s=1.0 /(k * k) ;
x=x+s;
k++;
}
System.out. println(x);
int n=1;
double x=0;
double s;
do
{
s=1.0 /(n * n) ;
x=x+5 ;
n++;
}
while (s>0.01);
System. out. println(x);
Which loop below is an equivalent loop?
A) double x=0;
double s=1;
for (int k=1 ; s>0.01 ; k++ )
{
s=1.0 /(k * k) ;
x=x+s ;
}
System. out. println(x);
B) double x=0;
double s=1;
for (int k=1 ; k<100 ; k++ )
{
s=1.0 /(k * k) ;
x=x+5;
}
System. out.println(x);
C) double x=0;
double s=1;
int k=10;
while (s>0.01)
{
s=1.0 /(k * k) ;
x=x+s ;
k++;
}
System.out. println(x);
D) double x=0;
double s=10;
int k=1;
while (s>0.01)
{
s=1.0 /(k * k) ;
x=x+s;
k++;
}
System.out. println(x);
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
61
Is the following code snippet legal?
A)Yes, it is legal but does not print anything.
B)Yes, it is legal and prints "Do you think in Java?" once.
C)Yes, it is legal and prints "Do you think in Java?" twice.
D)No, it is not legal and gives a compilation error.

A)Yes, it is legal but does not print anything.
B)Yes, it is legal and prints "Do you think in Java?" once.
C)Yes, it is legal and prints "Do you think in Java?" twice.
D)No, it is not legal and gives a compilation error.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
62
Which of the following statements is correct about a sentinel?
A)A sentinel is a value that creates a bridge between a data set and unrelated input.
B)A sentinel is a value that is part of the data to be processed by the program.
C)A sentinel is a value that terminates a program.
D)A sentinel is a value that indicates the end of an input sequence.
A)A sentinel is a value that creates a bridge between a data set and unrelated input.
B)A sentinel is a value that is part of the data to be processed by the program.
C)A sentinel is a value that terminates a program.
D)A sentinel is a value that indicates the end of an input sequence.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
63
What does the following code snippet display?
A)It displays random Unicode characters.
B)It displays random ASCII characters.
C)Nothing because it has compilation error.
D)It displays the hexadecimal characters between '0' and 'F.'

A)It displays random Unicode characters.
B)It displays random ASCII characters.
C)Nothing because it has compilation error.
D)It displays the hexadecimal characters between '0' and 'F.'
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
64
Which of the loop(s) test the condition after the loop body executes?
I.the for loop
II.the while loop
III.the do loop
A)I only
B)II only
C)III only
D)II and III
I.the for loop
II.the while loop
III.the do loop
A)I only
B)II only
C)III only
D)II and III
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
65
What will be the output of the following code snippet?
A)Hello will be displayed infinitely many times.
B)There will be no output as the code snippet will not compile.
C)There will be no output after successful compilation.
D)Hello will be displayed only once.

A)Hello will be displayed infinitely many times.
B)There will be no output as the code snippet will not compile.
C)There will be no output after successful compilation.
D)Hello will be displayed only once.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
66
How do you fix this code snippet to make it print out the sum when the user enters a letter rather than a double? The user must enter at least one double when the program begins.
A)while (in.hasData());
B)while (!in.hasEnded());
C)while (in.hasNextDouble());
D)while (hasData);

A)while (in.hasData());
B)while (!in.hasEnded());
C)while (in.hasNextDouble());
D)while (hasData);
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
67
Which of the following loop(s) could possibly not enter the loop body at all?
I.the for loop
II.the while loop
III.the do loop
A)I only
B)I and II only
C)II and III only
D)I and III only
I.the for loop
II.the while loop
III.the do loop
A)I only
B)I and II only
C)II and III only
D)I and III only
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
68
Which of the following loops executes exactly 10 times?
A) for (int i=0 ; i<=10 ; i++ ) { }
B) Int i=0;
boolean found = false;
do
{
i++;
if ( i% 10==0)
{
found = true;
}
}
while ( i<10 && ! found);
C) int i = 0;
while (i <=10)
{
i++;
}
D) for (int i=1 ; i<=10 ; i++ )
A) for (int i=0 ; i<=10 ; i++ ) { }
B) Int i=0;
boolean found = false;
do
{
i++;
if ( i% 10==0)
{
found = true;
}
}
while ( i<10 && ! found);
C) int i = 0;
while (i <=10)
{
i++;
}
D) for (int i=1 ; i<=10 ; i++ )
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
69
Which statement, inserted at the indicated blank line, will correctly terminate this loop when the end of input is reached (detected when the user enters the letter Q)?
A)stop;
B)done = 1;
C)exit;
D)done = true;

A)stop;
B)done = 1;
C)exit;
D)done = true;
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
70
How many times does the following loop execute?
A)1 time
B)2 times
C)3 times
D)4 times

A)1 time
B)2 times
C)3 times
D)4 times
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
71
Which of the following should be used when you need to ask a user to enter one data item and repeat the prompt if the data is invalid?
I.the for loop
II.the while loop
III.the do loop
A)I only
B)I and II
C)III only
D)II only
I.the for loop
II.the while loop
III.the do loop
A)I only
B)I and II
C)III only
D)II only
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
72
Suppose that a program asks a user to enter multiple integers, either positive or negative, to do some calculation.The data entry will stop when the user enters a certain value to indicate the end of the data.What value should the program use as the sentinel?
A)0
B)-1
C)999
D)an alphabetic character
A)0
B)-1
C)999
D)an alphabetic character
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
73
What is the output of the code snippet given below?
A)a
B)ae
C)aeiou
D)aei

A)a
B)ae
C)aeiou
D)aei
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
74
What will be the output of the following code snippet?
A)Hello will be displayed infinitely many times.
B)There will be no output as the code snippet will not compile.
C)There will be no output after successful compilation.
D)Hello will be displayed only once.

A)Hello will be displayed infinitely many times.
B)There will be no output as the code snippet will not compile.
C)There will be no output after successful compilation.
D)Hello will be displayed only once.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
75
What is the sentinel value in the following code snippet?

A)0
B)1
C)2
D)-1

A)0
B)1
C)2
D)-1
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
76
Which statement is correct about the execution of the loop in the following code fragment?
A)The loop will execute only when 0 is entered.
B)The execution of the loop is independent of user input.
C)The program prints the count of positive inputs.
D)The loop will execute at least once even if the user has entered the sentinel value.

A)The loop will execute only when 0 is entered.
B)The execution of the loop is independent of user input.
C)The program prints the count of positive inputs.
D)The loop will execute at least once even if the user has entered the sentinel value.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
77
What is the outcome of the following code snippet?
A)No output will be displayed because of a compilation error.
B)Hello will be displayed only once.
C)Hello will be displayed infinite times.
D)No output will be displayed even after successful compilation of the code snippet.

A)No output will be displayed because of a compilation error.
B)Hello will be displayed only once.
C)Hello will be displayed infinite times.
D)No output will be displayed even after successful compilation of the code snippet.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
78
What is the output of this code snippet? 
A) 2 4 7 11 16 22
B) 1 3 5 7 9
C) 2 3 5 6 7
D) 2 4 6 8

A) 2 4 7 11 16 22
B) 1 3 5 7 9
C) 2 3 5 6 7
D) 2 4 6 8
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
79
How many times is the text "Let's have fun with Java." printed when this code snippet is run?
A)1
B)2
C)3
D)10

A)1
B)2
C)3
D)10
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck
80
What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1?
A)The Average Salary: 0.0
B)The Average Salary: 30.0
C)No data!
D)There will be no output as the code snippet will not compile.

A)The Average Salary: 0.0
B)The Average Salary: 30.0
C)No data!
D)There will be no output as the code snippet will not compile.
Unlock Deck
Unlock for access to all 107 flashcards in this deck.
Unlock Deck
k this deck