wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

CS Awesome Unit 4 Review

Total questions: 46

Worksheet time: 1hrs 25mins

Name
Class
Date
1.

What is the output?


int a = 1, b = 2;

System.out.println(++a+”,”+b++);

a)

1,2

b)

2,2

c)

2,3

d)

3,3

2.

What will the following program segment display?


for(int a = 2; a <= 10; a += 3)

System.out.print(a+””);

a)

2 5 8

b)

2 5

c)

2 5 8 11

d)

2

3.

What is each repetition of a loop known as?

a)

cycle

b)

revlolution

c)

orbit

d)

iteration

4.

This is a variable that controls the number of iterations performed by a loop.

a)

loop control variable

b)

accumulator

c)

iteration register variable

d)

repetition meter

5.

This type of loop has no way of ending and repeats until the program is interrupted.

a)

a. indeterminate

b)

b. interminable

c)

c. infinite

d)

d. timeless

6.

This expression executes at the end of each iteration of the for loop.

a)

a. initialization expression

b)

b. test expression

c)

c. update expression

d)

d. statement block

7.

This expression is executed by the for loop only once, regardless of the number of iterations.

a)

a. initialization expression

b)

b. test expression

c)

c. update expression

d)

d. preincrement expression

8.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
 System.out.println("Welcome to Java");
 count++;
}
a)
8
b)
9
c)
10
d)
0
9.

What is the output of the following code?

int x = 0;

while (x < 4) {

x = x + 1;

}

System.out.println("x is " + x);

a)

x is 0

b)

x is 1

c)

x is 3

d)

x is 4

10.
 Analyze the following code.
int count = 0;
while (count < 100) {
 // Point A
 System.out.println("Welcome to Java!");
 count++;
 // Point B
}
 // Point C
a)
count < 100 is always true at Point B 
b)
 count < 100 is always false at Point B
c)
 count < 100 is always true at Point A  and count < 100 is always false at Point C
d)
 count < 100 is always true at Point Coint C
11.
Which of the following loops prints "Welcome to Java" 10 times?
a)
A:
for (int count = 1; count <= 10; count++) {
 System.out.println("Welcome to Java");
b)
for (int count = 2; count < 10; count++) {
 System.out.println("Welcome to Java");
}
c)
for (int count = 1; count < 10; count++) {
 System.out.println("Welcome to Java");
}
d)
for (int count = 0; count <= 10; count++) {
 System.out.println("Welcome to Java");
}
12.
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
 System.out.print(i + " ");
 i++;
}
a)
1 2 3 4 5 6 7 8 9
b)
1 2 3 4 5 6 7 8 9 10
c)
1 2 3 4 5 
d)
1 3 5 7 9
13.
What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
 y += i;
}
System.out.println(y);
a)
1
b)
36
c)
45
d)
9
14.
How many times is the println statement executed?
for (int i = 0; i < 10; i++) 
 for (int j = 0; j < 10; j++)
 System.out.println(i * j);
a)
100
b)
10
c)
45
d)
20
15.
What is the number of iterations in the following loop:
 for (int i = 1; i < n; i++) {
 // iteration
 }
a)
2*n
b)
 n
c)
n - 1
d)
n + 1
16.

What will be displayed when the following code is executed?

int number = 6;

while (number > 0) {

number -= 3;

System.out.print(number + " ");

}

a)

6,3,0

b)

6,3

c)

3,0

d)

0

17.

Which of the following is a type of loop in Java?

a)

loop

b)

while

c)

iteration

d)

i

18.

What does the following loop print?

for(int i = 1; i<3; i++)

{ System.out.print(i); }

a)

12

b)

012

c)

0123

d)

123

19.

What will the following loop print?


int i = 0;

while (i>0)

{ System.out.println(i); }

a)

Infinite loop

b)

012345678910

c)

0

d)

Nothing

20.

What will the following loop print?

int i = 1;

while (i>0)

{ System.out.println(i); }

a)

012345678910

b)

nothing

c)

infinite loop

d)

0

21.

What does the following loop print?


for(i = i +1; i<5; int i = 0;)

{ System.out.println(i) }

a)

01234

b)

12345

c)

error

d)

0

22.

Which of the following includes the correct syntax for a for loop?

a)

for(int i = 0, i<10, i++)

b)

for(int i =0, run = 10)

c)

for(int i = 0; run = 10)

d)

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

23.

for(int i; i<10; i++) {} Why won’t this code segment run?

a)

There is a missing semicolon at the end.

b)

For should be capitalized

c)

The person did not set the beginning value of i.

d)

The person did not set the ending value of i.

24.

What does this block of code do?

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

System.out.println("hello");

a)

Prints the String "hello" ten times.

b)

Prints the String "hello"

c)

Heebee Jeebies

d)

Nothing. The code does not run.

25.

A while loop carries on as long as...

a)

The condition in the brackets is true

b)

The condition in the brackets is false

c)

i < 10

d)

the for loop has not ended

26.

A while loop has the following format:

a)

while (condition)

{

...

}

b)

while

{

...

}

c)

continue while (condition)

{

...

}

d)

while [condition]

[

...;

]

27.

OUTPUT?

int i = 1;

while (i < 5) {

System.out.println("Hello");

i++;

}

a)

Hello

Hello

Hello

Hello

Hello

b)

Hello

Hello

Hello

Hello

c)

Hello

Hello

Hello

d)

Hello

28.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

29.

In total, how many times is the inner loop executed?

a)

5

b)

10

c)

15

d)

50

e)

Infinite Loop

30.

How many times does the loop print a *

a)

40

b)

20

c)

24

d)

30

e)

Infinite Loop

31.

What does the following code print?

a)

A rectangle of 8 rows with 5 stars per row

b)

A rectangle of 8 rows with 4 stars per row

c)

A rectangle of 6 rows with 5 stars per row

d)

A rectangle of 6 rows with 4 stars per row

e)

Infinite Looo

32.

What does the following code print?

a)

A rectangle of 9 rows with 5 stars per row

b)

A rectangle of 6 rows with 6 stars per row

c)

A rectangle of 7 rows with 5 stars per row

d)

A rectangle of 7 rows with 6 stars per row

e)

Infinite Loop

33.

What does the following code print?

a)

5 6 7 8 9

b)

4 5 6 7 8 9 10 11 12

c)

3 5 7 9 11

d)

3 4 5 6 7 8 9 10 11 12

34.

How many times does the following method print a *?

a)

9

b)

6

c)

7

d)

10

35.
True/False
If a loop condition is never satisfied then the loop does not execute.
a)
True
b)
False
36.
a)

1 2 4 8 16 32 64

b)

1 2 4 8 16 32

c)

2 4 8 16 32 64

d)

2 4 8 16 32

e)

4 8 16 32 64

37.
a)

200 66 22 7 2

b)

66 22 7 2

c)

200 66 22 2

d)

200 66 22

e)

7

38.
a)

10

b)

15

c)

17

d)

22

e)

25

39.

2

a)

5

b)

7

c)

9

d)

13

e)

20

40.
a)

-1 0 2 5

b)

-1 0 3 7

c)

-1 1 4 8

d)

0 2 5 9

e)

0 1 3 7

41.
a)

0.0

b)

-4.0

c)

5

d)

The program will end successfully, but nothing will be printed.

e)

Nothing will be printed. Run-time error: ArithmeticException.

42.

What will the following program segment display?


for(int a = 2; a <= 10; a += 3)

System.out.print(a+””);

a)

2 5 8

b)

2 5

c)

2 5 8 11

d)

2

43.
How many times will the following code print "Welcome to Java"?
int count = 0;
while (count < 10) {
 System.out.println("Welcome to Java");
 count++;
}
a)
8
b)
9
c)
10
d)
0
44.
What is the output of the following code?
int x = 0;
while (x < 4) {
 x = x + 1;
}
System.out.println("x is " + x);
a)
0
b)
1
c)
3
d)
4
45.

What are the values of var1 and var2 when the code finishes executing?

a)

var1 = 1, var2 = 1

b)

var1 = 2 , var2 = 0

c)

var1 = 3, var2 = -1

d)

var1 = 0, var2 = 2

e)

The loop will cause a run-time error for division by zero

46.

What are the values of x and y when the code finishes executing?

a)

x = 5, y = 2

b)

x = 2, y = 5

c)

x = 3, y = 4

d)

x = 4, y = 3

e)

Infinite Loop