UNIT 3 COMP SCI

Ace your homework & exams now with Quizwiz!

Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the following boolean expressions is equivalent to !(a && b) || c ?

!a || !b || c

! ( ! (a != b ) && (b > 7) ) is equivalent to which of the following?

( a != b) || (b <= 7)

Assume that the boolean variables a, b, c, and d have been declared and initialized. Consider the following expression. !( !( a && b ) || ( c || !d )) Which of the following is equivalent to the expression?

( a && b ) && ( !c && d )

!((a >= b) && !(c < d)) Which of the following is equivalent to the expression above?

(a < b) || (c < d)

The following table shows the results of several calls to compareThree. Call Result compareThree(4, 5, 6) true compareThree(6, 5, 4) true compareThree(5, 4, 6) false compareThree(3, 4, 4) violates precondition Which of the following can be used to replace /* missing condition */ so that compareThree will work as intended when called with parameters that satisfy its precondition?

(x > y) == (y > z)

int start = 4; int end = 5; boolean keepGoing = true; if (start < end && keepGoing) { if (end > 0) { start += 2; end++; } else { end += 3; } } if (start < end) { if (end == 0) { end += 2; start++; } else { end += 4; } } What is the value of end after the code segment is executed?

6

String str1 = new String("Advanced Placement"); String str2 = new String("Advanced Placement"); if (str1.equals(str2) && str1 == str2) { System.out.println("A"); } else if (str1.equals(str2) && str1 != str2) { System.out.println("B"); } else if (!str1.equals(str2) && str1 == str2) { System.out.println("C"); } else if (!str1.equals(str2) && str1 != str2) { System.out.println("D"); } What, if anything, is printed when the code segment is executed?

B

public void conditionalTest(int a, int b) { if ((a > 0) && (b > 0)) { if (a > b) System.out.println("A"); else System.out.println("B"); } else if ((b < 0) || (a < 0)) System.out.println("C"); else System.out.println("D"); } What is printed as a result of the call conditionalTest(3, -2)?

C

At a certain high school students receive letter grades based on the following scale. Which of the following code segments will assign the correct string to grade for a given integer score ?

I and III only

Which of the following code segments will properly update the variable grade based on a student's performance on the bonus questions? I. if (bonusOne && bonusTwo && bonusThree) grade += 5; II. if (bonusOne || bonusTwo || bonusThree) grade += 5; III. if (bonusOne) grade += 5; if (bonusTwo) grade += 5; if (bonusThree) grade += 5;

I only

I. if ((n % 3 == 0) && (n % 4 == 0)) return n * 10; else return n; Scoring Guide Unit 3 Test Review AP Computer Science A Page 25 of 47 II. if ((n % 3 == 0) || (n % 4 == 0)) return n * 10; return n; III. if (n % 3 == 0) if (n % 4 == 0) return n * 10; return n;

II only

public class Person { private String name; public String getName() { return name; } } public class Book { private String author; private String title; private Person borrower; public Book(String a, String t) {author = a; title = t; borrower = null;} public void printDetails() {System.out.print("Author: " + author + " Title: " + title); if ( /* missing condition */ ) {System.out.println(" Borrower: " + borrower.getName());} } public void setBorrower(Person b) { borrower = b; } } Which of the following can replace /* missing condition */ so that the printDetails method CANNOT cause a run-time error? I. !borrower.equals(null) II. borrower != null III. borrower.getName() != null

II only

public String exercise(int input) { if (input < 10) { return "alpha"; } if (input < 5) { return "beta"; } if (input < 1) { return "gamma"; } return "delta"; } Assume that the int variable x has been initialized in another method in the same class. Which of the following describes the conditions under which the method call exercise(x) will return "gamma" ?

The method will never return "gamma".

Code Segment I double points = 0.0; if (grade > 89) { points += 4.0; } else if (grade > 79) { points += 3.0; } else if (grade > 69) { points += 2.0; } else { points += 0.0; } System.out.println(points); Code Segment II double points = 0.0; if (grade > 89) { points += 4.0; } if (grade > 79) { grade += 3.0; } if (grade > 69) { points += 2.0; } if (grade < 70) { points += 0.0; } System.out.println(points); Which of the following statements correctly compares the values printed by the two methods?

The two code segments print the same value only when grade is below 80.

Which of the following best describes the value of the Boolean expression shown below? a && !(b || a)

The value is always false.

if (false && true || false) { if (false || true && false) { System.out.print("First"); } else { System.out.print("Second"); } } if (true || true && false) { System.out.print("Third"); } What is printed as a result of executing the code segment?

Third

boolean c = (a && b) || (!a && b); Under which of the following conditions will c be assigned the value false ?

When b has the value false

Assume obj1 and obj2 are object references. Which of the following best describes when the expression obj1 == obj2 is true?

When obj1 and obj2 refer to the same object

int x = 7; int y = 4; boolean a = false; boolean b = false; if (x > y) { if (x % y >= 3) { a = true; x -= y; } else { x += y; } } if (x < y) { if (y % x >= 3) { b = true; x -= y; } else { x += y; } } What are the values of a, b, and x after the code segment has been executed?

a = true, b = false, x = 7

Assume that x and y are boolean variables and have been properly initialized. (x && y) && !(x || y)

false always

if (a < b || c != d) { System.out.println("dog"); } else { System.out.println("cat"); } Assume that the int variables a, b, c, and d have been properly declared and initialized. Which of the following code segments produces the same output as the given code segment for all values of a, b, c, and d ?

if (a >= b && c == d) { System.out.println("cat"); } else { System.out.println("dog"); }

if (one.equals(two)) { System.out.println("one dot equals two"); } if (one.equals(three)) { System.out.println("one dot equals three"); } if (two == three) { System.out.println("two equals equals three"); } What, if anything, is printed as a result of executing the code segment?

one dot equals two

String alpha = new String("APCS"); String beta = new String("APCS"); String delta = alpha; System.out.println(alpha.equals(beta)); System.out.println(alpha == beta); System.out.println(alpha == delta); What is printed as a result of executing the code segment?

true false true

Consider the following declarations. int valueOne, valueTwo; Assume that valueOne and valueTwo have been initialized. Which of the following evaluates to true if valueOne and valueTwo contain the same value?

valueOne == valueTwo

Which of the following always evaluates to the same value as the expression above? ( x || y ) && x

x

int x = 3; int y = -1; if (x - 2 > y) { x -= y; } if (y + 3 >= x) { y += x; } System.out.print("x = " + x + " y = " + y); What is printed as a result of the execution of the code segment?

x = 4 y = -1


Related study sets

Business Foundations: Chapter 10

View Set

4.12.1 Python Control Structures Quiz

View Set