Comp test 3

1)
The idea that program instructions execute in order (linearly) unless otherwise specified through a
conditional statement is known as

B) flow of control

2)
Of the following if statements, which one correctly executes three instructions if the condition is
true?

D)
if (x < 0)
{
a = b * 2;
y = x;
z = a - y;
}

3)
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?

B)
if (x > 0) x++;
else if (x < 0) x--;

if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
4) If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?

C) 3

if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
5) If x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?

B) 2

if (a > 0)
if (b < 0)
x = x + 5;
else
if (a > 5)
x = x + 4;
else
x = x + 3;
else
x = x + 2;
6) If x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?

E) 5

7)
Consider the following code that will assign a letter grade of 'A', 'B', 'C', 'D', or 'F' depending on a
student's test score

B) This code will work correctly only if grade < 70

8)
Assume that count is 0, total is 20 and max is 1. The following statement will do which of the
following? if (count != 0 && total / count > max) max = total / count;

E) The condition short circuits and the assignment statement is not executed

9)
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 conditiona

10)
Consider the following 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?
if (condition1)
if (condition2)
statement1;
else statement2

A) statement2 will only execute if condition1 is false and condition2 is false

11)
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 following conditions:
Condition 1: (x < y && x > 0)
Condition 2: (a != d || x != 5)
Condition 3: !(true && false)
Conditi

E) Conditions 2, 3 and 4 are all true, Condition 1 is not

12) The break statement does which of the following?

D) transfers control out of the current control structure such as a loop or switch statement

13) If a break occurs within the innermost loop of a nested loop that is three levels deep

A) when the break is encountered just the innermost loop is "broken

14) Every Interator

D) has a hasNext( ) method

15)
If x is an int where x = 1, what will x be after the following loop terminates?
while (x < 100)
x *= 2;

A) 128

16)
If x is an int where x = 0, what will x be after the following loop terminates?
while (x < 100)
x *= 2

E) none of the above, this is an infinite loop

17)
How many times will the following loop iterate?
int x = 10;
while (x > 0)
{
System.out.println(x);
x--;
}

C) 10 times

18) You might choose to use a switch statement instead of nested if-else statements if

A) the variable being tested might equal one of only a few int values

19) If a switch statement is written that contains no break statements whatsoever

E) none of the above

20) A continue statement
switch (x)
{
case 3 : x += 1;
case 4 : x += 2;
case 5 : x += 3;
case 6 : x++;
case 7 : x += 2;
case 8 : x--;
case 9 : x++
}

A) may be used within any Java loop statement

21) If x is currently equal to 5, what will the value of x be after the switch statement executes?

D) 11

22) If x is currently equal to 3, what will the value of x be after the switch statement executes?

A) 12

23) The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as

A) y = (x < 0) ? x : 0

24)
Given the following code, where x = 0, what is the resulting value of x after the for-loop
terminates?
for (int i=0;i<5;i++)

E) 10

25) The do loop differs from the while loop in that

C) the do loop will always execute the body of the loop at least once

26)
How many times will the following loop iterate?
int x = 10;
do {
System.out.println(x);
x--;
} while (x > 0);

A) 11 times

27)
Given that s is a String, what does the following loop do?

E) it prints s out backwards

28)
The following nested loop structure will execute the inner most statement (x++) how many times?
for (int j = 0; j < 100; j++)
for (int k = 100; k > 0; k--)
x++;

E) 10,000

29)
What will the following code do? Assume s is a String, x is an int initialized to 10, page is a
Graphics object, and this is part of a paint method for an applet.
boolean isVowel = false;
String vowels = "aeiou";
for (int j = 0; j < s.length( ); j++)

C)
The String s is printed down the applet in two columns with vowels appearing in the left
column and all other characters in the right column

30) Which of the following statements are true about Java loops?

C) all three loop statements are functionally equivalent

31)
Rewrite the following set of if statements using a nested if-else structure.
if (score >= 90) grade = 'A';
if (score >= 80 && score < 90) grade = 'B';
if (score >= 70 && score < 80) grade = 'C';
if (score >= 60 && score < 70) grade = 'D';
if (score <

if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else if (score >= 60) grade = 'D';
else grade = 'F';

32)
Explain what is meant by short circuiting and provide an example of short circuiting a condition with && and
provide an example of short circuiting a condition with | |.

Short circuiting is when a boolean expression does not have to be completely evaluated because the final answer is
determined with just a partial evaluation. This can occur in an expression with && if one part of the expression
evaluates to false, and in

33)
The following code has a syntax error immediately before the word else. What is the error and why does it
arise? Fix the code so that this statement is a legal if-else statement.
if (x < 0) ; x++;
else x--;

The error is "else without if" and it arises because of the ";" after the condition but before x++. The Java compiler
determines that the if clause is in fact ; (no statement) and that x++; is a statement that follows the if statement.
Therefore, since x+

34)
String s1 is said to overlap String s2 if all of the characters in s1 also appear in s2. Write a set of code that will
set the boolean variable overlap to true if s1 overlaps s2 and false otherwise. Assume both s1 and s2 have
already been input.

)
boolean overlap = true; // assume the two Strings will overlap
int j = 0;
while (overlap && j < s1.length( ))
{
boolean found = false; // search in s2 for character at position j in s1
int k = 0;
while (!found && k < s2.length( )) // if found, set found

35)
Rewrite the following nested if-else statement as an equivalent switch statement.
if (letter == 'A' | | letter == 'a') System.out.println("Excellent");
else if (letter == 'B' | | letter == 'b') System.out.println("You can do better");
else if (letter

switch (letter)
{
case 'A', 'a' : System.out.println("Excellent"); break;
case 'B', 'b' : System.out.println("You can do better"); break;
case 'C', 'c' : System.out.println("Try harder"); break;
case 'D', 'd' : System.out.println("Try much harder"); break

36)
Given the following tax table information, write Java code to assign the double taxRate appropriately given the
double pay. Select the selection statement (if, if-else, switch) that makes the most sense.
If pay is more than 100,000, tax rate is 40%
If

Use a nested if-else structure as it requires the least amount of code. A switch statement is not possible because pay is a
double and the switch statement can only be used for integral types.
if (pay > 100000)
taxRate = 0.40;
else if (pay > 60000)
taxRat

37)
Show the output that would occur from the following code, including proper spacing.
for (j = 0; j < 5; j++)
{
for (k = 0; k < 5; k++)
if (j!=k)
System.out.print(' ');
else
System.out.print('*');
System.out.println( );

) *
*
*
*
*
The outer loop iterates from 0 to 4, and for each iteration, the inner loop also iterates from 0 to 4. For iteration of the
inner loop, if j and k are not the same, a blank is output, otherwise an *. So, if j = 0 and k = 2, then two blanks are

38)
How many times will the System.out.println(*); statement execute inside of the following nested for-loops?
for (j=0; j<10; j++)
for (k=10;k>j;k--)
System.out.println("*");

55

39)
Write a "query-controlled" loop that will continue to input int values from the user, adding each to the int value
sum, and then ask if the user has another value to input, until the user says that there are no more values.
Assume that cs1.Keyboard ha

)
int x, sum = 0;
String query;
do {
System.out.println("Enter an int value to be summed");
x = Keyboard.readInt( );
sum += x;
System.out.println("Do you have any more ints to input? (Yes or No) ");
query = Keyboard.readString( );
} while (query.equalsIgn

40)
Write some code that inputs a set of int values and computes its average. Ask the user first how many int
values will be input. Make sure that your code cannot produce a division by zero error. Assume that
cs1.Keyboard has been imported.

int numberOfValues, j, current, sum = 0;
System.out.println("Enter the number of ints you will be inputting");
numberOfValues = Keyboard.readInt( );
for (j = 0; j < numberOfValues; j++)
{
System.out.println("Enter your next int value");
current = Keyboard

41)
A data verification loop is a loop that asks the user to input a value within a restricted range repeatedly until the
user has input a value within the requested range. Write a data verification loop that that inputs an int value x
within the range 0

)
int x;
do {
System.out.println("Enter an int value between 0 and 100");
x = Keyboard.readInt( );
} while (x >= 0 && x <= 100);

42) Describe a situation where you should use a do loop and a situation where you should use a while loop

The do loop is used if you want to execute the loop body at least one time. This is useful for data verification (asking
the user for a value, and only if the user has entered an improper value does your code repeat and try again). A while
loop does not n

43)
Rewrite the following if-else statement using a conditional operator.
if (x > y) z = x; else z = y;

z = (x > y) ? x : y;

44)
The following for-loop is odd in that the loop increment value changes during iterations of the loop. Determine
the number of times the loop iterates and the final value of j after the loop terminates.
int k = 0;
for (j = 0; j < 20; j += k)
k++;

6 times with j = 21. Explanation: The first iteration has j = 0, k is then incremented in the loop body to 1 and j is
incremented after the loop body executes to be j + k = 0 + 1 = 1. For the second iteration, k is incremented to 2 and j is
incremented to

45)
Write a set of code that outputs all of the int values between 1 and 100 with 5 values per line, and each of those 5
values spaced out equally. Use a single for-loop to solve this problem.

for (int j = 1; j <= 100; j++)
{
System.out.print(j + "\t");
if (j % 5 == 0) System.out.println( );
}

46)
The following code has a syntax error immediately before the word else. What is the error and why does it
arise? Fix the code so that this statement is a legal if-else statement.
if (x < 0) ; x++;
else x--;

if (x < 0) x++;
else x--;