Chapter 5 Quiz: Making Decisions — IF, CASE, and Boolean Logic

Instructions: Choose the single best answer for each question. Answers are provided at the end.


Question 1. What is the output of this code when x is 7?

if x > 10 then
  WriteLn('A');
WriteLn('B');
  • (a) A
  • (b) B
  • (c) A followed by B
  • (d) No output

Question 2. Which of the following correctly uses if..then..else?

  • (a) if x > 5 then WriteLn('big'); else WriteLn('small');
  • (b) if x > 5 then WriteLn('big') else WriteLn('small');
  • (c) if x > 5 WriteLn('big') else WriteLn('small');
  • (d) if x > 5 then: WriteLn('big') else: WriteLn('small');

Question 3. In the expression if x > 5 and y < 10 then, Pascal evaluates this as:

  • (a) if (x > 5) and (y < 10) then — the intended meaning
  • (b) if x > (5 and y) < 10 then — a syntax error
  • (c) if (x > (5 and y)) < 10 then — likely a type mismatch error
  • (d) The code compiles and runs correctly without parentheses

Question 4. What does the {$B-} compiler directive do?

  • (a) Enables bounds checking
  • (b) Enables complete Boolean evaluation
  • (c) Enables short-circuit Boolean evaluation
  • (d) Disables Boolean type checking

Question 5. Given a := True, b := False, what is the value of a or b?

  • (a) True
  • (b) False
  • (c) Undefined
  • (d) Compiler error

Question 6. Which of the following types can be used as a case selector?

  • (a) Real
  • (b) String
  • (c) Char
  • (d) Double

Question 7. What happens if no case label matches the selector and there is no else clause?

  • (a) The program crashes with a runtime error
  • (b) The case statement is skipped entirely
  • (c) The first label's statement executes by default
  • (d) The compiler refuses to compile the program

Question 8. According to De Morgan's Laws, not (A or B) is equivalent to:

  • (a) (not A) or (not B)
  • (b) (not A) and (not B)
  • (c) not A or B
  • (d) A and B

Question 9. In this code, which if does the else belong to?

if a > 0 then
  if b > 0 then
    WriteLn('Both')
  else
    WriteLn('?');
  • (a) The outer if (a > 0)
  • (b) The inner if (b > 0)
  • (c) It is ambiguous and causes a compiler error
  • (d) It depends on the indentation

Question 10. What is the purpose of a guard clause?

  • (a) To protect variables from being modified
  • (b) To validate input early and exit if invalid, keeping the main logic flat
  • (c) To guard against compiler warnings
  • (d) To prevent the case statement from falling through

Question 11. Which case label syntax is valid in Pascal?

  • (a) case x of 'hello': WriteLn('hi'); end;
  • (b) case x of 1..5: WriteLn('range'); end;
  • (c) case x of 1.5: WriteLn('float'); end;
  • (d) case x of x > 5: WriteLn('big'); end;

Question 12. What is the output when score is 85?

case score of
  90..100: WriteLn('A');
  80..89:  WriteLn('B');
  70..79:  WriteLn('C');
else
  WriteLn('Below C');
end;
  • (a) A
  • (b) B
  • (c) C
  • (d) Below C

Question 13. With short-circuit evaluation enabled ({$B-}), what happens when x = 0 in this code?

if (x <> 0) and (100 div x > 5) then
  WriteLn('Yes');
  • (a) Division by zero error
  • (b) Prints "Yes"
  • (c) The second condition is not evaluated; nothing is printed
  • (d) Compiler error

Question 14. How many WriteLn calls execute when n is 25?

if n > 10 then
begin
  WriteLn('A');
  WriteLn('B');
end;
WriteLn('C');
  • (a) 1 (only C)
  • (b) 2 (A and C)
  • (c) 3 (A, B, and C)
  • (d) 2 (A and B)

Question 15. What is the correct way to check if a character is a lowercase vowel?

  • (a) if ch = 'a' or 'e' or 'i' or 'o' or 'u' then
  • (b) if ch in ['a', 'e', 'i', 'o', 'u'] then
  • (c) if (ch = 'a') or (ch = 'e') or (ch = 'i') or (ch = 'o') or (ch = 'u') then
  • (d) Both (b) and (c) are correct

Question 16. Why does Pascal's case statement require ordinal types?

  • (a) Because Niklaus Wirth preferred ordinal types
  • (b) Because the compiler can build a jump table for O(1) dispatch
  • (c) Because non-ordinal types cannot be compared
  • (d) Because it is a limitation that has never been fixed

Question 17. What is wrong with this code?

if balance = 0.0 then
  WriteLn('Zero balance');
  • (a) Nothing — it is correct
  • (b) balance should use := instead of =
  • (c) Comparing Real values with = is unreliable due to floating-point precision
  • (d) WriteLn cannot be inside an if statement

Question 18. In Pascal, what prevents the C-style bug of writing if (x = 5) when you mean if (x == 5)?

  • (a) Pascal does not have an = operator
  • (b) Pascal uses = for comparison and := for assignment, and := cannot appear in a Boolean expression
  • (c) Pascal requires the then keyword, which prevents the error
  • (d) There is no such protection in Pascal

Question 19. What is branch coverage?

  • (a) Testing that every variable is assigned at least once
  • (b) Testing that every line of code executes at least once
  • (c) Testing with inputs that exercise every branch of every conditional
  • (d) Testing that every procedure is called at least once

Question 20. Given the following code, which statement is true?

case grade of
  'A': WriteLn('Excellent');
  'B': WriteLn('Good');
  'C': WriteLn('Average');
  'D': WriteLn('Below average');
  'F': WriteLn('Failing');
end;
  • (a) If grade is 'E', the program crashes
  • (b) If grade is 'E', nothing happens — the case is skipped
  • (c) The code will not compile because there is no else clause
  • (d) If grade is 'a', it matches the 'A' label (case-insensitive)

Answer Key

  1. (b)x is 7, so x > 10 is False. The WriteLn('A') is skipped. WriteLn('B') is not part of the if and always executes.

  2. (b) — No semicolon before else. Option (a) has a semicolon that terminates the if, making else orphaned.

  3. (c)and has higher precedence than > and <, so 5 and y is evaluated first as a bitwise operation. This produces a type mismatch or unexpected result. Always parenthesize: (x > 5) and (y < 10).

  4. (c){$B-}` enables short-circuit evaluation. `{$B+} enables complete evaluation.

  5. (a)True or False evaluates to True.

  6. (c)Char is an ordinal type. Real, String, and Double are not ordinal.

  7. (b) — In Free Pascal, if no label matches and there is no else, the entire case statement is skipped. No error occurs, but the behavior may not be what you intended. (Note: the ISO Pascal standard leaves this behavior undefined, and some implementations raise an error.)

  8. (b) — De Morgan's Law: not (A or B) = (not A) and (not B).

  9. (b)else always matches the nearest unmatched if, which is the inner one (b > 0).

  10. (b) — A guard clause validates early, exits if invalid, and keeps the main logic at a shallow nesting level.

  11. (b) — Range labels like 1..5 are valid. String literals (a) cannot be case labels. Float values (c) are not ordinal. Expressions (d) are not valid labels.

  12. (b) — 85 falls in the range 80..89, so "B" prints.

  13. (c)x <> 0 is False, so short-circuit evaluation skips the second operand. No division occurs, and nothing is printed.

  14. (c)n > 10 is True, so the begin..end block executes (A and B). Then C always executes. Total: 3.

  15. (d) — Both the in set operator (b) and the chained or comparisons (c) are correct Pascal. Option (a) is syntactically invalid.

  16. (b) — Ordinal types have a finite, countable set of values that map to integers, enabling the compiler to build a jump table for O(1) dispatch.

  17. (c) — Floating-point comparison with = is unreliable due to rounding errors. Use Abs(balance) < epsilon instead.

  18. (b) — Pascal uses := for assignment and = for comparison. Since := is a statement (not an expression), it cannot appear where a Boolean value is expected. This eliminates the entire class of accidental-assignment bugs.

  19. (c) — Branch coverage ensures that every branch of every conditional (both true and false paths) is exercised by at least one test input.

  20. (b) — In Free Pascal without an else clause, an unmatched value simply skips the case. Also, case on Char is case-sensitive, so 'a' does not match 'A'.