Boolean Expressions and if Statements
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 logical statements is equivalent to !(A && B) Where A and B are boolean values?
!A || !B
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?
?
Which of the following statements is true?
An if- else if statement must always start with an if statement.
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"); }
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
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!
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!
In the following line of code: boolean shortCircuit = true || (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated?
No
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!
Consider the following code snippet if ( x == y) { // Statement A } else { // Statementerm-23t B }
Statement A
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 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
Why do we use if statements in Java?
To do something only if a condition is 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 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
In the following line of code:boolean shortCircuit = true && (5 / 0 == 0); Will the 5 / 0 == 0 operation be evaluated?
Yes
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 if statement is written properly?
if(boolean expression) { // code to execute // code to execute }
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 does this Java expression evaluate to? 80 >= 80
true
What does this Java expression evaluate to? true && !false
true