Chapter 4 Quiz: Input, Output, and Formatting
Test your understanding of console I/O and formatting in Pascal. Answers are at the end.
Question 1. What is the primary difference between Write and WriteLn?
(a) Write can only output strings; WriteLn can output any type.
(b) WriteLn moves the cursor to the next line after output; Write does not.
(c) Write is faster because it does not output a newline character.
(d) WriteLn automatically adds spaces between arguments; Write does not.
Question 2. What is the output of the following code?
Write('Hello');
Write(' ');
WriteLn('World');
WriteLn('!');
(a) Hello World on one line, ! on the next line
(b) Hello World! all on one line
(c) Hello, , World, and ! each on separate lines
(d) Hello World on one line, then a blank line, then !
Question 3. What does WriteLn(42:8) produce?
(a) The string 42:8
(b) The number 42 left-justified in an 8-character field
(c) The number 42 right-justified in an 8-character field
(d) The number 42 followed by 8 spaces
Question 4. What is the output of WriteLn(3.14159:10:3)?
(a) 3.142 (right-justified, rounded to 3 decimal places, in a 10-character field)
(b) 3.14159000 (padded to 10 characters with zeros)
(c) 3.142 (left-justified, 3 decimal places)
(d) 3.14159:10:3 (literal text)
Question 5. If a Real variable x contains the value 7.5 and you write WriteLn(x), what happens?
(a) The output is 7.5
(b) The output is 7.50
(c) The output is in scientific notation (e.g., 7.5000000000000E+000)
(d) A compilation error occurs because Real values cannot be printed without formatting
Question 6. What happens if you specify a field width smaller than the value requires? For example, WriteLn(12345:3).
(a) The value is truncated to 3 characters: 123
(b) The value is displayed in full: 12345 (field width is a minimum, not maximum)
(c) A runtime error occurs
(d) The value wraps to the next line
Question 7. Which of the following correctly implements the prompt-read pattern?
(a) WriteLn('Enter name: '); ReadLn(name);
(b) Write('Enter name: '); ReadLn(name);
(c) Write('Enter name: '); Read(name);
(d) Both (b) and (c) are correct
Question 8. What does ReadLn do that Read does not?
(a) ReadLn can read string values; Read can only read numbers.
(b) ReadLn discards the remaining characters on the input line (including the newline); Read does not.
(c) ReadLn displays a prompt to the user; Read does not.
(d) ReadLn validates the input type; Read accepts any input.
Question 9. A program uses Read(c) where c is a Char variable. The user types A and presses Enter. What character does c contain, and what remains in the input buffer?
(a) c contains 'A'; the buffer is empty
(b) c contains 'A'; the newline character remains in the buffer
(c) c contains the newline character; 'A' remains in the buffer
(d) c contains 'A'; the space character remains in the buffer
Question 10. A user types banana when the program expects an integer (ReadLn(n) where n: Integer). What happens?
(a) n is set to 0
(b) n is set to -1 and an error flag is set
(c) The program crashes with runtime error 106 (invalid numeric format)
(d) The string "banana" is stored in n as its ASCII value
Question 11. What is the purpose of the Val procedure?
(a) To validate that a file exists before opening it (b) To convert a string to a numeric type and report whether the conversion succeeded (c) To assign a value to a constant at runtime (d) To check whether a variable has been initialized
Question 12. Given the code Val('12.5abc', amount, code), what are the values of amount and code after execution?
(a) amount = 12.5, code = 0
(b) amount is undefined, code = 4
(c) amount is undefined, code = 5
(d) amount = 12.5, code = 5
Question 13. Which is the recommended approach for reading numeric input from users in an interactive program?
(a) Use ReadLn(numericVariable) directly and handle runtime errors with try-catch
(b) Read the input as a string with ReadLn(str), then convert with Val(str, num, code)
(c) Use Read(numericVariable) because it does not consume the newline
(d) Use {$I-} before every input statement
Question 14. What does WriteLn with no arguments do?
(a) Nothing — it is a no-op (b) Outputs a blank line (just a newline character) (c) Causes a compilation error (d) Outputs the word "nil"
Question 15. In a formatted table, you want all numbers in a column to align on the decimal point. Which approach achieves this?
(a) Use the same :width:decimals specifier for every number in the column
(b) Use Write instead of WriteLn for each number
(c) Pad each number with leading zeros
(d) Convert all numbers to strings before printing
Question 16. What unit provides the ClrScr procedure for clearing the terminal screen in Free Pascal?
(a) SysUtils
(b) System
(c) CRT
(d) Console
Question 17. Why is ReadLn preferred over Read for interactive console programs?
(a) ReadLn is faster than Read
(b) ReadLn can read string types, but Read cannot
(c) ReadLn clears the input buffer after reading, preventing leftover characters from interfering with subsequent input operations
(d) ReadLn automatically validates input types
Question 18. Consider this code:
var a, b: Integer;
begin
Write('Enter a: ');
ReadLn(a);
Write('Enter b: ');
ReadLn(b);
WriteLn('a = ', a:5, ' b = ', b:5, ' sum = ', (a + b):5);
end.
If the user enters 10 for a and 25 for b, what is the output of the last line?
(a) a = 10 b = 25 sum = 35
(b) a = 10 b = 25 sum = 35
(c) a =10 b =25 sum =35
(d) a = 10 b = 25 sum = 35 (with leading spaces to fill 5-character fields)
Answers
-
(b) —
WriteLnappends a newline after the output;Writedoes not. Both can output any type and neither adds spaces between arguments. -
(a) — The two
Writecalls produceHelloon the same line.WriteLn('World')appendsWorldand ends the line.WriteLn('!')prints!on the next line. -
(c) — The
:8specifier right-justifies42in an 8-character field, producing 6 leading spaces followed by42. -
(a) — The value is rounded to 3 decimal places (3.142) and right-justified in a 10-character field. Total characters: 5 (3.142) plus 5 leading spaces = 10.
-
(c) — Without formatting specifiers, Real values are displayed in Pascal's default scientific notation.
-
(b) — The field width is a minimum, not a maximum. If the value needs more characters than the specified width, Pascal prints the full value without truncation.
-
(b) —
Write(notWriteLn) keeps the cursor on the prompt line.ReadLn(notRead) properly consumes the entire input line. This is the correct prompt-read pattern. -
(b) —
ReadLnreads the requested values and then discards everything else on the line, including the newline character.Readleaves the remaining characters (including the newline) in the buffer. -
(b) —
Read(c)reads the character'A'intocbut does not consume the newline that the user typed by pressing Enter. The newline remains in the buffer. -
(c) — Pascal's
ReadLnattempts to convert the input to the variable's declared type. When it fails, the program terminates with runtime error 106 (invalid numeric format). -
(b) —
Val(str, num, code)attempts to convert stringstrto a numeric value innum, and setscodeto 0 on success or to the position of the first invalid character on failure. -
(c) — The conversion fails at position 5 (the letter 'a'). The value of
amountis undefined when the conversion fails;codeis set to 5. -
(b) — Reading as a string first (which never fails) and then converting with
Valis the safest approach. It avoids runtime errors and gives you control over error handling. -
(b) —
WriteLnwith no arguments simply outputs a newline character, producing a blank line in the output. -
(a) — Using the same
:width:decimalsfor all values in a column ensures that every number occupies the same total width and has the same number of decimal places, causing the decimal points to align. -
(c) — The
CRTunit providesClrScr,GotoXY,TextColor, and other console manipulation procedures. This unit originated in Borland's Turbo Pascal. -
(c) —
ReadLndiscards the rest of the line after reading the requested values, preventing leftover characters (especially newline characters) from being consumed by subsequent read operations. -
(d) — Each value is displayed right-justified in a 5-character field.
10becomes10(3 spaces + 2 digits),25becomes25, and35becomes35. The literal strings between them are printed as-is.