NEW
Font size
WorksheetsCS Awesome Unit 4 Review
Total questions: 46
Worksheet time: 1hrs 25mins
What is the output?
int a = 1, b = 2;
System.out.println(++a+”,”+b++);
1,2
2,2
2,3
3,3
What will the following program segment display?
for(int a = 2; a <= 10; a += 3)
System.out.print(a+””);
2 5 8
2 5
2 5 8 11
2
What is each repetition of a loop known as?
cycle
revlolution
orbit
iteration
This is a variable that controls the number of iterations performed by a loop.
loop control variable
accumulator
iteration register variable
repetition meter
This type of loop has no way of ending and repeats until the program is interrupted.
a. indeterminate
b. interminable
c. infinite
d. timeless
This expression executes at the end of each iteration of the for loop.
a. initialization expression
b. test expression
c. update expression
d. statement block
This expression is executed by the for loop only once, regardless of the number of iterations.
a. initialization expression
b. test expression
c. update expression
d. preincrement expression
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
What is the output of the following code?
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
x is 0
x is 1
x is 3
x is 4
int count = 0;
while (count < 100) {
// Point A
System.out.println("Welcome to Java!");
count++;
// Point B
}
// Point C
for (int count = 1; count <= 10; count++) {
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
}
System.out.println("Welcome to Java");
}
System.out.println("Welcome to Java");
}
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
i++;
}
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
System.out.println(i * j);
for (int i = 1; i < n; i++) {
// iteration
}
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
6,3,0
6,3
3,0
0
Which of the following is a type of loop in Java?
loop
while
iteration
i
What does the following loop print?
for(int i = 1; i<3; i++)
{ System.out.print(i); }
12
012
0123
123
What will the following loop print?
int i = 0;
while (i>0)
{ System.out.println(i); }
Infinite loop
012345678910
0
Nothing
What will the following loop print?
int i = 1;
while (i>0)
{ System.out.println(i); }
012345678910
nothing
infinite loop
0
What does the following loop print?
for(i = i +1; i<5; int i = 0;)
{ System.out.println(i) }
01234
12345
error
0
Which of the following includes the correct syntax for a for loop?
for(int i = 0, i<10, i++)
for(int i =0, run = 10)
for(int i = 0; run = 10)
for(int i = 0; i < 10; i++)
for(int i; i<10; i++) {} Why won’t this code segment run?
There is a missing semicolon at the end.
For should be capitalized
The person did not set the beginning value of i.
The person did not set the ending value of i.
What does this block of code do?
for( int i = 0; i < 10; i++)
System.out.println("hello");
Prints the String "hello" ten times.
Prints the String "hello"
Heebee Jeebies
Nothing. The code does not run.
A while loop carries on as long as...
The condition in the brackets is true
The condition in the brackets is false
i < 10
the for loop has not ended
A while loop has the following format:
while (condition)
{
...
}
while
{
...
}
continue while (condition)
{
...
}
while [condition]
[
...;
]
OUTPUT?
int i = 1;
while (i < 5) {
System.out.println("Hello");
i++;
}
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
How many stars are printed?
7
3
4
5
Infinite Loop
In total, how many times is the inner loop executed?
5
10
15
50
Infinite Loop
How many times does the loop print a *
40
20
24
30
Infinite Loop
What does the following code print?
A rectangle of 8 rows with 5 stars per row
A rectangle of 8 rows with 4 stars per row
A rectangle of 6 rows with 5 stars per row
A rectangle of 6 rows with 4 stars per row
Infinite Looo
What does the following code print?
A rectangle of 9 rows with 5 stars per row
A rectangle of 6 rows with 6 stars per row
A rectangle of 7 rows with 5 stars per row
A rectangle of 7 rows with 6 stars per row
Infinite Loop
What does the following code print?
5 6 7 8 9
4 5 6 7 8 9 10 11 12
3 5 7 9 11
3 4 5 6 7 8 9 10 11 12
How many times does the following method print a *?
9
6
7
10
If a loop condition is never satisfied then the loop does not execute.
1 2 4 8 16 32 64
1 2 4 8 16 32
2 4 8 16 32 64
2 4 8 16 32
4 8 16 32 64
200 66 22 7 2
66 22 7 2
200 66 22 2
200 66 22
7
10
15
17
22
25
2
5
7
9
13
20
-1 0 2 5
-1 0 3 7
-1 1 4 8
0 2 5 9
0 1 3 7
0.0
-4.0
5
The program will end successfully, but nothing will be printed.
Nothing will be printed. Run-time error: ArithmeticException.
What will the following program segment display?
for(int a = 2; a <= 10; a += 3)
System.out.print(a+””);
2 5 8
2 5
2 5 8 11
2
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
What are the values of var1 and var2 when the code finishes executing?
var1 = 1, var2 = 1
var1 = 2 , var2 = 0
var1 = 3, var2 = -1
var1 = 0, var2 = 2
The loop will cause a run-time error for division by zero
What are the values of x and y when the code finishes executing?
x = 5, y = 2
x = 2, y = 5
x = 3, y = 4
x = 4, y = 3
Infinite Loop
