Unit 3: Boolean Expression and if Statements Quizzes
(3.5) Which of the below is a logical operator? 1. ! 2. ## 3. ** 4. %
1. !
(3.6) Which of the following logical statements is equivalent to !(A || B) Where A and B are boolean values 1. !A && !B 2. !A || !B 3. !A || B 4. A && B
1. !A && !B
(3.3) Consider the following code snippet if (x == y) { // Statement A } else { // Statement B } Which statement will be executed if x = 3 and y = 3? 1. Statement A 2. Statement B 3. Statement A and Statement B 4. Impossible to tell
1. Statement A
(3.3) Choose the answer that will always produce the same output as the if-else statement below. Assume that grams is an int variable that has been declared and initialized earlier in the program. if (grams < 130) { System.out.println("Your hamster is a healthy weight"); } else { System.out.println("Your hamster is overweight"); } 1. if (grams < 130) { System.out.println("Your hamster is a healthy weight"); } if (grams >= 130) { System.out.println("Your hamster is overweight"); } 2. if (grams == 130) { System.out.println("Your hamster is a healthy weight"); } if (grams != 130) { System.out.println("Your hamster is overweight"); } 3. if (grams < 130) { System.out.println("Your hamster is a healthy weight"); } System.out.println("Your hamster is overweight");
1. if (grams < 130) { System.out.println("Your hamster is a healthy weight"); } if (grams >= 130) { System.out.println("Your hamster is overweight"); }
(3.1) Which of the following is NOT a relational operator? 1. < 2. ? 3. == 4. >=
2. ?
(3.3) Consider the following code snippet if (x == y) { // Statement A } else { // Statement B } Which statement will be executed if x = 15 and y = 20? 1. Statement A 2. Statement B 3. Statement A and Statement B 4. Impossible to tell
2. Statement B
(3.2) Which if statement is written properly? 1. if (boolean expression): // code to execute // code to execute 2. if boolean expression { // code to execute // code to execute } 3. if(boolean expression) { // code to execute // code to execute } 4. if boolean expression [ // code to execute // code to execute ]
3. if(boolean expression) { // code to execute // code to execute }
(3.1) Which of the following symbols is the "not equal" symbol? 1. == 2. not = 3. != 4. <>
3. !=
(3.6) Which of the following logical statements is equivalent to !(A && B) Where A and B are boolean values 1. !A && B 2. !A && !B 3. !A || !B 4. !A || B
3. !A || !B
(3.7) Which of the following Boolean expressions will yield true after the code snippet executes? String str1 = new String("Karel"); String str2 = "CodeHS"; String str3 = "Karel"; str2 = str1; 1. str1 == str2 && str1 == str3 2. str1 != str2 && str1.equals(str3) 3. str1 == str2 && str1.equals(str3) 4. None of the above will evaluate to true after this code snippet executes.
3. str1 == str2 && str1.equals(str3)
(3.4) Which of the following statements is true? 1. There can only be one else-if statement in an if-else if statement. 2. An if- else if statement must always have an else clause. 3. An if- else if statement cannot have an else clause. 4. An if- else if statement must always start with an if statement.
4. An if- else if statement must always start with an if statement.
(3.1) What is the difference between == and =?
= is used for assignment, while == is used to check for equality.
(3.5) What value of x would make this boolean expression evaluate to false? (x < 10) && (x != 5)
Any value greater than or equal to 10
(3.4) The following code snippet should print the letter grade associated with the integer value in grade.However, the if - else if statements are in the wrong order. (A) else if (grade < 80) { System.out.println("C"); } (B) else { System.out.println("A"); } (C) if(grade < 70) { System.out.println("D"); } (D) else if (grade < 90) { System.out.println("B"); } What order should these statements be in such that the letter grade is A if the grade is above 90, B if the grade is between 80 and 90, C if the grade is between 70 and 80, and D if the grade is less than 70?
C, A, D, B
(3.6) What boolean value is held in the following boolean variable if hasHeadlight is true and hasBikelight is false? boolean cannotNightBike = !(hasHeadlight || hasBikelight);
False
(3.2) What is the output of this program? int numTreeRings = 50; System.out.println("How old is the tree?"); if(numTreeRings > 10) { System.out.println("Still young!"); } if(numTreeRings > 40) { System.out.println("Pretty old!"); } if(numTreeRings > 150) { System.out.println("Very old!"); }
How old is the tree? Still young! Pretty old!
(3.2) What is the output of this program? int numTreeRings = 50; System.out.println("How old is the tree?"); if(numTreeRings < 20) { System.out.println("Still young!"); } if(numTreeRings < 50) { System.out.println("Pretty old!"); } if(numTreeRings < 100) { System.out.println("Very old!"); }
How old is the tree? Very old!
(3.2) What is the output of this program? int phLevel = 9; if(phLevel < 7) { System.out.println("It is acidic!"); } if(phLevel > 7) { System.out.println("It is basic!"); } if(phLevel == 7) { System.out.println("It is neutral!"); }
It is basic!
(3.5) In the following line of code: boolean shortCircuit = true || (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated?
No
(3.4) What is the output of the following code snippet? char combo = 'B'; if(combo == 'A') { System.out.println("Tamale it is!"); } else if (combo == 'B') { System.out.println("Quesadilla it is!"); } else { System.out.println("That is not a combo of ours"); }
Quesadilla it is!
(3.7) What is the proper way to compare String values in Java?
The .equals() String method
(3.3) What is the output of this code? String firstName = "Karel"; String lastName = "The Dog"; if (firstName.length() > lastName.length()) { System.out.println(firstName + " is longer than " + lastName); } else { System.out.println(lastName + " is longer than " + firstName); }
The Dog is longer than Karel
(3.2) Why do we use if statements in Java?
To do something only if a condition is true
(3.6) What boolean value is held in the following boolean variable if hasBike is true and hasHelmet is false? boolean cannotBike = !(hasBike && hasHelmet);
True
(3.7) Logical equality compares the data of the objects.
True
(3.7) Reference equality uses the equality operator (==) and compares the references (addresses in memory) of two objects.
True
(3.4) What is the output of the following code snippet? double ticketPrice = 12.75; if(ticketPrice > 15) { System.out.println("Too expensive!"); } else if(ticketPrice > 10) { System.out.println("Typical"); } else if(ticketPrice > 5) { System.out.println("Great deal!"); } else { System.out.println("Must be a scam"); }
Typical
(3.5) In the following line of code: boolean shortCircuit = true && (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated?
Yes
(3.1) Consider the code snippet below. String name = "Karel"; String checkName = new String("Karel"); boolean nameMatches = name == checkName; Note that we forced Java to create a new String object by using new and calling the String constructor.Recall that if you set two String variables to the same String literal, Java tries to be efficient and uses the same object. With this in mind, what will the value of nameMatches be?
false
(3.7) Given the following Circle class public class Circle { private int radius; public Circle(int theRadius) { radius = theRadius; } public void setRadius(int newRadius) { radius = newRadius; } public int getRadius() { return radius; } public boolean equals(Circle other) { return radius == other.getRadius(); } } What is the output of the following code snippet? Circle one = new Circle(10); Circle two = new Circle(10); System.out.println(one == two);
false
(3.1) What does this Java expression evaluate to? 80 >= 80
true
(3.5) What does this Java expression evaluate to? true && !false
true
(3.7) Given the following Circle class public class Circle { private int radius; public Circle(int theRadius) { radius = theRadius; } public void setRadius(int newRadius) { radius = newRadius; } public int getRadius() { return radius; } public boolean equals(Circle other) { return radius == other.getRadius(); } } What is the output of the following code snippet? public void run() { Circle one = new Circle(5); Circle two = new Circle(10); foo(two); System.out.println(one.equals(two)); } public void foo(Circle x) { x.setRadius(5); }
true