WorksheetsCS 241 Practice Test Week 3
Total questions: 54
Worksheet time: 4hrs 13mins
What would be printed?
10 9 8 7 6 5 4 3 2
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
If the value of x at the end of this statement is 6, which of the following statements should replace /*Missing If Body*/?
x ← x - 4
x ← x - 3
x ← x - 5
x ← x - 2
If the value of x at the end of this statement is 9, which of the following statements should replace /*Missing If Body*/?
x ← x - 4
x ← x - 5
x ← x - 3
x ← x - 2
If the value of result at the end of the method call f(5) is 120, which of the following statements should replace /*Missing For Body*/?
result ← result * num
result ← result - i
result ← result / i
result ← result * i
If the value of result at the end of the method call tenthPower(2) is 1024, which of the following statements should replace /*Missing For Body*/?
result ← result - i
result ← result * num
result ← result * i
result ← result / num
How many times will the action statement happen in the loop shown?
27
25
26
24
What is the output of this loop process?
****24
****23
*****24
*****23
How many stars will be output by this loop?
Infinitely many
5
6
7
None
What would be the output of the main method?
0 2
2
0 2 4
2 4
Which of the following loops will produce the same output?
int x ← 75
while (x < 40) print x x ← x - 5
end while
int x ← 75
while (x ≥ 40) print x x ← x - 10
end while
int x ← 80
while (x > 45) x ← x - 5 print x
end while
int x ← 80
while (x > 40) x ← x - 10 print x
end while
Which of the following loops would produce the same output as the for loop in the factorial method?
int x ← 1
while (x ≤ num) result ← result * x x ← x + 1
end while
int x ← 0
while (x ≤ num) result ← result * x x ← x + 1
end while
int x ← 1
while (x < num) result ← result * x x ← x + 1
end while
int x ← 1
while (x ≤ num) x ← x + 1 result ← result * x
end while
Given the flowchart above, which of these loops describes the program?
int x ← 5 int y ← 20
while (y > x) y ← y - 1
end while
int x ← 5 int y ← 20
while (y ≥ x) y ← y - 1
end while
int x ← 20 int y ← 5
while (y ≥ x) y ← y - 1
end while
int x ← 5 int y ← 20
while (y ≤ x) y ← y - 1
end while
Given the flowchart above, which of these loops describes the program?
int j ← 5
while (j > 20) j ← j - 5
end while
int j ← 5
while (j ≤ 5) j ← j + 1
end while
int j ← 5
while (j ≤ 20) j ← j + 5
end while
int j ← 5
while (j ≥ 5) y ← j + 5
end while
Which of these loop styles is considered a pretest loop?
I. For
II. While
III. do While
III only
All three
I and II
I only
II only
What is output by this pseudocode segment?
10
0
15
6
21
What is output by this pseudocode segment?
No output due to an infinite loop
-55
45
55
-45
Given the flowchart above, which of these loops describes the program?
int j ← -5
do { print j j ← j + 1 }
while (j ≤ 0)
int j ← -5
do { j ← j + 1 print j }
while (j ≤ 0)
int j ← -5
do { print j j ← j + 1 }
while (j ≥ 0)
int j ← -5
while (j < 0) print j j ← j + 1
end while
What would be the output of the main method?
10 8 6 4 2 0 -2
8 6 4 2 0
-2
10 8 6 4 2 0
What would be the output of the main method?
10 5 3 1
10 5 3
10 5 3 1 -1
5 3 1 -1
What would be the output of the main method?
Nothing
10
10 8
8
Which of the following loops will produce the same output?
int x ← 110
do { x ← x - 10 print x
while (x > 10)
int x ← 100
do { x ← x - 10 print x
while (x > 0)
int x ← 100
do { x ← x - 10 print x
while (x > 10)
int x ← 110
do { print x x ← x - 10
while (x > 10)
Which of the following loops will produce the same output?
int x ← 10
do { print x + " " x ← x + 5
while (x < 30)
int x ← 10
do { x ← x + 5 print x + " "
while (x <= 30)
int x ← 30
do { print x + " " x ← x - 5
while (x > 0)
int x ← 10
do { x ← x + 5 print x + " "
while (x < 30)
Which of these loop styles is considered a posttest loop?
I. for
II. while
III. do While
All three
III only
I only
II only
I and II
******21
*****20
*****21
******20
How many stars will be output by this loop?
None
5
Infinitely Many
7
6
Which of the following loops will produce the same output?
for ( int x ← 15; x >= 1; x ← x - 3 )
print x x ← x - 5
end for
int x ← 15 while ( x > 0 )
if (x > 12) x ← x - 5 print x
else x ← x - 3 end if end while
int x ← 15 while ( x >= 0 )
if (x > 12) x ← x - 5
else x ← x - 3 end if print x end while
for ( int x ← 15; x >= 0; x ← x - 5 )
x ← x - 5
print x end for
Which of the following loops will produce the same output?
int x ← 10
do { print x if (x % 5 == 0) x ← x + 2 else x ← x + 5 end if }
while (x < 20)
int x ← 10
for(x % 5 == 0; x < 20; x ← x + 2) x ← x + 5 print x
end for
int x ← 10
for(x; x % 5 == 0; x ← x + 5) print x
end for
int x ← 10
do { if (x % 5 == 0) x ← x + 2 else x ← x + 5 end if print x }
while (x < 20)
Given the flowchart above, which of these loops describes the program?
int books ← 12 for (int read ← 0; read < books; read ← read + 1)
if (read == books) print "You've read " + books + " books."
end if end for
int books ← 12 for (int read ← 0; read < 12; read++)
if (read == books) print "You've read " + books + " books."
end if end for
int read ← 0 int books ← 12
do { read ← read + 1 } while (read < books)
print "You've read " + read + " books."
int read ← 0 int books ← 12 while (read <= books)
print "You've read " + read + " books." read ← read + 1
end while
Given the flowchart above, which of these loops describes the program?
int x ← 5 int y ← 20
do { y ← y - 4 } while (y <= x)
print y
int x ← 5 int y ← 20
while (y > x) y ← y - 4
end while
int x ← 5 int y ← 20
while (x <= y) y ← y - 4
end while
int x ← 5
for (int y ← 20; x < y; y++) y ← y - 4
print y end for
If the value of result at the end of the method call f(10) is 55, which of the following statements should replace /*Missing For Body*/?
result ← result * i
result ← result + num
result ← result * num
result ← result + i
If the statement "I will not skateboard in the halls" prints 500 times, which of the following statements should replace /*start; check; step*/?
int x ← 0; x < 500; x ← x + 1
int x ← 0; x > 500; x ← x + 1
int x ← 1; x < 500; x ← x + 1
int x ← 0; x ≤ 500; x ← x + 1
If the value of result at the end of the method call f(10) is 4, which of the following statements should replace /*Missing For Body*/?
result ← result - num
result ← result - 2
result ← result + num
result ← result + 2
What is the output of the code segment shown?
1 2 3 4 5
0 1 2 3 4 5
0 1 3 6 10 15
1 3 6 10 15
What is output by the code segment shown?
0 1 2 3 4 5
0 1 3 6 10
1 3 6 10 15
1 2 3 4 5
What is returned by the call below?
f( 4 )
7
4
0
2
What is the output of the call below?
f( 3 )
1 2 3
3 2 1
1
0
What is returned by the call below?
f( 10, 3 )
1
10
3
0
What is returned by the call below?
f( 15, 27 )
3
5
0
10
What is returned by the call below?
power( 2, 3 )
8
16
9
3
What is returned by the call below?
f( 11, 3 )
10
13
26
5
Which control structure listed below is the primary one used in a recursive method?
All of these
if else statement
Sequential processing
Switch statement
What is output by this pseudocode segment?
81
3
27
243
9
Which of these terms refer to the general process of repeating code elements inside a program? Check all that apply.
recursion
branching
iteration
looping
sequence
In the code segment shown, match each line with the term that best describes the code on that line.
1. for(int x = 0;
2. x < 10;
3. x++)
4. print x
Start, Check, Step, Action
Start, Step, Check, Action
Step, Start, Check, Action
Start, Action, Step, Check
How many times can the start of a loop happen?
More than four times
Once
Twice
Four times
Three times
During the execution of the code segment shown, how many times does the "check" portion of the loop occur?
for(int x = 0; x < 10; x++)
print x
11
10
9
8
7
For the code segment shown, what is the last value printed?
for(int x = 0; x < 10; x++)
print x
10
9
8
7
6
Which <start>, <check> and <step> options in the choices below will cause the action of this loop to occur 10 times?
Select all that apply.
int x ← 5 ; x < 33; x+= 3
int x ← 1 ; x ≤ 10 ; x++
int x ← 0 ; x < 10; ++x
int x ← 1 ; x < 10; x++
int x ← 0 ; x ≤ 10; ++x
Which loop styles will ALWAYS act at least once?
do While
repeat until
for
while
Which of the terms listed represents the part of the recursive process that causes repetition?
Loop
if else statement
Base case
Recursive call
Which of the terms listed represents the part of the recursive method that stops the repetitive process?
if else statement
Recursive call
Loop
Base case
Which of the statements listed below are true regarding the use of loops and recursion? Check all that apply.
Use a loop when possible, and only use recursion when a loop is not possible, or is less effective.
There are some things a recursive process can do that a loop cannot or does with great difficulty.
Loops are more memory efficient than recursive processes.
Both loops and recursion are processes used to repeat tasks in a program.
What statement best describes a value that could be input for x so the while loop shown below will never act?
Any value greater than 23
The value 23
Any value less than 23
Any value except 23
How many times will the action statement happen in the loop shown?
0
1
2
3
