Conditional Statements (if-then-else) and Boolean Variables (AP CSA)
Java MCQ with Answers
| <--Back to MCQs | Next to Using Loops--> |
Conditional Statements and Boolean Variables in Java
Boolean Variables
In Java, a boolean variable is a primitive data type that holds one of two values: true or false. Boolean variables are commonly used as flags or conditions to control the flow of a program. A boolean expression is formed using relational operators (>, <, >=, <=, ==, !=) and combined using logical operators: && (AND — both operands must be true), || (OR — at least one operand must be true), and ! (NOT — inverts the boolean value).
Conditional Statements (if-then-else)
Tips for AP-CSA Exam
In the AP Computer Science A (AP CSA) exam, questions on this topic test the ability to trace nested if-else chains, apply De Morgan's laws to simplify boolean expressions, and predict program output for given input values.
Q1. Identify the relationship between the integers n1 and n2 for the following Boolean expression to evaluate to true and print "Hello"?
if ((n1 >= n2) && (!(n1 <= n2))) System.out.println("Hello");
(A) (n1 > n2)
(B) (n1 >= n2)
(C) (n1 < n2)
(D) Evaluates to true irrespective of values of n1 and n2
(E) Never evaluate to true irrespective of values of n1 and n2
Answer:
View Output(A) (n1 > n2)
Explanation:
Let's divide the given boolean expression which comprises of two conditions:
Important that since both conditions are joined using a logical AND (&&), both should evaluate to true for the whole expression to be true.
C1 : (n1 >= n2)
C2: (! (n1 <= n2)) -> Rewritten as !(n1 < n2) or !(n1==n2)
Simplify C2, we get: (n1> n2) or (n1 != n2)
Now, break C1 into two parts: (n1 > n2) or (n1 == n2)
Hence, both C1 and C2 will be true only when (n1 > n2).
Q2. What will be the output of the following method checkStream() when invoked with the following values: math = 95, sci = 87 and ela= 90?
public static void checkStream(int math, int sci, int ela)
{
if ((math >=90) && (ela > 90))
{
if (sci >= 90) System.out.println("Science Stream");
else System.out.println("Business Stream");
}
else
if ((ela>=80) && (sci >=75))
System.out.println("Commerce Stream");
else System.out.println("Arts Stream");
}
(A) Science Stream
(B) Business Stream
(C) Commerce Stream
(D) Arts Stream
(E) No output - none of the conditions gets satisfied
Answer:
View Output(C) Commerce Stream
Explanation:
Let us workout the given code statement by statement:
if ((math >=90) && (ela > 90)) -> if ((95 >=90) && (90 > 90))
evaluates to false and moves to the else statement, which is:
if ((ela>=80) && (sci >=75)) -> if ((90>=80) && (87 >=75))
evaluates to true and executes the following print statement and prints: Commerce Stream
Q3. What will be the output of the method checkQuadrant() when invoked with the following values: x = -4 and y = 0?
public static void checkQuadrant(int x, int y)
{
if ((x > 0) && (y > 0))
{
System.out.println("Quadrant I");
}
else
if ((x < 0) && (y > 0))
System.out.println("Quadrant II");
else
if ((x < 0) && (y < 0))
System.out.println("Quadrant III");
else
if ((x > 0) && (y < 0))
System.out.println("Quadrant IV");
}
(A) Quadrant I
(B) Quadrant II
(C) Quadrant III
(D) Quadrant IV
(E) No output - none of the conditions gets satisfied
Answer:
View Output(E) No output - none of the conditions gets satisfied
Explanation:
Let us workout the given code statement by statement:
Note: We can even deduce the answer without working through the whole code since one of the variables (y) has a value equal to 0 (zero) and no where in the code any of the boolean condition has an equal to 0 (zero) condition. So, we can easily say that there will be no output.
But we must still verify by traversing each statement.
if ((x > 0) && (y > 0)) - > if ((-4 > 0) && (0 > 0))
evaluates to false; and moves to next statement:
if ((x < 0) && (y> 0)) -> if ((-4 < 0) && (0> 0))
evaluates to false; and again moves to the next statement:
Next, if ((x < 0) && (y < 0)) -> if ((-4 < 0) && (0 < 0))
also evaluates to false; and moves to the next statement:
Finally, if ((x> 0) && (y < 0)) -> if ((-4 > 0) && (0 < 0))
evaluates to false; Hence, no output.
Q4. Four integers a, b, c, and d are to be compared to find the largest of them. Which one of the following codes will fail to do so?
[Assume none of the numbers are equal to each other]
(A) if ((a > b) && (a > c) && (a > d))
System.out.println("a is the largest");
if ((b > c) && (b > a) && (b > d))
System.out.println("b is the largest");
if ((c > a) && (c > b) && (c > d))
System.out.println("c is the largest");
if ((d > a) && (d > b) && (d > c))
System.out.println("d is the largest");
(B) if ((a > b) && (a > c) && (a > d))
System.out.println("a is the largest");
else
if ((b > c) && (b > a) && (b > d))
System.out.println("b is the largest");
else
if ((c > a) && (c > b) && (c > d))
System.out.println("c is the largest");
else
if ((d > a) && (d > b) && (d > c))
System.out.println("d is the largest");
(C) if ((a > b) && (a > c))
{
if (a > d) System.out.println("a is the largest");
}
if ((b > c) && (b > a))
{
if (b > d) System.out.println("b is the largest");
}
if ((c > a) && (c > b))
{
if (c > d) System.out.println("c is the largest");
}
if ((d > a) && (d > b))
{
if (d > c) System.out.println("d is the largest");
}
(D) if ((d > a) && (d > b) && (d > c))
System.out.println("d is the largest");
if ((c > a) && (c > b) && (c > d))
System.out.println("c is the largest");
if ((b > c) && (b > a) && (b > d))
System.out.println("b is the largest");
if ((a > b) && (a > c) && (a > d))
System.out.println("a is the largest");
(E) if ((a > b) && (a > c) && (a > d))
{
System.out.println("d is the largest");
}
else if ((b > c) && (b > a) && (b > d))
{
System.out.println("c is the largest");
}
else if ((c > a) && (c > b) && (c > d))
{
System.out.println("b is the largest");
}
else if ((d > a) && (d > b) && (d > c))
{
System.out.println("a is the largest");
}
Answer:
View Output(E) if ((a > b) && (a > c) && (a > d))
{
System.out.println("d is the largest");
}
else if ((b > c) && (b > a) && (b > d))
{
System.out.println("c is the largest");
}
else if ((c > a) && (c > b) && (c > d))
{
System.out.println("b is the largest");
}
else if ((d > a) && (d > b) && (d > c))
{
System.out.println("a is the largest");
}
Explanation:
(E) is the incorrect code because although the logic is correct, the println statements are incorrect.
First if statement evaluates 'a to be the largest' but prints that 'd to be the largest'. Same is with other statements.
Q5. What will be the output of the following code?
if ((true || false) && false)
System.out.println("true");
else
System.out.println("false");
(A) true
(B) false
(C) No output - none of the conditions gets satisfied
(D) Compile time error - malformed Boolean expression in if statement
(E) Runtime error - incorrect use of true and false
Answer:
View Output(B) false
Explanation:
The boolean expression can be re-written as follows:
Let C1: (true || false)
Then, the condition becomes: (C1 && false)
For && (AND) operator to return true, both the operands should be true. However, we see here, that one operand is already false. Hence, whatever may be the value of C1, the whole boolean expression will always evaluate to false.
Q6. Which one of the following expressions always evaluates to true?
(A) ((false || true) && (false || false))
(B) (!(false) && (false || !false))
(C) (!(true) || (false && !false))
(D) (!(true) || (!false && !true))
(E) (((true) || !true) && !true)
Answer:
View Output(B) (!(false) && (false || !false))
Explanation:
| Option (A) will evaluate to false. This
expression can be simplified and written as two separate conditions:
C1: (false || true) and C2: (false || false). Expression becomes: (C1 && C2) Now, C2 will always evaluate to false; two false connected by logical OR operator. Hence, the whole expression will always evaluate to false; since C1 and C2 are connected by logical AND, and C2 is always false. |
| Option (B) will evaluate to true. This
expression can be simplified and written as two separate conditions:
C1: !(false) and C2: (false || !false) Expression becomes: (C1 && C2) Now, C1 will always evaluate to true. C2 can be simplified to (false || true) -> true Now since, both C1 and C2 evaluate to true; their logical AND will also be true. |
| Option (C) will evaluate to false. This
expression can be simplified and written as two separate conditions:
C1: !(true) and C2:(false && !false) Expression becomes: (C1 || C2). Even if one of the two, C1 or C2 evaluates to true, the entire expression will evaluate to true, since they are connected by logical OR. Now, C1 will always evaluate to false. C2 can be simplified to (false && true) -> false Now since, both C1 and C2 evaluate to false; their logical OR will also be false. |
| Option (D) will evaluate to false.
This expression can be simplified and written as two separate conditions: C1: !(true) and C2: (!false && !true) Expression becomes: (C1 || C2). Even if one of the two, C1 or C2 evaluates to true, the entire expression will evaluate to true, since they are connected by logical OR. Now, C1 will always evaluate to false. C2 can be simplified to (true && false) -> false Now since, both C1 and C2 evaluate to false; their logical OR will also be false. |
| Option (E) will evaluate to false.
This expression can be simplified and written as two separate conditions: C1: ((true) || !true) and C2: !true Expression becomes: (C1 && C2). Both C1 and C2 must evaluate to true for the whole expression to evaluate to true. However, C2 evaluates to false. Hence, the whole expression evaluates to false. |
Q7. Which one of the following expressions always evaluates to false?
Note: 's' and 'b' are character literals.
(A) (((1 > 0) && true) || !(0 == 0))
(B) ((('s' == 's') && true) || false)
(C) ((('b' >= 's') || true) && !false)
(D) ((('b' >= 's') && (1 > 0)) && !false)
(E) None of the option evaluates to false
Answer:
View Output(D) ((('b' >= 's') && (1 > 0)) && !false)
Explanation:
| Option (A) will evaluate to true. This
expression can be simplified and written as two separate conditions:
C1: ((1 > 0) && true) and C2: !(0 == 0) Expression becomes: (C1 || C2). C2 evaluates to !(true) -> false. Since, C1 and C2 are connected through logical OR, we must evaluate the value of C1. C1 can be simplified to: (true && true), since (1 > 0) is true. Hence, finally C1 evaluates to true. The final expression becomes: (true || false) -> true. |
| Option (B) will evaluate to true. This
expression can be simplified and written as two separate conditions:
C1: (('s' == 's') && true) and C2: false Expression becomes: (C1 || C2). Since, C1 and C2 are connected through logical OR, we must evaluate the value of C1; C2 is already false. C1 can be simplified to: (true && true), since ('s' == 's') is true. Hence, finally C1 evaluates to true. The final expression becomes: (true || false) -> true. |
| Option (C) will evaluate to true. This
expression can be simplified and written as two separate conditions:
C1: (('b' >= 's') || true) and C2: !false Expression becomes: (C1 && C2). Since, C1 and C2 are connected through logical AND, we must evaluate the value of both C1 and C2. C2 can easily be simplified: !false -> true. C1 can be simplified to: (false || true), since ('b' >= 's') is false. Hence, finally C1 evaluates to true. The final expression becomes: (true && true) -> true. |
| Option (D) will evaluate to false. This
expression can be simplified and written as two separate conditions:
C1: (('b' >= 's') && (1>0)) and C2: !false Expression becomes: (C1 && C2). Since, C1 and C2 are connected through logical AND, we must evaluate the value of both C1 and C2. C2 can easily be simplified: !false -> true. C1 can be simplified to: (false && true), since ('b' >= 's') is false and (1>0) is true. Hence, finally C1 evaluates to false. The final expression becomes: (false && true) -> false. |
Q8. When does the following expression evaluates to false?
((!x || (x || !y)) == (x || y))
(A) Only when x is set to true
(B) Only when y is set to true
(C) When both x and y are false
(D) Always evaluates to false
(E) Never evaluates to false
Answer:
View Output(C) When both x and y are false
Explanation:
We need to calculate the value of the expression for all the possible combination of values of x and y, as shown in the table below:
| x | y | ((!x || (x || !y)) == (x || y)) |
|---|---|---|
| true | true |
((!true || (true || ! true)) == (true || true)) -> ((false|| (true || false)) == (true)) -> ((false || (true)) == (true)) -> ((true) == (true)) -> true |
| true | false |
((!true || (true || ! false)) == (true || false)) -> ((false || (true || true)) == (true)) -> ((false || (true)) == (true)) -> ((true) == (true)) -> true |
| false | true |
((!false || (false || ! true)) == (false || true)) -> ((true || (false || false)) == (true)) -> ((true || (false)) == (true)) -> ((true) == (true)) -> true |
| false | false |
((!false || (false || ! false)) == (false || false)) -> ((true || (false || true)) == (false)) -> ((true || (true)) == (false)) -> ((true) == (false)) -> false |
Q9. What will be the output of the following code if bit1 = true and bit2 = false ?
if (bit1 == false)
{
if (bit2 == false)
System.out.println("false " + bit1);
else
System.out.println("true " + bit1);
}
else if (bit2 == false)
System.out.println("true "+ bit2);
else
System.out.println("false "+ bit2);
(A) true false
(B) false true
(C) true true
(D) false false
(E) None of the option
Answer:
View Output(A) true false
Explanation:
Since bit1 is initialised to true, the first if condition evaluates to false and the corresponding else construct gets executed.
Furthermore, the statement if (bit2 == false) now evaluates to true and prints "true" + value of bit2 which is false.
Q10. Two switch S1 and S2 (to control the patio lights) are installed at two different floors of the house. The lights will be ON only if both the switches S1 and S2 are in ON position or both are in OFF position. The lights will be OFF for all other positions of S1 and S2. Which one of the following codes fails to produce the desired output?
(A) if (S1 == true)
{
if (S2 == false)
System.out.println("Lights are OFF");
else
System.out.println("Lights are ON");
}
else if (S2 == true)
System.out.println("Lights are OFF");
else
System.out.println("Lights are ON");
(B) if ((S1 == true) && (S2 == true))
{
System.out.println("Lights are ON");
}
else if ((S1 == false) && (S2 == false))
System.out.println("Lights are ON");
else
System.out.println("Lights are OFF");
(C) if (S1 == true)
{
if (S2 == true)
System.out.println("Lights are ON");
else
System.out.println("Lights are OFF");
}
else if (S2 == false)
System.out.println("Lights are ON");
else
System.out.println("Lights are OFF");
(D) if (S1 == true)
{
if (S2 == false)
System.out.println("Lights are ON");
else
System.out.println("Lights are OFF");
}
else if (S2 == true)
System.out.println("Lights are OFF");
else
System.out.println("Lights are ON");
Answer:
View Output(D) if (S1 == true)
{
if (S2 == false)
System.out.println("Lights are ON");
else
System.out.println("Lights are OFF");
}
else if (S2 == true)
System.out.println("Lights are OFF");
else
System.out.println("Lights are ON");
Explanation:
| Option Code (A) | ||
|---|---|---|
| S1 | S2 | Output |
| true | true | ON |
| true | false | OFF |
| false | true | OFF |
| false | false | ON |
| Correct | ||
| Option Code (B) | ||
| S1 | S2 | Output |
| true | true | ON |
| true | false | OFF |
| false | true | OFF |
| false | false | ON |
| Correct | ||
| Option Code (C) | ||
| S1 | S2 | Output |
| true | true | ON |
| true | false | OFF |
| false | true | OFF |
| false | false | ON |
| Correct | ||
| Option Code (D) | ||
| S1 | S2 | Output |
| true | true | OFF |
| true | false | ON |
| false | true | OFF |
| false | false | ON |
| Incorrect | ||
Q11. What will be printed when the following code is executed, if month = 12?
public static void getSeason(int month)
{
if (month >=12)
System.out.println("Incorrect month");
if ((month <=3) && (month >=1))
System.out.println("It's Winter");
else if ((month > 3) && (month <= 5))
System.out.println("It's Spring");
else if ((month > 5) && (month <= 8))
System.out.println("It's Summer");
else if ((month > 8) && (month <= 10))
System.out.println("It's Fall");
else System.out.println("It's Winter again");
}
(A) It's Winter
(B) It's Winter again
(C) Incorrect month
It's Winter again
(D) Incorrect month
It's Winter
(E) Incorrect month
Answer:
View Output(C) Incorrect month
It's Winter again
Explanation:
The program executes the first if condition (month >=12), which evaluates to true and hence prints "Incorrect month".
Then, since there is NO else clause, execution will move on to execute the next if statement and finally the last else clause will be executed and will print "It's Winter again".
Q12. What will be values of all the lights at t = 29 and t = 52?
int redLight =0, greenLight =0, yellowLight =0;
int t =0;
for (t =1; t <=60; t++)
{
if ((t >= 1) && (t < 10))
redLight = 1;
else
if ((t>= 10) && (t <= 40))
{
redLight =0;
greenLight =1;
}
else if ((t>= 40) && (t <= 50))
{
greenLight =0;
yellowLight =1;
}
else {
redLight =1;
yellowLight =0;
}
}
(A) At t= 29, redLight =1; greenLight=0, yellowLight=0
At t= 52, redLight =0; greenLight=1, yellowLight=0
(B) At t= 29, redLight =0; greenLight=1, yellowLight=0
At t= 52, redLight =0; greenLight=1, yellowLight=0
(C) At t= 29, redLight =1; greenLight=1, yellowLight=0
At t= 52, redLight =0; greenLight=0, yellowLight=0
(D) At t= 29, redLight =0; greenLight=1, yellowLight=0
At t= 52, redLight =1; greenLight=0, yellowLight=0
(E) At t= 29, redLight =0; greenLight=0, yellowLight=1
At t= 52, redLight =0; greenLight=1, yellowLight=0
Answer:
View Output(D) At t= 29, redLight =0; greenLight=1, yellowLight=0
At t= 52, redLight =1; greenLight=0, yellowLight=0
Explanation:
Left for reader to execute.
| <--Back to MCQs | Next to Using Loops--> |