Booleans
Which of the below is a logical operator?
!
Which of the following symbols is the "not equal" symbol?
!=
Which of the following logical statements is equivalent to !(A || B) Where A and B are boolean values
!A && !B
Which of the following statements is true?
An if- else if statement must always start with an if statement.
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!
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!
Why do we use if statements in Java?
To do something only if a condition is true
Logical equality compares the data of the objects.
True
In the following line of code: boolean shortCircuit = true && (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated?
Yes
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
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
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;
str1 == str2 && str1.equals(str3)
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
What does this Java expression evaluate to?
true
What does this Java expression evaluate to? true && !false
true
Reference equality uses the equality operator (==) and compares the references (addresses in memory) of two objects.
True
What boolean value is held in the following boolean variable if hasBike is true and hasHelmet is false? boolean cannotBike = !(hasBike && hasHelmet);
True
What is the difference between == and =?
= is used for assignment, while == is used to check for equality.
Which of the following is NOT a relational operator?
?
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!
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!
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
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"); }
if(grams < 130) { System.out.println("Your hamster is a healthy weight"); } if(grams >= 130) { System.out.println("Your hamster is overweight"); }
What value of x would make this boolean expression evaluate to false? (x < 10) && (x != 5)
Any value greater than or equal to 10
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
What boolean value is held in the following boolean variable if hasHeadlight is true and hasBikelight is false? boolean cannotNightBike = !(hasHeadlight || hasBikelight);
False
In the following line of code: boolean shortCircuit = true || (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated?
No
What is the proper way to compare String values in Java?
The .equals() String method
Consider the following code snippet if ( x == y) { // Statement A } else { // Statement B } "` Which statement will be executed if x = 3 and y = 3?
Statement A
Consider the following code snippet if ( x == y) { // Statement A } else { // Statement B } "` Which statement will be executed if x = 15 and y = 20?
Statement B
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
Which if statement is written properly?
if(boolean expression) { // code to execute // code to execute }
Which of the following logical statements is equivalent to !(A && B) Where A and B are boolean values
!A || !B