Lewis/Loftus/Cocking, 3/e: Chapter 3 Test Bank TB 1

Chapter 3: Program Statements

TEST

Multiple Choice Questions:

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

e)all of the above

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

e)Could occur in any of the above

3)Which of the following would not be considered an algorithm?

a)a recipe

b)a computer program

c)pseudocode

d)a shopping list

e)travel directions

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

e)None of the above, all four levels would require equal creativity

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)sequentiality

e)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;

b){

if (x < 0)

a = b * 2;

y = x;

z = a – y;

}

c)if { (x < 0)

a = b * 2;

y = x;

z = a – y ;

}

d)if (x < 0)

{

a = b * 2;

y = x;

z = a – y;

}

e)b, c and d are all correct, but not a

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--;

b)if (x > 0) x++;

else if (x < 0) x--;

c)if (x > 0) x++;

if (x < 0) x--;

else x = 0;

d)if (x == 0) x = 0;

else x++;

x--;

e)x++;

x--;

Given the nested if-else structure below, answer questions 8-10.

if (a > 0)

if (b < 0)

x = x + 5;

else

if (a > 5)

x = x + 4;

else

x = x + 3;

else

x = x + 2;

8)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

e)5

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

e)5

10)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)4

e)5

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

if(score >= 90) grade = 'A';

if(score >= 80) grade = 'B';

if(score >= 70) grade = 'C';

if(score >= 60) grade = 'D';

else grade = ‘F’;

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

e)This code will not work correctly under any circumstances

12)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;

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

e)The condition will not compile because it uses improper syntax

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

e)There is nothing wrong with the logic at all

14)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)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

e)statement2 will never execute

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 following conditions:

Condition 1: (x < y & x > 0)

Condition 2: (a != d || x != 5)

Condition 3: !(true & false)

Condition 4: (x > y || a == 'A' || d != 'A')

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

e)All 4 Conditions are false

16)If x is an int where x = 1, what will x be after the following loop terminates?

while (x < 100)

x *= 2;

a)2

b)64

c)100

d)128

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

while (x < 100)

x *= 2;

a) 2

a)64

b)100

c)128

d)None of the above, this is an infinite loop

18)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++)

x += i;

a)0

b)4

c)5

d)10

e)15

19)How many times will the following loop iterate?

int x = 10;

while (x > 0)

{

System.out.println(x);

x--;

}

a)0 times

b)1 time

c)9 times

d)10 times

e)11 times

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( ))

e)length(s1) == length(s2)

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

e) (s.charAt(0) == s.charAt(last))

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

for(int j = s.length( ); j > 0; j--)

System.out.print(s.charAt(j-1));

a)it prints s out backwards

b)it prints s out forwards

c)it prints s out backwards after skipping the last character

d)it prints s out backwards but does not print the 0th character

e)it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

23)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++;

a)100

b)200

c)10,000

d)20,000

e)1,000,000

Consider the following paint method and answer questions 33-34:

public void paint(Graphics page)

{

int x, y = 200;

page.setColor(Color.blue);

for(x = 100; x < 200; x += 20)

page.fillRect(x, y, 10, y-x);

}

24)This paint method will draw several bars (sort of like a bar graph). How many bars will be displayed?

a)4

b)5

c)6

d)10

e)20

25)The size of each rectangle (bar)

a)increases in height while staying the same width

b)increases in width while staying the same height

c)increases in both width and height

d)stays the same size

e)decreases in height while staying the same width

26)Considering the (incomplete) code below, x is probably

while (x.hasNext())

{

… x.next() …

}

a)a primitive type

b)a String

c)an iterator

d)a random number generator

e)a file

True/False Questions:

27)Only difficult programming problems require a pseudocode solution before the programmer creates the implementation (program) itself.

28)In Java, if and if-else are selection statements.

29)In Java, the symbol “=” and the symbol “==” are used synonymously (interchangeably).

30)When comparing any primitive type of variable, == should always be used to test to see if two values are equal.

31)The statements x++; and ++x; will accomplish the same thing.

32)The statement { } is a legal block.

33)The statement if(a >= b) a++; else b--; will do the same thing as the statement if (a < b) b--; else a++;.

34)An if statement may or may not have an else clause, but an else clause must be part of an if statement.

35)In order to compare int and double variables, you can use <, >, ==, !=, <=, >=, but to compare char and String variables, you must use compareTo( ), equals( ) and equalsIgnoreCase( ).

36)The following for-loop is an infinite loop.

for(int j = 0; j < 1000; ) i++;

37)The following loop is syntactically valid.

for(int j = 0; j < 1000; j++) j--;

For questions 12-14, assume that boolean done = false, int x = 10, int y = 11, String s = "Help" and String t = "Goodbye".

38)The expression (!done & x <= y) is true.

39)The expression (s.concat(t).length( ) < y) is true.

40)The expression (done | | s.compareTo(t) < 0) is true.

41)In Java, it is possible to create an infinite loop out of while loops, but not for-loops.

42)The programmer creates pseudocode during the implementation phase of program development.

43)Software design is the first step in program development.

44)The relational operators < and > can be used to tell whether one string comes before another alphabetically.

45)The expression ‘Z’ < ‘a’ is true.

46)The expression a || b will be true if either a or b is true, but not if both are true.

.

47)The statement num -= num; will have the same result as the statement num = 0;

48)If the integer variable answer is 5, then after answer %= 8; is executed, answer will be 3.

49)Any for loop can be written as a while loop.

50)The third portion of a for loop, the increment portion, must increment or decrement a variable by 1.

51)When reading an unknown amount of input from the user, a for loop would be the best choice.

52)The for loop for (Team team : teamList) { … } is invalid and will cause a syntax error.

53)A foreach statement can be used to loop through all the possible values of an enumerated type.

Extra Credit Free-form Questions:Record your answers on a blank sheet of paper.

1)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 < 60) grade = 'F';

2)Given the following tax table information, write Java code to assign the double taxRate appropriately given the double pay.

If pay is more than 100,000, tax rate is 40%

If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%

If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%

If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%

If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%

If pay is less than or equal to 5,000, tax rate is 0%

3)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( );

}

4)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("*");