Font size
WorksheetsCF1 Quiz 4 Review: Conditionals, Arrays, and Loops
Total questions: 117
Worksheet time: 2hrs 45mins
Strings should always be compared in an if statement with a == symbol.
TRUE
FALSE
=! is the symbol for “not equal”
TRUE
FALSE
What is the output of the code shown in the image?
False
Next Round
Game Finished
finished
Which statement(s) properly implements an equality comparison of two String objects?
A, only
B, only
A and C
All are acceptable
Which of the following is used to assign a value to a variable?
=
==
.equals()
!=
What is the output of the code shown?
Sandy
Squidward
Patrick
Spongebob
What is the output of the code shown?
Hello!!
Goodbye!!
Pieces of code that only run "under certain conditions":
conditional statements
control flow statments
syntax
variables
What is a condition?
JavaScript
Something that has to be true in order for an action to be carried out.
syntax
pseudocode
This is a catch-all for every other situation besides the one where the condition is true.
then
do
else
if
A variable
is a placeholder for a piece of information that can change.
is like a bucket for holding information.
can be written as letters or with longer words.
all of these
Which symbol means "not equal to"?
==
<=
>=
!=
What is missing from the command: System.___.println("Hello World");
prt()
Sys
java
out
Which is the correct way to end a command statement in Java?
;
,
.
:
Which boolean operator should replace the @ in this code?
=
<=
==
equal
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?
It is too hot
It is pleasant
It is hot but not too much
runtime error
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");
}
Error
Incorrect code
x is greater than y
Error
Incorrect code
int marks = 50;
if( marks > 70 ){
System.out.println("Distinction");
}
else if( marks > 35 ){
System.out.println("Pass");
}
else{
System.out.println("Fail");
}
Pass
Distinction
Fail
Pass
Fail
What will be the output of following statement-
System.out.println('A'=='a');
A==a
65==97
true
false
The if -else statement executes a block of code if a specified condition is
true
false
both of these
None of these
What does the else do in Java?
You put it after the If statement. If the statement is false then the program will run the code inside the else statement.
If there is an else after the if then the condition is automatically false
It tells the user to ignore the line immediately after
It tells the computer to jump back up to line 1 of the program.
What is/are true about pseudocode?
somewhere between English and a coding language.
more precise than English but less precise than a coding language.
used by programmers to figure out the logic and flow of their programs without having to worry about syntax.
all of these
Which of these is a correct way of declaring an array of integers?
int[] arrayA = new int[10];
int arrayB = new int[20];
int arrayC[] = int[30];
int[] arrayD = new int[40]
Suppose you create a typical array of Strings, containing the subjects you will see next year. Which of the following is NOT true.
You may remove one of the subjects (it will be replaced by null).
You may add another subject to the array.
You may replace one subject with another one different.
You may copy the contents of that array into another array with the same size.
What is the output of this code inside the main method?
int[] myNums;
System.out.println(myNums[0]);
null
There's no output.
An error appears.
0
What is the output of this code inside the main method?
int[] myNums = new int[3];
System.out.println(myNums[1]);
null
There's no output.
An error appears.
0
What is the output of this code inside the main method?
String[] myWords = new String[5];
System.out.println(myWords[5]);
null
There's no output.
An error appears.
0
What is the output of this code inside the main method?
String[] myWords = new String[5];
System.out.println(myWords[4]);
null
There's no output.
An error appears.
0
Which of the following declares an array of integers named number?
int number;
int [ ] number;
int new number [ ];
int number = int [ ];
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]) ;
1
2
3
4
What is the output of the following code fragment?
int [ ] odd = {1, 3, 5, 7, 9, 11 };
System.out.println( odd[0] + " " + odd[3] ) ;
1 5
6
1 7
8
For which of the following situations is only one array NOT appropriate?
Working with 30 scores on the Java exam.
Working with names and ages of students.
Working with mileage during the weeks of vacation.
Working with company monthly sales totals.
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]);
44 10
2 10
54
44 6
What are the indeces for this array? int [ ] k = { 11, 12, 13, 14, 15};
0, 1, 2, 3, 4
1, 2, 3, 4, 5
11, 12, 13, 14, 15
10, 11, 12, 13, 14
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);
2
9
2 3 5 7 8 9
2 7 3 5 8 9
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 ] + " ");
1 2 3 4 5
6 7 8 9 10
2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
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]);
2004 2006 0
2004 2006
4010
an error will occur
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]);
2004 2006 0
2004 2006
4010
an error will occur
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] + " ");
2 4 6 8 10
2 4 6 8
2 4 6
2 4 6 8 10 12 14 16 18 20
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);
1
2
1 3 6 9 2 5 7
7
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] + " ");
Joe Sue Tom Jill Patty
Patty Jill Tom Sue Joe
Joe Tom Patty
Sue Jill
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);
2 4 6 8 1 3 5 7
2 4 6 8
7
36
What is the length of this array?
double[ ] stuff = {1.5, 2.5, 3.5, 4.5, 5.5, 6.5};
1
5
6
7
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
Find the output
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
Compile error
Exception
Garbage Value
Garbage Value
0
0
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]);
}
}
Compile error
Exception
Garbage Value
Garbage Value
0
0
class Demo1
{
public static void main(String args[])
{
int i[] = new int[10];
System.out.println(i[10]);
}
}
0
Garbage Value
Out-of-bounds exception
Error
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
While loops are not really important or necessary in Java
True
False
How many stars are printed?
7
3
4
5
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
10
15
17
22
25
200 66 22 7 2
66 22 7 2
200 66 22 2
200 66 22
7
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);
Why are loops used in programming?
To make your code look more complex.
To avoid using if statements.
To make your program look more professional.
To run blocks of code repeatedly.
Each time through a loop is called a(n) ______________.
iteration
culmination
concatenation
itemization
What is the final value of x?
int x = 0;
while (x < 5)
x++;
6
5
4
0
infinite loop
What is the final value of x?
int x = 4;
while (x <= 5)
x++;
6
5
4
0
infinite loop
What is the final value of x?
int x = 3;
while (x > 5)
x++;
6
5
4
3
infinite loop
What is the final value of x?
int x = 0;
for(int i = 1; i < 8; i += 3)
x += i;
12
22
7
0
infinite loop
What is the final value of x?
int x = 0;
for(int i = 1; i < 8; i += 3)
x += i;
12
22
7
0
infinite loop
What is the final value of x?
int x = 0;
for(int i = 5; i > 0; i --)
x ++;
5
4
6
0
infinite loop
What is the output of the following code?
for(int i = 3; i < 20; i +=4)
System.out.print(i + " ");
3 7 11 15 19 23
19
3 7 11 15 19
7 11 15 19
23
What is the final value of x?
int x = 3;
for(int i = 4; i > 3; i += 2)
x++;
3
4
5
7
infinite loop
What is the output of the following code?
int x = 7;
while(x <= 23) {
System.out.print(x + " ");
x+=4; }
7 11 15 19
7 11 15 19 23
23
7 11 15 19 23 27
27
What is the output of the following code?
for(int i = 2; i < 60; i *= 3)
System.out.print(i + " ");
162
2 6 18 54
6 18 54
2 6 18 54 162
54
What is the output of the following code?
int x = 26;
while(x > 0) {
System.out.print(x + " ");
x /= 2; }
26 13 6 3 1
26 13 6.5 3.25 1.625
26 13 6 3 1 0
26 13 6 3
infinite loop
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 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
What is the output of the following code?
int x = 26;
while(x > 0) {
System.out.print(x + " ");
x /= 2; }
26 13 6 3 1
26 13 6.5 3.25 1.625
26 13 6 3 1 0
26 13 6 3
infinite loop
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?
It is too hot
It is pleasant
It is hot but not too much
runtime error
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");
}
Error
Incorrect code
x is greater than y
Error
Incorrect code
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");
}
Hello World
Hello World
Error
Error
Hello world
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)?
Equilateral triangle
Scalene triangle
Isosceles triangle
All of the above
Which of the following is a conditional statement?
if
if else
if else if
all of the above
Select correct output-
y is 0
y is 2
y is 1
y is 10
Which of the following statements is true.
for every 'if' there should be an 'else'.
for every 'else' there should be an 'if'.
'if' block can exist without an 'else'
'if' statement can be used inside another 'if' statement
Nested ifs can be replaced with which logical operator?
||
&&
!
none
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java");
count++;
}
Which loop loops a set number of times?
for
while
infinite
loopy
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
Which of these is the correct syntax for a loop?
for(int i = 0; i<10; 1++){}
fore(integer i = 0; i>10; i++){}
for(int i = 0; i<10){}
for(int i){}
Which of the following is the correct way to create a String?
String s = "I am cool";
string string = i am cool;
String s = I am cool
String s = "I" am cool;
Which of the following is a type of loop in Java?
loop
while
iteration
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 loop should you use if you know how many times you are going to loop?
For loop
Do while loop
While loop
No loop
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
What are the legal indexes for the array ar, given the following declaration:
int[] ar = {2, 4, 6, 8 };
0, 1, 2, 31, 2, 3, 40, 2, 4, 6The array called values is shown in the picture. What is returned by values[5]?
2
12
6
8
int[] nums ={2, 3, 5, 8, 9, 11};
How would you access the second element in nums?
num(2)
num[2]
num(1)
num[1]
When there is an error in the logic of the loop because the stop condition is never reached.
Arrays
Infinite Loop
Looping
Each item in an array has a numbered position.
True
False
_____ loops let us loop a block of code a known amount of times.
while
for
_____ loops let us loop a block of code an unknown amount of times.
for
while
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);
}
}
10
12
11
Compilation Error
