Deck 6: Loops

Full screen (f)
exit full mode
Question
The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args)
{
Int j = 2;
Int result = 0;
Int number = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a number: ");
Number = reader.nextInt();
While (j <= number / 2) // better is while (j * j <= number)
{
If (number % j == 0)
{
Result = 1;
}
J++;
}
If (result == 1)
{
System.out.println("Number: " + number + " is Not Prime.");
}
Else
{
System.out.println("Number: " + number + " is Prime. ");
}
}

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. // incorrect if number is 1
D) The code snippet will loop forever.
Use Space or
up arrow
down arrow
to flip the card.
Question
What output does this while loop generate? j = 6;
While (j > 0)
{
System.out.print(j + ", ");
J--;
}

A) No output is generated.
B) 6, 5, 4, 3, 2, 1
C) 6, 5, 4, 3, 2, 1,
D) The output is infinite.
Question
How many times does the code snippet below display "Hello"?
Int i = 0;
While (i != 15)
{
System.out.println("Hello");
I++;
}

A) Infinite times
B) 14 times
C) 15 times
D) 16 times
Question
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 15)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);

A) The value of sum is 0
B) The value of sum is 105
C) The value of sum is 120
D) The value of sum is 136
Question
What is the output of the following code snippet?
Int i = 1;
While (i <= 10)
{
System.out.println("Inside the while loop");
I = i + 10;
}

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.
Question
What is the output of the following code snippet?
Int i = 1;
While (i < 20)
{
System.out.print(i + " ");
I = i + 2;
If (i == 15)
{
I = 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
Question
How many times does the following code fragment display "Hi"?
Int i = 10;
While (i >= 0)
{
System.out.println("Hi");
I--;
}

A) 9 times
B) 10 times
C) 11 times
D) 12 times
Question
What are the values of i and j after the following code fragment runs?
Int i = 60;
Int j = 50;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i=" + i + ", j=" + j);

A) i = 1951, j = 0
B) i = 1951, j = 45
C) i = 65, j = 1
D) i = 65, j = 45
Question
How many times does the code snippet given below display "Loop Execution"?
Int i = 1;
While (i != 10)
{
System.out.println ("Loop Execution");
I++;
}

A) Infinite times
B) 8 times
C) 9 times
D) 10 times
Question
What is the output of the code snippet given below? String s = "abcde";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}

A) No output
B) abcd
C) abcde
D) bcde
Question
What is the output of the code snippet given below?
Int i = 0;
While (i != 9)
{
System.out.println("" + i);
I = i + 2;
}

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)
Question
What is the output of the code snippet given below? String s = "12345";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}

A) No output
B) 1234
C) 12345
D) 2345
Question
What are the values of i and j after the following code snippet is run?
Int i = 10;
Int j = 20;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i = " + i + ", j = " + j);

A) i = 45, j = 1
B) i = 351, j = 0
C) i = 351, j = 2
D) i = 1311, j = 35
Question
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
Question
What is the output of the code fragment given below?
Int i = 0;
Int j = 0;
While (i < 125)
{
I = i + 2;
J++;
}
System.out.println(j);

A) 0
B) 62
C) 63
D) The code fragment displays no output because it does not compile.
Question
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 11)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);

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
Question
What is the output of the following code snippet?
Int i = 1;
While (i < 10)
{
System.out.print(i + " ");
I = i + 2;
If (i == 5)
{
I = 9;
}
}

A) 1 3 5
B) 1 3 9
C) 1 3 5 7 9
D) 1 3 5 9
Question
How many times will the following loop run?
Int i = 0;
While (i < 10)
{
System.out.println(i);
I++;
}

A) 0
B) 8
C) 9
D) 10
Question
What is the output of the following code snippet? double a = 2;
Int n = 16;
Double r = 1;
Double b = a;
Int i = n;
While (i > 0)
{
If (i % 2 == 0) // n is even
{
B = b * b;
I = i / 2;
}
Else
{
R = r * b;
I--;
}
}
System.out.println("r = " + r);

A) r = 16.0
B) r = 128.0
C) r = 4096.0
D) r = 65536.0
Question
What is the output of the code fragment given below?
Int i = 0;
Int j = 0;
While (i < 27)
{
I = i + 2;
J++;
}
System.out.println("j=" + j);

A) j=27
B) j=12
C) j=13
D) j=14
Question
What is the output of the code snippet given below?
Int i = 0;
While (i != 11)
{
System.out.print(" " + i);
I = i + 2;
}

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)
Question
What will be the result of running the following code fragment?
Int year = 0;
Double rate = 5;
Double principal = 10000;
Double interest = 0;
While (year < 10)
{
Interest = (principal * year * rate) / 100;
System.out.println("Interest " + interest);
}

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.
Question
What are the values of i and j after the following code snippet executes?
Int i = 60;
Int j = 50;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println(i);
System.out.println(j);

A) i = 65, j = 1
B) i = 65, j = 45
C) i = 1951, j = 0
D) i = 1951, j = 45
Question
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++;
Question
How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}

A) Ten times.
B) The code snippet does not run because of a compile error.
C) Infinite loop.
D) Only one time.
Question
How many times does the loop execute in the following code fragment?
Int i;
For (i = 0; i < 50; i = i + 4)
{
System.out.println(i);
}

A) 11
B) 12
C) 13
D) 14
Question
What is the output of the code snippet given below?
Int i = 0;
While (i != 11)
{
System.out.print(i + " ");
I = i + 3;
}

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)
Question
Select the statement that correctly completes the loop in this code snippet.
Int years = 20;
Double balance = 10000;
While (years > 0)
{
__________
Double interest = balance * rate / 100;
Balance = balance + interest;
}

A) years++;
B) years--;
C) balance++;
D) balance--;
Question
What is the output of the following code snippet?
Int i = 1;
While (i != 9)
{
System.out.print(i + " ");
I++;
If (i == 9)
{
System.out.println("End");
}
}

A) 1 End
B) 1 End (infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (infinite loop)
Question
Which of the following is considered 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++)
Question
What values does counter variable i assume when this loop executes? for (int i = 20; i >= 2; i = i - 6)
{
System.out.print(i + ",");
}

A) 20, 14, 8, 2
B) 20, 14, 8, 2, -4
C) 20, 14, 8
D) 14, 8, 2
Question
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.
Question
What is the output of the code snippet given below?
Int i;
Int j = 0;
For (i = 0; i < 5; i++)
{
If (i % 2 == 0)
{
I = i + 2;
J++;
}
Else
{
I++;
J = j + 2;
}
J++;
}
System.out.println("i=" + i + ", j=" + j);

A) i=7, j =7
B) i =7, j =6
C) i =6, j =7
D) i =5, j =5
Question
What changes do you need to make in the following code snippet to display "Let us learn Java" exactly 10 times?
Int i = 0;
While (i <= 10)
{
System.out.println("Let us learn Java");
I++;
}

A) while (i < 9)
B) while (i < 11)
C) while (i < 12)
D) int i = 1;
Question
When hand tracing, drawing a line through the value stored in a variable means that

A) The value stored there has changed to something new
B) The variable is the wrong data type for the code being executed
C) The expression being evaluated uses that variable
D) The variable must be inside a loop
Question
What is the output of the following code snippet?
Int f1 = 0;
Int f2 = 1;
Int fRes;
System.out.print(f1 + " ");
System.out.print(f2 + " ");
For (int i = 1; i < 10; i++)
{
FRes = f1 + f2;
System.out.print(fRes + " ");
F1 = f2;
F2 = fRes;
}
System.out.println();

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
Question
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.
Question
What is the output of this code snippet? String str = "ABCabc";
Char ch;
Int i = 0;
While (i < str.length())
{
Ch = str.charAt(i);
If (Character.isLowerCase(ch))
{
System.out.print(i + " ");
}
Else
{
I++;
}
}

A) 3 4 5
B) 3
C) 3 3 3 3 3 ... (infinite loop)
D) 0 1 2
Question
When hand-tracing the loop in the code snippet below, which variables are important to evaluate?
Int i = 10;
Int j = 5;
Int k = -10;
Int sum = 0;
While (i > 0)
{
Sum = sum + i + j;
I--;
System.out.println("Iteration: " + i);
}

A) The variables i and j
B) The variables i and sum
C) The variables i, j, and k
D) The variables j and k
Question
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) do
Question
What does the method below return?
Int findSomething (String str)
{
Int position = 0;
While (position < str.length() &&
(str.charAt (position) != 'e'))
{
Position++;
}
Return position;
}

A) The position of the first 'e' in the string or the length of the string if there is no 'e'
B) The position of the last 'e' in the string or the length of the string if there is no 'e'
C) The position of the first character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
D) The position of the last character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
Question
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
Question
What does the following code snippet print?
Int a = 120;
Int b = 90;
Int n1 = Math.abs(a);
Int n2 = Math.abs(b);
Int result = 1;
For (int k = 1; k <= n1 && k <= n2; k++)
{
If (n1 % k == 0 && n2 % k == 0)
{
Result = k;
}
}
System.out.println(result);

A) 10
B) 20
C) 30
D) 40
Question
What will be printed by the statements below?
Int val = 1;
Int sum = 0;
While (val < 5)
{
Sum = sum + val;
Val++;
}
System.out.print (sum);

A)4
B)5
C)10
D)15
Question
Which statement about this code snippet is accurate?
Int years = 50;
Double balance = 10000;
Double targetBalance = 20000;
Double rate = 3;
For (int i = 1; i <= years; i++)
{
If (balance >= targetBalance)
{
I = years + 1;
}
Else
{
Double interest = balance * rate / 100;
Balance = balance + interest;
}
}

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.
Question
What is the output of the following code snippet?
Int counter = 1;
For (double i = 0.01; i <= 1.0; i = i + 0.01)
{
Counter++;
}
System.out.println(counter);

A) 100
B) 49.50
C) 60.5
D) 10
Question
What will be printed by the statements below? for (int ctr = 0; ctr < 10; ctr++)
{
System.out.print (ctr + " ");
}

A)0 1 2 3 4 5 6 7 8 9 10
B)0 1 2 3 4 5 6 7 8 9
C)0 2 4 6 8
D)0 1 3 5 7 9
Question
Which of the following loops will print the odd numbers between 0 and 20? a. int num = 1;
While (num < 10)
{
Int value = num * 2 - 1;
System.out.print (value + " ");
Num ++;
}
B) int num = 1;
While (num < 20)
{
Int value = num * 2 - 1;
System.out.print (value + " ");
Num ++;
}
C) int num = 1;
While (num < 10)
{
System.out.print (num + " ");
Num += 2;
}
D) int num = 1;
While (num < 20)
{
Num += 2;
System.out.print (num + " ");
}
Question
What will be printed by the statements below?
Int val = 1;
Int sum = 0;
While (val < 5)
{
Sum = 0;
Sum = sum + val;
Val++;
}
System.out.print (sum);

A)15
B)10
C)5
D)4
Question
What will be printed by the statements below?
Int a = 10;
While (a > 5)
{
A = a - 2;
System.out.print (a + " ");
}

A)10 8 6
B)10 8 6 4
C)8 6
D)8 6 4
Question
What is the output of the code below? for (int val = 0; val < 4; val ++)
{
System.out.print ("+");
For (int num = 0; num < val; num++)
{
System.out.print ("0");
}
}

A)+0+00+000+0000
B)+000+000+000+000
C)++0+00+000
D)++++000000
Question
What does the following loop compute? Scanner in = new Scanner (System.in);
Int sum = 0;
Int count = 0;
While (in.hasNextInt())
{
Int value = in.nextInt();
If (value > 0)
{
Sum += value;
Count++;
}
}
Double result = (sum * 1.0)/count;

A)The average of all the integers in the input
B)The sum of all the positive integers in the input divided by the number of integers in the input
C) The average of all the positive integers in the input
D)The second smallest value in the input
Question
How many times does the following loop execute? for (double d = 1; d != 10; d++)
{
D = d / 3;
System.out.print(d + " ");
}

A) 10
B) 9
C) 8
D) An infinite number of times
Question
What will be printed by the statements below?
Int a = 10;
While (a > 5)
{
System.out.print (a + " ");
A = a - 2;
}

A)10 9 8 7 6 5
B)10 8 6 4
C)10 8 6
D)10 8
Question
What will be printed by the statements below? for (int ctr = 10; ctr > 5; ctr--)
{
System.out.print (ctr + " ");
}

A)10 9 8 7 6 5
B)10 9 8 7 6
C)5 6 7 8 9 10
D)6 7 8 9 10
Question
Which of the following conditions can be added to the code below so it will loop until the value of sum is greater than 100? Scanner in = new Scanner (System.in);
Int sum = 0;
Do {
Sum += in.nextInt();
}
While (/* put condition here */)

A)sum != 0
B)sum <= 100
C)sum > 100
D)sum == 100
Question
Which of the following is considered a loop with a problematic condition?

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 = 20; i >= 10; i--)
Question
Which of the following loops will print the odd numbers between 0 and 20?

A) int num = 1;
While (num < 20)
{
System.out.print (num + " ");
Num += 2;
}
B) int num = 1;
While (num < 20)
{
System.out.print (num + " ");
Num ++;
}
C)
Int num = 0;
While (num < 20)
{
System.out.print (num + " ");
Num += 2;
}
D)
Int num = 1;
While (num < 20)
{
Num += 2;
System.out.print (num + " ");
}
Question
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 ( ; ; )
Question
Is the code snippet written below legal? String s = "1234";
For (int i = 0; i <= 5; i++)
{
System.out.print);
}

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.
Question
Assume the following variable has been declared and given a value as shown: Random rand = new Random();
Int number = rand.nextInt (27) * 2 + 3;
What are the smallest and largest values number may be assigned?

A)3, 57
B)0, 27
C)3, 55
D)0, 26
Question
How many times is the text "Let's have fun with Java." printed when this code snippet is run?
Int i = 0;
Do
{
System.out.println("Let's have fun with Java.");
I++;
If (i % 2 == 0)
{
I = 10;
}
}
While (i <= 10);

A) 1
B) 2
C) 3
D) 10
Question
Given the following code snippet, what should we change to have 26 alphabet characters in the string str? String str = "";
For (char c = 'A'; c < 'Z'; c++)
{
Str = str + c;
}

A) int c = 'A'
B) str = c + str;
C) c <= 'Z'
D) Must change to use a do loop
Question
What is the output of the following loop?
Int s = 1;
Int n = 1;
Do
{
S = s + n;
N++;
}
While (s < 10 * n);
System.out.println(s);

A) 211
B) 210
C) 120
D) 123
Question
Choose the loop that is equivalent to this loop.
Int n = 1;
Double x = 0;
Double s;
Do
{
S = 1.0 / (n * n);
X = x + s;
N++;
}
While (s > 0.01);

A) double x = 0;
Double s = 1;
For (int k = 1; s > 0.01; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
B) double x = 0;
Double s = 1;
For (int k = 1; k < 100; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
C) double x = 0;
Double s = 1;
Int k = 10;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
}
D) double x = 0;
Double s = 10;
Int k = 1;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
Question
Which of the following loop(s) could possibly not enter the loop body at all?
I) for loop
II) while loop
III) do loop

A) I only
B) I and II only
C) II and III only
D) I and III only
Question
Is the following code snippet legal? boolean b = false;
Do
{
System.out.println("Do you think in Java?");
}
While (b !=

A) Yes, it is legal but does not print anything.
B) Yes, it is legal and prints "Do you think in Java?" once.
B);
C) Yes, it is legal and prints "Do you think in Java?" twice.
D) No, it is not legal and gives a compilation error.
Question
Which of the loop(s) test the condition after the loop body executes?
I) for loop
II) while loop
III) do loop

A) I only
B) II only
C) III only
D) II and III
Question
Assume the following variable has been declared and given a value as shown: Random rand = new Random();
Which of the following will generate a random integer in the range - 20 to 20, inclusive, where each value has an equal chance of being generated?

A)rand.nextInt (-20, 20)
B)rand.nextInt(20) - 41
C)rand.nextInt (-20) + 40
D)rand.nextInt(41) - 20
Question
What is the output of the code snippet given below? String s = "12345";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);

A) No output
B) No output (infinite loop)
C) 12345
D) 2345
Question
How many times does the following loop run?
Int i = 0;
Int j = 1;
Do
{
System.out.println("" + i + ";" + j);
I++;
If (i % 3 == 0)
{
J--;
}
}
While (j >= 1);

A) 1 time
B) 2 times
C) 3 times
D) 4 times
Question
Assume the following variable has been declared and given a value as shown: Random rand = new Random();
Double number = rand.nextDouble () * 2 + 3;
What are the smallest and largest values number may be assigned?

A)3)0, 5.0
B)0)0, 6.0
C)-3.0, 3.0
D)0)0, 3.0
Question
What for loop can be used in the indicated area so the code will print: ****
***
**
*
For (int val = 0; val < 4; val ++)
{
System.out.print ("+");
// Put for loop here
{
System.out.print ("*");
}
System.out.println ();
}

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--)
Question
How many times does the following loop run?
Int i = 0;
Int j = 1;
Do
{
System.out.println("" + i + ";" + j);
I++;
If (i % 2 == 0)
{
J--;
}
}
While (j >= 1);

A) 0 times
B) 1 times
C) 2 times
D) 4 times
Question
What is the output of the code below? for (int val = 0; val < 4; val ++)
{
Int sum = val;
For (int num = 0; num < val; num++)
{
Sum = sum + num;
}
System.out.print (sum + " ");
}

A)0 1 3 6
B)0 1 4 10
C)0 2 5 9
D)0 2 7 16
Question
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
Question
What is the output of the code snippet given below? String s = "abcde";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);

A) No output
B) No output (infinite loop)
C) abcde
D) bcde
Question
What is the output of the code snippet given below? String s = "abcdefghijkl";
Int i = 1;
Do
{
If (i > 2)
{
System.out.print(s.substring (1, i));
}
I++;
}
While (i < 5);

A) abcd
B) bcde
C) bcbcd
D) cdef
Question
What is the result when the following code is run? double x = 1;
Double y = 1;
Int i = 0;
Do
{
Y = x / 2;
X = x + y;
I = i + 1;
}
While (x < 2.5);
System.out.print(i + " ");

A) 1
B) 2
C) 3
D) 4
Question
Which of the following loops executes the statements inside the loop before checking the condition?

A) for
B) while
C) do
D) do-for
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/128
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 6: Loops
1
The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args)
{
Int j = 2;
Int result = 0;
Int number = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a number: ");
Number = reader.nextInt();
While (j <= number / 2) // better is while (j * j <= number)
{
If (number % j == 0)
{
Result = 1;
}
J++;
}
If (result == 1)
{
System.out.println("Number: " + number + " is Not Prime.");
}
Else
{
System.out.println("Number: " + number + " is Prime. ");
}
}

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. // incorrect if number is 1
D) The code snippet will loop forever.
B
2
What output does this while loop generate? j = 6;
While (j > 0)
{
System.out.print(j + ", ");
J--;
}

A) No output is generated.
B) 6, 5, 4, 3, 2, 1
C) 6, 5, 4, 3, 2, 1,
D) The output is infinite.
C
3
How many times does the code snippet below display "Hello"?
Int i = 0;
While (i != 15)
{
System.out.println("Hello");
I++;
}

A) Infinite times
B) 14 times
C) 15 times
D) 16 times
C
4
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 15)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);

A) The value of sum is 0
B) The value of sum is 105
C) The value of sum is 120
D) The value of sum is 136
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
5
What is the output of the following code snippet?
Int i = 1;
While (i <= 10)
{
System.out.println("Inside the while loop");
I = i + 10;
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
6
What is the output of the following code snippet?
Int i = 1;
While (i < 20)
{
System.out.print(i + " ");
I = i + 2;
If (i == 15)
{
I = 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 128 flashcards in this deck.
Unlock Deck
k this deck
7
How many times does the following code fragment display "Hi"?
Int i = 10;
While (i >= 0)
{
System.out.println("Hi");
I--;
}

A) 9 times
B) 10 times
C) 11 times
D) 12 times
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
8
What are the values of i and j after the following code fragment runs?
Int i = 60;
Int j = 50;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i=" + i + ", j=" + j);

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 128 flashcards in this deck.
Unlock Deck
k this deck
9
How many times does the code snippet given below display "Loop Execution"?
Int i = 1;
While (i != 10)
{
System.out.println ("Loop Execution");
I++;
}

A) Infinite times
B) 8 times
C) 9 times
D) 10 times
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
10
What is the output of the code snippet given below? String s = "abcde";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}

A) No output
B) abcd
C) abcde
D) bcde
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
11
What is the output of the code snippet given below?
Int i = 0;
While (i != 9)
{
System.out.println("" + i);
I = i + 2;
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
12
What is the output of the code snippet given below? String s = "12345";
Int i = 1;
While (i < 5)
{
System.out.print);
I++;
}

A) No output
B) 1234
C) 12345
D) 2345
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
13
What are the values of i and j after the following code snippet is run?
Int i = 10;
Int j = 20;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println("i = " + i + ", j = " + j);

A) i = 45, j = 1
B) i = 351, j = 0
C) i = 351, j = 2
D) i = 1311, j = 35
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
14
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
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
15
What is the output of the code fragment given below?
Int i = 0;
Int j = 0;
While (i < 125)
{
I = i + 2;
J++;
}
System.out.println(j);

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 128 flashcards in this deck.
Unlock Deck
k this deck
16
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 11)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);

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 128 flashcards in this deck.
Unlock Deck
k this deck
17
What is the output of the following code snippet?
Int i = 1;
While (i < 10)
{
System.out.print(i + " ");
I = i + 2;
If (i == 5)
{
I = 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 128 flashcards in this deck.
Unlock Deck
k this deck
18
How many times will the following loop run?
Int i = 0;
While (i < 10)
{
System.out.println(i);
I++;
}

A) 0
B) 8
C) 9
D) 10
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
19
What is the output of the following code snippet? double a = 2;
Int n = 16;
Double r = 1;
Double b = a;
Int i = n;
While (i > 0)
{
If (i % 2 == 0) // n is even
{
B = b * b;
I = i / 2;
}
Else
{
R = r * b;
I--;
}
}
System.out.println("r = " + r);

A) r = 16.0
B) r = 128.0
C) r = 4096.0
D) r = 65536.0
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
20
What is the output of the code fragment given below?
Int i = 0;
Int j = 0;
While (i < 27)
{
I = i + 2;
J++;
}
System.out.println("j=" + j);

A) j=27
B) j=12
C) j=13
D) j=14
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
21
What is the output of the code snippet given below?
Int i = 0;
While (i != 11)
{
System.out.print(" " + i);
I = i + 2;
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
22
What will be the result of running the following code fragment?
Int year = 0;
Double rate = 5;
Double principal = 10000;
Double interest = 0;
While (year < 10)
{
Interest = (principal * year * rate) / 100;
System.out.println("Interest " + interest);
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
23
What are the values of i and j after the following code snippet executes?
Int i = 60;
Int j = 50;
Int count = 0;
While (count < 5)
{
I = i + i;
I = i + 1;
J = j - 1;
J = j - j;
Count++;
}
System.out.println(i);
System.out.println(j);

A) i = 65, j = 1
B) i = 65, j = 45
C) i = 1951, j = 0
D) i = 1951, j = 45
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
24
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++;
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
25
How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
26
How many times does the loop execute in the following code fragment?
Int i;
For (i = 0; i < 50; i = i + 4)
{
System.out.println(i);
}

A) 11
B) 12
C) 13
D) 14
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
27
What is the output of the code snippet given below?
Int i = 0;
While (i != 11)
{
System.out.print(i + " ");
I = i + 3;
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
28
Select the statement that correctly completes the loop in this code snippet.
Int years = 20;
Double balance = 10000;
While (years > 0)
{
__________
Double interest = balance * rate / 100;
Balance = balance + interest;
}

A) years++;
B) years--;
C) balance++;
D) balance--;
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
29
What is the output of the following code snippet?
Int i = 1;
While (i != 9)
{
System.out.print(i + " ");
I++;
If (i == 9)
{
System.out.println("End");
}
}

A) 1 End
B) 1 End (infinite loop)
C) 1 2 3 4 5 6 7 8 End
D) 1 2 3 4 5 6 7 8 End (infinite loop)
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
30
Which of the following is considered 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++)
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
31
What values does counter variable i assume when this loop executes? for (int i = 20; i >= 2; i = i - 6)
{
System.out.print(i + ",");
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
32
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.
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
33
What is the output of the code snippet given below?
Int i;
Int j = 0;
For (i = 0; i < 5; i++)
{
If (i % 2 == 0)
{
I = i + 2;
J++;
}
Else
{
I++;
J = j + 2;
}
J++;
}
System.out.println("i=" + i + ", j=" + j);

A) i=7, j =7
B) i =7, j =6
C) i =6, j =7
D) i =5, j =5
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
34
What changes do you need to make in the following code snippet to display "Let us learn Java" exactly 10 times?
Int i = 0;
While (i <= 10)
{
System.out.println("Let us learn Java");
I++;
}

A) while (i < 9)
B) while (i < 11)
C) while (i < 12)
D) int i = 1;
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
35
When hand tracing, drawing a line through the value stored in a variable means that

A) The value stored there has changed to something new
B) The variable is the wrong data type for the code being executed
C) The expression being evaluated uses that variable
D) The variable must be inside a loop
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
36
What is the output of the following code snippet?
Int f1 = 0;
Int f2 = 1;
Int fRes;
System.out.print(f1 + " ");
System.out.print(f2 + " ");
For (int i = 1; i < 10; i++)
{
FRes = f1 + f2;
System.out.print(fRes + " ");
F1 = f2;
F2 = fRes;
}
System.out.println();

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 128 flashcards in this deck.
Unlock Deck
k this deck
37
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.
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
38
What is the output of this code snippet? String str = "ABCabc";
Char ch;
Int i = 0;
While (i < str.length())
{
Ch = str.charAt(i);
If (Character.isLowerCase(ch))
{
System.out.print(i + " ");
}
Else
{
I++;
}
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
39
When hand-tracing the loop in the code snippet below, which variables are important to evaluate?
Int i = 10;
Int j = 5;
Int k = -10;
Int sum = 0;
While (i > 0)
{
Sum = sum + i + j;
I--;
System.out.println("Iteration: " + i);
}

A) The variables i and j
B) The variables i and sum
C) The variables i, j, and k
D) The variables j and k
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
40
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) do
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
41
What does the method below return?
Int findSomething (String str)
{
Int position = 0;
While (position < str.length() &&
(str.charAt (position) != 'e'))
{
Position++;
}
Return position;
}

A) The position of the first 'e' in the string or the length of the string if there is no 'e'
B) The position of the last 'e' in the string or the length of the string if there is no 'e'
C) The position of the first character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
D) The position of the last character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
42
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
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
43
What does the following code snippet print?
Int a = 120;
Int b = 90;
Int n1 = Math.abs(a);
Int n2 = Math.abs(b);
Int result = 1;
For (int k = 1; k <= n1 && k <= n2; k++)
{
If (n1 % k == 0 && n2 % k == 0)
{
Result = k;
}
}
System.out.println(result);

A) 10
B) 20
C) 30
D) 40
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
44
What will be printed by the statements below?
Int val = 1;
Int sum = 0;
While (val < 5)
{
Sum = sum + val;
Val++;
}
System.out.print (sum);

A)4
B)5
C)10
D)15
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
45
Which statement about this code snippet is accurate?
Int years = 50;
Double balance = 10000;
Double targetBalance = 20000;
Double rate = 3;
For (int i = 1; i <= years; i++)
{
If (balance >= targetBalance)
{
I = years + 1;
}
Else
{
Double interest = balance * rate / 100;
Balance = balance + interest;
}
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
46
What is the output of the following code snippet?
Int counter = 1;
For (double i = 0.01; i <= 1.0; i = i + 0.01)
{
Counter++;
}
System.out.println(counter);

A) 100
B) 49.50
C) 60.5
D) 10
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
47
What will be printed by the statements below? for (int ctr = 0; ctr < 10; ctr++)
{
System.out.print (ctr + " ");
}

A)0 1 2 3 4 5 6 7 8 9 10
B)0 1 2 3 4 5 6 7 8 9
C)0 2 4 6 8
D)0 1 3 5 7 9
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
48
Which of the following loops will print the odd numbers between 0 and 20? a. int num = 1;
While (num < 10)
{
Int value = num * 2 - 1;
System.out.print (value + " ");
Num ++;
}
B) int num = 1;
While (num < 20)
{
Int value = num * 2 - 1;
System.out.print (value + " ");
Num ++;
}
C) int num = 1;
While (num < 10)
{
System.out.print (num + " ");
Num += 2;
}
D) int num = 1;
While (num < 20)
{
Num += 2;
System.out.print (num + " ");
}
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
49
What will be printed by the statements below?
Int val = 1;
Int sum = 0;
While (val < 5)
{
Sum = 0;
Sum = sum + val;
Val++;
}
System.out.print (sum);

A)15
B)10
C)5
D)4
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
50
What will be printed by the statements below?
Int a = 10;
While (a > 5)
{
A = a - 2;
System.out.print (a + " ");
}

A)10 8 6
B)10 8 6 4
C)8 6
D)8 6 4
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
51
What is the output of the code below? for (int val = 0; val < 4; val ++)
{
System.out.print ("+");
For (int num = 0; num < val; num++)
{
System.out.print ("0");
}
}

A)+0+00+000+0000
B)+000+000+000+000
C)++0+00+000
D)++++000000
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
52
What does the following loop compute? Scanner in = new Scanner (System.in);
Int sum = 0;
Int count = 0;
While (in.hasNextInt())
{
Int value = in.nextInt();
If (value > 0)
{
Sum += value;
Count++;
}
}
Double result = (sum * 1.0)/count;

A)The average of all the integers in the input
B)The sum of all the positive integers in the input divided by the number of integers in the input
C) The average of all the positive integers in the input
D)The second smallest value in the input
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
53
How many times does the following loop execute? for (double d = 1; d != 10; d++)
{
D = d / 3;
System.out.print(d + " ");
}

A) 10
B) 9
C) 8
D) An infinite number of times
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
54
What will be printed by the statements below?
Int a = 10;
While (a > 5)
{
System.out.print (a + " ");
A = a - 2;
}

A)10 9 8 7 6 5
B)10 8 6 4
C)10 8 6
D)10 8
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
55
What will be printed by the statements below? for (int ctr = 10; ctr > 5; ctr--)
{
System.out.print (ctr + " ");
}

A)10 9 8 7 6 5
B)10 9 8 7 6
C)5 6 7 8 9 10
D)6 7 8 9 10
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
56
Which of the following conditions can be added to the code below so it will loop until the value of sum is greater than 100? Scanner in = new Scanner (System.in);
Int sum = 0;
Do {
Sum += in.nextInt();
}
While (/* put condition here */)

A)sum != 0
B)sum <= 100
C)sum > 100
D)sum == 100
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
57
Which of the following is considered a loop with a problematic condition?

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 = 20; i >= 10; i--)
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
58
Which of the following loops will print the odd numbers between 0 and 20?

A) int num = 1;
While (num < 20)
{
System.out.print (num + " ");
Num += 2;
}
B) int num = 1;
While (num < 20)
{
System.out.print (num + " ");
Num ++;
}
C)
Int num = 0;
While (num < 20)
{
System.out.print (num + " ");
Num += 2;
}
D)
Int num = 1;
While (num < 20)
{
Num += 2;
System.out.print (num + " ");
}
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
59
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 ( ; ; )
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
60
Is the code snippet written below legal? String s = "1234";
For (int i = 0; i <= 5; i++)
{
System.out.print);
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
61
Assume the following variable has been declared and given a value as shown: Random rand = new Random();
Int number = rand.nextInt (27) * 2 + 3;
What are the smallest and largest values number may be assigned?

A)3, 57
B)0, 27
C)3, 55
D)0, 26
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
62
How many times is the text "Let's have fun with Java." printed when this code snippet is run?
Int i = 0;
Do
{
System.out.println("Let's have fun with Java.");
I++;
If (i % 2 == 0)
{
I = 10;
}
}
While (i <= 10);

A) 1
B) 2
C) 3
D) 10
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
63
Given the following code snippet, what should we change to have 26 alphabet characters in the string str? String str = "";
For (char c = 'A'; c < 'Z'; c++)
{
Str = str + c;
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
64
What is the output of the following loop?
Int s = 1;
Int n = 1;
Do
{
S = s + n;
N++;
}
While (s < 10 * n);
System.out.println(s);

A) 211
B) 210
C) 120
D) 123
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
65
Choose the loop that is equivalent to this loop.
Int n = 1;
Double x = 0;
Double s;
Do
{
S = 1.0 / (n * n);
X = x + s;
N++;
}
While (s > 0.01);

A) double x = 0;
Double s = 1;
For (int k = 1; s > 0.01; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
B) double x = 0;
Double s = 1;
For (int k = 1; k < 100; k++)
{
S = 1.0 / (k * k);
X = x + s;
}
C) double x = 0;
Double s = 1;
Int k = 10;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
}
D) double x = 0;
Double s = 10;
Int k = 1;
While (s > 0.01)
{
S = 1.0 / (k * k);
X = x + s;
K++;
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
66
Which of the following loop(s) could possibly not enter the loop body at all?
I) for loop
II) while loop
III) 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 128 flashcards in this deck.
Unlock Deck
k this deck
67
Is the following code snippet legal? boolean b = false;
Do
{
System.out.println("Do you think in Java?");
}
While (b !=

A) Yes, it is legal but does not print anything.
B) Yes, it is legal and prints "Do you think in Java?" once.
B);
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 128 flashcards in this deck.
Unlock Deck
k this deck
68
Which of the loop(s) test the condition after the loop body executes?
I) for loop
II) while loop
III) do loop

A) I only
B) II only
C) III only
D) II and III
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
69
Assume the following variable has been declared and given a value as shown: Random rand = new Random();
Which of the following will generate a random integer in the range - 20 to 20, inclusive, where each value has an equal chance of being generated?

A)rand.nextInt (-20, 20)
B)rand.nextInt(20) - 41
C)rand.nextInt (-20) + 40
D)rand.nextInt(41) - 20
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
70
What is the output of the code snippet given below? String s = "12345";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);

A) No output
B) No output (infinite loop)
C) 12345
D) 2345
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
71
How many times does the following loop run?
Int i = 0;
Int j = 1;
Do
{
System.out.println("" + i + ";" + j);
I++;
If (i % 3 == 0)
{
J--;
}
}
While (j >= 1);

A) 1 time
B) 2 times
C) 3 times
D) 4 times
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
72
Assume the following variable has been declared and given a value as shown: Random rand = new Random();
Double number = rand.nextDouble () * 2 + 3;
What are the smallest and largest values number may be assigned?

A)3)0, 5.0
B)0)0, 6.0
C)-3.0, 3.0
D)0)0, 3.0
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
73
What for loop can be used in the indicated area so the code will print: ****
***
**
*
For (int val = 0; val < 4; val ++)
{
System.out.print ("+");
// Put for loop here
{
System.out.print ("*");
}
System.out.println ();
}

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 128 flashcards in this deck.
Unlock Deck
k this deck
74
How many times does the following loop run?
Int i = 0;
Int j = 1;
Do
{
System.out.println("" + i + ";" + j);
I++;
If (i % 2 == 0)
{
J--;
}
}
While (j >= 1);

A) 0 times
B) 1 times
C) 2 times
D) 4 times
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
75
What is the output of the code below? for (int val = 0; val < 4; val ++)
{
Int sum = val;
For (int num = 0; num < val; num++)
{
Sum = sum + num;
}
System.out.print (sum + " ");
}

A)0 1 3 6
B)0 1 4 10
C)0 2 5 9
D)0 2 7 16
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
76
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
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
77
What is the output of the code snippet given below? String s = "abcde";
Int i = 1;
Do
{
If (i > 1)
{
System.out.print);
}
}
While (i < 5);

A) No output
B) No output (infinite loop)
C) abcde
D) bcde
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
78
What is the output of the code snippet given below? String s = "abcdefghijkl";
Int i = 1;
Do
{
If (i > 2)
{
System.out.print(s.substring (1, i));
}
I++;
}
While (i < 5);

A) abcd
B) bcde
C) bcbcd
D) cdef
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
79
What is the result when the following code is run? double x = 1;
Double y = 1;
Int i = 0;
Do
{
Y = x / 2;
X = x + y;
I = i + 1;
}
While (x < 2.5);
System.out.print(i + " ");

A) 1
B) 2
C) 3
D) 4
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
80
Which of the following loops executes the statements inside the loop before checking the condition?

A) for
B) while
C) do
D) do-for
Unlock Deck
Unlock for access to all 128 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 128 flashcards in this deck.