Ch05 Solutions
-
Upload
darkangel298 -
Category
Documents
-
view
356 -
download
26
Transcript of Ch05 Solutions
SolutionsChapter 5: Decisions
Multiple Choice
1. What kind of statement lets a program carry out different actions depending on a condition?A) ifB) decisionC) whichD) condition
Ans: ASection Ref: Section 5.1 The if StatementTitle: TB 5.1 What kind of statement lets a program carry out different actions depending on a condition?Difficulty: Easy
2. What kind of statement groups several statements together?A) simpleB) booleanC) blockD) condition
Ans: CSection Ref: Section 5.1 The if StatementTitle: TB 5.2 What kind of statement groups several statements together?Difficulty: Easy
3. Block statements are enclosed by _____________?A) < >B) { }C) [ ]D) ( )
Ans: BSection Ref: Section 5.1 The if StatementTitle: TB 5.3 Block statements are enclosed by _____________?Difficulty: Easy
4. Conditions are enclosed by _____________?A) ( )B) < >
C) [ ]D) { }
Ans: ASection Ref: Section 5.1 The if StatementTitle: TB 5.4 Conditions are enclosed by _____________?Difficulty: Easy
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)
Ans: DSection Ref: Section 5.1 The if StatementTitle: TB 5.5 Which if condition correctly checks whether there are sufficient funds in the bank account?Difficulty: Easy
6. The following code is an example of what kind of statement? balance = balance - amount;
A) simpleB) blockC) loopD) compound
Ans: ASection Ref: Section 5.1 The if StatementTitle: TB 5.6 The following code is an example of what kind of statement?Difficulty: Medium
7. The following code is an example of what kind of statement?if (balance >= amount) balance = balance - amount;
A) loopB) compoundC) simpleD) block
Ans: BSection Ref: Section 5.1 The if StatementTitle: TB 5.7 The following code is an example of what kind of statement?
Difficulty: Medium
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");
Ans: CSection Ref: Section 5.1 The if StatementTitle: TB 5.8 Which Java statement correctly implements the following pseudocode for a grade higher than 90 (inclusive):Difficulty: Medium
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 > balanceD) Always prints out "Insufficient funds".
Ans: DSection Ref: Section 5.1 The if StatementTitle: TB 5.9 Which statement is true about the following code fragment involving a conditional?Difficulty: Medium
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) conditionalB) switchC) question markD) if/else
Ans: ASection Ref: Special Topic 5.1 The Selection OperatorTitle: TB 5.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?Difficulty: Easy
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) 3B) 2C) 0D) 4
Ans: DSection Ref: Special Topic 5.1 The Selection OperatorTitle: TB 5.11 What is the value of the selection?Difficulty: Easy
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) 0B) 4C) 2D) 1
Ans: DSection Ref: Special Topic 5.1 The Selection OperatorTitle: TB 5.12 What is the value of the selection?Difficulty: Easy
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;
Ans: ASection Ref: Special Topic 5.1 The Selection OperatorTitle: TB 5.13 Which statement is similar to the statement given involving the selection operator?Difficulty: Hard
14. What kind of operator compares two values? A) conditionalB) booleanC) relationalD) predicate
Ans: CSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.14 What kind of operator compares two values? Difficulty: Easy
15. Which operator tests for equality? A) !=B) ===C) =D) ==
Ans: DSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.15 Which operator tests for equality? Difficulty: Easy
16. Which of the following is a relational operator?A) !=B) ||C) /D) &&
Ans: ASection Ref: Section 5.2 Comparing ValuesTitle: TB 5.16 Which of the following is a relational operator?
Difficulty: Medium
17. What is the opposite of the operator < ?A) >B) >=C) <=D) =>
Ans: BSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.17 What is the opposite of the operator < ?Difficulty: Easy
18. What is the opposite of the operator > ?A) =<B) <=C) <D) =>
Ans: BSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.18 What is the opposite of the operator > ?Difficulty: Easy
19. Which operator should you use to test whether an object reference is null?A) ==B) >=C) <=D) =
Ans: ASection Ref: Section 5.2 Comparing ValuesTitle: TB 5.19 Which operator should you use to test whether an object reference is null?Difficulty: Medium
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) ...
Ans: ASection Ref: Section 5.2 Comparing ValuesTitle: TB 5.20 Which is the preferred approach to determine whether two integer variables (i1 and i2) are equal?Difficulty: Easy
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) ...
Ans: CSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.21 Which is the preferred approach to determine whether two double variables (d1 and d2) are equal?Difficulty: Medium
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))
Ans: ASection Ref: Section 5.2 Comparing ValuesTitle: TB 5.22 Which condition should be used to test whether the contents of two strings referenced by string1 and string2 have the same value?Difficulty: Easy
23. Which method compares strings in dictionary order?A) compareB) compareDictionary
C) equalsD) compareTo
Ans: DSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.23 Which method compares strings in dictionary order?Difficulty: Easy
24. What does it mean if the following test is true: s.compareTo(t) < 0? A) s is nullB) t is nullC) s comes before t in the dictionaryD) s comes after t in the dictionary
Ans: CSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.24 What does it mean if the following test is true: s.compareTo(t) < 0 ?Difficulty: Medium
25. What does it mean if string1 comes after string2 in dictionary order? A) string2.compareTo(string1) > 0B) string1.compareTo(string2) < 0C) string1.compareTo(string2) > 0D) string2.compareTo(string1) <> 0
Ans: CSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.25 What does it mean if string1 comes after string in dictionary order?Difficulty: Medium
26. If string1 and string2 are equal, then string1.compareTo(string2) _______. A) .equals(0)B) == 0C) == trueD) == null
Ans: BSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.26 If string1 and string2 are equal, then string1.compareTo(string2) ______.Difficulty: Medium
27. Which choice indicates that a string variable refers to no string?
A) nilB) ""C) nullD) empty
Ans: CSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.27 Which choice indicates that a string variable refers to no string?Difficulty: Medium
28. Which choice denotes an empty string?A) emptyB) ""C) nilD) null
Ans: BSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.28 Which choice denotes an empty string?Difficulty: Medium
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");
Ans: BSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.29 Which statement correctly checks whether hello1 and hello2 have the same value and displays the string "Hello"?Difficulty: Medium
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");
Ans: CSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.30 Which statement correctly checks whether hello1 and hello3 are identical string references and displays the string "Hello"?Difficulty: Medium
31. Which operator or method tests whether two object references are identical?A) ==B) equalsC) =D) identity
Ans: ASection Ref: Section 5.2 Comparing ValuesTitle: TB 5.31 Which operator or method tests whether two object references are identical?Difficulty: Easy
32. Which operator or method should you use to determine whether the contents of objects have the same value?A) ==B) equalsC) =D) same
Ans: BSection Ref: Section 5.2 Comparing Values
Title: TB 5.32 Which operator or method should you use to determine whether the contents of objects have the same value?Difficulty: Easy
33. Consider the following code:Faculty f = new Faculty(...);Student s = new Student(...); // Assume f is assigned as the advisor of the studentFaculty 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())) {...} ;
Ans: DSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.33 Which statement correctly checks whether the faculty advisor of a student is in the department in which a student is majoring?Difficulty: Medium
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) {...} ;
Ans: BSection Ref: Section 5.2 Comparing ValuesTitle: TB 5.34 Which statement correctly checks whether the student has been assigned a faculty advisor?Difficulty: Medium
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";
Ans: ASection Ref: Section 5.3 Multiple AlternativesTitle: TB 5.35 Which is the correct Java code to assign a letter grade to a student based on a score?Difficulty: Medium
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) compareB) ifElseC) switchD) sequence
Ans: CSection Ref: Special Topic 5.2 The switch StatementTitle: TB 5.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?Difficulty: Easy
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; ... }
Ans: ASection Ref: Special Topic 5.2 The switch StatementTitle: TB 5.37 Which code fragment involving a switch correctly maps a letter grade to a gpa value?Difficulty: Medium
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) nullB) "A"C) "B"D) ""
Ans: DSection Ref: Section 5.3 Multiple AlternativesTitle: TB 5.38 What is the value of letterGrade when the grade is 95?Difficulty: Medium
39. What type has a finite set of values?A) StringB) doubleC) intD) enumerated
Ans: DSection Ref: Special Topic 5.3 Enumerated TypesTitle: TB 5.39 What type has a finite set of values?Difficulty: Medium
40. An enumerated type variable can have ___A) an infinite number of values or nullB) a finite number of predetermined values but not nullC) a finite number of predeternimed values, or nullD) an infinite number of values
Ans: CSection Ref: Special Topic 5.3 Enumerated TypesTitle: TB 5.40 An enumerated type variable can have ___Difficulty: Medium
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 }
Ans: ASection Ref: Special Topic 5.3 Enumerated TypesTitle: TB 5.41 Which statement correctly declares an enumerated type for the classification of students?Difficulty: Medium
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) {...};
Ans: BSection Ref: Special Topic 5.3 Enumerated TypesTitle: TB 5.42 Which statement correctly checks the value of an enumerated type variable?Difficulty: Medium
43. Which type has the two values: true and false?A) enumeratedB) predicateC) flagD) boolean
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.43 Which type has the two values: true and false?Difficulty: Easy
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
Ans: BSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.44 Which operator takes a single condition and evaluates to true if that condition is false and false if that condition is true?Difficulty: Easy
45. What type of method returns a boolean value?A) voidB) staticC) finalD) predicate
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.45 What type of method returns a boolean value?Difficulty: Easy
46. The following code fragment is an example of a ___ method. public boolean hasAvailableFunds(){ return balance > 0;}
A) predicateB) conditionalC) staticD) relational
Ans: ASection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.46 This is an example of a ___ method.Difficulty: Medium
47. The operators &&, ||, and ! are what type of operators?A) conditionalB) conjunctionC) relationalD) boolean
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.47 The operators &&, ||, and ! are what type of operators?Difficulty: Easy
48. What is the name of the && operator?A) andB) notC) ampersandD) or
Ans: ASection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.48 What is the name of the && operator?Difficulty: Easy
49. What is the name of the || operator?A) notB) andC) vertical bar
D) or
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.49 What is the name of the || operator?Difficulty: Easy
50. What is the name of the ! operator?A) notB) exclamation markC) orD) and
Ans: ASection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.50 What is the name of the ! operator?Difficulty: Easy
51. Which operator combines several tests into a new test that passes only whenall conditions are true?A) ==B) ||C) !D) &&
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.51 Which operator combines several tests into a new test that passes only whenall conditions are true?Difficulty: Easy
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) &&
Ans: ASection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.52 Which operator combines several tests into a new test that succeeds if at least one of the conditions is true?Difficulty: Easy
53. What is the value of the following boolean expression when x is 100? 0 < x && x < 100
A) falseB) trueC) The code has a syntax error and will not compile.D) The code compiles but has a logic error.
Ans: ASection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.53 What is the value of the boolean expression involving and?Difficulty: Medium
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) trueC) falseD) The code compiles but has a logic error.
Ans: BSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.54 What is the value of the boolean expression involving or?Difficulty: Medium
55. What is the value of the following boolean expression when x is -10? 0 < x || x < 100
A) trueB) The code compiles but has a logic error.C) The code has a syntax error and will not compile.D) false
Ans: BSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.55 What is the value of the boolean expression involving or?Difficulty: Medium
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) trueC) The code has a syntax error and will not compile.D) false
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.56 What is the value of the boolean expression involving not?Difficulty: Medium
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) trueB) The code has a syntax error and will not compile.C) falseD) The code compiles but has a logic error.
Ans: CSection Ref: Section 5.2 Comparing Values, Section 5.4 Using Boolean ExpressionsTitle: TB 5.57 What is the value of the boolean condition that compares objects?Difficulty: Medium
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) falseB) trueC) The code compiles but has a logic error.D) The code has a syntax error and will not compile.
Ans: BSection Ref: Section 5.2 Comparing Values, Section 5.4 Using Boolean ExpressionsTitle: TB 5.58 What is the value of the boolean condition that compares objects?Difficulty: Medium
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) trueB) The code compiles but has a logic error.C) falseD) The code has a syntax error and will not compile.
Ans: ASection Ref: Section 5.2 Comparing Values, Section 5.4 Using Boolean ExpressionsTitle: TB 5.59 What is the value of the boolean condition that compares values?Difficulty: Medium
60. Which of the following conditions tests whether x is between 1 and 10 (inclusive)?A) 1 <= x && x < 10B) 1 < x && x <= 10C) 10 >= x && x >= 1D) 1 <= x <= 10
Ans: CSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.60 Which of the following conditions tests whether x is between 1 and 10 (inclusive)?Difficulty: Medium
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) falseD) true
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.61 What is the value of the boolean condition that compares strings?Difficulty: Medium
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);
Ans: CSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.62 Which of the following conditions is equivalent to the code fragment involving boolean expressions?Difficulty: Medium
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) == '?'
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.63 Which of the following statements determines whether the input string is a question?Difficulty: Medium
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) == '>'
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.64 Which of the following statements determines whether the input string is enclosed in angle brackets?Difficulty: Medium
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.
Ans: ASection Ref: Special Topic 5.4: Lazy Evaluation of Boolean OperatorsTitle: TB 5.65 What is the definition of lazy evaluation in the context of boolean operators?Difficulty: Easy
66. Which of the following operators uses lazy evaluation?A) &B) |C) !D) &&
Ans: DSection Ref: Special Topic 5.4: Lazy Evaluation of Boolean OperatorsTitle: TB 5.66 Which of the following operators uses lazy evaluation?Difficulty: Medium
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;
Ans: DSection Ref: Special Topic 5.4: Lazy Evaluation of Boolean OperatorsTitle: TB 5.67 Which statement illustrates a correct application of lazy evaluation?Difficulty: Medium
68. Consider the following code fragment:validInput = input != null && Integer.parseInt(input) > 0;
Under which condition will lazy evaluation occur?A) Integer.parseInt(input) <= 0B) input != nullC) Integer.parseInt(input) > 0
D) input == null
Ans: DSection Ref: Special Topic 5.4: Lazy Evaluation of Boolean OperatorsTitle: TB 5.68 Which statement illustrates a correct application of lazy evaluation?Difficulty: Medium
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');
Ans: ASection Ref: Special Topic 5.4: Lazy Evaluation of Boolean OperatorsTitle: TB 5.69 Which statement completes the code fragment, illustrating a correct application of lazy evaluation?Difficulty: Medium
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")
Ans: BSection Ref: Special Topic 5.4: Lazy Evaluation of Boolean OperatorsTitle: TB 5.70 Which statement illustrates a correct application of lazy evaluation?Difficulty: Medium
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")) {...}
Ans: DSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.71 Which of the following conditions tests whether the user input is NOT Yes (ignoring case)?Difficulty: Hard
72. You can store the outcome of a condition in what type of variable?A) predicateB) logicalC) booleanD) conditional
Ans: CSection Ref: Section 5.4 Using Boolean ExpressionsTitle: TB 5.72 You can store the outcome of a condition in what type of variable?Difficulty: Easy
73. Which of the following conditions tests whether x is NOT between 1 and 10 (inclusive)?A) 10 < x || x < 1B) !(1 <= x <= 10)C) 1 <= x || x <= 10D) 1 > x && x <= 10
Ans: ASection Ref: Special Topic 5.5 DeMorgans LawsTitle: TB 5.73 Which of the following conditions tests whether x is NOT between 1 and 10 (inclusive)?Difficulty: Hard
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) != ">"
Ans: ASection Ref: Special Topic 5.5 DeMorgans Laws
Title: TB 5.74 Which of the following statements determines whether the input string is NOT enclosed in angle brackets?Difficulty: Hard
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 > 62B) age >= 18 || age <= 62C) age <= 18 || age >= 62D) age >= 18 && age <= 62
Ans: DSection Ref: Special Topic 5.5 DeMorgans LawsTitle: TB 5.75 Which of the following statements determines whether a person is ineligible for a junior or senior discount?Difficulty: Hard