Conditionals Questions with Answers

38 practice questions on Conditionals (Pseudocode & Code Tracing) with answers and step-by-step solutions. Try 10 samples below, then practise all 38 free in the AptiNest app.

Sample questions

Question 1Easy

What will be printed by the following pseudocode?

Integer x, y
Set x = 15, y = 12
if (x > y)
    if (x - y > 5)
        Print x - y
    else
        Print x + y
    End if
else
    if (y mod x == 0)
        Print y / x
    else
        Print y mod x
    End if
End if

/ denotes integer division (the fractional part is discarded). Printed values are separated by spaces.

  1. 27
  2. 28
  3. 29
  4. 26

Answer: A. 27

Explanation

Follow the outer and inner conditions; the branch taken prints 27.

Step-by-step solution

  1. x = 15 is greater than y = 12, so the first branch runs.
  2. x - y is 3, which is not greater than 5, so the inner else runs and prints x + y.
  3. 15 + 12 = 27. The output is 27: option A.

Why the other options are wrong

  • B. 28 is one more than the correct sum of 27.
  • C. 29 is two more than the correct sum of 27.
  • D. 26 is one less than the correct sum of 27.

Tip: Follow the if chain one condition at a time and print only the value on the branch that runs.

Question 2Easy

What is the output of the following pseudocode?

Integer n
Set n = -7
if (n > 0)
    Print 1
else if (n < 0)
    Print 0 - 1
else
    Print 0
End if

Printed values are separated by spaces.

  1. -2
  2. -1
  3. 1
  4. 0

Answer: B. -1

Explanation

The program prints the sign of -7: -1.

Step-by-step solution

  1. n = -7, so n > 0 is false.
  2. n < 0 is true.
  3. Print 0 - 1 = -1.

Why the other options are wrong

  • A. -2 is not printed by any branch.
  • C. 1 is printed only when n > 0.
  • D. 0 is printed only when n == 0.

Tip: Check the first true condition in the chain.

Question 3Easy

What will be printed by the following pseudocode?

Integer a, b, c
Set a = 7, b = 3, c = 11
if (a > b and a > c)
    Print a - b
else if (b > c)
    Print b - c
else
    Print c - a
End if

Printed values are separated by spaces.

  1. 4
  2. 6
  3. 5
  4. 3

Answer: A. 4

Explanation

Check the conditions in order: the first true branch prints 4.

Step-by-step solution

  1. a = 7, b = 3, c = 11.
  2. Check if a > b and a > c: 7 > 3 and 7 > 11 is false.
  3. Next check if b > c: 3 > 11 is false.
  4. Else print c - a: 11 - 7 = 4.

Why the other options are wrong

  • B. 6 would need b minus c in the else-if branch, but that branch is not taken here.
  • C. 5 is wrong; the else branch prints c minus a, which is 11 minus 7 = 4.
  • D. 3 is the value of b, not the printed value; the else branch prints c minus a = 4.

Tip: Check conditions sequentially; the first true one executes.

Question 4Easy

What will be printed by the following pseudocode?

Integer n
Set n = 30
if (n mod 2 == 0)
    n = n / 2
else
    n = n * 3
End if
if (n > 20)
    n = n - 20
End if
Print n

/ denotes integer division (the fractional part is discarded).

  1. 15
  2. 70
  3. 16
  4. 0

Answer: A. 15

Explanation

The first if halves even n or triples odd n; the second subtracts 20 if the result exceeds 20: 15.

Step-by-step solution

  1. n = 30 is even, so n becomes 30 / 2 = 15.
  2. 15 is not more than 20, so the second if does nothing.
  3. The output is 15: option A.

Why the other options are wrong

  • B. 70 is wrong; 30 is even, so the odd rule does not run.
  • C. 16 is one more than 15; the even rule gives 30 / 2 = 15.
  • D. 0 is wrong; the output is 15.

Tip: Apply the first if-else to the starting value, then check the second if on the new value.

Question 5Medium

Predict the output of the following pseudocode.

Integer p, q, r
Set p = 4, q = 6, r = 2
if (p > q and q > r or p == r)
    Print p + q + r
else
    Print p * q - r
End if

Printed values are separated by spaces.

  1. 21
  2. 23
  3. 24
  4. 22

Answer: D. 22

Explanation

and binds tighter than or:
the condition is (p > q and q > r) or (p == r).
It is decided first, then the branch prints 22.

Step-by-step solution

  1. The condition is (p > q and q > r) or (p == r), because and binds tighter than or.
  2. p > q is 4 > 6, which is false, and p == r is 4 == 2, which is false. So the condition is false.
  3. The else branch prints p × q - r = 24 - 2 = 22: option D.

Why the other options are wrong

  • A. 21 is wrong; the else branch prints 24 - 2, which is 22.
  • B. 23 is one more than the correct result of 22.
  • C. 24 is p × q without subtracting r.

Tip: And binds tighter than or, so split the condition into its parts before checking it.

Question 6Medium

What will be the output of the following pseudocode?

Integer a, b
Set a = 8, b = 20
if (a > 10 or b > 10)
    a = a + b
    if (a > 25)
        b = a - 25
    End if
else
    b = b * 2
End if
Print a, b

Printed values are separated by spaces.

  1. 28 3
  2. 28 4
  3. 29 3
  4. 28 2

Answer: A. 28 3

Explanation

Follow the branches: a = 28 and b = 3.

Step-by-step solution

  1. A is set to 8, b is set to 20.
  2. Check if (a > 10 or b > 10): 8 > 10 is false, 20 > 10 is true, so condition is true.
  3. a = a + b → 8 + 20 = 28.
  4. Check if (a > 25): 28 > 25 is true.
  5. b = a - 25 → 28 - 25 = 3.
  6. Print a, b → 28 3.

Why the other options are wrong

  • B. 28 4 is wrong; b becomes a minus 25, which is 28 minus 25 = 3.
  • C. 29 3 is wrong; a is 8 plus 20 = 28, not 29.
  • D. 28 2 is wrong; b becomes a minus 25 = 3, not 2.

Question 7Medium

What will be the output of the following pseudocode?

Integer x, r
Set x = 21, r = 0
if (x > 0)
    if (x mod 2 == 0)
        r = 1
    else if (x mod 3 == 0)
        r = 2
    else
        r = 3
    End if
else
    r = 4
End if
Print r * x
  1. 42
  2. 43
  3. 44
  4. 41

Answer: A. 42

Explanation

x is positive and odd; the inner else-if chain sets r = 2, so r * x = 42.

Step-by-step solution

  1. x = 21 is greater than 0, so the outer if runs.
  2. 21 is odd, so the first test fails. 21 mod 3 is 0, so the else-if sets r = 2.
  3. r × x = 2 × 21 = 42. The output is 42: option A.

Why the other options are wrong

  • B. 43 is not r × x; the product is 42.
  • C. 44 would need r = 2 and x = 22, but x is 21.
  • D. 41 is wrong; 2 × 21 is 42.

Tip: In an else-if chain, the first true test decides r and the rest are skipped.

Question 8Medium

What will be the output of the following pseudocode?

Integer x, y
Set x = 5, y = 9
if (!(x > y) and x != 0)
    x = y - x
else
    y = x - y
End if
Print x + y
  1. 14
  2. 15
  3. 12
  4. 13

Answer: D. 13

Explanation

! negates the condition. Here the branch leaves x = 4, y = 9, so the output is 13.

Step-by-step solution

  1. X is set to 5 and y is set to 9.
  2. ! negates (x > y), which is false, making it true.
  3. X != 0 is true, so the condition is true.
  4. x = y - x becomes x = 9 - 5 = 4.
  5. Print x + y gives 4 + 9 = 13.

Why the other options are wrong

  • A. 14 would need x to be 5 after the update; x becomes 9 minus 5, which is 4.
  • B. 15 is wrong; x is 4 after the subtraction, and 4 plus 9 is 13.
  • C. 12 is wrong; 4 plus 9 is 13, not 12.

Tip: Check the order of operations and truth values carefully in logical conditions.

Question 9Hard

What is the output of the following pseudocode?

Integer y, c
Set y = 12, c = 0
if (y > 0 and y mod 3 == 0)
    c = c + 1
End if
if (y > 10 or y mod 2 == 0)
    c = c + 2
End if
if (!(y < 20))
    c = c + 4
End if
Print c
  1. 5
  2. 2
  3. 3
  4. 4

Answer: C. 3

Explanation

Each independent if adds its value when true: 1 for a positive multiple of 3, 2 for y > 10 or even, 4 for y >= 20. Total 3.

Step-by-step solution

  1. y=12: first if is true (positive, multiple of 3), c=1.
  2. Second if is true (y>10), c=3.
  3. !(12<20) is false, so +4 does not apply.
  4. Print 3.

Why the other options are wrong

  • A. 5 wrongly adds 4.
  • B. 2 misses the first test.
  • D. 4 only adds the third test.

Tip: Evaluate every if separately.

Question 10Hard

What is the output of the following pseudocode?

Integer y, m, d
Set y = 2100, m = 2, d = 0
if (m == 2)
    if ((y mod 4 == 0 and y mod 100 != 0) or y mod 400 == 0)
        d = 29
    else
        d = 28
    End if
else if (m == 4 or m == 6 or m == 9 or m == 11)
    d = 30
else
    d = 31
End if
Print d
  1. 30
  2. 27
  3. 29
  4. 28

Answer: D. 28

Explanation

This returns the number of days in month 2 of year 2100: 28.

Step-by-step solution

  1. m = 2, so the February branch runs.
  2. 2100 is divisible by 100 but not by 400, so it is not a leap year.
  3. So d = 28, the correct answer.

Why the other options are wrong

  • A. 30 is wrong; 30 days is for April, June, September and November.
  • B. 27 is wrong; February has 28 days in a common year.
  • C. 29 is wrong; 2100 is not a leap year.

Tip: A year is a leap year if it is divisible by 4, except century years, which must also be divisible by 400.

Practise all 38 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Pseudocode & Code Tracing topics · All topics