NEW
Font size
S
M
L
XL
WorksheetsJava - Flow of Control Quiz
Total questions: 26
Worksheet time: 24mins
Name
Class
Date
1.
During program development, software requirements specify
a)
how the program will accomplish the task
b)
what the task is that the program must perform
c)
how to divide the task into subtasks
d)
how to test the program when it is done
2.
In which phase of program development would you expect the programmer(s) to determine the classes and objects needed?
a)
Software requirements
b)
Software design
c)
Software implementation
d)
Software testing
3.
Which of the following would not be considered an algorithm?
a)
a recipe
b)
a computer program
c)
pseudocode
d)
a shopping list
4.
If a programmer follows the four phases of program development as intended, which of the four phases should require the least amount of creativity?
a)
Software requirements
b)
Software design
c)
Software implementation
d)
Software testing
5.
The idea that program instructions execute in order (linearly) unless otherwise specified through a conditional statement is known as
a)
boolean execution
b)
conditional statements
c)
try and catch
d)
flow of control
6.
Of the following if statements, which one correctly executes three instructions if the condition is true?
a)
if (x < 0)
a = b * 2;
y = x;
z = a – y;
a = b * 2;
y = x;
z = a – y;
b)
{
if (x <0)
a = b*2;
y=x;
z=a-y;
}
if (x <0)
a = b*2;
y=x;
z=a-y;
}
c)
if { (x < 0)
a = b * 2;
y = x;
z = a – y;
}
a = b * 2;
y = x;
z = a – y;
}
d)
if (x <0)
{
a = b*2;
y = x;
z = a - y;
}
{
a = b*2;
y = x;
z = a - y;
}
7.
Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?
a)
if (x > 0) x++;
else x--;
else x--;
b)
if (x > 0) x++;
else if (x < 0) x--;
else if (x < 0) x--;
c)
if (x > 0) x++;
if (x < 0) x--;
else x = 0;
if (x < 0) x--;
else x = 0;
d)
if (x == 0) x = 0;
else x++;
x--;
else x++;
x--;
8.
Given the nested if-else structure above, If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
9.
Given the nested if-else structure above, if x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
10.
Given the nested if-else structure above, if x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
5
11.
Consider the above code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score.
a)
This code will work correctly in all cases
b)
This code will work correctly only if grade >= 60
c)
This code will work correctly only if grade < 60
d)
This code will work correctly only if grade < 70
12.
Assume that count is 0, total is 20 and max is 1. The above statement will do which of the following?
a)
The condition short circuits and the assignment statement is not executed
b)
The condition short circuits and the assignment statement is executed without problem
c)
The condition does not short circuit causing a division by zero error
d)
The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
13.
What is wrong, logically, with the following code?if (x > 10) System.out.println("Large");else if (x > 6 && x <= 10) System.out.println("Medium");else if (x > 3 && x <= 6) System.out.println("Small");else System.out.println("Very small");
a)
There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
b)
There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
c)
The logical error is that no matter what value x is, “Very small” is always printed out
d)
The logical error is that no matter what value x is, “Large” is always printed out
14.
Consider the above outline of a nested if-else structure which has more if clauses than else clauses. Which of the statements below is true regarding this structure?
a)
syntactically it is invalid to have more if clauses than else clauses
b)
statement2 will only execute if condition1 is false and condition2 is false
c)
statement2 will only execute if condition1 is true and condition2 is false
d)
statement2 will only execute if condition1 is false, it does not matter what condition2 is
15.
Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the above conditions:
a)
All 4 Conditions are true
b)
Only Condition 2 is true
c)
Condition 2 and Condition 4 are true only
d)
Conditions 2, 3 and 4 are all true, Condition 1 is not
16.
If x is an int where x = 1, what will x be after the above loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
17.
If x is an int where x = 0, what will x be after the following loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
18.
Given the above code, where x = 0, what is the resulting value of x after the for-loop terminates?
for(int i=0; i<5; i++)
x += i;
for(int i=0; i<5; i++)
x += i;
a)
4
b)
5
c)
10
d)
15
19.
How many times will the above loop iterate?
a)
0
b)
9
c)
10
d)
11
20.
Given two String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use?
a)
(s1.equals(s2))
b)
(s1.length( ).equals(s2))
c)
(s1.length( ).equals(s2.length( ))
d)
(s1.length( ) == s2.length( ))
21.
Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character?
a)
(s.charAt(0) == s.charAt(s.length( )))
b)
(s.charAt(1) == s.charAt(s.length( )))
c)
(s.charAt(0) == s.charAt(s.length( ) - 1))
d)
(s.charAt(0) == s.charAt(s.length( ) + 1))
22.
Given that s is a String, what does the above loop do?
a)
it prints s out forwards
b)
it prints s out backwards after skipping the last character
c)
it prints s out backwards
d)
it prints s out backwards but does not print the 0th character
23.
The above nested loop structure will execute the inner most statement (x++) how many times?
a)
100
b)
200
c)
10000
d)
20000
24.
Consider the following paint method above. This paint method will draw several bars (sort of like a bar graph). How many bars will be displayed?
a)
4
b)
5
c)
20
d)
200
25.
Consider the following paint method above. This paint method will draw several bars (sort of like a bar graph). The size of each rectangle (bar)
a)
increases in height while staying the same width
b)
decreases in height while staying the same width
c)
increases in width while staying the same height
d)
increases in both width and height
26.
Considering the (incomplete) code above, x is probably
a)
a primitive type
b)
a String
c)
an iterator
d)
a random number generator
Reset
