Chapter: Chapter 5: Decisions

Study Guide

Multiple Choice

1. What kind of statement lets a program carry out different actions depending on a condition?

A) if

B) decision

C) which

D) condition

2. What kind of statement groups several statements together?

A) simple

B) boolean

C) block

D) condition

3. Block statements are enclosed by ______?

A)

B) { }

C) [ ]

D) ( )

4. Conditions are enclosed by ______?

A) ( )

B)

C) [ ]

D) { }

5. Which if condition correctly checks whether there are sufficient funds in the bank account?

A) if (balance = amount)

B) if {balance >= amount}

C) if [balance >= amount]

D) if (balance >= amount)

6. The following code is an example of what kind of statement?

balance = balance - amount;

A) simple

B) block

C) loop

D) compound

7. The following code is an example of what kind of statement?

if (balance >= amount)

balance = balance - amount;

A) loop

B) compound

C) simple

D) block

8. Which Java statement correctly implements the following pseudocode:

Display "A" when the student's grade is higher than 90 (inclusive)

A) if (studentGrade = 90)

System.out.println("A");

B) if (studentGrade > 90)

System.out.println("A");

C) if (studentGrade >= 90)

System.out.println("A");

D) if (studentGrade < 90)

System.out.println("A");

9. Which statement is true about the following code fragment involving a conditional?

if (amount > balance) ;

System.out.println("Insufficient funds");

A) Never prints out "Insufficient funds".

B) Does not compile.

C) Only prints out "Insufficient funds" when amount > balance

D) Always prints out "Insufficient funds".

10. What is the following operator called where the value of the expression is either value1 if the condition is true or value2 if it is false?

condition ? value1 : value2;

A) conditional

B) switch

C) question mark

D) if/else

11. Consider the code fragment involving a selection operator:

q = (p % 4) != 0 ? (p % 4) : 4;

What is the value of q when p is 8?

A) 3

B) 2

C) 0

D) 4

12. Consider the code fragment involving a selection operator:

q = (p % 4) != 0 ? (p % 4) : 4;

What is the value of q when p is 9?

A) 0

B) 4

C) 2

D) 1

13. The following statement involving the selection operator is similar to which statement?

y = x >= 0 ? x : -x;

A) if (x >= 0)

y = x;

else

y = -x;

B) if (x >= 0)

y = x;

C) if (x >= 0)

y = -x;

D) if (x >= 0)

y = -x;

else

y = x;

14. What kind of operator compares two values?

A) conditional

B) boolean

C) relational

D) predicate

15. Which operator tests for equality?

A) !=

B) ===

C) =

D) ==

16. Which of the following is a relational operator?

A) !=

B) ||

C) /

D)

17. What is the opposite of the operator < ?

A)

B) >=

C) <=

D) =>

18. What is the opposite of the operator > ?

A) =<

B) <=

C)

D) =>

19. Which operator should you use to test whether an object reference is null?

A) ==

B) >=

C) <=

D) =

20. Which is the preferred approach to determine whether two integer variables (i1 and i2) are equal, assuming the following declarations?

int i1;

int i2;

final double EPSILON = 1E-14;

A) if (i1 == i2) ...

B) if Math.abs(i1 - i2) <= EPSILON ...

C) if (Math.abs(i1 - i2) <= EPSILON) ...

D) if (i1 = i2) ...

21. Which is the preferred approach to determine whether two double variables (d1 and d2) are equal, assuming the following declarations?

double d1;

double d2;

final double EPSILON = 1E-14;

A) if (d1 = d2) ...

B) if Math.abs(d1 - d2) <= EPSILON ...

C) if (Math.abs(d1 - d2) <= EPSILON) ...

D) if (d1 == d2) ...

22. Which condition should be used to test whether the contents of two strings referenced by string1 and string2 have the same value?

A) (string1.equals(string2))

B) (String.equals(string1, string2))

C) (string1 == string2)

D) (Equals(string1, string2))

23. Which method compares strings in dictionary order?

A) compare

B) compareDictionary

C) equals

D) compareTo

24. What does it mean if the following test is true: s.compareTo(t) < 0?

A) s is null

B) t is null

C) s comes before t in the dictionary

D) s comes after t in the dictionary

25. What does it mean if string1 comes after string2 in dictionary order?

A) string2.compareTo(string1) > 0

B) string1.compareTo(string2) < 0

C) string1.compareTo(string2) > 0

D) string2.compareTo(string1) > 0

26. If string1 and string2 are equal, then string1.compareTo(string2) ______.

A) .equals(0)

B) == 0

C) == true

D) == null

27. Which choice indicates that a string variable refers to no string?

A) nil

B) ""

C) null

D) empty

28. Which choice denotes an empty string?

A) empty

B) ""

C) nil

D) null

29. Consider the following code:

String greeting = "Hello, World!";

String hello1 = "Hello";

String hello2 = greeting.substring(0,6);

String hello3 = hello1;

Which statement correctly checks whether hello1 and hello2 have the same value and displays the string "Hello"?

A) if hello2 == hello1

System.out.println("Hello");

B) if (hello1.equals(hello2))

System.out.println("Hello");

C) if (hello1 == hello2)

System.out.println("Hello");

D) if hello2.equals(hello1)

System.out.println("Hello");

30. Consider the following code:

String greeting = "Hello, World!";

String hello1 = "Hello";

String hello2 = greeting.substring(0,6);

String hello3 = hello1;

Which statement correctly checks whether hello1 and hello3 are identical string references and displays the string "Hello"?

A) if hello3.equals(hello1)

System.out.println("Hello");

B) if (hello1.equals(hello3))

System.out.println("Hello");

C) if (hello1 == hello3)

System.out.println("Hello");

D) if hello3 == hello1

System.out.println("Hello");

31. Which operator or method tests whether two object references are identical?

A) ==

B) equals

C) =

D) identity

32. Which operator or method should you use to determine whether the contents of objects have the same value?

A) ==

B) equals

C) =

D) same

33. Consider the following code:

Faculty f = new Faculty(...);

Student s = new Student(...); // Assume f is assigned as the advisor of the student

Faculty deptChair = f;

Which statement correctly checks whether the faculty advisor of a student is in the department in which a student is majoring, assuming that getMajor, getAdvisor, and getDept are valid accessor methods?

A) if s.getAdvisor.getDept.equals(s.getMajor) {...} ;

B) if s.getAdvisor().getDept().equals(s.getMajor()) {...} ;

C) if (s.getMajor.equals(s.getAdvisor.getDept)) {...} ;

D) if (s.getMajor().equals(s.getAdvisor().getDept())) {...} ;

34. Consider the following code:

Student s = new Student(...);

Which statement correctly checks whether the student has been assigned a faculty advisor, assuming that getAdvisor is a valid accessor method?

A) if (s.getAdvisor() = null) {...} ;

B) if (s.getAdvisor() == null) {...} ;

C) if s.getAdvisor() = null {...} ;

D) if (s.getAdvisor == null) {...} ;

35. Which is the correct Java code to assign a letter grade (A, B, or C) to a student based on a score?

A) if (score >= 90)

letterGrade="A";

else

if (score >= 80)

letterGrade = "B";

else

letterGrade = "C";

B) if (score >= 90)

letterGrade="A"

else

if (score >= 80)

letterGrade = "B"

else

letterGrade = "C";

C) if (score >= 70)

letterGrade="C";

else

if (score >= 80)

letterGrade = "B";

letterGrade = "A";

D) if (score >= 90)

letterGrade="A";

if (score >= 80)

letterGrade = "B";

if (score >= 70)

letterGrade = "C";

36. What type of statement can be used to implement a sequence of if/else/else that compares a single value against several constant alternatives?

A) compare

B) ifElse

C) switch

D) sequence

37. Which code fragment involving a switch correctly maps a letter grade to a gpa value, assuming the following declarations?

String letterGrade;

double gpaValue;

A) Char letter = letterGrade.charAt(0);

switch (letter)

{

'A': gpaValue = 4.0; break;

...

}

B) switch (letterGrade)

{

"A": gpaValue = 4.0;

...

}

C) switch (letterGrade)

{

"A": gpaValue = 4.0; break;

...

}

D) switch (letterGrade)

{

A: gpaValue = 4.0; break;

...

}

38. Consider the following code fragment:

String letterGrade = "";

if (grade < 90)

if (grade >= 80)

letterGrade = "B";

else

letterGrade = "A";

System.out.println(letterGrade);

What is the value of letterGrade when the grade is 95?

A) null

B) "A"

C) "B"

D) ""

39. What type has a finite set of values?

A) String

B) double

C) int

D) enumerated

40. An enumerated type variable can have ___

A) an infinite number of values or null

B) a finite number of predetermined values but not null

C) a finite number of predeternimed values, or null

D) an infinite number of values

41. Which statement correctly declares an enumerated type for the classification of students?

A) public enum StudentClassification { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }

B) public enumerated StudentClassification { "FRESHMAN", "SOPHOMORE", "JUNIOR", "SENIOR"}

C) public enum StudentClassification = { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }

D) public enumerated StudentClassification = { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }

42. Which statement correctly checks the value of an enumerated type variable, assuming the following declarations?

public enum Semester { SPRING, SUMMER, FALL, WINTER }

Semester currentSemester; // Assume assigned a valid value

A) if (currentSemester == FALL) {...};

B) if (currentSemester == Semester.FALL) {...};

C) if (currentSemester.equals(Semester.FALL)) {...};

D) if currentSemester.equals(Semester.FALL) {...};

43. Which type has the two values: true and false?

A) enumerated

B) predicate

C) flag

D) boolean

44. Which operator takes a single condition and evaluates to true if that condition is false and false if that condition is true?

A) ~

B) !

C) !=

D) not

45. What type of method returns a boolean value?

A) void

B) static

C) final

D) predicate

46. The following code fragment is an example of a ___ method.

public boolean hasAvailableFunds()

{

return balance > 0;

}

A) predicate

B) conditional

C) static

D) relational

47. The operators &, ||, and ! are what type of operators?

A) conditional

B) conjunction

C) relational

D) boolean

48. What is the name of the operator?

A) and

B) not

C) ampersand

D) or

49. What is the name of the || operator?

A) not

B) and

C) vertical bar

D) or

50. What is the name of the ! operator?

A) not

B) exclamation mark

C) or

D) and

51. Which operator combines several tests into a new test that passes only when

all conditions are true?

A) ==

B) ||

C) !

D)

52. Which operator combines several tests into a new test that succeeds if at least one of the conditions is true?

A) ||

B) ==

C) !

D)

53. What is the value of the following boolean expression when x is 100?

0 < x & x < 100

A) false

B) true

C) The code has a syntax error and will not compile.

D) The code compiles but has a logic error.

54. What is the value of the following boolean expression when s is "y"?

s.equalsIgnoreCase("Y") || s.equalsIgnoreCase("N")

A) The code has a syntax error and will not compile.

B) true

C) false

D) The code compiles but has a logic error.

55. What is the value of the following boolean expression when x is -10?

0 < x || x < 100

A) true

B) The code compiles but has a logic error.

C) The code has a syntax error and will not compile.

D) false

56. What is the value of the following boolean expression when x is 100?

!(0 < x)

A) The code compiles but has a logic error.

B) true

C) The code has a syntax error and will not compile.

D) false

57. Consider the following code fragment:

Circle c1 = new Circle(3);

Circle c2 = new Circle(3);

Circle c3 = c2;

boolean condition = c1 == c2;

What is the value of condition?

A) true

B) The code has a syntax error and will not compile.

C) false

D) The code compiles but has a logic error.

58. Consider the following code fragment:

Circle c1 = new Circle(3);

Circle c2 = new Circle(3);

Circle c3 = c2;

boolean condition = c3.equals(c1);

What is the value of condition assuming that two circles are considered equal if they have the same radius value?

A) false

B) true

C) The code compiles but has a logic error.

D) The code has a syntax error and will not compile.

59. Consider the following code fragment:

int i1 = 7;

int i2 = (2 * 4) - 1;

int i3 = 11 - 2 * 2;

boolean condition = i1 == i2;

What is the value of condition?

A) true

B) The code compiles but has a logic error.

C) false

D) The code has a syntax error and will not compile.

60. Which of the following conditions tests whether x is between 1 and 10 (inclusive)?

A) 1 <= x & x < 10

B) 1 < x & x <= 10

C) 10 >= x & x >= 1

D) 1 <= x <= 10

61. Consider the following code fragment:

String s1 = "Y";

String s2 = "y"

boolean condition = s2.equalsIgnoreCase(s1);

What is the value of condition?

A) The code compiles but has a logic error.

B) The code has a syntax error and will not compile.

C) false

D) true

62. Consider the following code fragment:

response == 'Y' || value <= 6 & value >= 1;

Which of the following conditions is equivalent to the code fragment?

A) (response == 'Y' || (1 <= value <= 6));

B) ((response == 'Y' || value <= 6) & value >= 1);

C) (response == 'Y' || (value <= 6 & value >= 1));

D) ((response == 'Y' || value >= 1) & value <= 6);

63. Which of the following statements determines whether the input string is a question?

A) input.charAt(input.length()) == '?'

B) input.charAt(input.length()-1).equals("?")

C) input.charAt(input.length()-1) == "?"

D) input.charAt(input.length()-1) == '?'

64. In XML, an element has an opening tag and a matching closing tag. Tag names are enclosed by an opening angle bracket (<) and a closing angle bracket (>). Which of the following statements determines whether the input string is enclosed in angle brackets?

A) input.charAt(0).equals("<") & input.charAt(input.length()-1).equals(">")

B) input.charAt(0) == "<" & input.charAt(input.length()-1) == ">"

C) input.charAt(0) == '<' & input.charAt(input.length()) == '>'

D) input.charAt(0) == '<' & input.charAt(input.length()-1) == '>'

65. What is the definition of lazy evaluation in the context of boolean operators?

A) Logical expressions are evaluated from left to right, and evaluation stops as soon as the truth value is determined.

B) Logical expressions are combined into a new test that succeeds if at least one of the conditions is true.

C) Logical expressions are combined into a new test that evaluates to true if that condition is false and false if that condition is true.

D) Logical expressions are combined into a new test that passes only when all conditions are true.

66. Which of the following operators uses lazy evaluation?

A)

B) |

C) !

D)

67. Consider the following code fragment:

String input = JOptionPane.showInputDialog("Enter item quantity: ");

Which statement illustrates a correct application of lazy evaluation?

A) validInput = input != null & Integer.parseInt(input) > 0;

B) validInput = Integer.parseInt(input) > 0 & input != null;

C) validInput = Integer.parseInt(input) > 0 & input != null ;

D) validInput = input != null & Integer.parseInt(input) > 0;

68. Consider the following code fragment:

validInput = input != null & Integer.parseInt(input) > 0;

Under which condition will lazy evaluation occur?

A) Integer.parseInt(input) <= 0

B) input != null

C) Integer.parseInt(input) > 0

D) input == null

69. Consider the following code fragment:

String input = JOptionPane.showInputDialog("Continue (Y or N)? ");

if (input != null)

______;

Which statement illustrates a correct application of lazy evaluation?

A) validInput = input.equals("Y") || input.equals("N");

B) validInput = input.equals('Y') || input.equals('N');

C) validInput = input.equals("Y" || "N");

D) validInput = input.equals('Y' || 'N');

70. Consider the following code fragment:

validInput = input.equals("Y") || input.equals("N");

Under which condition will lazy evaluation occur?

A) !input.equals("N")

B) input.equals("Y")

C) input.equals("N")

D) !input.equals("Y")

71. Consider the following code fragment:

String input = in.next();

Which of the following conditions tests whether the user input is NOT Yes (ignoring case)?

A) if !(input.equalsIgnoreCase("Yes")) {...}

B) if !(input.equalsIgnoreCase('Yes')) {...}

C) if (!input.equalsIgnoreCase('Yes')) {...}

D) if (!input.equalsIgnoreCase("Yes")) {...}

72. You can store the outcome of a condition in what type of variable?

A) predicate

B) logical

C) boolean

D) conditional

73. Which of the following conditions tests whether x is NOT between 1 and 10 (inclusive)?

A) 10 < x || x < 1

B) !(1 <= x <= 10)

C) 1 <= x || x <= 10

D) 1 > x & x <= 10

74. In XML, an element has an opening tag and a matching closing tag. Tag names are enclosed by an opening angle bracket (<) and a closing angle bracket (>). Which of the following statements determines whether the input string is NOT enclosed in angle brackets?

A) input.charAt(0) != '<' || input.charAt(input.length()-1) != '>'

B) input.charAt(0) != '<' & input.charAt(input.length()) != '>'

C) input.charAt(0) == '<' & input.charAt(input.length()-1) == '>'

D) input.charAt(0) != "<" || input.charAt(input.length()-1) != ">"

75. Which of the following statements determines whether a person is ineligible for a junior or senior discount, assuming that a person is eligible for the discount if they are younger than 18 or older than 62?

A) age >= 18 & age > 62

B) age >= 18 || age <= 62

C) age <= 18 || age >= 62

D) age >= 18 & age <= 62