CodeHS: Boolean Expressions and if Statements Quiz (3.8)

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What will the following code output when executed? String word = "elementary"; if (word.length() > 8) { System.out.println("Long Word"); } if (word.length() > 5) { System.out.println("Medium Word"); } else if (word.length() > 0) { System.out.println("Short Word"); } else { System.out.println("No Word"); } A. Long Word Medium Word B. Long Word Medium Word Short Word C. Long Word D. Long Word Medium Word Short Word No Word E. No Word

A. Long Word Medium Word

Given the following: if (x > y) { y *= 2; } else if (y > x) { x *= 2; } if (x > y) { y *= 2; } if (y > x) { x *= 2; } What will the final values of x and y be if their initial values are: x = 12; y = 5; A. x = 24 y = 20 B. x = 12 y = 20 C. x = 12 y = 10 D. x = 24 y = 5

A. x = 24 y = 20

A student is trying to determine if the following two expressions are equivalent. A. x && (!x || y) B. x && !(x || !y) What values of x and y would prove that the expressions are NOT equivalent? A. x = true y = true B. x = true y = false C. x = false y = true D. x = false y = false

A. x = true y = true

Which expression is true? A. true && !true B. !false || !true C. true && false D. false || false || !true

B. !false || !true

What will this method call output given yesOrNo(true)? public String yesOrNo(boolean myBoolean) { if(myBoolean == true) { return "Yes"; } else { return "No"; } } A. "No" B. "Yes" C. 0 D. This program will error

B. "Yes"

A company uses the following table to determine pay rate based on hours worked: Hours Worked Rate 1-40 $10 41-50 $15 50+ $20 The following method is intended to represent this table: public int calculateRate(int hours) { if (hours < 40) { return 10; } else if (hours < 50) { return 15; } else { return 20; } } Which of the following test cases can be used to show that the code does NOT work as intended? A. calculateRate(35); B. calculateRate(40); C. calculateRate(45); D. calculateRate(55);

B. calculateRate(40);

Assuming weekday and holiday are properly initialized booleans, which expression would be equivalent to the following: !(!weekday || holiday) A. weekday || !holiday B. weekday && !holiday C. !weekday && !holiday D. !(weekday || !holiday)

B. weekday && !holiday

What values for x and y will cause the program to execute the /* missing code */? if (x > 10) { x -= 5; if (x > 10 || y <= 10) { x ++; y++; } else { /* missing code */ } } A. x = 18 y = 12 B. x = 12 y = 8 C. x = 12 y = 12 D. x = 18 y = 18 E. x = 18 y = 8

C. x = 12 y = 12

What will this code return given checkMethod(false, true)? public int checkMethod(boolean x, boolean y) { if(!x) { if(y) { return -1; } else { return 1; } } else { return 0; } } A. 1 B. 0 C. -1 D. This method call will error

C. -1

What will this program print if the value of grade is 80? if(grade > 90) { System.out.println("A"); } else if(grade > 80) { System.out.println("B"); } else if(grade > 70) { System.out.println("C"); } A. A B. B C. C D. Nothing

C. C

In order to ride the zip line, you need to be at least 12 years old and weigh at least 75 pounds, but no more than 300 pounds. Which of the following code segments would correctly determine if you can ride the zip line? I. if (age >= 12 && 75 <= weight <= 300) { return true; } return false; II. if (age >= 12) { if (weight >= 75 && weight <= 300) { return true; } } return false; III. if (age >= 12 && (weight >= 75 || weight <= 300)) { return true; } return false; A. I and II only B. I only C. II only D. III only E. I, II, III

C. II only

What will this code output? if (true && true && false) { System.out.println("Hello Karel"); } if (true && 4 == 2 + 2) { System.out.println("Second if statement!"); } A. Hello Karel B. Hello Karel Second if statement! C. Second if statement! D. This program will print nothing

C. Second if statement!

The following method is designed to return true if the passed phrase contains either the word cat or dog. public boolean containsPet(String input) { if (input.indexOf("cat") >= 0) { return true; } else if (input.indexOf("dog") >= 0) { return true; } else { return false; } } Which of the following test cases can be used to show the code does NOT work as intended? A. containsPet("I have a dog."); B. containsPet("I don't have pets."); C. containsPet("I can catch fish."); D. containsPet("My dog caught my cat");

C. containsPet("I can catch fish.");

What will the values of x and y be after this code segment runs? int x = 100; int y = 100; if (x <= 100) { if (y > 100) { x = 200; } else { x = 99; } } else { x++; } y = x + y; A. x = 100 y = 200 B. x = 99 y = 100 C. x = 101 y = 201 D. x = 99 y = 199

D. x = 99 y = 199

Assuming a and b are properly initialized boolean values, which expression would be equivalent to the following: !(a && b) A. a || b B. !(a || b) C. !a && !b D. !a || !b

D. !a || !b

Given the following call, what value would be returned for findTheMiddle(2110890125)? public static String findTheMiddle(int number) { String stringNum = "" + number; int mid = stringNum.length()/2; if(stringNum.length() % 2 == 1) { return stringNum.substring(mid,mid+1); } else { return stringNum.substring(mid-1,mid+1); } } A. 8 B. The code will error C. 9 D. 89

D. 89

What will the following code output when executed? String breakfast = new String("Pizza"); String lunch = new String("Pizza"); String dinner = breakfast; if (breakfast==lunch) { System.out.print("A"); } if (breakfast.equals(lunch)) { System.out.print("B"); } if (breakfast==dinner) { System.out.print("C"); } else if (breakfast.equals(dinner)) { System.out.print("D"); } else { System.out.print("E"); } A. ABCD B. ABC C. BCD D. BC E. E

D. BC

The following code is intended to return only even numbers. If an even number is passed to the method, it should return that number. If an odd number or zero is passed, it should return the next highest even number. 1: public int returnEven(int number) 2: { 3: if (number % 2 == 0) 4: { 5: return number; 6: } 7: else if (number == 0) 8: { 9: return number + 2; 10: } 11: else 12: { 13: return number + 1; 14: } 15: } Does the code work as intended? A. Yes. B. No, the mod function on line 3 should read number % 2 == 1 to find even numbers. C. No, the else if on line 7 and the else on line 11 should just be if statements. D. No. Zero will get returned on line 5 and not make it to line 7.

D. No. Zero will get returned on line 5 and not make it to line 7.

Given a, b, and c are properly initialized boolean values, what values would make the following expression false? (a || b) || (b || c) || (!a || b); A. a and b must be different values B. a must be false C. b and c must have the same values D. Nothing. The expression will always be true.

D. Nothing. The expression will always be true.

Given the following statements, which options will print true? String ursa = new String("2849"); String major = new String("2849"); I. System.out.println(ursa.equals(major)); II. System.out.println(ursa == major); III. System.out.println(ursa.equals("2849")); IV. System.out.println(major == "2849"); A. I, II, III, and IV B. IV only C. I, III, and IV only D. II and IV only E. I and III only

E. I and III only


Kaugnay na mga set ng pag-aaral

Chapter 1-Definition of Cognition

View Set

Assignment 11 - The Seven Signs: Signs 4-7 and the Seven Plagues

View Set

Life Insurance Policy Provisions

View Set

nurs 203 midterm prep u questions

View Set

How to chat and high five in quizlet

View Set