Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CS 241 Practice Test Week 3

Total questions: 54

Worksheet time: 4hrs 13mins

Name
Class
Date
1.

What would be printed?

a)

10 9 8 7 6 5 4 3 2

b)

10 9 8 7 6 5 4 3 2 1

c)

10 9 8 7 6 5 4 3 2 1

d)

10 9 8 7 6 5 4 3 2 1

2.

If the value of x at the end of this statement is 6, which of the following statements should replace /*Missing If Body*/?

a)

x ← x - 4

b)

x ← x - 3

c)

x ← x - 5

d)

x ← x - 2

3.

If the value of x at the end of this statement is 9, which of the following statements should replace /*Missing If Body*/?

a)

x ← x - 4

b)

x ← x - 5

c)

x ← x - 3

d)

x ← x - 2

4.

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*/?

a)

result ← result * num

b)

result ← result - i

c)

result ← result / i

d)

result ← result * i

5.

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*/?

a)

result ← result - i

b)

result ← result * num

c)

result ← result * i

d)

result ← result / num

6.

How many times will the action statement happen in the loop shown?

a)

27

b)

25

c)

26

d)

24

7.

What is the output of this loop process?

a)

****24

b)

****23

c)

*****24

d)

*****23

8.

How many stars will be output by this loop?

a)

Infinitely many

b)

5

c)

6

d)

7

e)

None

9.

What would be the output of the main method?

a)

0 2

b)

2

c)

0 2 4

d)

2 4

10.

Which of the following loops will produce the same output?

a)

int x ← 75

while (x < 40) print x x ← x - 5

end while

b)

int x ← 75

while (x ≥ 40) print x x ← x - 10

end while

c)

int x ← 80

while (x > 45) x ← x - 5 print x

end while

d)

int x ← 80

while (x > 40) x ← x - 10 print x

end while

11.

Which of the following loops would produce the same output as the for loop in the factorial method?

a)

int x ← 1

while (x ≤ num) result ← result * x x ← x + 1

end while

b)

int x ← 0

while (x ≤ num) result ← result * x x ← x + 1

end while

c)

int x ← 1

while (x < num) result ← result * x x ← x + 1

end while

d)

int x ← 1

while (x ≤ num) x ← x + 1 result ← result * x

end while

12.

Given the flowchart above, which of these loops describes the program?

a)

int x ← 5 int y ← 20

while (y > x) y ← y - 1

end while

b)

int x ← 5 int y ← 20

while (y ≥ x) y ← y - 1

end while

c)

int x ← 20 int y ← 5

while (y ≥ x) y ← y - 1

end while

d)

int x ← 5 int y ← 20

while (y ≤ x) y ← y - 1

end while

13.

Given the flowchart above, which of these loops describes the program?

a)

int j ← 5

while (j > 20) j ← j - 5

end while

b)

int j ← 5

while (j ≤ 5) j ← j + 1

end while

c)

int j ← 5

while (j ≤ 20) j ← j + 5

end while

d)

int j ← 5

while (j ≥ 5) y ← j + 5

end while

14.

Which of these loop styles is considered a pretest loop?

I. For

II. While

III. do While

a)

III only

b)

All three

c)

I and II

d)

I only

e)

 II only

15.

What is output by this pseudocode segment?

a)

10

b)

0

c)

15

d)

6

e)

21

16.

What is output by this pseudocode segment?

a)

No output due to an infinite loop

b)

-55

c)

45

d)

55

e)

-45

17.

Given the flowchart above, which of these loops describes the program?

a)

int j ← -5

do { print j j ← j + 1 }

while (j ≤ 0)

b)

int j ← -5

do { j ← j + 1 print j }

while (j ≤ 0)

c)

int j ← -5

do { print j j ← j + 1 }

while (j ≥ 0)

d)

int j ← -5

while (j < 0) print j j ← j + 1

end while

18.

What would be the output of the main method?

a)

10 8 6 4 2 0 -2

b)

 8 6 4 2 0

c)

-2

d)

10 8 6 4 2 0

19.

What would be the output of the main method?

a)

10 5 3 1

b)

10 5 3

c)

10 5 3 1 -1

d)

5 3 1 -1

20.

What would be the output of the main method?

a)

Nothing

b)

10

c)

10 8

d)

8

21.

Which of the following loops will produce the same output?

a)

int x ← 110

do { x ← x - 10 print x

while (x > 10)

b)

int x ← 100

do { x ← x - 10 print x

while (x > 0)

c)

int x ← 100

do { x ← x - 10 print x

while (x > 10)

d)

int x ← 110

do { print x x ← x - 10

while (x > 10)

22.

Which of the following loops will produce the same output?

a)

int x ← 10

do { print x + " " x ← x + 5

while (x < 30)

b)

int x ← 10

do { x ← x + 5 print x + " "

while (x <= 30)

c)

int x ← 30

do { print x + " " x ← x - 5

while (x > 0)

d)

int x ← 10

do { x ← x + 5 print x + " "

while (x < 30)

23.

Which of these loop styles is considered a posttest loop?

I. for

II. while

III. do While

a)

All three

b)

III only

c)

I only

d)

 II only

e)

 I and II

24.
a)

******21

b)

*****20

c)

*****21

d)

******20

25.

How many stars will be output by this loop?

a)

None

b)

5

c)

Infinitely Many

d)

7

e)

6

26.

Which of the following loops will produce the same output?

a)

for ( int x ← 15; x >= 1; x ← x - 3 )

print x x ← x - 5

end for

b)

int x ← 15 while ( x > 0 )

if (x > 12) x ← x - 5 print x

else x ← x - 3 end if end while

c)

int x ← 15 while ( x >= 0 )

if (x > 12) x ← x - 5

else x ← x - 3 end if print x end while

d)

for ( int x ← 15; x >= 0; x ← x - 5 )

x ← x - 5

print x end for

27.

Which of the following loops will produce the same output?

a)

int x ← 10

do { print x if (x % 5 == 0) x ← x + 2 else x ← x + 5 end if }

while (x < 20)

b)

int x ← 10

for(x % 5 == 0; x < 20; x ← x + 2) x ← x + 5 print x

end for

c)

int x ← 10

for(x; x % 5 == 0; x ← x + 5) print x

end for

d)

int x ← 10

do { if (x % 5 == 0) x ← x + 2 else x ← x + 5 end if print x }

while (x < 20)

28.

Given the flowchart above, which of these loops describes the program?

a)

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

b)

int books ← 12 for (int read ← 0; read < 12; read++)

if (read == books) print "You've read " + books + " books."

end if end for

c)

int read ← 0 int books ← 12

do { read ← read + 1 } while (read < books)

print "You've read " + read + " books."

d)

int read ← 0 int books ← 12 while (read <= books)

print "You've read " + read + " books." read ← read + 1

end while

29.

Given the flowchart above, which of these loops describes the program?

a)

int x ← 5 int y ← 20

do { y ← y - 4 } while (y <= x)

print y

b)

int x ← 5 int y ← 20

while (y > x) y ← y - 4

end while

c)

int x ← 5 int y ← 20

while (x <= y) y ← y - 4

end while

d)

int x ← 5

for (int y ← 20; x < y; y++) y ← y - 4

print y end for

30.

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*/?

a)

result ← result * i

b)

result ← result + num

c)

result ← result * num

d)

result ← result + i

31.

If the statement "I will not skateboard in the halls" prints 500 times, which of the following statements should replace /*start; check; step*/?

a)

int x ← 0; x < 500; x ← x + 1

b)

int x ← 0; x > 500; x ← x + 1

c)

int x ← 1; x < 500; x ← x + 1

d)

int x ← 0; x ≤ 500; x ← x + 1

32.

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*/?

a)

result ← result - num

b)

result ← result - 2

c)

result ← result + num

d)

result ← result + 2

33.

What is the output of the code segment shown?

a)

1 2 3 4 5

b)

0 1 2 3 4 5

c)

0 1 3 6 10 15

d)

1 3 6 10 15

34.

What is output by the code segment shown?

a)

0 1 2 3 4 5

b)

0 1 3 6 10

c)

1 3 6 10 15

d)

1 2 3 4 5

35.

What is returned by the call below?

f( 4 )

a)

7

b)

4

c)

0

d)

2

36.

What is the output of the call below?

f( 3 )

a)

1 2 3

b)

3 2 1

c)

1

d)

0

37.

What is returned by the call below?

f( 10, 3 )

a)

1

b)

10

c)

3

d)

0

38.

What is returned by the call below?

f( 15, 27 )

a)

3

b)

5

c)

0

d)

10

39.

What is returned by the call below?

power( 2, 3 )

a)

8

b)

16

c)

9

d)

3

40.

What is returned by the call below?

f( 11, 3 )

a)

10

b)

13

c)

26

d)

5

41.

Which control structure listed below is the primary one used in a recursive method?

a)

All of these

b)

if else statement

c)

Sequential processing

d)

Switch statement

42.

What is output by this pseudocode segment?

a)

81

b)

3

c)

27

d)

243

e)

9

43.

Which of these terms refer to the general process of repeating code elements inside a program? Check all that apply.

a)

recursion

b)

branching

c)

iteration

d)

looping

e)

sequence

44.

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

a)

Start, Check, Step, Action

b)

Start, Step, Check, Action

c)

Step, Start, Check, Action

d)

Start, Action, Step, Check

45.

How many times can the start of a loop happen?

a)

More than four times

b)

Once

c)

Twice

d)

Four times

e)

Three times

46.

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

a)

11

b)

10

c)

9

d)

8

e)

7

47.

For the code segment shown, what is the last value printed?

for(int x = 0; x < 10; x++)

print x

a)

10

b)

9

c)

8

d)

7

e)

6

48.

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.

a)

int x ← 5 ; x < 33; x+= 3

b)

int x ← 1 ; x ≤ 10 ; x++

c)

int x ← 0 ; x < 10; ++x

d)

int x ← 1 ; x < 10; x++

e)

int x ← 0 ; x ≤ 10; ++x

49.

Which loop styles will ALWAYS act at least once?

a)

do While

b)

repeat until

c)

for

d)

while

50.

Which of the terms listed represents the part of the recursive process that causes repetition?

a)

Loop

b)

if else statement

c)

Base case

d)

Recursive call

51.

Which of the terms listed represents the part of the recursive method that stops the repetitive process?

a)

if else statement

b)

Recursive call

c)

Loop

d)

Base case

52.

Which of the statements listed below are true regarding the use of loops and recursion? Check all that apply.

a)

Use a loop when possible, and only use recursion when a loop is not possible, or is less effective.

b)

There are some things a recursive process can do that a loop cannot or does with great difficulty.

c)

Loops are more memory efficient than recursive processes.

d)

Both loops and recursion are processes used to repeat tasks in a program.

53.

What statement best describes a value that could be input for x so the while loop shown below will never act?

a)

Any value greater than 23

b)

The value 23

c)

Any value less than 23

d)

Any value except 23

54.

How many times will the action statement happen in the loop shown?

a)

0

b)

1

c)

2

d)

3