AP JAVA Ch. 3 Test

Name:

The following are Multiple Choice. Please write your answers in CAPITAL letters.

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 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 24-25:

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

AP JAVA Ch. 3 Test

Name:

The following are Free-Response questions.

27) 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';

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

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

30) Write 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 Scanner has been imported and the variable scan is declared properly.

31) A truth table shows, for the various true or false values of boolean variables, what the result of a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean variables.

a b c a & (!b | | c)

false false false

false false true

false true false

false true true

true false false

true false true

true true false

true true true

32) A truth table shows, for the various true or false values of boolean variables, what the result of a boolean condition is. Fill in the following truth table. Assume that a, b and c are boolean variables.

a b c ! (a & b) | | ! (a & c)

false false false

false false true

false true false

false true true

true false false

true false true

true true false

true true true

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

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

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

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

Bouns: 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 and 100. Assume that Scanner has been imported and the variable scan is declared properly.