What are the only two values a Java boolean can store?
true and false
What prints?
int points = 14;
if (points >= 10)
System.out.println("Bonus");
else
System.out.println("Keep trying");
Bonus
What makes a selection statement nested?
An if or if-else statement appears inside another selection statement’s branch.
Which Java operator means logical AND?
&&
Simplify !!ready
ready
If int tickets = 12;, what is the value of tickets >= 12?
true
int age = 15;
if (age < 13)
System.out.println("Child");
else if (age < 18)
System.out.println("Teen");
else
System.out.println("Adult");
Teen
What prints?
boolean member = true;
int visits = 6;
if (member) {
if (visits >= 5)
System.out.println("Free pass");
else
System.out.println("Member price");
} else {
System.out.println("Regular price");
}
Free pass
Evaluate the expression.
int temperature = 72;
boolean raining = false;
temperature > 65 && !raining
true
Rewrite !(score < 70) without using !
score >= 70
Write a Boolean expression that checks whether score is not equal to 100.
score != 100
int level = 2;
if (level > 5)
System.out.print("Gold ");
System.out.print("Player");
Player
What prints?
int age = 14;
boolean adultPresent = true;
if (age >= 16) {
System.out.println("Enter alone");
} else {
if (adultPresent)
System.out.println("Enter with adult");
else
System.out.println("Wait outside");
}
Enter with adult
Write a Boolean expression that is true when height is at least 48 and at most 78.
height >= 48 && height <= 78
Rewrite !(sunny && warm) using De Morgan’s Law
!sunny || !warm
What value is stored in result?
int a = 7;
int b = 2;
boolean result = (a / b == 3);
true
Integer division makes 7 / 2 equal to 3
int n = 4;
if (n > 2)
n += 3;
if (n > 6)
n--;
else
n += 2;
System.out.println(n);
6
What prints?
int x = 9;
int y = 2;
if (x > 5)
if (y > 4)
System.out.println("A");
else
System.out.println("B");
B
When is my daughter's birthday?
Rewrite !(age < 12 || age >= 65) without using !
age >= 12 && age < 65
Write a statement that creates a Boolean variable named hasCat, which is true when the String variable message contains "cat". Use indexOf.
boolean hasCat = message.indexOf("cat") >= 0;
Fix the code so it prints "Even" for even integers and "Odd" otherwise.
int number = 8;
if number % 2 = 0)
System.out.println("Even")
else
System.out.println("Odd");
Change the = to ==
need opening (
need semicolon
Write nested selection statements for this rule:
If balance >= 20, print "Discount purchase" when hasCoupon is true and "Regular purchase" otherwise. If balance < 20, print "Save more".
if (balance >= 20) {
if (hasCoupon)
System.out.println("Discount purchase");
else
System.out.println("Regular purchase");
} else {
System.out.println("Save more");
}
Write an if-else statement that prints "Access granted" when a person has a ticket OR is a volunteer, but only if they are NOT banned. Otherwise, print "Access denied".
Use hasTicket, volunteer, and banned.
if ((hasTicket || volunteer) && !banned)
System.out.println("Access granted");
else
System.out.println("Access denied");
Are !(a && b) and !a && !b equivalent? If not, give values that prove it
No. For a = true and b = false, the first expression is true, but the second is false.