Chapter 2

Answers to Exam Preparation Exercises

2.     Dwits.

(a) XYZ                  Invalid - must end with 1, 2, or 3

(b) 123                    Invalid - must start with X, Y, or Z

(c) Xl                      Valid

(d) 23Y                   Invalid - must start with X, Y, or Z

(e) XY12                Valid

(f) Y2Y                   Invalid - must end with 1, 2, or 3

(g) ZY2                   Valid

(h) XY23Xl            Valid

6.     False. Reserved words are just that -- reserved for use by Pascal. They may not be used as variable names.

7.     False.  A block consists of an optional definitions part, followed by an optional declarations part and a compound statement (the definitions part was missing from the question).  See the syntax diagram on page 63.

8.     Two factors that contribute to the readability of programs are well-chosen variable names and well-formatted source code.

9.     A string variable can contain any number of characters from zero to 255.  A variable of type Char can hold a single character.

10.   There are zero characters in a null string.

12.   The identifier Computer is used to name something, such as a program or a variable.  'Computer' is the character string made up of the characters: 'C', 'o', 'm', 'p', 'u', 't', 'e' and 'r'.

14.   Identify the syntax errors in the program:

The first line is missing a terminating semi-colon.

 

The second line, the comment, is missing a closing *)

 

CONSTANT is not the proper keyword for beginning the constant declaration section; it needs to be replaced with the keyword CONST.

 

In the declaration of constant First, the colon needs to be replaced with an equal sign and the string 'Martin' is missing an opening single quote.

 

In the declaration of constant Mid, the colon needs to be replaced with an equal sign and the string 'Luther' is missing an opening single quote.

 

In the declaration of constant Last, the semi-colon needs to be replaced with an equal sign; the string 'King' is missing the opening and closing single quotes; and the declaration is missing a terminating semi-colon.

 

VARIABLES is not the proper keyword for beginning the variable declaration section; it needs to be replaced with the keyword VAR.

 

In both variable declarations the := needs to be replaced with a colon.

 

The variable declaration for Name is terminated with a comma, which needs to be replaced with a semicolon.

 

The variable declaration for Initial declares Initial as type Character which is not defined. The compiler considers Character to be an identifier. Character needs to be replaced with Char.

 

In the assignment statement for Name, the string literals Martin, Luther and King are considered variables, but since they have not been declared this causes an error.  To treat  them as strings each needs a beginning and an ending single quote.

 

In the statement Writeline("Name = ", Name);, Writeline is treated as an undeclared identifier; it should be replaced with the procedural call to Writeln. In addition, the double quotes need to be replaced with single quotes.

 

In the Write statement the semi-colon and the comma are interchanged.

Answers to Programming Warm-Up Exercises

1.     Writeln('Sally S. Student');

3.     Writeln statements to print out values in exercise 2.

Writeln('Make: ', Make);

Writeln('Model: ', Model); 

Writeln('Color: ', Color);

Writeln; 

Writeln('Plate_Type: ', Plate_Type);

Writeln('Class: ', Class);

5.     Change program example by adding the following declaration to the variable declarations:

First_Mid_Last: String;

Add the following two statements:

First_Mid_Last := First + ' ' + Middle + '. ' + Last;

Writeln('Name in first-initial-last is ', First_Mid_Last);

6.     This is a laboratory exercise for the student and requires no answer here.