What is the correct syntax of an if statement in Java?
Which operator checks if two values are equal in Java?
==
Write an if statement that prints “Hello!” only if userInput equals "Yes" (case-sensitive).
What is the purpose of the default case in a switch statement?
Executes if no other case matches.
What symbols do you use for a block of code in an if statement?
{}
Rewrite this as an if-else statement: If temperature is above 100, print “Hot”; otherwise, print “Cool.”
What are 3 values of num that make this true:
num <=2 && num >=1
1, 1.5, 2, ...
Why can’t you use == to compare Strings in Java?
== compares memory addresses, not content
What happens if you forget break; after a case?
Execution falls through to the next case(s).
What is wrong with this condition?
Cannot be <0 and >100 at the same time. Use || instead.
Explain what a nested if statement is and give a real-world example.
A nested if statement is an if statement inside another if statement.
Write a condition that is true if x is a number outside of 1-16.
Given "Apple".compareTo("Apricot"), what would the result be (positive, negative, or zero). Explain why.
Negative because 'l' < 'r'
Identify the problem in this switch code.
switch (score) {
case (score > 90):
grade = 'A';
break;
default:
grade = 'F';
}
case must be a constant value, not an expression. Use if-else instead.
What is my daughter's name?
Bonus for her middle name.
Bonus bonus for her last name.
Hayden Jo Curtis
What is a decision structure?
A type of structure allows a program to choose between multiple paths based on a condition.
Write a condition that is true only if age is at least 18 and hasID is true.
if (age >= 18 && hasID)
Given "apple".compareTo("Apple"), what would the result be (positive, negative, or zero). Explain why.
Because lowercase 'a' > uppercase 'A'
What would be printed:
Find the error in this code and explain how to fix it:
y is declared inside the if without a block, so it is out of scope outside.
Write a nested if statement that prints:
"Eligible for discount" if age is 65 or older
But only if isMember is true
Otherwise, print the appropriate message explaining why they are not eligible
if (age >= 65) {
if (isMember) {
System.out.println("Eligible for discount");
} else {
System.out.println("Must be a member to receive discount");
}
} else {
System.out.println("Must be 65 or older for discount");
}
Explain the difference between relational operators and logical operators.
Relational operators compare two values and return true or false.
Examples: >, <, ==, !=
Logical operators combine or modify Boolean expressions.
Examples: &&, ||, !
Positive. The first three letters match, but "caterpillar" is longer, and longer strings are considered greater.
What would be printed:
Here
There
Explain the difference between a sequence structure and a decision structure.
Sequence structure: statements execute in order.
Decision structure: program chooses a path based on a condition.