If Statements
Logical & Relational Operators
Strings & Comparisons
Switch Statements
Debugging & Scope
100

What is the correct syntax of an if statement in Java?

if (BooleanExpression) {
    // statement(s)
}
100

Which operator checks if two values are equal in Java?

==

100

Write an if statement that prints “Hello!” only if userInput equals "Yes" (case-sensitive).

if (userInput.equals("Yes")) {
    System.out.println("Hello!");
}
100

What is the purpose of the default case in a switch statement?

Executes if no other case matches.

100

What symbols do you use for a block of code in an if statement?

{}

200

Rewrite this as an if-else statement: If temperature is above 100, print “Hot”; otherwise, print “Cool.”

if (temperature > 100) {
    System.out.println("Hot");
} else {
    System.out.println("Cool");
}
200

What are 3 values of num that make this true:
num <=2 && num >=1

1, 1.5, 2, ...

200

Why can’t you use == to compare Strings in Java?

== compares memory addresses, not content

200

What happens if you forget break; after a case?

Execution falls through to the next case(s).

200

What is wrong with this condition?

if (count < 0 && count > 100)



Cannot be <0 and >100 at the same time. Use || instead.

300

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.

300

Write a condition that is true if x is a number outside of 1-16.

if (x < 1 || x > 16)



300

Given "Apple".compareTo("Apricot"), what would the result be (positive, negative, or zero). Explain why.

Negative because 'l' < 'r'

300

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.

300

What is my daughter's name?

Bonus for her middle name.

Bonus bonus for her last name.

Hayden Jo Curtis

400

What is a decision structure?

A type of structure allows a program to choose between multiple paths based on a condition.

400

Write a condition that is true only if age is at least 18 and hasID is true.

if (age >= 18 && hasID)

400

Given "apple".compareTo("Apple"), what would the result be (positive, negative, or zero). Explain why.

Because lowercase 'a' > uppercase 'A'

400

What would be printed:


int score = 85;
switch (score / 10) {
    case 10: System.out.println("A+"); break;
    case 9: System.out.println("A"); break;
    case 8: System.out.println("B"); break;
    case 7: System.out.println("C"); break;
    default: System.out.println("F"); break;
}



B
400

Find the error in this code and explain how to fix it:

int x = 10;
if (x > 5)
    int y = x + 2;
System.out.println(y);



y is declared inside the if without a block, so it is out of scope outside.


500

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");

}

500

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: &&, ||, !

500
Given "caterpillar".compareToIgnoreCase("Cat"), what would the result be (positive, negative, or zero). Explain why.


Positive.  The first three letters match, but "caterpillar" is longer, and longer strings are considered greater.



500

What would be printed:


char letter = 'C';
switch (letter) {
    case 'A':
    case 'B':
        System.out.println("Hey");
        break;
    case 'C':
        System.out.println("Here");
    case 'D':
        System.out.println("There");
        break;
    default:
        System.out.println("Hi");
}



Here
There

500

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.

M
e
n
u