3.8.1 Boolean Expressions and If Statements Quiz

Ace your homework & exams now with Quizwiz!

Assuming a and b are properly initialized boolean values, which expression would be equivalent to the following: !(a && b)

!a || !b

Which expression is true? a. true && !true b. !false || !true c. true && false d. false || false || !true

!false || !true

What will this method call output given yesOrNo(true)? public String yesOrNo(boolean myBoolean) { if(myBoolean == true) { return "Yes"; } else { return "No"; } }

"Yes"

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

-1

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

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

BC

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

C

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

I and III only

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;

II only

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

Long Word Medium Word

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

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

Nothing. The expression will always be true.

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

Second if statement!

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?

calculateRate(40);

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?

containsPet("I can catch fish.");

Assuming weekday and holiday are properly initialized booleans, which expression would be equivalent to the following: !(!weekday || holiday)

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 */ } }

x = 12 y = 12

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;

x = 24 y = 20

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;

x = 99 y = 199

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?

x = true y = true


Related study sets

PrepU Patho Ch. 19 Disorders of Cardiac Function

View Set

APES chapter 4 practice problems

View Set

EAQ quiz #1 - sedative & hypnotics

View Set

MKTG 320 - TEST 2 - DR CANO (ch. 5-9)

View Set

Circles & Volume: Part 1 Unit Test Review

View Set

Survey of Operating Systems Chapter 9 - Linux on the Desktop

View Set

Bible Competition Packet (Genesis Ch 17-19)

View Set

Pharmacology Made Easy 4.0 - Introduction to Pharmacology

View Set