Boolean Expressions and If Statements Practice Questions

AP (Advanced Placement) · AP Computer Science A · 150 free MCQs with instant results and detailed explanations.

150
Total
47
Easy
74
Medium
29
Hard

Start Practicing Boolean Expressions and If Statements

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from Boolean Expressions and If Statements

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What will be the output of the following code snippet? if (true || false) { System.out.println("Hello"); } else { System.out.println("World"); }
A. Hello
B. World
C. Hello World
D. No output
Show Answer & Explanation
Correct Answer: A
The condition 'true || false' evaluates to true, hence the code inside the if block executes, printing 'Hello'.
Q2
Easy
What is the value of the following expression: (5 > 3) && (2 < 1)?
A. true
B. false
C. 5
D. 1
Show Answer & Explanation
Correct Answer: B
The expression evaluates to false because the second condition (2 < 1) is false, making the entire AND expression false.
Q3
Easy
If the variable 'x' is set to 10, what will the following code output? if (x % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }
A. Even
B. Odd
C. 10
D. 0
Show Answer & Explanation
Correct Answer: A
Since 10 is divisible by 2 with no remainder, the condition 'x % 2 == 0' is true, so 'Even' is printed.
Q4
Medium
What will be the output of the following code snippet? ```java boolean a = true; boolean b = false; boolean c = !a && b; System.out.println(c); ```
A. true
B. false
C. Compilation error
D. Runtime error
Show Answer & Explanation
Correct Answer: B
The expression '!a' evaluates to false, and 'false && b' (where b is false) yields false. Therefore, the output is false.
Q5
Medium
Given the following code, what will be printed when `x` is 15? ```java int x = 15; if (x % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } ```
A. Even
B. Odd
C. Error
D. None
Show Answer & Explanation
Correct Answer: B
Since 15 is not divisible by 2, the condition `x % 2 == 0` evaluates to false, thus the output will be 'Odd'.
Q6
Medium
What will the following code snippet output? ```java int a = 5; int b = 10; if (a < b) { System.out.println("a is less than b"); } else if (a > b) { System.out.println("a is greater than b"); } else { System.out.println("a is equal to b"); } ```
A. a is less than b
B. a is greater than b
C. a is equal to b
D. No output
Show Answer & Explanation
Correct Answer: A
The condition 'a < b' evaluates to true because 5 is indeed less than 10, so the first block executes, printing 'a is less than b'.
Q7
Medium
If the following code is executed, what will be the output when `num` is 8? ```java int num = 8; if (num >= 10) { System.out.println("Greater or equal to 10"); } else if (num >= 5) { System.out.println("Between 5 and 10"); } else { System.out.println("Less than 5"); } ```
A. Greater or equal to 10
B. Between 5 and 10
C. Less than 5
D. No output
Show Answer & Explanation
Correct Answer: B
The value 8 meets the condition 'num >= 5' but not 'num >= 10', so it executes the second block, printing 'Between 5 and 10'.
Q8
Hard
Given the following Java code snippet, what will be the output when the method `checkValue(int x)` is called with `x = 10`? ```java public void checkValue(int x) { if (x > 5 && x < 15) { System.out.println("Valid"); } else { System.out.println("Invalid"); } } ```
A. Valid
B. Invalid
C. Error
D. No Output
Show Answer & Explanation
Correct Answer: A
The condition `x > 5 && x < 15` evaluates to true when `x` is 10, thus "Valid" is printed.
Q9
Hard
Consider the following code snippet: ```java boolean a = true; boolean b = false; boolean c = true; boolean result; result = a && (b || c); ``` What will be the value of `result` after the execution of this code?
A. true
B. false
C. undefined
D. null
Show Answer & Explanation
Correct Answer: B
The expression `b || c` evaluates to true (because c is true), but since `a` is true and `b` is false, the overall expression evaluates to false.
Q10
Hard
What will be the output of the following Java code snippet? ```java int x = 5; if (x > 3 && x < 10) { System.out.println("A"); } else { System.out.println("B"); } ```
A. A
B. B
C. No output
D. Error
Show Answer & Explanation
Correct Answer: A
The condition 'x > 3 && x < 10' evaluates to true because x is 5. Therefore, 'A' is printed.

Showing 10 of 150 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 150 Questions →

Boolean Expressions and If Statements โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 150 practice MCQs for the chapter Boolean Expressions and If Statements in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 47 easy, 74 medium, 29 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.