Chapter 27 Self-Assessment Quiz: Introduction to Lazarus

Test your understanding of the concepts introduced in Chapter 27. Try to answer each question before revealing the answer.


Section 1: Multiple Choice

Q1. Lazarus is best described as:

(a) A commercial IDE for Delphi development (b) A free, open-source RAD IDE for Free Pascal (c) A web-based code editor for Pascal (d) A text-mode IDE similar to Turbo Pascal

Answer (b) Lazarus is a free, open-source Rapid Application Development IDE that uses the Free Pascal compiler. It provides a visual form designer, component palette, and the LCL (Lazarus Component Library) for building cross-platform GUI applications.

Q2. In the event-driven programming model, what does the application do most of the time?

(a) Executes code from top to bottom (b) Waits in the event loop for messages from the operating system (c) Continuously polls the keyboard and mouse for input (d) Runs all event handlers simultaneously in separate threads

Answer (b) The application spends most of its time waiting in the event loop (message loop) for the operating system to deliver messages about user actions or system events. When a message arrives, the loop dispatches it to the appropriate handler.

Q3. Which file in a Lazarus project contains the visual layout of a form?

(a) .lpr (b) .pas (c) .lfm (d) .lpi

Answer (c) The .lfm file (Lazarus Form File) contains the visual layout: component positions, sizes, properties, and event connections. The .pas file contains the Pascal source code, the .lpr is the main project file, and .lpi contains project configuration.

Q4. What is the Sender parameter in an event handler like procedure TForm1.Button1Click(Sender: TObject)?

(a) The form that owns the button (b) The component that triggered the event (c) The previous event handler that ran (d) The message loop that dispatched the event

Answer (b) The Sender parameter identifies the component that triggered the event. This is especially useful when multiple components share the same event handler — you can use Sender to determine which one was clicked.

Q5. What happens if an event handler takes 10 seconds to complete?

(a) The application runs the handler in a background thread automatically (b) The application remains responsive because events are processed in parallel (c) The application appears frozen — it cannot repaint or respond to user input (d) The operating system cancels the handler after a timeout

Answer (c) Event handlers run on the main thread, blocking the message loop. While a handler is executing, no other events can be processed — the application cannot repaint, respond to clicks, or handle keyboard input. The OS may display a "Not Responding" indicator.

Q6. The LCL achieves cross-platform compatibility through:

(a) Drawing all controls from scratch using pixel-level rendering (b) A widgetset architecture that maps LCL calls to native platform toolkits (c) Using Java's Swing library as a backend (d) Compiling to WebAssembly and running in a browser

Answer (b) The LCL uses widgetsets — platform-specific layers that translate LCL component calls into native widgets. On Windows, the win32 widgetset creates Windows API controls. On macOS, the cocoa widgetset creates Cocoa controls. On Linux, gtk2 or qt5 widgetsets create GTK or Qt controls.

Q7. Which LCL class is the base for controls that have their own operating system window handle and can receive keyboard focus?

(a) TGraphicControl (b) TWinControl (c) TComponent (d) TPersistent

Answer (b) TWinControl is the base class for controls with their own OS window handle (HWND on Windows). These controls can receive keyboard focus and process keyboard messages. TGraphicControl is lighter — it draws on its parent's surface and cannot receive focus. Examples: TEdit is a TWinControl (users type in it), TLabel is a TGraphicControl (it just displays text).

Section 2: True or False

Q8. Lazarus applications require the Free Pascal runtime library to be installed on the target machine.

Answer False. Free Pascal compiles to native machine code and statically links the runtime library by default. The resulting executable is standalone — no runtime, interpreter, or virtual machine needs to be installed on the target machine. This is one of Pascal's key advantages: the compiled binary is self-contained.

Q9. The .lfm form file uses a binary format that cannot be edited by hand.

Answer False. The .lfm file uses a human-readable text format. You can view and edit it by right-clicking the form and choosing "View Source (.lfm)." The format is straightforward: nested object blocks with property assignments. While you typically use the visual designer, hand-editing is possible.

Q10. In a Lazarus application, the Application.Run call in the .lpr file starts the event loop.

Answer True. Application.Run enters the main message loop, which processes events until the application terminates. Before this call, the application initializes and creates forms. After this call returns (when the user closes the application), the program ends.

Section 3: Short Answer

Q11. Explain the difference between a console program's ReadLn and a GUI program's event handler as mechanisms for receiving user input. How is the flow of control different?

Answer In a console program, ReadLn blocks execution at a specific point in the code — the program pauses, waits for the user to type something, and then continues from that exact point. The programmer controls where input happens. In a GUI program, event handlers do not block — they are called by the message loop when the user performs an action. The programmer does not control when the user will click, type, or interact. The flow of control is inverted: in console programs, the program calls for input; in GUI programs, input calls the program.

Q12. You have a form with a TButton named btnSave and a TEdit named edtFilename. Write the event handler that shows a message dialog saying "File saved: " followed by the text from the edit field. Use ShowMessage.

Answer
procedure TForm1.btnSaveClick(Sender: TObject);
begin
  ShowMessage('File saved: ' + edtFilename.Text);
end;

Q13. Why does the chapter emphasize separating business logic from UI code? Give one concrete disadvantage of putting all logic directly inside event handlers.

Answer Separating business logic from UI code makes the logic reusable, testable, and independent of the presentation layer. If validation logic is inside Button1Click, you cannot reuse that validation when you add a keyboard shortcut, import from a file, or write unit tests. You also cannot change the UI (e.g., switch from a string grid to a list view) without rewriting the logic. One concrete disadvantage: if the expense total calculation is inside the click handler, and you later add a "Load from File" feature, you must duplicate the calculation logic or extract it — work that would have been unnecessary if it had been separated from the start.

Section 4: Predict the Output

Q14. Given the following event handler, what appears in Label1 after the user clicks Button1 three times?

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Label1.Caption = '' then
    Label1.Caption := '1'
  else
    Label1.Caption := IntToStr(StrToInt(Label1.Caption) * 2);
end;

Assume Label1.Caption starts as an empty string.

Answer After click 1: Label1 shows "1" (empty string branch taken).
After click 2: Label1 shows "2" (1 * 2 = 2).
After click 3: Label1 shows "4" (2 * 2 = 4).
The label doubles with each click after the first.

Q15. Two buttons share the same event handler:

procedure TForm1.SharedClick(Sender: TObject);
begin
  Label1.Caption := Label1.Caption + (Sender as TButton).Name;
end;

If the user clicks Button1, then Button2, then Button1, what is Label1.Caption? (Assume it starts empty.)

Answer "Button1Button2Button1" — each click appends the Name property of the clicked button (not the Caption, but the Name). The as operator safely casts Sender to TButton, allowing access to the Name property.