Chapter 2 Exercises

Part A: Conceptual Review

Exercise 2.1 — Explain the difference between a compiler and an interpreter. Give one advantage of each approach.

Exercise 2.2 — List the three stages of the compilation pipeline in order. What is the input and output of each stage?

Exercise 2.3 — What is the role of the linker? Why can't the compiler produce a complete executable by itself?

Exercise 2.4 — In your own words, explain what the PATH environment variable does and why it matters for using Free Pascal from the command line.

Exercise 2.5 — A friend tells you: "Pascal is a waste of time because you have to compile the program every time you change it. In Python, you just run the file." Write a brief response explaining what advantages the compilation step provides.

Exercise 2.6 — What is the difference between WriteLn and Write? (Hint: try replacing WriteLn with Write in the Hello World program and observe the output.)

Exercise 2.7 — Why does every Pascal program end with end. (period) rather than end; (semicolon)?

Exercise 2.8 — What is a "uses clause" in a Pascal program? Why did we include uses SysUtils; in the PennyWise shell even though the program does not strictly need it?


Part B: Predict the Output

For each program below, predict what will be displayed on the screen. Then type the program, compile it, and check your prediction.

Exercise 2.9

program Predict1;
begin
  WriteLn('Line 1');
  WriteLn('Line 2');
  WriteLn('Line 3');
end.

Exercise 2.10

program Predict2;
begin
  WriteLn('A');
  WriteLn;
  WriteLn('B');
  WriteLn;
  WriteLn;
  WriteLn('C');
end.

Exercise 2.11

program Predict3;
begin
  Write('Hello ');
  Write('World');
  WriteLn('!');
  WriteLn('Done.');
end.

Exercise 2.12

program Predict4;
begin
  WriteLn('2 + 2 = ', 2 + 2);
  WriteLn('10 * 5 = ', 10 * 5);
  WriteLn('100 / 4 = ', 100 div 4);
end.

Exercise 2.13

program Predict5;
begin
  WriteLn('She said, ''Hello!''');
end.

(Hint: Two single quotes in a row inside a string produce a literal single-quote character.)


Part C: Setup Verification (Hands-On)

These exercises verify that your development environment is correctly configured. Complete each one and record the result.

Exercise 2.14 — Open a terminal and run fpc -v. Copy the output. What version of Free Pascal is installed? What target platform does it report?

Exercise 2.15 — Create a file called verify.pas with the following content:

program Verify;
begin
  WriteLn('Free Pascal is working!');
  WriteLn('Compiled on: ', {$I %DATE%}, ' at ', {$I %TIME%});
end.

Compile it with fpc verify.pas. Run the resulting executable. Record the output. Then delete the .o file and run the executable again to verify that the executable does not need the object file to run.

Exercise 2.16 — Open Lazarus. Create a new Simple Program project. Replace the default code with:

program LazVerify;
begin
  WriteLn('Lazarus IDE is working!');
  ReadLn;
end.

Save the project and press F9 to compile and run. Verify that the output appears in a terminal window.

Exercise 2.17 — Using the command line, navigate to the directory containing your compiled verify executable. List the files in the directory and record: 1. The size of verify.pas (the source file) 2. The size of verify.exe / verify (the executable) 3. The ratio of executable size to source size

How much larger is the executable than the source? Why is it so much bigger? (Hint: Think about what the linker adds.)

Exercise 2.18 — Deliberately introduce an error into a program. Create broken.pas:

program Broken;
begin
  WriteLn('This line is fine.');
  WriteLn('This line has no closing quote);
  WriteLn('This line will never be reached.');
end.

Try to compile it with fpc broken.pas. Record the exact error message. What line and column does the compiler report? Fix the error and compile again.


Part D: Exploration and Experimentation

Exercise 2.19 — Write a program called about_me.pas that displays at least five lines of information about yourself (name, age, favorite color, where you are from, why you are learning Pascal). Use WriteLn for each line. Compile and run it.

Exercise 2.20 — Write a program called box.pas that draws a box using ASCII characters:

+------------------+
|                  |
|  Pascal is fun!  |
|                  |
+------------------+

Use WriteLn for each line of the box. The box must be at least 20 characters wide.

Exercise 2.21 — Write a program called math_demo.pas that uses WriteLn to display the results of several arithmetic operations:

5 + 3 = 8
10 - 4 = 6
6 * 7 = 42
20 div 3 = 6
20 mod 3 = 2

Use Pascal's arithmetic operators: +, -, *, div (integer division), and mod (remainder). For example: WriteLn('5 + 3 = ', 5 + 3);

Exercise 2.22 — Modify the PennyWise shell from Section 2.9 to include your name as the "developer" in the welcome banner. Add a line that says:

Developer: [Your Name]

Recompile and run to verify.

Exercise 2.23 — Create a program called multiline_comment.pas that demonstrates all three comment styles:

program MultilineComment;
{ This is a brace comment }
(* This is a parenthesis-asterisk comment *)
// This is a line comment
begin
  WriteLn('All three comment styles work!');
end.

Compile and verify that comments are truly ignored by the compiler. Then try nesting comments: put a { } comment inside another { } comment. What happens? Try putting a (* *) comment inside a { } comment. What happens? Record your findings.


Part E: Challenge Problems

Exercise 2.24 — Write a program called ruler.pas that prints a 40-character ruler:

1234567890123456789012345678901234567890
|    |    |    |    |    |    |    |
0    5   10   15   20   25   30   35  40

This tests your ability to align output carefully. (You may need to experiment with spacing.)

Exercise 2.25 — Research: Run fpc -h (or fpc --help) from the command line. This displays all available compiler options. Find and write down the flags that: 1. Set the optimization level 2. Generate debugging information 3. Change the output file name 4. Show all warnings

You do not need to understand all of these yet — but knowing they exist gives you a preview of the compiler's capabilities.

Exercise 2.26 — Write a program called triangle.pas that prints a right triangle using asterisks:

*
**
***
****
*****
******
*******

You must do this using only WriteLn statements (no loops — we have not learned those yet). How many WriteLn calls did you need? In Chapter 6, you will solve this same problem with a loop that takes only three lines.

Exercise 2.27 — Create a program called system_info.pas that uses compiler macros to display information about your system:

program SystemInfo;
begin
  WriteLn('Free Pascal Version: ', {$I %FPCVERSION%});
  WriteLn('Target OS: ', {$I %FPCTARGETOS%});
  WriteLn('Target CPU: ', {$I %FPCTARGETCPU%});
  WriteLn('Date compiled: ', {$I %DATE%});
  WriteLn('Time compiled: ', {$I %TIME%});
end.

Compile and run this program. Then compile it again an hour later and run the new executable alongside the old one. Do they produce different output? Why or why not? (Hint: Think about when the macros are evaluated — compile time or run time?)


Solutions to Selected Exercises

Exercise 2.11 Solution:

Hello World!
Done.

The first two Write calls print on the same line (no newline appended). The WriteLn prints ! and then moves to a new line. Then Done. appears on the next line.

Exercise 2.12 Solution:

2 + 2 = 4
10 * 5 = 50
100 / 4 = 25

WriteLn evaluates the arithmetic expressions and prints the results. div performs integer division (no decimal point).

Exercise 2.13 Solution:

She said, 'Hello!'

Inside a Pascal string, two consecutive single quotes ('') represent a single literal quote character.

Exercise 2.17 Hint: The source file is typically a few hundred bytes. The executable is 150-400 KB. The executable is hundreds of times larger because the linker includes the runtime library code — startup/shutdown procedures, I/O handling, string management, and operating system interface code — even though our program only uses WriteLn.

Exercise 2.27 Hint: The compiler macros are evaluated at compile time. Both executables will show the same system info (OS, CPU, FPC version) but different compilation dates/times. If you run either executable at any later time, it still shows the date/time it was compiled, not the date/time it is being run. This is a concrete demonstration of the difference between compile time and run time.