What keyword begins a decision statement that executes code only when a condition is true?
Answer: `if`
Which relational operator checks whether two values are equal?
Answer: `==`. `=` assigns a value; `==` compares values.
Which flowchart shape represents a decision, such as checking whether a student passed an exam?
Answer: A diamond. A decision usually branches into Yes/No or True/False paths.
What is the purpose of a `switch` statement in Java?
Answer: It selects which code block to execute based on the value of an expression, matching it against different cases.
What is wrong with this condition?
if (age = 18) {
Answer: It uses `=` instead of `==`. To compare, use `if (age == 18) {`.
What is the purpose of an `if-else` statement?
Answer: It executes one block if the condition is true and another block if the condition is false.
What do the logical operators `&&` and `||` mean?
Answer: `&&` means AND; `||` means OR.
In a flowchart, what do the two branches coming from a decision usually represent?
Answer: Possible outcomes of the condition, such as True/False or Yes/No.
What is the purpose of a `case` label inside a `switch` statement?
Answer: It identifies a specific value to match against the switch expression.
What will this code display?
int number = 5;
if (number > 10);
System.out.println("Hello");
Answer: `Hello`. The semicolon ends the `if` statement, so the print statement runs regardless of the condition.
What will this code display?
int age = 17;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
Answer: `Minor`. Explanation: 17 is not greater than or equal to 18.
What does this expression evaluate to?
int x = 10;
int y = 5;
System.out.println(x > 5 && y < 3);
Answer: `false`. The first condition is true, but the second is false; AND requires both to be true.
A program checks `score >= 60`. The score is 72. Which branch should the flowchart follow, and what should the program do if that branch displays "Pass"?
Answer: Follow the True/Yes branch and display "Pass".
What will this code display?
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
}
Answer: `Tuesday`.
The goal is to print "Valid" only when age is between 13 and 19 inclusive. Find the logical error:
if (age >= 13 || age <= 19) {
System.out.println("Valid");
}
Answer: Replace `||` with `&&`:
if (age >= 13 && age <= 19) {
System.out.println("Valid");
}
Both comparisons must be true.
Identify the error in this code:
int score = 85;
if score >= 60 {
System.out.println("Pass");
}
Answer: The condition needs parentheses: `if (score >= 60) {`.
What does this expression evaluate to?
int age = 16;
boolean hasPermission = true;
System.out.println(age >= 16 && hasPermission);
Answer: `true`, because both conditions are true.
A program should print "A" for 90 or higher, "B" for 80–89, and "C" below 80. Why should the highest score range be checked first?
Answer: It prevents a score such as 95 from being classified by a broader condition like `score >= 80` before the A condition is checked.
What can happen if you leave out `break` in a traditional Java `switch` statement?
Answer: Execution can continue into statements under following case labels; this is called fall-through.
What is the problem if the goal is to print "A" for scores of 90 or higher and "B" for 80–89?
if (score >= 80) {
System.out.println("B");
} else if (score >= 90) {
System.out.println("A");
}
Answer: The conditions are in the wrong order. Scores of 90+ satisfy the first condition, so A is never printed. Correct order:
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
}
Write an `if-else` statement that prints "Pass" when `score` is at least 60 and "Fail" otherwise.
Answer:
if (score >= 60) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Write a condition to check whether `temperature` is between 60 and 80, including both endpoints.
`temperature >= 60 && temperature <= 80`.
Plan decision logic that prints "Free" if `age < 5`, "Discount" if age is 5 through 12, and "Full Price" if age > 12. Give the conditions in order.
Answer:
if (age < 5) {
System.out.println("Free");
} else if (age <= 12) {
System.out.println("Discount");
} else {
System.out.println("Full Price");
}
Explanation: If the first condition is false, checking `age <= 12` covers ages 5 through 12.
What will this code display, and why?
int choice = 1;
switch (choice) {
case 1:
System.out.println("Start");
case 2:
System.out.println("Continue");
break;
default:
System.out.println("Exit");
}
Answer:
Start
Continue
Explanation: There is no `break` after case 1, so execution falls through into case 2. The break in case 2 prevents default from executing.
Debug this code so it prints "Weekend" for days 6 or 7 and "Weekday" for days 1–5. Identify the problem and fix it.
int day = 7;
switch (day) {
case 6:
System.out.println("Weekend");
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}
Answer: With day 6, the code prints Weekend twice because case 6 falls through. Group the cases:
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}