Quiz: Variables, Types, and Expressions
Instructions: Choose the best answer for each question. Answers are provided at the end.
Q1. Which of the following is the correct way to declare an Integer variable named count in Pascal?
A) Integer count;
B) count = Integer;
C) var count: Integer;
D) count := Integer;
Q2. What is the value of 17 div 5?
A) 3.4 B) 3 C) 4 D) 2
Q3. What is the value of 17 mod 5?
A) 3.4 B) 3 C) 2 D) 5
Q4. Which operator is used for assignment in Pascal?
A) =
B) :=
C) ==
D) <-
Q5. What type would be most appropriate for storing whether a light switch is on or off?
A) Integer
B) Char
C) String
D) Boolean
Q6. What does Ord('A') return?
A) 1
B) 65
C) 'A'
D) 97
Q7. What is the output of WriteLn(Trunc(4.9))?
A) 5 B) 4 C) 4.9 D) 4.0
Q8. Which of the following will cause a compile error in Pascal?
A) var x: Real; x := 5;
B) var x: Integer; x := 5.0;
C) var x: Integer; x := 5 div 2;
D) var x: Real; x := 5 / 2;
Q9. What is the result of True and (not False)?
A) True B) False C) Compile error D) Runtime error
Q10. In Free Pascal, what is the index of the first character in the string 'Hello'?
A) 0 B) 1 C) -1 D) It depends on the string type
Q11. What is the output of WriteLn(Chr(66))?
A) 66
B) 'B'
C) B
D) A
Q12. Which of the following correctly converts the string '42' to an integer?
A) Integer('42')
B) Ord('42')
C) StrToInt('42')
D) Round('42')
Q13. What is the problem with if age >= 18 and age <= 65 then?
A) You cannot use and with integers
B) and has higher precedence than >=, causing a type error
C) age must be a Boolean variable
D) There is no problem; this code is correct
Q14. What is the value of 2 + 3 * 4?
A) 20 B) 14 C) 24 D) 12
Q15. Which keyword introduces the constant declaration section in Pascal?
A) var
B) let
C) const
D) define
Q16. What happens if you use a variable without initializing it first?
A) It is automatically set to 0 B) It contains whatever garbage was in that memory location C) The program refuses to compile D) It is automatically set to the type's default value
Q17. The expression 10 / 3 in Pascal produces a value of type:
A) Integer
B) Real
C) Byte
D) It depends on the variable storing the result
Q18. What is the maximum value a Byte variable can hold?
A) 127 B) 255 C) 256 D) 65535
Q19. Which directive enables range checking to detect integer overflow at runtime?
A) {$B+}
B) {$R+}
C) {$H+}
D) {$J-}
Q20. Pascal's strong typing is best described as:
A) A limitation that makes Pascal harder to use than modern languages B) A compile-time safety net that catches type-related bugs before the program runs C) A runtime check that slows down program execution D) A feature that only matters for large programs
Answer Key
| Question | Answer | Explanation |
|---|---|---|
| Q1 | C | Pascal syntax: var name: Type; |
| Q2 | B | div performs integer division: 17 / 5 = 3 remainder 2, so div returns 3 |
| Q3 | C | mod returns the remainder: 17 = 3*5 + 2, so mod returns 2 |
| Q4 | B | Pascal uses := for assignment; = is used for comparison and constant definition |
| Q5 | D | A light switch has exactly two states (on/off), which maps to True/False |
| Q6 | B | Ord('A') returns the ASCII value of 'A', which is 65 |
| Q7 | B | Trunc removes the decimal part without rounding: 4.9 becomes 4 |
| Q8 | B | You cannot store a Real literal (5.0) in an Integer variable without explicit conversion |
| Q9 | A | not False = True; True and True = True |
| Q10 | B | Pascal strings are 1-indexed; the first character is at position 1 |
| Q11 | C | Chr(66) returns the character with ASCII code 66, which is 'B'; WriteLn prints it as B |
| Q12 | C | StrToInt (from SysUtils) converts a string representation to an integer |
| Q13 | B | and has higher precedence than >=, so it tries to evaluate 18 and age first, causing a type error. Parentheses are required: (age >= 18) and (age <= 65) |
| Q14 | B | Multiplication has higher precedence than addition: 3*4=12, then 2+12=14 |
| Q15 | C | const introduces the constant section; var introduces variables |
| Q16 | B | Pascal does not automatically initialize local variables; they contain random memory contents |
| Q17 | B | The / operator always produces a Real result, regardless of operand types |
| Q18 | B | Byte is an unsigned 8-bit type: 0 to 255 |
| Q19 | B | {$R+}` enables range checking; `{$B+} controls Boolean evaluation; {$H+}` controls string type; `{$J-} makes typed constants read-only |
| Q20 | B | Strong typing catches type-related bugs at compile time, before the program runs — it is a safety feature, not a limitation |