Chapter 3: Using Operators and Decision Constructors

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

30: What is the output of the following code snippet? int x = 10, y = 5; boolean w = true, z = false; x = w ? y++ : y--; w = !z; System.out.print((x+y)+" "+(w ? 5 : 10)); A: The code does not compile B: 10 10 C: 11 5 D: 12 5

C: 11 5 Read carefully. x = w ? y++ : y--; 5 = true ? 5++ : 5--; Note: x= 5, y = 6 w = !z; w = true ((x+y)+" "+(w ? 5 : 10)); ((5 + 6)+" "+(true ? 5 : 10)) 11 5

32: What is the value of 12 + 6 * 3 % (1 + 1) in java ? A: 0 B: 12 C: 14 D: None of the above.

B: 12

49: Fill in the blanks. The operators -, ______________________, ________________________, ______________________, and % are listed in the same or increasing level of operator precedence. A: +, /, * B: --, -, * C: ++, /, * D: *, ++, %

A: +, /, *

23: What is the output of the following application? package transporter; public class Rematerialize { public static void main(String[] input) { int dog = 11; int cat = 3; int partA = dog/cat; int partB = dog % cat; int newDog = partB + partA * cat; System.out.print(newDog); } } A: 9 B: 11 C: 15 D: The code does not compile.

B: 11

17: What is the output of the following application? package Jungle; public class TheBigRace { public static void main(String[] in) { int tiger = 2; short lion = 3; long winner = lion+2*(tiger + lion); System.out.print(winner); } } A: 11 B: 13 C: 25 D: None of the above.

B: 13

24: What is the output of the following application? package dessert; public class IceCream { public final static void main(String... args) { int flavors = 30; int eaten = 0; switch(flavors) { case 30: eaten++; case 40: eaten+=2; default: eaten--; } System.out.print(eaten); } } A: 1 B: 2 C: 3 D: The code does not compile.

B: 2 No break statement as soon as the first case statement matches, then each statement will execute. 1 > 1+2 > -1 = 2

06: What is the value of thatNumber after the execution of the following code snippet? long thatNumber = 5 >= 5 ? 1+2 : 1*1; if(++thatNumber < 4) thatNumber += 1; A: 3 B: 4 C: 5 D: The answer cannot be determined until runtime.

B: 4 The prefix effects thatNumber outside of the conditional statement

13: Which statement about case statements of a switch statement is not true? A: A case value can be final. B: A case statement must be terminated with a break statement. C: A case value can be a literal expression. D: A case value must match the data type of the switch variable, or able to be promoted.

B: A case statement must be terminated with a break statement. Memorize this: A case statement must be a constant expression, such as a literal or final variable.

29: Which statement about the logical operators & and && is true? A: The & and && operators are interchangeable, always producing the same results at runtime. B: The & operator always evaluates both operands, while the && operator may only evaluate the left operand. C: Both expressions evaluate to true if either operand is true. D: The & operator always evaluates both operands, while the && operator may only evaluate the right operand.

B: The & operator always evaluates both operands, while the && operator may only evaluate the left operand.

40: What is the output of the following application? package transporter; public class TurtleVsHare { public static void main(String[] arguments) { int turtle = 10 * (2 + (3 + 2) / 5); int hare = turtle < 5 ? 10 : 25; System.out.print(turtle < hare ? "Hare wins!" : "Turtle wins" ); } } A: Hare wins! B: Turtle wins! C: The code does not compile. D: The code compiles but throws a division by zero error at runtime.

B: Turtle wins!

07: Which statement immediately exits a switch statement, skipping all remaining case or default branches? A: exit B: break C: goto D: continue

B: break

01: Which of the following variable types is not permitted in a switch statement ? A: String B: double C: int D: char

B: double

27: For a given non-null String myTestVariable, what is the resulting value of executing the statement meTestVariable.equals(null)? A: true B: false C: The statement does not compile. D: The statement compiles but will produce an exception when used at runtime.

B: false

18: Given the following code snippet, assuming dayOfWeek is an int, what variable type of saturday is not permitted ? final _____ saturday = 6; switch(dayOfWeek) { default: System.out.print("Another Weekday"); break; case saturday: System.out.print("Weekend!"); } A: byte B: long C: int D: None of the above.

B: long Any value that can be implicitly promoted to int will work for the case statement with an input. Since switch statements do not support long values, and long values cannot be converted to int without a possible loss of data, B IS CORRECT HERE. Memorize which variable types are not accepted in switch statements.

16: Fill in the blanks: The __________________ operator increases the value of a variable by 1 and returns the new value, while the ________________ operator decreases the value of a variable by 1 and returns the original value. A: pre-increment [++v], pre-decrement[--v] B: pre-increment [++v], post-decrement[v--] C: post-increment [v++], pre-decrement[--v] D: post-increment [++v], post-decrement[v--]

B: pre-increment [++v], post-decrement[v--]

35: Fill in the blanks. The operators +, ______________________, ________________________, ______________________, and ++ are listed in the same or increasing level of operator precedence. A: *, --, / B: %, -, * C: /, *, % D: *, -, /

C: /, *, % https://quizlet.com/510084085/order-of-operator-precedence-flash-cards/

36: What statement about the ^ operator is correct ? A: If one of the operands of ^ is true, then the result is always true. B: There is a conditional form of the operator, denoted as ^^. C: If both operands of ^ are true, the result is true. D: The ^ operator can only be applied to boolean values.

C: If both operands of ^ are true, the result is true. (XOR) here only true if one is true.

34: Which of the following is "NOT" a possible result of executing the following application? public class ConditionallyLogical { public static void main(String... data) { if(data.length>=1 && (data[0].equals("sound") || data[0].equals ("logic")) && data.length<2) { System.out.print(data.length[0]); } } } A: Nothing is printed B: sound is printed C: The application throws an exception at runtime. D: logic is printed.

C: The application throws an exception at runtime. Left hand side of short circuit operator is evaluated first. Therefor exception is the only thing that is unexpected.

20: What is the output of the following application? package recreation; public class Dancing { public static void main(String[] vars) { int leaders = 10 * (2 + (1 + 2 / 5); int followers = leaders * 2; System.out.print(leaders + followers < 10 ? "Too few" : "Too many"); } } A: Too few B: Too many C: The code does not compile D: The code compiles but throws a division by zero error at compile.

C: The code does not compile Notice the uneven number of parenthesis

50: What is the output of the following application? public class Baby { public static String play(int toy, int age){ final String game; if(toy<2) game = age > 1 ? 1 : 10; //p1 else game = age > 3 ? "Ball" : "Swim"; //p2 return game; } public static void main(String[] variables) { System.out.print(play(5,2)); } } A: Ball B: Swim C: The code does not compile due to p1. D: The code does not compile due to p2.

C: The code does not compile due to p1.

25: What is the output of the following application? package mode; public class Transportation { public static String travel(int distance) { return distance<1000 ? "train" : 10; } public final static void main(String... answer) { System.out.print(travel(500)); } } A: train B: 10 C: The code does not compile. D: The code compiles but throws an exception at runtime.

C: The code does not compile. Return type on method is String. Ternary option for false will cause an error. Compatible data types.

15: What is the output of the following code snippet? int hops = 0; int jumps = 0; jumps = hops++; if(jumps) System.out.print("Jump!") else System.out.print("Hop!") A: Jump! B: Hop! C: The code does not compile. D: The code compiles but throws an Exception at runtime.

C: The code does not compile. Memorize this: Java does not automatically convert integer values to boolean values for conditional statements.

09: What is the output of the following application? 1: package voting; public class Election { 2: public void calculateResult(Integer candidateA, Integer candidateB) { 3: boolean process = candidateA == null || candidateA.intValue() < 10; 4: boolean value = candidateA && candidateB; 5: System.out.print(process || value); 6: } 7: public static void main(String[] unused) { 8: new Election().calculateResult(null,203); 9: } 10: } A: true B: false C: The code does not compile. D: The code compiles but throws a NullPointerException on line 3 at runtime.

C: The code does not compile. Line 4. candidate A and candidate B are both Integer and the operator short circuit operator && can only be applied to Boolean expressions.

08: Which statement about ternary expressions is true? A: In some cases, both expressions to the right of the conditional operator in a ternary expression will be evaluated at runtime. B: Ternary expression require parenthesis for proper evaluation. C: The ternary expressions are a convenient replacement for an if-then-else statement. D: Ternary expressions support int and boolean expressions for the left-most operand.

C: The ternary expressions are a convenient replacement for an if-then-else statement.

05: Which of the following statements about a default branch in a switch statement is correct ? A: All switch statements must include a default statement. B: The default statement is required to be placed after all case statements. C: Unlike a case statement, the default statement does not take a value. D: A default statement can only be used when at least one case statement is present.

C: Unlike a case statement, the default statement does not take a value.

03: What is the output of the following application ? package registration; public class NameCheck { public static void main(String... data) { String john = "john"; String jon = new String(john); System.out.print((john==jon)+" "+(john.equals(jon))); } } A: true true B: true false C: false true D: false true

C: false true

11: Which statement about if-then else statements is true? A: An if-then statement is required to have an else statement. B: If the boolean test of an if-then statement evaluates to false, then the target clause of the if-then statement will still be evaluated. C: An if-then statement is requires to cast an object. D: An if-then statement can execute a single statement or a block.

D: An if-then statement can execute a single statement or a block.

12: What is the output of the following application? package restaurant; public class Pieces { public static void main(String[] info) { int flair = 15; if(flair >= 15 && flair < 37) { System.out.print("Not enough"); } if(flair==37) { System.out.print("Just right") } else { System.out.print("Too many"); } } } A: Not Enough B: Just Right C: Too many D: None of the above

D: None of the above Since it would be be both A and C. Both if statements execute.

19: Given the following code snippet, what is the value of dinner after it is executed? int time = 11; int day = 4; String dinner = time > 10 ? day ? "Takeout" : "Salad" : "Leftovers"; A: Takeout B: Salad C: The code does not compile but would compile if parenthesis were added. D: None of the above.

D: None of the above.

47: What is the value of (5 + (!2 + 8) * 3 - 3 % 2)/2 in Java? A: 2 B: 11 C: 16 D: None of the above.

D: None of the above.

04: What is the output of the following application? package planning; public class ThePlan { public static void main(String[] args) { int plan = 1; plan = plan++ + --plan if (plan==1) { System.out.print("Plan A"); } else { if(plan==2) System.out.print("Plan B"); } else System.out.print("Plan C"); } } A: Plan A B: Plan B C: Plan C D: None of the above.

D: None of the above. Second if statement is not part of anything.

38: What variable type of red allows the following application to compile ? package tornado; public class Kansas { public static void main(String[] arg) { int colorOfRainbow = 10; _________ red = 5; switch(colorRainbow) { default: System.out.print("Home"); break; case red: System.out.print("Home"); } } A: long B: double C: int D: None of the above.

D: None of the above. The variable must be final. The value of case statement must be a constant, literal value, or a final variable.

43: Fill in the blanks: The __________ operator is true if either of the operands are true, while the ___________ operator flips a boolean value. A: +,- B: &&, ! C: |, - D: ||, !

D: ||, !

44: Given the following code snippet, what is the value of movieRating after it is executed? int character = 5; int story = 3; double movieRating = character <= 4 ? 3 : story>1 ? 2 : 1; A: 2.0 B: 3.0 C: The code does not compile but would compile if parenthesis were added. D: None of the above.

A: 2.0

46: Which of the following results is "NOT" a possible result of executing the following application? public class OutsideLogic { public static void main(String... weather) { System.out.print(weather[0]!=null && weather[0].equals("sunny")&& !false ? "Go Outside" : "Stay Inside"); } } A: Nothing is printed. B: The application throws an exception at runtime. C: Go outside is printed. D: Stay Inside is printed.

A: Nothing is printed.

31. What is the output of the following application? package bob; public class AreYouBob { public static void main(String[] unused) { String bob = new String("bob"); String notBob = bob; System.out.print((bob == notBob)+" "+(bob.equals(notBob))); } } A: true true B: true false C: false true D: false false

A: true true

02: What is the value of tip after executing the following code snippet? int meal = 5; int tip = 2; int total = meal + (meal>6 ? ++tip : --tip); A:1 B:2 C:3 D:6

A:1

39: Which two operators would be used to test if a number is equal to or greater than 5.21 but strictly less then 8.1? A: > and <= B: >= and > C: < and >= D: < and >

C: < and >=

48: Diagram here. A: B: C: D:

Diagram here.

26: Fill in the blanks: Given two non-null String objects with reference names apples and oranges, if apples __________ oranges evaluates to true, then apples ___________ oranges must also evaluate to true. A: ==, equals() B: !=, equals() C: equals(), == D: equals(), !=

A: ==, equals()

42: What is the output of the following application? package yoyo ; public class TestGame { public String runTest(boolean spinner, boolean roller) { if(spinner=roller) return "up"; else return ? "down" : "middle"; } public static final void main(String pieces[]) { final TestGame tester = new TestGame(); System.out.print(tester.runTest(false,true) ); } } A: up B: middle C: down D: This code does not compile.

A: up

22: Fill in the blanks: The ____________ operator is used to find the difference between two members, while the __________ operator is used to find the remainder when one number is divided be another ? A: /, % B: -,% C: %, < D: -, ||

B: -,%

21: What is the output of the following application? package schedule; public class PrintWeek { public static final void main(String[] days) { System.out.print(5 + 6 + "7" + 8 + 9); } } A: 56789 B: 11789 C: 11717 D: The code does not compile.

B: 11789

10:What is the output of the following application? package dinosaur; public class Park { public static void main(String... arguments) { int pterodactyl = 6; long triceratops = 3 ; if(pterodactyl % 3 >= 1) triceratops++; triceratops--; System.out.print(triceratops); } A: 2 B: 3 C: 4 D: The code does not compile.

A: 2

41: What is the output of the following application? public class CountEntries { public static int getResult(int threshold) { return threshold > 5 ? 1 : 0; } public static void main(String[] days) { System.out.print(getResult(5)+getResult(1)+getResult(0)+getResult(2)); } } A:0 B:1 C:0000 D:1111

A:0

45: Fill in the blanks: A switch statement can have __________ case statements and ___________ default statements. A: at most one, at least one B: any number of, at most one C: at least one, any number of D: at least one, at most one

B: any number of, at most one

28: How many 1s are outputted when the following application is compiled and run? package city; public class Road { public static void main(String... in) { int intersections = 100; int streets = 200; if(intersections < 150) { System.out.print("1"); } else if (streets && intersections > 1000) { System.out.print("2"); } if(intersections < 500) { System.out.print("1"); else System.out.print("2"); } } A: None B: One C: Two D: The code does not compile.

D: The code does not compile. streets && intersections is not possible, streets is not a boolean expression and cannot be used as the left hand side of the logical operator.

14: Diagram Here A: B: C: D:

Diagram Here

33: Diagram Here A: B: C: D:

Diagram Here

37: Diagram here. A: B: C: D:

Diagram here.


संबंधित स्टडी सेट्स

MSI Exam #5: Ch. 47 (related to diabetes) & 48

View Set

Culinary Essentials Chapter 15: Cooking Techniques

View Set

Chapter 13: A House Divided, 1840-1861

View Set

Chapter 18: Alterations of Hormonal regulation

View Set

advanced physical assessment - Pediatric

View Set

Chapter 7: LTM, Encoding, Retrieval, and Consolidation

View Set

ch. 8 pre reading and Ch. 5&8 quiz

View Set