Chapter 3: Branches (Conditionals) and Related Logic, String Operations

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

If the input sets int x with 5 and int y with 7, what is the ending value of z? z is declared as a boolean. z = (x > y); True False Error: No value assigned to z due to a runtime error Error: No value assigned to z due to a syntax error

False

For what values of integer x will Branch 3 execute? If x < 10 : Branch 1Else If x > 9: Branch 2Else: Branch 3 Value 10 or larger Value 10 only Values between 9 and 10 For no values (never executes)

For no values (never executes)

For what values of x will "Medium" be output? If x > 40: Output "Large"Else If x > 20: Output "Medium"Else If x > 10: Output "Small" Any x larger than 20 Any x smaller than 40 Any x from 21 to 40 Any x from 10 to 40

Any x from 21 to 40

To quit, a user types 'q'. To continue, a user types any other key. Which expression evaluates to true if a user should continue? key == 'q' !(key == 'q') (!key) == 'q' key == (!'q')

!(key == 'q')

What is the ending value of userString? (Note the question asks about userString, not about x). userString = "FaceBook"; x = Character.toLowerCase(userString.charAt(4)); "Facebook" "FaceBook" "Faceook" "b"

"FaceBook"

How is the expression evaluated? ! x - 3 > 0 ((!x) - 3) > 0 (!(x - 3) > 0 (!x) - (3 > 0) !((x - 3) > 0)

((!x) - 3) > 0

Which expression for XXX outputs "Modern era" for any year 1980 and later? if (year < 2000) { // Output "Past" } else if (XXX) { // Output "Modern era" } year > 1980 year >= 1980 year >= 2000 (No such expression exists)

(No such expression exists)

A restaurant gives a discount for children under 10. They also give the discount for adults over 55. Which expression evaluates to true if a discount should be given? (age < 10) && (age > 55) (age < 10) || (age > 55) (age >= 10) && (age <= 55) (age >= 10) || (age <= 55)

(age < 10) || (age > 55)

Both must be true for a person to ride: (1) At least 5 years old, (2) Taller than 36 inches. Which expression evaluates to true if a person can ride? (age >= 5) && (height > 36) (age >= 5) || (height > 36) (age > 5) && (height <= 36) (age > 5) || (height <= 36)

(age >= 5) && (height > 36)

Which expression correctly evaluates 13 < num < 30? (num < 13) && (num < 30) (num < 13) || (num < 30) (num > 13) && (num < 30) (num > 13) || (num < 30)

(num > 13) && (num < 30)

Which expression evaluates to false if x is 0 and y is 10? (x == 0) && (y == 10) (x == 0) && (y == 20) (x == 0) || (y == 10) (x == 0) || (y == 20)

(x == 0) && (y == 20)

Which expression follows the practice of using parentheses to make the intended order of evaluation explicit? x > y && !a == b (x > y) && (!a == b) (x > (y)) && ((!a == b)) ((x > y) && !a == (b)) ((x > y)) && ((!a == b))

(x > y) && (!a == b)

Which expression for YYY correctly outputs that x is between 50-100? if (YYY) { // Output "50, 51, ..., 99, 100" } (x >= 50) || (x <= 100) 50 >= x <= 100 50 <= x <= 100 (x >= 50) && (x <= 100)

(x >= 50) && (x <= 100)

If the input is 5, what is the output? int x; int z = 0; x = scnr.nextInt(); if (x > 9) z = 3; z = z + 1; System.out.print(z); 0 1 3 4

1

If the input is 12, what is the final value for numItems? int x; int numItems = 0; x = scnr.nextInt(); if (x <= 12) { numItems = 100; } else { numItems = 200; } numItems = numItems + 1; 100 101 200 201

101

What values for x cause Branch 1 to execute? If x > 100 : Branch 1Else If x > 200: Branch 2 100 or larger 101 or larger 100 to 200 101 to 200

101 or larger

What is the final value of y? int x = 6; int y = 2; if (x < 10) { if (y < 5) { y = y + 1; } else { y = 7; } } else { y = y + 10; } 3 7 12 13

3

What is the ending value of numItems if myChar = 'X'? switch (myChar) { case 'W': numItems = 1; case 'X': numItems = 2; case 'Y': numItems = 3; case 'Z': numItems = 4; } 2 3 4 9

4

What is the ending value of x? int x; userText = "mississippi"; x = userText.indexOf("i", 3); 1 4 7 10

4

Which value of x results in short circuit evaluation, causing y < 4 to not be evaluated? (x >= 7) && (y < 4) 6 7 8 No such value

6

Which value of y results in short circuit evaluation, causing z == 99 to not be evaluated? (y > 50) || (z == 99) 40 50 60 No such value

60

What is the final value of y? int x = 77; int y = 4; if (x == 77) { y = y + 1; } if (x < 100) { y = y + 1; } if (x > 77) { y = y + 1; } y = y + 1; 5 6 7 8

7

Given str = "Cat", what is the result of this statement? str.charAt(1) = "ab"; str is "abt" str is "Catab" str is "Cab" Error: Assigning a character with a string is not allowed

Error: Assigning a character with a string is not allowed

What is y's ending value? int x; int y = 0; x = scnr.nextInt(); if (x = 20) { y = 3; } Always 0 no matter what the input Always 20 no matter what the input 3 when the input is 20, else 0 Error: Compiler will not allow the expression x = 20

Error: Compiler will not allow the expression x = 20

Given strings str1 = "Hello" and str2 = "Goodbye ", what is the ending value of string str3? str3 = str1 + str2; Hello Goodbye HelloGoodbye Goodbye Hello Error: Cannot add variables of type string

HelloGoodbye

If the input is 5, what is the output? int x; x = scnr.nextInt(); if (x < 10) { System.out.print("Live "); } else if (x < 20) {System.out.print("long "); } else if (x < 30) { System.out.print("and "); } System.out.print("prosper!"); Live Live long Live prosper! Error: The compiler will complain about a missing else statement

Live prosper!

If the input is -5, what is the output? If x less than 0 Put "Low " to output Else if x greater than 0 Put "OK " to output Else if x less than -3 Put "Very low " to output Low Low Very low (Runs, but no output) Error: The compiler will complain about a missing else statement

Low

Which expression best determines if double variable x is 74.7? Math.abs(x - 74.7) != 0.0001 Math.abs(x - 74.7) > 0.0001 Math.abs(x - 74.7) < 0.0001 Math.abs(x - 74.7) == 0.0001

Math.abs(x - 74.7) < 0.0001

What is the output for x = 15?switch (x) { case 10: // Output: "First " break; case 20: // Output: "Second " break; default: // Output: "No match " break; } First Second First Second No match

No match

For what values of x does the default case execute in the code below? x is declared as an integer. switch (x) { case 2: ... break; case 3: ... break; case 4: ... break; default: ... // When does this execute? } Only for value 5 Only for all values greater than 4 Only for values that are not 2, 3, or 4 For any value

Only for values that are not 2, 3, or 4

What does this code do? If x less than y Put x to output Else Put y to output Outputs the lesser of the two integers; if they are equal, outputs that integer Outputs the lesser of the two integers; if they are equal, outputs nothing Outputs the greater of the two integers; if they are equal, outputs that integer Outputs the greater of the two integers; if they are equal, outputs nothing

Outputs the lesser of the two integers; if they are equal, outputs that integer

What value of x outputs "Junior"? if (x < 56) { // Output "Sophomore" } else if (x > 56) { // Output "Senior" } else { // Output "Junior" } Value 56 Values 57 or larger Values 55 or 57 No such value

Value 56

If the input is 7, what is the output? If x less than 10 Put "Tiny " to output Else Put "Not tiny " to output If x less than 100 Put "Small " to output Tiny Tiny Small Not tiny Small (No output)

Tiny Small

Which expressions for YYY and ZZZ will output "Young" for ages less than 20 and "Young but not too young" for ages between 10 and 20? int u; u = scnr.nextInt(); if (YYY) { System.out.print("Young"); if (ZZZ) { System.out.println(" but not too young"); } } YYY: u < 20 ZZZ: u < 10 YYY: u < 20 ZZZ: u > 10 YYY: u > 20 ZZZ: u < 10 YYY: u > 20 ZZZ: u > 10

YYY: u < 20 ZZZ: u > 10

Which expressions for XXX and YYY correctly outputs the text for pre-teen and teenager? Choices are in the form XXX / YYY. if (XXX) { // Output "Pre-teen" } else if (YYY) { // Output "Teenager" } age <= 13 / age > 13 age < 13 / age < 19 age <13 / age <= 19 age >= 13 / age <= 19

age <13 / age <= 19

Given string str1 and string str2, which expression is true? str1 = "Hi";str2 = "Hello"; str1.compareTo(str2) < 0 str1.compareTo(str2) > 0 str1.equals(str2) (Comparison not possible)

str1.compareTo(str2) > 0

Given x = 1, y = 2, and z = 3, how is the expression evaluated? In the choices, items in parentheses are evaluated first. (x == 5) || (y == 2) && (z == 5) false OR (true AND false) --> false OR false --> false false OR (true AND false) --> false OR true --> true (false OR true) AND false --> true AND false --> false (false OR true) AND false --> true AND false --> true

false OR (true AND false) --> false OR false --> false

Which expression for XXX causes the code to output the strings in alphabetical order? (Assume the strings are lowercase) if (XXX) { System.out.println(firstStr + " " + secondStr); } else { System.out.println(secondStr + " " + firstStr); } firstStr.equals(secondStr) firstStr.compareTo(secondStr) < 0 firstStr.compareTo(secondStr) > 0 !firstStr.equals(secondStr)

firstStr.compareTo(secondStr) < 0

Which if branch executes when an account lacks funds and has not been used recently? hasFunds and recentlyUsed are booleans and have their intuitive meanings. if (!hasFunds && !recentlyUsed) if (!hasFunds && recentlyUsed) if (hasFunds && !recentlyUsed) if (hasFunds && recentlyUsed)

if (!hasFunds && !recentlyUsed)

What is the relation between a string's length and the string's last index? lastIndex == length lastIndex == length - 1 lastIndex == length + 1l astIndex == length + 2

lastIndex == length - 1

For the string "Orange", what character is at index 3? r a n g

n

Which expressions for XXX, YYY, and ZZZ output "Great job" for scores above 90, and "Nice try" otherwise? Choices are in the form XXX / YYY / ZZZ. int score; score = scnr.nextInt(); if (XXX) { System.out.println(YYY); } else { System.out.println(ZZZ); } score < 91 / "Great job" / "Nice try" score < 91 / "Nice try" / "Great job" score > 90 / "Nice try" / "Great job" (Not possible for given code)

score < 91 / "Nice try" / "Great job"

For the string "on time", what character is at index 3? (Space) t i m

t

Which expression for YYY outputs "Quarter century!" when the input is 25? int x; x = scnr.nextInt(); if (YYY) { System.out.println("Nothing special"); } else { System.out.println("Quarter century!"); } x == 25 x != 25 x <> 25 x == !25

x != 25

Which expressions for YYY and ZZZ correctly output the indicated ranges? Assume int x's value will be 0 or greater. Choices are in the form YYY / ZZZ. if (YYY) { // Output "0-29" } else if (ZZZ) { // Output "30-39" } else { // Output "40+" } x < 29 / x >= 29 x < 30 / x >= 30 x < 30 / x < 40 x > 29 / x > 40

x < 30 / x < 40

Given string s is "Sunday", what is the ending value of char myChar? myChar = s.charAt(s.length() - 1); a y " No such character

y


Ensembles d'études connexes

Med Surg Test 2 GI (Chapters 42, 43)

View Set

IST 430 Knowledge Management Chapters 5-9

View Set

BA 101; (bonus chapter A) Working in a Legal Environment.

View Set

Ch. 33 accounts payable and accounting procedures

View Set

Psychology 1300: Chapter 4 (consciousness)

View Set

Essence of the Old Testament Chap 18-20 Rebuilding the Temple Ezra, Nehemiah, and Esther

View Set