Chapter 10 Quiz: Strings — Text Processing in Pascal

Instructions. Select the best answer for each question. Each question has exactly one correct answer unless stated otherwise.


Q1. What is the maximum number of characters a ShortString can hold?

A) 127 B) 255 C) 256 D) 65,535


Q2. With the compiler directive {$H+} enabled, the plain String type maps to:

A) ShortString B) AnsiString C) UnicodeString D) PChar


Q3. What does the following code output?

var s: String;
begin
  s := 'Hello';
  WriteLn(s[1], s[5]);
end.

A) He B) Ho C) el D) lo


Q4. What value does Pos('xyz', 'Hello, world!') return?

A) -1 B) 0 C) 1 D) An error is raised


Q5. Given s := 'Programming';, what is the value of Copy(s, 4, 4)?

A) 'gram' B) 'gra' C) 'ramm' D) 'ogra'


Q6. Which of the following is a procedure (modifies the string variable in place) rather than a function (returns a new string)?

A) Copy B) Pos C) Delete D) UpperCase


Q7. What does this code output?

var s: String;
begin
  s := 'abcdef';
  Delete(s, 3, 2);
  WriteLn(s);
end.

A) 'abef' B) 'adef' C) 'abcf' D) 'abcdef'


Q8. After executing the following code, what is the value of s?

var s: String;
begin
  s := 'Hello World';
  Insert('Beautiful ', s, 7);
end.

A) 'Hello Beautiful World' B) 'Beautiful Hello World' C) 'Hello WBeautiful orld' D) 'Hello Beautiful World'


Q9. What is the result of UpperCase('Hello 123!')?

A) 'HELLO 123!' B) 'HELLO123' C) An error — numbers cannot be uppercased D) 'hello 123!'


Q10. What does Ord('A') return?

A) 1 B) 41 C) 65 D) 97


Q11. Which expression correctly converts the character '7' to the integer 7?

A) Integer('7') B) Ord('7') C) Ord('7') - Ord('0') D) Chr(7)


Q12. What is the output of the following code?

var n: Integer; code: Integer;
begin
  Val('42abc', n, code);
  WriteLn(n, ' ', code);
end.

A) 42 0 B) 42 3 C) 0 1 D) 42 5


Q13. Which function from SysUtils provides a default value when string-to-integer conversion fails?

A) StrToInt B) StrToIntDef C) Val D) IntToStr


Q14. What is the value of the expression 'Banana' < 'apple'?

A) True — because 'B' (66) < 'a' (97) B) False — because 'B' comes after 'a' alphabetically C) True — because 'Banana' is shorter D) This causes a compilation error


Q15. In the AnsiString implementation, what happens when you write t := s; where both are AnsiString?

A) All characters are copied to a new buffer B) The reference count is incremented and both variables point to the same buffer C) A compile-time error occurs D) The string is converted to ShortString first


Q16. Consider the following CSV line: 'John,"Smith, Jr.",42'. A correct CSV parser should split this into:

A) 'John', '"Smith', ' Jr."', '42' B) 'John', 'Smith, Jr.', '42' C) 'John', 'Smith', ' Jr.', '42' D) 'John,"Smith', ' Jr."', '42'


Q17. What is the output of Format('Value: %8.2f', [3.14])?

A) 'Value: 3.14' B) 'Value: 3.14' C) 'Value: 3.140000' D) 'Value: 00003.14'


Q18. In the Crypts of Pascalia command parser, the NormalizeInput function performs three operations. Which is NOT one of them?

A) Trimming leading and trailing whitespace B) Converting to lowercase C) Removing all punctuation D) Collapsing multiple spaces to single spaces


Q19. What does the wildcard pattern '*.pas' match when tested with WildcardMatch('*.pas', text)?

A) Only the exact string '*.pas' B) Any string that ends with '.pas' C) Any string containing '.pas' somewhere D) Any string that starts with '.' and ends with 'pas'


Q20. Which of the following best describes why Val is preferred over StrToInt when parsing data from files?

A) Val is faster B) Val reports the exact position of the first invalid character, allowing precise error messages C) Val works with floating-point numbers while StrToInt does not D) Val automatically trims whitespace


Answer Key

  1. BShortString stores up to 255 characters. Byte 0 holds the length, bytes 1–255 hold the characters, for 256 bytes total storage.

  2. B{$H+} makes the unqualified String type an alias for AnsiString (heap-allocated, dynamically sized, reference-counted).

  3. Bs[1] is 'H' and s[5] is 'o', so the output is Ho.

  4. BPos returns 0 when the substring is not found.

  5. ACopy('Programming', 4, 4) extracts 4 characters starting at position 4: 'g', 'r', 'a', 'm' = 'gram'.

  6. CDelete is a procedure that modifies the string in place. Copy, Pos, and UpperCase are functions that return values without modifying the original.

  7. ADelete(s, 3, 2) removes 2 characters starting at position 3 ('c' and 'd'), leaving 'abef'.

  8. AInsert('Beautiful ', s, 7) inserts at position 7 (where 'W' was), shifting 'World' to the right.

  9. AUpperCase converts lowercase letters to uppercase and leaves all other characters (digits, punctuation) unchanged.

  10. C — The ASCII/ordinal value of 'A' is 65.

  11. COrd('7') - Ord('0') = 55 - 48 = 7. This is the standard technique for converting a digit character to its numeric value.

  12. BVal parses '42' successfully (n = 42) but encounters 'a' at position 3, so code = 3. Note: n receives the value parsed before the error.

  13. BStrToIntDef returns a specified default value if the conversion fails, rather than raising an exception.

  14. A — String comparison is lexicographic based on ordinal values. 'B' has ordinal 66, 'a' has ordinal 97. Since 66 < 97, 'Banana' < 'apple' is True.

  15. BAnsiString uses reference counting. Assignment increments the reference count; actual copying only occurs when one copy is modified (copy-on-write).

  16. B — A correct CSV parser recognizes quoted fields. The quotes around Smith, Jr. indicate that the comma inside is part of the field value, not a delimiter.

  17. B%8.2f means total width 8 characters, 2 decimal places. 3.14 occupies 4 characters, right-justified in 8 gives 4 leading spaces: ' 3.14'. With 'Value: ' prefix, the full output is 'Value: 3.14'.

  18. CNormalizeInput trims whitespace, lowercases, and collapses multiple spaces. It does not remove punctuation.

  19. B — The * wildcard matches any sequence of characters (including none). So '*.pas' matches any string ending with '.pas'.

  20. BVal sets the code parameter to the position of the first invalid character (or 0 on success), enabling precise error reporting. StrToInt raises an exception on failure with less positional detail.