Exam Questions Ch.6 Loops - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.
Package Title: Testbank
Course Title: Big Java
Chapter Number: 6 Loops
Question type: Multiple Choice
1) 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
Title: How many iterations of while loop?
Difficulty: Easy
Section Reference 1: 6.1 The while Loop
2) 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)
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
3) 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
Title: How many iterations of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
4) 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
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
5) 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
Title: TB 6.6 What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
6) 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.
Title: Will while loop with if/else produce desired result?
Difficulty: Hard
Section reference 1: 6.1 The while Loop
7) 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
Title: What are values of variables after while loop executes?
Difficulty: Hard
Section Reference 1: 6.1 The while Loop
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
Title: What type of error is an off-by-one error?
Difficulty: Easy
Section Reference 1: 6.1 The while Loop
9) 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
Title: How many times does while loop execute?
Difficulty: Easy
Section Reference 1: 6.1 The while Loop
10) 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
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
11) 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.
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
12) 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
Title: How many times does while loop display result?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
13) What is the output of the code snippet given below?
String s = "abcde";
int i = 1;
while (i < 5)
{
System.out.print(s.substring(i, i + 1));
i++;
}
a) No output
b) abcd
c) abcde
d) bcde
Title: What is the output of while loop with substring?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
14) What is the output of the code snippet given below?
String s = "12345";
int i = 1;
while (i < 5)
{
System.out.print(s.substring(i, i + 1));
i++;
}
a) No output
b) 1234
c) 12345
d) 2345
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
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.
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
16) 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
Title: What is output of while loop with nested if?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
17) 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
Title: What are values of i and j after while loop?
Difficulty: Hard
Section Reference 1: 6.1 The while Loop
18) 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
Title: What is the output of while loop that sums?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
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
Title: What is the output of while loop with nested if?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
20) 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.
Title: What output does this while loop generate?
Difficulty: Medium
Section Ref: 6.1 while Loops
21) 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
Title: What is the output of code snippet with while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
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.
Title: What is result of while loop?
Difficulty: Easy
Section Reference 1: 6.1 The while Loop
23) 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++;
Title: Which while loop executes 10 times?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
24) 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)
Title: What is output of while loop?
Difficulty: Hard
Section Reference 1: 6.1 The while Loop
25) 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;
Title: What changes needed so while loop executes 10 times?
Difficulty: Easy
Section Reference 1: 6.1 The while Loop
26) 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)
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
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)
Title: What is output of while loop?
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
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--;
Title: Insert appropriate code in while loop
Difficulty: Medium
Section Reference 1: 6.1 The while Loop
29) 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
Title: Which variables are important to monitor when hand-tracing a code snippet?
Difficulty: Easy
Section Reference 1: 6.2
30. 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
Title: What does it mean to draw a line through values when hand-tracing?
Difficulty: Medium
Section Reference 1: 6.2
31. 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.
Title: Which statement about Boolean conditions is true?
Difficulty: Medium
Section Reference 1: 6.2
32. 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
Title: What are values of variables after while loop executes?
Difficulty: Hard
Section Reference 1: 6.2 Problem Solving: Hand-Tracing
33. 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.
Title: Why is hand tracing valuable?
Difficulty: Easy
Section Reference 1: 6.2 Problem Solving: Hand-Tracing
34) 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
Title: How many times does for loop execute?
Difficulty: Easy
Section Reference 1: 6.3 The for Loop
35) 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.
Title: How many times does for loop execute?
Difficulty: Hard
Section Reference 1: 6.3 The for Loop
36) 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
Title: What is output of for loop with nested if/else?
Difficulty: Hard
Section Reference 1: 6.3 The for Loop
37) 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++)
Title: Loop with dubious bound
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
38) 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
Title: In which loop header can you declare multiple update expressions …?
Difficulty: Easy
Section Reference 1: 6.4 The do Loop
39) 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
Title: Which values does the counter variable assume in for loop?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
40) 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
Title: What is the output of for loop?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
41) 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
Title: What is the output of for loop with floating-point bound
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
42) 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
Title: What does complex for loop with nested if print?
Difficulty: Hard
Section Reference 1: 6.3 The for Loop
43) 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 ( ; ; )
Title: Which of the following for loops is illegal?
Difficulty: Hard
Section Reference 1: 6.3 The for Loop
44) 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
Title: How many times does the following loop execute?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
45) 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
Title: Why is this loop considered bad style?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
46) 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--)
Title: Which is a loop with a problematic condition?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
47) Is the code snippet written below legal?
String s = "1234";
for (int i = 0; i <= 5; i++)
{
System.out.print(s.substring(i, i + 1));
}
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.
Title: Are there errors in for loop?
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
48) 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.
Title: For loop with inside if statement
Difficulty: Medium
Section Reference 1: 6.3 The for Loop
49) 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
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
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
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
51) 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
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
52) 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
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
53) 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
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
54) 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
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
55) 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 + " ");
}
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
56) Which of the following loops will print the odd numbers between 0 and 20?
- int num = 1;
while (num < 10)
- int num = 1;
{
int value = num * 2 – 1;
System.out.print (value + " ");
num ++;
}
- int num = 1;
while (num < 20)
- int num = 1;
{
int value = num * 2 – 1;
System.out.print (value + " ");
num ++;
}
- int num = 1;
while (num < 10)
- int num = 1;
{
System.out.print (num + " ");
num += 2;
}
- int num = 1;
while (num < 20)
- int num = 1;
{
num += 2;
System.out.print (num + " ");
}
Title: What will be printed by the statements below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
57) 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
Title: 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?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
58) 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
Title: What does the following loop compute?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
59) 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'
Title: What does the method below return?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
60) 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
Title: What is the output of the code below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
61) 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
Title: What is the output of the code below?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
62) 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--)
Title: What for loop can be used in the indicated area so the code will print?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
63) 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
Title: 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?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
64) 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
Title: What are the smallest and largest values number may be assigned?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
65) 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
Title: What are the smallest and largest values number may be assigned?
Difficulty: Medium
Section Reference 1: 6.3 Common Array Algorithms
66) Which of the following loops executes the statements inside the loop before checking the condition?
a) for
b) while
c) do
d) do-for
Title: Which loop executes before checking condition?
Difficulty: Easy
Section Reference 1: 6.4 The do Loop
67) 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
Title: TB 6.15 Which loop does not check condition before executing?
Difficulty: Easy
Section Reference 1: 6.4 The do Loop
68) 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
Title: How many times does do loop with nested if execute?
Difficulty: Hard
Section Reference 1: 6.4 The do Loop
69) 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
Title: What is result of do loop with calculation?
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
70) 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
Title: Using for loop with character as loop variable
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
71) What is the output of the code snippet given below?
String s = "abcde";
int i = 1;
do
{
if (i > 1)
{
System.out.print(s.substring(i, i + 1));
}
}
while (i < 5);
a) No output
b) No output (infinite loop)
c) abcde
d) bcde
Title: What is output of do loop with nested if?
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
72) What is the output of the code snippet given below?
String s = "12345";
int i = 1;
do
{
if (i > 1)
{
System.out.print(s.substring(i, i + 1));
}
}
while (i < 5);
a) No output
b) No output (infinite loop)
c) 12345
d) 2345
Title: What is output of do loop with nested if?
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
73) 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
Title: What is output of do loop with nested if?
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
74) 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++;
Title: Choose the loop that is equivalent to this loop.
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
75) 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
Title: What is the output of the following do loop?
Difficulty: Hard
Section Reference 1: 6.4 The do Loop
76) 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
Title: Which loop(s)may not enter loop body at all?
Difficulty: Easy
Section Reference 1: 6.4 The do Loop
77) 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
Title: Which loop(s) test the condition after the loop body?
Difficulty: Easy
Section Reference 1: 6.4 The do Loop
78) 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
Title: How many times does do loop with nested if execute?
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
79) Is the following code snippet legal?
boolean b = false;
do
{
System.out.println("Do you think in Java?");
}
while (b != b);
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.
Title: Are there errors in do loop?
Difficulty: Hard
Section Reference 1: 6.4 The do Loop
80) 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
Title: How many times does do loop with nested if run?
Difficulty: Hard
Section Reference 1: 6.4 The do Loop
81) What is the output of the code snippet given below?
String s = "aeiou";
int i = 0;
do
{
System.out.print(s.substring(i, i + 1));
i++;
if (i >= 3)
{
i = 5;
}
}
while (i < 5);
a) a
b) ae
c) aeiou
d) aei
Title: What is output of do loop with nested if?
Difficulty: Hard
Section Reference 1: 6.4 The do Loop
82) What is the last output line of the code snippet given below?
int i = 5;
while (i >= 1)
{
int num = 1;
for (int j = 1; j <= i; j++)
{
System.out.print(num + "**");
num = num * 2;
}
System.out.println();
i = i - 1;
}
a) No output
b) 1**2
c) 1**2**3
d) 1**
Title: What is output of nested loops?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
83) What is the output of this code snippet?
int s = 1;
int n = 1;
do
{
s = s + n;
System.out.print(s + " ");
n++;
}
while (s < 3 * n);
a) 2 4 7 11 16 22
b) 1 3 5 7 9
c) 2 3 5 6 7
d) 2 4 6 8
Title: What is output of do-while loop?
Difficulty: Medium
Section Reference 1: 6.4 The do Loop
84) What is the sentinel value in the following code snippet?
public static void main(String[] args)
{
int age = 0;
int sumOfAges = 0;
int stop = 1;
Scanner reader = new Scanner(System.in);
System.out.println("Enter an age (-1 to stop): ");
age = reader.nextInt();
while (age != -1)
{
sumOfAges = sumOfAges + age;
System.out.println("Enter an age (-1 to stop): ");
age = reader.nextInt();
}
System.out.println("Sum of ages " + sumOfAges);
return 0;
}
a) 0
b) 1
c) 2
d) -1
Title: Which is the sentinel in this snippet?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
85) 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?
public static void main(String[] args)
{
double sum = 0;
int count = 0;
double salary = 0;
double average = 0;
Scanner reader = new Scanner(System.in);
System.out.println("Enter salaries (-1 to stop): ");
while (salary != -1)
{
salary = reader.nextInt();
if (salary != -1)
{
sum = sum + salary;
count++;
}
}
if (count > 0)
{
average = sum / count;
System.out.println("The Average Salary: " + average);
}
else
{
System.out.println ("No data!");
}
return 0;
}
a) The Average Salary: 0
b) The Average Salary: 30
c) The Average Salary: 24.83333
d) There will be no output as the code snippet will not compile.
Title: What is output of snippet with input that includes a sentinel?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
86) What will be the output of the following code snippet?
boolean token = false;
while (token)
{
System.out.println("Hello");
}
a) “Hello” will be displayed infinite times.
b) No output because of compilation error.
c) No output after successful compilation.
d) “Hello” will be displayed only once.
Title: What is output of while loop with Boolean condition?
Difficulty: Easy
Section Reference 1: 6.5 Application: Processing Sentinel Values
87) What will be the output of the following code snippet?
boolean token = false;
do
{
System.out.println("Hello");
}
while (token);
a) “Hello” will be displayed infinite times.
b) No output because of compilation error.
c) No output after successful compilation.
d) “Hello” will be displayed only once.
Title: What is output of do loop with Boolean condition?
Difficulty: Easy
Section Reference 1: 6.5 Application: Processing Sentinel Values
88) What is the outcome of the following code snippet?
boolean val1 = true;
boolean val2 = false;
while (val1)
{
if (val1)
{
System.out.println("Hello");
}
val1 = val2;
}
a) No output will be displayed because of 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.
Title: TB 6.25 What is output of while loop with nested if and Boolean conditions?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
89) Insert a statement that will correctly terminate this loop when the end of input is reached.
boolean done = false;
while (!done)
{
String input = in.next();
if (input.equalsIgnoreCase("Q"))
{
__________
}
else
{
double x = Double.parseDouble(input);
data.add(x);
}
}
a) stop;
b) done = 1;
c) exit;
d) done = true;
Title: Insert code to terminate a loop
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
90) What does the following code snippet display?
char ch1 = '\u0000';
char ch2 = '\uffff';
for (int i = 0; i < 1000; i++)
{
if (i % 50 == 0)
{
System.out.println();
}
System.out.print((char)(ch1 + Math.random() * (ch2 - ch1 + 1)));
}
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'.
Title: What does for loop with character creation display?
Difficulty: Hard
Section Reference 1: 6.5 Application: Processing Sentinel Values
91) 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++)
Title: Which of the following loops executes exactly 10 times?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
92) 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 code use as the sentinel?
a) 0
b) -1
c) 999
d) An alphabetic character
Title: What should be the sentinel value in this situation?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
93) How do you fix this code snippet to make it print out the sum when the user enters Q?
System.out.print("Enter a value, Q to quit: ");
double sum = 0;
Scanner in = new Scanner(System.in);
boolean hasData = true;
do
{
double value = in.nextDouble();
sum = sum + value;
System.out.print("Enter a value, Q to quit: ");
}
while (in.hasNext());
System.out.println("sum " + sum);
a) while (in.hasData());
b) while (!in.hasEnded());
c) while (in.hasNextDouble());
d) while (hasData);
Title: How do you fix code snippet to print when the user enters Q?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
94) Which of the following loop(s) 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. for loop
II. while loop
III. do loop
a) I only
b) I and II
c) III only
d) II only
Title: Which loop(s) to use for data validation and retry?
Difficulty: Easy
Section Reference 1: 6.5 Application: Processing Sentinel Values
95) 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.
Title: Which statement about sentinels is correct?
Difficulty: Easy
Section Reference 1: 6.5 Application: Processing Sentinel Values
96) Which statement is correct about the execution of the loop in the following code fragment?
double num;
int incr = 0;
Scanner reader = new Scanner(System.in);
do
{
incr = incr + 1;
System.out.println("Please enter a number (0 when done): ");
num = reader.nextDouble();
}
while (num != 0);
System.out.println("" + incr);
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.
Title: Which statement is true about do loop with user input?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
97) What will be the output of the following code snippet?
boolean token1 = true;
while (token1)
{
for (int i = 0; i < 10; i++)
{
System.out.println("Hello");
}
token1 = false;
}
a) No output.
b) Hello will be displayed 10 times.
c) Hello will be displayed 9 times.
d) Hello will be displayed infinite times.
Title: What is output of while loop with Boolean condition?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
98) When will the loop in the following code snippet stop?
java.util.Scanner in = new java.util.Scanner(System.in);
double sum = 0;
int count = 0;
System.out.print("Enter values, Q to quit: ");
do
{
double value = in.nextDouble();
sum = sum + value;
count++;
System.out.print("Enter values, Q to quit: ");
}
while (in.hasNextDouble() && count < 100);
I. When the user enters an integer
II. When the user enters an alphabetic character
III. After the user enters 100 numbers
a) I or II
b) II only
c) III only
d) II or III
Title: When does do loop with sentinel stop?
Difficulty: Medium
Section Reference 1: 6.5 Application: Processing Sentinel Values
99) Storyboards are a helpful part of the design process because the storyboard develops
a) A pseudocode description of the algorithm being designed
b) The mathematical formulas required for computing a correct answer
c) The information needed to solve the problem, and how to present that information
d) The amount of time and space needed to find a solution
Title: What is the role of the storyboard?
Difficulty: Easy
Section Reference 1: 6.6 Problem Solving: Storyboards
100) When designing storyboards, it is a good idea to use different colors to
a) Make it easy to distinguish between user input and program output.
b) Match the colors your program will use when it is finally designed.
c) Emphasize the difference between numbers and words.
d) Draw lines to divide up panels into different regions.
Title: What is the role of colors when designing using storyboards?
Difficulty: Easy
Section Reference 1: 6.6 Problem Solving: Storyboards
101) Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed?
a) Hand-tracing can confirm code that implements gear selection.
b) Pseudocode can guide algorithm design through divide-and-conquer strategy.
c) A storyboard can be used.
d) The physical gears can lead to ideas for the correct algorithm to use.
Title: How can user interaction be designed for an example problem?
Difficulty: Medium
Section Reference 1: 6.6 Problem Solving: Storyboards
102) Which statement about storyboards is true?
a) A storyboard can help prevent potential user confusion early in the design process.
b) Storyboards are used primarily to understand how implemented programs work.
c) The storyboard helps to train users about how to use software.
d) Storyboards have no relationship to the structure of an actual working program.
Title: Which statement about storyboards is true?
Difficulty: Medium
Section Reference 1: 6.6 Problem Solving: Storyboards
103) How many times does the following loop execute?
int upperCaseLetters = 0;
String str = "abcdEfghI";
boolean found = false;
for (int i = 0; i < str.length() && !found; i++)
{
char ch = str.charAt(i);
if (Character.isUpperCase(ch))
{
found = true;
}
}
a) 9 times
b) 8 times
c) 5 times
d) 1 time
Title: Finding the first match
Difficulty: Easy
Section Reference 1: 6.7 Common Loop Algorithms
104) What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5?
double total = 0;
boolean hasValidNumber = true;
Scanner in = new Scanner(System.in);
while (in.hasNextDouble() && hasValidNumber)
{
double input = in.nextDouble();
if (input < 0)
{
hasValidNumber = false;
}
else
{
total = total + input;
}
}
System.out.println(total);
a) 15.0
b) 14.0
c) 12.0
d) 10.0
Title: What is the output of this code snippet with this user input?
Difficulty: Medium
Section Reference 1: 6.7 Common Loop Algorithms
105) What is the output of this loop?
int i = 0;
boolean found;
while (i < 20 && !found)
{
int sum = i * 2 + i * 3;
System.out.print(sum + " ");
if (sum > 50)
{
found = true;
}
i++;
}
a) 0 5 10 15 20 25 30 35 40 45 50 55
b) 0
c) No output, compilation error
d) 0 5 10
Title: What is the output of loop with Boolean?
Difficulty: Hard
Section Reference 1: 6.7 Common Loop Algorithms
106) How many times does the following loop execute?
int i = 0;
boolean found = false;
while (i < 100 && !found)
{
i++;
System.out.print(i + " ");
int j = i * i;
if ((i * i * i) % j == j)
{
found = true;
}
}
a) 10 times
b) 20 times
c) 100 times
d) An infinite number of times
Title: How many times does the following loop execute?
Difficulty: Hard
Section Reference 1: 6.7 Common Loop Algorithms
107) Which code snippet produces the sum of the first n even numbers?
a)
int sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
sum = sum + i;
}
}
b)
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum = sum + i * 2;
}
c)
int sum = 0;
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
sum = sum + i;
}
}
d)
int sum;
for (int i = 1; i <= n; i++)
{
sum = sum + i * 2;
Title: Which code snippet produces the sum of the first n even numbers?
Difficulty: Hard
Section Reference 1: 6.7 Common Loop Algorithms
108) Which of the following loops will print the odd numbers between 0 and 20?
a)for (int num = 1; num < 20; num++)
{
System.out.print (num % 2 + " ");
}
b)for (int num = 0; num < 20; num+= 2)
{
System.out.print (num + " ");
}
c)for (int num = 1; num < 20; num+= 2)
{
System.out.print (num + " ");
}
d)for (int num = 0; num < 20; num+= 2)
{
if (num % 2 == 1)
System.out.print (num + " ");
}
Title: Which of the following loops will print the odd numbers between 0 and 20?
Difficulty: Medium
Section Reference 1: 6.7 Two Dimensional Arrays
109) Which of the following conditions can be added to the code below so it will loop until the user enters "no" or "NO"?
Scanner in = new Scanner (System.in);
System.out.print ("Continue? ");
String response = in.next();
while (/* put condition here */)
{
System.out.println ("Hello beautiful!");
System.out.print ("Continue? ");
response = in.next();
}
a)response.equals("no") != response.equals("NO")
b)!(response.equals("no") || response.equals("NO"))
c)!response.equals("no") || !response.equals("NO")
d)!(response.equals("no") && response.equals("NO"))
Title: Which of the following conditions can be added to the code below so it will loop until the user enters "no" or "NO’?
Difficulty: Medium
Section Reference 1: 6.7 Two Dimensional Arrays
110) For which input values will the following loop not correctly compute the maximum of the values?
Scanner in = new Scanner (System.in);
int max = 0;
while (in.hasNextInt())
{
int value = in.nextInt();
if (value > max)
{
max = value;
}
}
a)3 3 3 3
b)0 1 2 9 107
c)107 2 9 -3
d)-4 -2 -10 -7
Title: For which input values will the following loop not correctly compute the maximum of the values?
Difficulty: Medium
Section Reference 1: 6.7 Two Dimensional Arrays
111) A loop inside another loop is called:
a) A sentinel loop
b) A nested loop
c) A parallel loop
d) A do/while loop
Title: What is a loop inside another loop?
Difficulty: Easy
Section Reference 1: 6.8 Nested Loops
112) What is the first and last value of i to be displayed by the following code snippet?
int n = 20;
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.println("" + i);
}
}
a) 0 and 20
b) 1 and 20
c) 0 and 19
d) 1 and 19
Title: What are first and last values displayed by nested for loop?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
113) How many times will the output line be printed in the following code snippet?
for (int num2 = 1; num2 <= 3; num2++)
{
for (int num1 = 0; num1 <= 2; num1++)
{
System.out.println("" + num2 + " " + num1);
}
}
a) 3 times
b) 6 times
c) 9 times
d) 12 times
Title: How many times will inner for loop execute?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
114) In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop?
int i;
int j;
for (i = 0; i <= 9; i++)
{
for (j = 1; j < 5; j++)
{
System.out.println("Hello");
if (j == 2)
{
j = 6;
}
}
}
a) When the value of j becomes 6
b) When the program executes completely
c) When the condition for the outer loop is met
d) When the value of i is incremented
Title: When does execution switch from inner to outer loop?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
115) What is the last output line of the code snippet given below?
int i = 0;
while (i < 10)
{
int num = 1;
for (int j = i; j > 1; j--)
{
System.out.print(j + " ");
num = num * 2;
}
System.out.println("***");
i++;
}
a) 3 2 ***
b) 9 8 7 6 5 4 3 2 ***
c) 8 7 6 5 4 3 2 ***
d) 2 ***
Title: What is output of nested loops?
Difficulty: Hard
Section Reference 1: 6.8 Nested Loops
116) Which for loop prints data across each row in the following code snippet?
int i;
int j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
System.out.print("X");
}
System.out.println("");
}
a) The inner for loop
b) The outer for loop
c) Both for loops
d) Another missing for loop
Title: Which for loop in snippet represents rows?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
117) What will be the output of the following code snippet?
int i;
int j;
for (i = 0; i < 7; i++)
{
for (j = 7; j > i; j--)
{
System.out.print("*");
}
System.out.println("");
}
a) A rectangle with six rows and seven columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop.
b) A right triangle with six rows and seven columns of asterisks. The number of columns increments by one on completion of one iteration of the inner loop.
c) A rectangle with seven rows and six columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop.
d) A right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.
Title: What is output of nested for loops?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
118) In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop?
int i;
int j;
for (i = 0; i <= 9; i++)
{
for (j = 1; j < 5; j++)
{
System.out.println("Hello");
}
}
a) When the value of j becomes 5
b) When the program executes completely
c) When the condition for the outer loop is met
d) When the value of i is incremented
Title: When does execution switch from inner to outer loop in snippet?
Difficulty: Medium
Section Reference 1: 6.8 Nested Loops
119) Which of the following activities can be simulated using a computer?
I. Waiting time in a line at a restaurant
II. Tossing a coin
III. Shuffling cards for a card game
a) I only
b) II only
c) I and II only
d) I, II, and III
Title: Which activities can be computer simulated?
Difficulty: Easy
Section Reference 1: 6.9 Application: Random Numbers and Simulations
120) What will be the range of the random numbers generated by the following code snippet?
Random generator = new Random();
int r1 = generator.nextInt(50) + 1;
a) Between 1 and 49
b) Between 0 and 50
c) Between 0 and 49
d) Between 1 and 50
Title: What is range of random numbers generated by snippet?
Difficulty: Easy
Section Reference 1: 6.9 Application: Random Numbers and Simulations
121) Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities? (Assume Random generator = new Random();)
a) int sum = generator.nextInt(6) + generator.nextInt(6) + 2
b) int sum = generator.nextInt(12) + 1
c) int sum = generator.nextInt(6 + 1) + generator.nextInt(6 + 1)
d) int sum = generator.nextInt(11) + 2
Title: Which code simulates throwing two dice and summing the result?
Difficulty: Hard
Section Reference 1: 6.9 Application: Random Numbers and Simulations
122) What is the data type of the number generated by the Random.nextDouble() method?
a) double
b) float
c) int
d) String
Title: What is type returned by random generator method?
Difficulty: Easy
Section Reference 1: 6.9 Application: Random Numbers and Simulations
123) What does the following code do?
int sum = 0;
final double count = 1000;
Random generator = new Random();
for (int i = 1; i <= count; i++)
{
sum = sum + generator.nextInt(101);
}
System.out.println(sum / count);
a) It simulates the outcome of throwing a coin.
b) It calculates the average of 1000 random numbers between 0 and 100.
c) It performs Monte Carlo simulation of fluid dynamics.
d) It calculates the average of 1000 random numbers between 1 and 101.
Title: What does code snippet with random numbers do?
Difficulty: Medium
Section Reference 1: 6.9 Application: Random Numbers and Simulations
124) How many times does the following loop execute?
double d;
Random generator = new Random();
double x = generator.nextDouble() * 100;
do
{
d = Math.sqrt(x) * Math.sqrt(x) - x;
System.out.println(d);
x = generator.nextDouble() * 10001;
}
while (d != 0);
a) Exactly once
b) Exactly twice
c) Can't be determined
d) Always infinite loop
Title: How many times does do loop execute?
Difficulty: Hard
Section Reference 1: 6.9
125) What does the following code snippet display?
char ch1 = '\u0000';
char ch2 = '\uffff';
Random generator = new Random();
for (int i = 0; i < 1000; i++)
{
if (i % 50 == 0)
{
System.out.println();
}
System.out.print((char)(ch1 + generator.nextDouble() * (ch2 - ch1 + 1)));
}
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'.
Title: What does for loop with character creation display?
Difficulty: Hard
Section Reference 1: 6.9 Application: Random Numbers and Simulations
126) Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities? (Assume Random generator = new Random();)
a) System.out.println(generator.nextInt(1) + " " + generator.nextInt(2));
b) System.out.println((generator.nextDouble() + 2) * 2);
c) System.out.println(generator.nextInt(1) + " " + generator.nextInt(1));
d) System.out.println(generator.nextInt(2) + " " + generator.nextInt(2));
Title: Which is correct for simulating the toss of a pair of coins?
Difficulty: Hard
Section Reference 1: 6.9 Application: Random Numbers and Simulations
127) Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the expressions simulates that random number? (Assume Random generator = new Random();)
a) generator.nextDouble() * 1000000
b) generator.nextDouble() * 100000
c) generator.nextDouble() * 9999990 + 1
d) generator.nextDouble() * 1000000 + 1
Title: Which code simulates a one in a million chance?
Difficulty: Hard
Section Reference 1: 6.9 Application: Random Numbers and Simulations
128) Which of the following code snippets will generate a random number between 0 (inclusive) and 79 (inclusive)? (Assume Random generator = new Random();)
a) int val = generator.nextInt(79) + 1;
b) int val = generator.nextInt(80) - 1;
c) int val = generator.nextInt(79);
d) int val = generator.nextInt(80);
Title: Which code generates random numbers 0-79?
Difficulty: Easy
Section Reference 1: 6.9 Application: Random Numbers and Simulations