wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CF1 Quiz 4 Review: Conditionals, Arrays, and Loops

Total questions: 117

Worksheet time: 2hrs 45mins

Name
Class
Date
1.

Strings should always be compared in an if statement with a == symbol.

a)

TRUE

b)

FALSE

2.

=! is the symbol for “not equal”

a)

TRUE

b)

FALSE

3.

What is the output of the code shown in the image?

a)

False

b)

Next Round

c)

Game Finished

d)

finished

4.

Which statement(s) properly implements an equality comparison of two String objects?

a)

A, only

b)

B, only

c)

A and C

d)

All are acceptable

5.

Which of the following is used to assign a value to a variable?

a)

=

b)

==

c)

.equals()

d)

!=

6.

What is the output of the code shown?

a)

Sandy

b)

Squidward

c)

Patrick

d)

Spongebob

7.

What is the output of the code shown?

a)

Hello!!

b)

Goodbye!!

8.

Pieces of code that only run "under certain conditions":

a)

conditional statements

b)

control flow statments

c)

syntax

d)

variables

9.

What is a condition?

a)

JavaScript

b)

Something that has to be true in order for an action to be carried out.

c)

syntax

d)

pseudocode

10.

This is a catch-all for every other situation besides the one where the condition is true.

a)

then

b)

do

c)

else

d)

if

11.

A variable

a)

is a placeholder for a piece of information that can change.

b)

is like a bucket for holding information.

c)

can be written as letters or with longer words.

d)

all of these

12.

Which symbol means "not equal to"?

a)

==

b)

<=

c)

>=

d)

!=

13.

What is missing from the command: System.___.println("Hello World");

a)

prt()

b)

Sys

c)

java

d)

out

14.

Which is the correct way to end a command statement in Java?

a)

;

b)

,

c)

.

d)

:

15.

Which boolean operator should replace the @ in this code?

a)

=

b)

<=

c)

==

d)

equal

16.

public void main()

{

double temp = 56.0;

if (temp <=54.0 && temp>40){

System.out.println(“It is too hot”);

}

else if ( temp<40 || temp>30){

System.out.println(“It is hot but not too much”);

}

else{

System.out.println(“It is pleasant”);

}

}

What will be the output?

a)

It is too hot

b)

It is pleasant

c)

It is hot but not too much

d)

runtime error

17.

PREDICT THE OUTPUT:

int x = 20;

int y = 18;

if(x>y){

System.out.println("x is greater than y");

}

else

{

System.out.println("Error");

System.out.println("Incorrect code");

}

a)

Error

b)

Incorrect code

c)

x is greater than y

d)

Error

Incorrect code

18.

int marks = 50;

if( marks > 70 ){

System.out.println("Distinction");

}

else if( marks > 35 ){

System.out.println("Pass");

}

else{

System.out.println("Fail");

}

a)

Pass

b)

Distinction

c)

Fail

d)

Pass

Fail

19.

What will be the output of following statement-

System.out.println('A'=='a');

a)

A==a

b)

65==97

c)

true

d)

false

20.

The if -else statement executes a block of code if a specified condition is

a)

true

b)

false

c)

both of these

d)

None of these

21.

What does the else do in Java?

a)

You put it after the If statement. If the statement is false then the program will run the code inside the else statement.

b)

If there is an else after the if then the condition is automatically false

c)

It tells the user to ignore the line immediately after

d)

It tells the computer to jump back up to line 1 of the program.

22.

What is/are true about pseudocode?

a)

somewhere between English and a coding language.

b)

more precise than English but less precise than a coding language.

c)

used by programmers to figure out the logic and flow of their programs without having to worry about syntax.

d)

all of these

23.

Which of these is a correct way of declaring an array of integers?

a)

int[] arrayA = new int[10];

b)

int arrayB = new int[20];

c)

int arrayC[] = int[30];

d)

int[] arrayD = new int[40]

24.

Suppose you create a typical array of Strings, containing the subjects you will see next year. Which of the following is NOT true.

a)

You may remove one of the subjects (it will be replaced by null).

b)

You may add another subject to the array.

c)

You may replace one subject with another one different.

d)

You may copy the contents of that array into another array with the same size.

25.

What is the output of this code inside the main method?

int[] myNums;

System.out.println(myNums[0]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

26.

What is the output of this code inside the main method?

int[] myNums = new int[3];

System.out.println(myNums[1]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

27.

What is the output of this code inside the main method?

String[] myWords = new String[5];

System.out.println(myWords[5]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

28.

What is the output of this code inside the main method?

String[] myWords = new String[5];

System.out.println(myWords[4]);

a)

null

b)

There's no output.

c)

An error appears.

d)

0

29.

Which of the following declares an array of integers named number?

a)

int number;

b)

int [ ] number;

c)

int new number [ ];

d)

int number = int [ ];

30.

What is the output of the following code fragments?

int [ ] fun = new int [5];    

fun[0] = 1;    

fun[1] = 2;    

fun[2] = 3;    

fun[3] = 4;    

fun[4] = 5;    

int j = 3;    

System.out.println(fun[ j-1]) ;

a)

1

b)

2

c)

3

d)

4

31.

What is the output of the following code fragment?    

int [ ] odd = {1, 3, 5, 7, 9, 11 };    

System.out.println( odd[0] + "  " + odd[3] ) ;

a)

1 5

b)

6

c)

1 7

d)

8

32.

For which of the following situations is only one array NOT appropriate?

a)

Working with 30 scores on the Java exam.

b)

Working with names and ages of students.

c)

Working with mileage during the weeks of vacation.

d)

Working with company monthly sales totals.

33.

What is the output of the following code fragment?     

int [ ] evens = {2, 4, 6, 8, 10};    

evens[0] = 44;    

evens[4] = evens[2];    

System.out.println(evens[0] + "  " + evens[4]);

a)

44 10

b)

2 10

c)

54

d)

44 6

34.

What are the indeces for this array?  int [ ] k = { 11, 12, 13, 14, 15};

a)

0, 1, 2, 3, 4

b)

1, 2, 3, 4, 5

c)

11, 12, 13, 14, 15

d)

10, 11, 12, 13, 14

35.

What is the output of the following code fragment?     

int [ ] items = {2, 7, 3, 5, 8, 9};     

int funny = items[0];     

for (int i = 0; i < items.length; i++)      {

               if (items[ i ] > funny)

                       funny = items[ i ]; 

     }     

System.out.print(funny);   

a)

2

b)

9

c)

2  3  5  7  8  9

d)

2  7  3  5  8  9

36.

What is the output for the following code fragment?      

int [ ] numbers = { 1, 2, 3, 4, 5, 6, 7,  8, 9, 10};

       for (int j = 0; j < numbers.length; j++)

       System.out.print(numbers[ j ] + " ");

a)

1 2 3 4 5

b)

6 7 8 9 10

c)

2 3 4 5 6 7 8 9 10

d)

1 2 3 4 5 6 7 8 9 10

37.

What is the output for the following code fragment?       

int [ ] car = new int [7];     

car[0] = 2004;     

car[1] = 2006;     

System.out.println(car[0] + " " + car[1] + " " car[7]);

a)

2004  2006  0

b)

2004  2006

c)

4010

d)

an error will occur

38.

What is the output for the following code fragment?       

int [ ] car = new int [7];     

car[0] = 2004;     

car[1] = 2006;     

System.out.println(car[0] + " " + car[1] + " " car[7]);

a)

2004  2006  0

b)

2004  2006

c)

4010

d)

an error will occur

39.

What is the output for the following code fragment?      

int [ ] array = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};      

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

System.out.print(array[i] + " ");

a)

2 4 6 8 10

b)

2 4 6 8

c)

2 4 6

d)

2 4 6 8 10 12 14 16 18 20

40.

What is the output for the following code fragment?      

int [ ] dedo = { 1, 3, 6, 9, 2, 5, 7};      

int que = dedo[0];      

for (int k = 0; k < dedo.length; k++) {          

if (dedo[k] < que)             

que = dedo[k];

       }      

System.out.print(que);

a)

1

b)

2

c)

1 3 6 9 2 5 7

d)

7

41.

What is the output from the following code fragment?       

String [ ] name = { "Joe", "Sue", "Tom", "Jill", "Patty"};      

for (int i = 0; i < name.length; i = i + 2)          

System.out.print(name[i] + " ");

a)

Joe Sue Tom Jill Patty

b)

Patty Jill Tom Sue Joe

c)

Joe Tom Patty

d)

Sue Jill

42.

What is the output of the following code fragment?       

int [ ] array = { 2, 4, 6, 8, 1, 3, 5, 7};       

int george = 0;       

for (int i = 0; i < array.length; i++)             

george += array[i];       

System.out.print(george);

a)

2 4 6 8 1 3 5 7

b)

2 4 6 8

c)

7

d)

36

43.

What is the length of this array?      

double[ ] stuff = {1.5,  2.5,  3.5,  4.5,  5.5,  6.5};

a)

1

b)

5

c)

6

d)

7

44.

Which of the following is FALSE about arrays on Java

a)

A java array is always an object

b)

Length of array can be changed after creation of array

c)

Arrays in Java are always allocated on heap

45.

Find the output

class Test {

public static void main(String args[]) {

int arr[2];

System.out.println(arr[0]);

System.out.println(arr[1]);

}

}

a)

Compile error

b)

Exception

c)

Garbage Value

Garbage Value

d)

0

0

46.

Find the output

class Test {

public static void main(String args[]) {

int arr [ ] = new int[2];

System.out.println(arr[0]);

System.out.println(arr[1]);

}

}

a)

Compile error

b)

Exception

c)

Garbage Value

Garbage Value

d)

0

0

47.

class Demo1

{

public static void main(String args[])

{

int i[] = new int[10];

System.out.println(i[10]);

}

}

a)

0

b)

Garbage Value

c)

Out-of-bounds exception

d)

Error

48.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
49.
How will you identify a single array?
a)
single curly brackets
b)
single square brackets
c)
multiple squares brackets
d)
single angled brackets
50.

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

51.

A while loop has the following format:

a)

while (condition)

{

...

}

b)

while

{

...

}

c)

continue while (condition)

{

...

}

d)

while [condition]

[

...;

]

52.

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

53.

While loops are not really important or necessary in Java

a)

True

b)

False

54.

How many stars are printed?

a)

7

b)

3

c)

4

d)

5

e)

Infinite Loop

55.

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

56.

How many times does the following method print a *?

a)

9

b)

6

c)

7

d)

10

57.
True/False
If a loop condition is never satisfied then the loop does not execute.
a)
True
b)
False
58.
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

59.
a)

10

b)

15

c)

17

d)

22

e)

25

60.
a)

200 66 22 7 2

b)

66 22 7 2

c)

200 66 22 2

d)

200 66 22

e)

7

61.

2

a)

5

b)

7

c)

9

d)

13

e)

20

62.
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

63.
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.

64.

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

65.
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
66.
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
67.

Why are loops used in programming?

a)

To make your code look more complex.

b)

To avoid using if statements.

c)

To make your program look more professional.

d)

To run blocks of code repeatedly.

68.

Each time through a loop is called a(n) ______________.

a)

iteration

b)

culmination

c)

concatenation

d)

itemization

69.

What is the final value of x?

int x = 0;

while (x < 5)

x++;

a)

6

b)

5

c)

4

d)

0

e)

infinite loop

70.

What is the final value of x?

int x = 4;

while (x <= 5)

x++;

a)

6

b)

5

c)

4

d)

0

e)

infinite loop

71.

What is the final value of x?

int x = 3;

while (x > 5)

x++;

a)

6

b)

5

c)

4

d)

3

e)

infinite loop

72.

What is the final value of x?

int x = 0;

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

x += i;

a)

12

b)

22

c)

7

d)

0

e)

infinite loop

73.

What is the final value of x?

int x = 0;

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

x += i;

a)

12

b)

22

c)

7

d)

0

e)

infinite loop

74.

What is the final value of x?

int x = 0;

for(int i = 5; i > 0; i --)

x ++;

a)

5

b)

4

c)

6

d)

0

e)

infinite loop

75.

What is the output of the following code?

for(int i = 3; i < 20; i +=4)

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

a)

3 7 11 15 19 23

b)

19

c)

3 7 11 15 19

d)

7 11 15 19

e)

23

76.

What is the final value of x?

int x = 3; 

for(int i = 4; i > 3; i += 2)

x++;

a)

3

b)

4

c)

5

d)

7

e)

infinite loop

77.

What is the output of the following code?

int x = 7;

while(x <= 23) {

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

x+=4; }

a)

7 11 15 19

b)

7 11 15 19 23

c)

23

d)

7 11 15 19 23 27

e)

27

78.

What is the output of the following code?

for(int i = 2; i < 60; i *= 3)

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

a)

162

b)

2 6 18 54

c)

6 18 54

d)

2 6 18 54 162

e)

54

79.

What is the output of the following code?

int x = 26;

while(x > 0) {

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

x /= 2; }

a)

26 13 6 3 1

b)

26 13 6.5 3.25 1.625

c)

26 13 6 3 1 0

d)

26 13 6 3

e)

infinite loop

80.

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

81.

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

82.

What is the output of the following code?

int x = 26;

while(x > 0) {

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

x /= 2; }

a)

26 13 6 3 1

b)

26 13 6.5 3.25 1.625

c)

26 13 6 3 1 0

d)

26 13 6 3

e)

infinite loop

83.

public static void main() {

double temp = 56.0;

if (temp <=54.0 && temp>40) {

System.out.println(“It is too hot”);

} else if ( temp<40 || temp>30) {

System.out.println(“It is hot but not too much”);

} else {

System.out.println(“It is pleasant”);

}

}

What will be the output?

a)

It is too hot

b)

It is pleasant

c)

It is hot but not too much

d)

runtime error

84.

PREDICT THE OUTPUT:

int x = 20;

int y = 18;

if(x>y) {

System.out.println("x is greater than y");

} else {

System.out.println("Error");

System.out.println("Incorrect code");

}

a)

Error

b)

Incorrect code

c)

x is greater than y

d)

Error

Incorrect code

85.

PREDICT THE OUTPUT:

int x = 60;

int y = 90;

if (y%x == 0 && y-x==30) {

System.out.println("Hello World");

} else {

System.out.println("Error");

}

a)

Hello World

b)

Hello World

Error

c)

Error

d)

Hello world

86.

5. If a, b and c are the sides of a triangle then which of the following statement is true for:

if(a==b && a==c && b==c)?

a)

Equilateral triangle

b)

Scalene triangle

c)

Isosceles triangle

d)

All of the above

87.

Which of the following is a conditional statement?

a)

if

b)

if else

c)

if else if

d)

all of the above

88.

Select correct output-

a)

y is 0

b)

y is 2

c)

y is 1

d)

y is 10

89.

Which of the following statements is true.

a)

for every 'if' there should be an 'else'.

b)

for every 'else' there should be an 'if'.

c)

'if' block can exist without an 'else'

d)

'if' statement can be used inside another 'if' statement

90.

Nested ifs can be replaced with which logical operator?

a)

||

b)

&&

c)

!

d)

none

91.
If x has the value 12, Java will only evaluate the left side of the compound expression in the statement: if (x > 1 && x < 10)
a)
True
b)
False
92.
Each element of an int array is automatically initialized to 0.
a)
True
b)
False
93.
The length of a one-dimensional array is one more than the index of the last element.
a)
True
b)
False
94.
Which is not a logical operator?
a)
And
b)
Or
c)
Not
d)
If
95.
In an array, the index of the first item is
a)
0
b)
1
c)
2
d)
3
96.
Each element in a String array is automatically initialized to
a)
true.
b)
false.
c)
null.
d)
Elements in a string array are not initialized.
97.
An array has a fixed length.
a)
True
b)
False
98.
Which property can be used to return the number of rows in an array?
a)
length
b)
size
c)
rank
d)
noRows
99.
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
100.

Which loop loops a set number of times?

a)

for

b)

while

c)

infinite

d)

loopy

101.
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

102.

Which of these is the correct syntax for a loop?

a)

for(int i = 0; i<10; 1++){}

b)

fore(integer i = 0; i>10; i++){}

c)

for(int i = 0; i<10){}

d)

for(int i){}

103.

Which of the following is the correct way to create a String?

a)

String s = "I am cool";

b)

string string = i am cool;

c)

String s = I am cool

d)

String s = "I" am cool;

104.

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

a)

loop

b)

while

c)

iteration

d)

i

105.

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.

106.

What loop should you use if you know how many times you are going to loop?

a)

For loop

b)

Do while loop

c)

While loop

d)

No loop

107.

Which of the following is FALSE about arrays on Java

a)

A java array is always an object

b)

Length of array can be changed after creation of array

c)

Arrays in Java are always allocated on heap

108.

What are the legal indexes for the array ar, given the following declaration:

int[] ar = {2, 4, 6, 8 };

a)
 0, 1, 2, 3
b)
 1, 2, 3, 4
c)
2, 4, 6, 8
d)
 0, 2, 4, 6
109.

The array called values is shown in the picture. What is returned by values[5]?

a)

2

b)

12

c)

6

d)

8

110.

int[] nums ={2, 3, 5, 8, 9, 11};

How would you access the second element in nums?

a)

num(2)

b)

num[2]

c)

num(1)

d)

num[1]

111.

When there is an error in the logic of the loop because the stop condition is never reached.

a)

Arrays

b)

Infinite Loop

c)

Looping

112.

Each item in an array has a numbered position.

a)

True

b)

False

113.

_____ loops let us loop a block of code a known amount of times.

a)

while

b)

for

114.

_____ loops let us loop a block of code an unknown amount of times.

a)

for

b)

while

115.
Why are arrays used?
a)
Arrays store multiple data values under one name, reducing the complexity of our program
b)
Arrays store more data than if we were using variables
c)
Arrays store more data than if we were using strings
116.
In an array, what is an element?
a)
The smallest number an array can hold
b)
The biggest number an array can hold
c)
An element is one value in an array
117.

Find the output of the following snippet.

public class Numbers {

public static void main(String args[]) {

int a=20;

int b=10;

if((a<b)&&(b++ <25)) {

System.out.println("The conditional is true!");

}

System.out.println(b);

}

}

a)

10

b)

12

c)

11

d)

Compilation Error