Unit 3 Test AP Comp Sci

Ace your homework & exams now with Quizwiz!

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

if (a < b || c != d) { System.out.println("dog"); } else { System.out.println("cat"); }

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

!( !( a && b ) || ( c || !d )) Which of the following are equivalent to the expression?

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

Consider the following code segments, which are each intended to convert grades from a 100-point scale to a 4.0-point scale and print the result. A grade of 90 or above should yield a 4.0, a grade of 80 to 89 should yield a 3.0, a grade of 70 to 79 should yield a 2.0, and any grade lower than 70 should yield a 0.0. Assume that grade is an int variable that has been properly declared and initialized. 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?

B

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"); }

boolean b2 = (num < -100) || (num > 0 && num < 100);

Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num ?

boolean b2 = false && (17 % 3 == 2);

boolean b1 = true && (17 % 3 == 1); Which of the following assigns the same value to b2 as the value stored in b1 ?

false

boolean x = (5 < 8) == (5 == 8); What is the value of x after the statement has been executed?

75.0

double regularPrice = 100; boolean onClearance = true; boolean hasCoupon = false; double finalPrice = regularPrice; if(onClearance) { finalPrice -= finalPrice * 0.25; } if(hasCoupon) { finalPrice -= 5.0; } System.out.println(finalPrice); What is printed as a result of executing the code segment?

degrees = 95;

if (degrees > 95) { System.out.println("School will be closed due to extreme heat"); } else { System.out.println("School is open"); }

Third

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"); }

I and III only

if (score >= 93)...

I and II only

if (x % 2 == 1) { System.out.println("YES"); } else { System.out.println("NO"); } Assuming that x is initialized to the same positive integer value as the original, which of the following code segments will produce the same output as the original code segment? I. if (x % 2 == 1) { System.out.print("YES"); } if (x % 2 == 0) { System.out.println("NO"); } II. if (x % 2 == 1) { System.out.println("YES"); } else if (x % 2 == 0) { System.out.println("NO"); } else { System.out.println("NONE"); } III. boolean test = x % 2 == 0; if (test) { System.out.println("YES"); } else { System.out.println("NO"); }

true

int a = 10; int b = 5 * 2; System.out.print(a == b);

randomNumber = 0.70;

int event = 0; if (randomNumber <= 0.70) { event = 1; } if (randomNumber <= 0.80) { event = 2; } else { event = 3; } which will show randomNumber does not work as intended?

27

int x = 7; if (x < 7) { x = 2 * x; } if (x % 3 == 1) { x = x + 2; } System.out.print(3 * x); What is printed as a result of executing the code segment?

value is: 2

int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); Else System.out.println("Value is: " + x / y);

a= true, b = false, x = 7

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

isLeapYear(1900)

public boolean isLeapYear(int val) { if ((val % 4) == 0) { return true; } else { return (val % 400) == 0; } }

6

public int pick(boolean test, int x, int y) { if (test) return x; else return y; } What value is returned by the following method call? pick(false, pick(true, 0, 1), pick(true, 6, 7))

I and III only

suburban zip code

Only when B is true

(a && (b || !a)) == a && b Which of the following best describes the conditions under which the expression will evaluate to true?

x = 10 and y = 30

(x <= 10) == (y > 25) Which of the following values for x and y will result in the expression evaluating to true ?

(y > 10000 || x > 1000) && (y > 10000 || x < 1500)

Assume that x and y have been declared and initialized with int values, consider the following Java expression. (y > 10000) || (x > 1000 && x < 1500)

II only

Boxes

choice > 10

Code Segment A if (choice > 10) { System.out.println("blue"); } else if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Code Segment B if (choice > 10) { System.out.println("blue"); } if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } What describes initial conditions of variable choice? that will cause two code segments to product diff output?

Expression I and expression II evaluate to the same value only when A and B differ.

I. A && B II. !A && !B

true false true

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

YY

What is printed as a result of the call message (5,15,5)?

II only

class Person

6

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

x = 4 y = -1

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


Related study sets

Leadership & Organization 7 Habit Midterm

View Set

Ch 1 Nurse's Role In Health Assessment: Collecting & Analyzing Data

View Set

Exam 3 Review Microeconomics, Ch 9 Microeconomics, Ch 8 Microeconomics, Chapter 7 Microeconomics

View Set

Personality Theory Quiz 2 Ch. 3+4

View Set

Chapter 33: Disorders of Renal Function

View Set

Los Ojos de Carmen English Translation Original RE-EDITED

View Set

Chapter 6: Volcanoes and Volcanic Hazards

View Set