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
casestatement 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
casestatement 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)
balanceshould use:=instead of= - (c) Comparing
Realvalues with=is unreliable due to floating-point precision - (d)
WriteLncannot be inside anifstatement
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
thenkeyword, 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
gradeis'E', the program crashes - (b) If
gradeis'E', nothing happens — thecaseis skipped - (c) The code will not compile because there is no
elseclause - (d) If
gradeis'a', it matches the'A'label (case-insensitive)
Answer Key
-
(b) —
xis 7, sox > 10is False. TheWriteLn('A')is skipped.WriteLn('B')is not part of theifand always executes. -
(b) — No semicolon before
else. Option (a) has a semicolon that terminates theif, makingelseorphaned. -
(c) —
andhas higher precedence than>and<, so5 and yis evaluated first as a bitwise operation. This produces a type mismatch or unexpected result. Always parenthesize:(x > 5) and (y < 10). -
(c) —
{$B-}` enables short-circuit evaluation. `{$B+}enables complete evaluation. -
(a) —
True or Falseevaluates toTrue. -
(c) —
Charis an ordinal type.Real,String, andDoubleare not ordinal. -
(b) — In Free Pascal, if no label matches and there is no
else, the entirecasestatement 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.) -
(b) — De Morgan's Law:
not (A or B)=(not A) and (not B). -
(b) —
elsealways matches the nearest unmatchedif, which is the inner one (b > 0). -
(b) — A guard clause validates early, exits if invalid, and keeps the main logic at a shallow nesting level.
-
(b) — Range labels like
1..5are valid. String literals (a) cannot be case labels. Float values (c) are not ordinal. Expressions (d) are not valid labels. -
(b) — 85 falls in the range 80..89, so "B" prints.
-
(c) —
x <> 0is False, so short-circuit evaluation skips the second operand. No division occurs, and nothing is printed. -
(c) —
n > 10is True, so thebegin..endblock executes (A and B). Then C always executes. Total: 3. -
(d) — Both the
inset operator (b) and the chainedorcomparisons (c) are correct Pascal. Option (a) is syntactically invalid. -
(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.
-
(c) — Floating-point comparison with
=is unreliable due to rounding errors. UseAbs(balance) < epsiloninstead. -
(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. -
(c) — Branch coverage ensures that every branch of every conditional (both true and false paths) is exercised by at least one test input.
-
(b) — In Free Pascal without an
elseclause, an unmatched value simply skips thecase. Also,caseonCharis case-sensitive, so'a'does not match'A'.