Chapter 37 Self-Assessment Quiz: Interfacing with the Operating System

Test your understanding of OS interfaces in Pascal. Try to answer each question before revealing the answer.


Section 1: Multiple Choice

Q1. Which function reads an environment variable in Free Pascal?

(a) ReadEnv('PATH') (b) GetEnvironmentVariable('PATH') (c) Env['PATH'] (d) System.GetEnv('PATH')

Answer (b) GetEnvironmentVariable from the SysUtils unit reads environment variables. It returns an empty string if the variable is not set.

Q2. What does TProcess with poWaitOnExit in its Options do?

(a) Terminates the process after a timeout (b) Blocks the calling program until the external process finishes (c) Starts the process in the background (d) Waits for user input before starting

Answer (b) The poWaitOnExit option causes TProcess.Execute to block until the launched process terminates. Without it, Execute returns immediately while the process runs in the background.

Q3. Which compiler directive detects that the code is being compiled for Linux?

(a) {$IF PLATFORM = 'LINUX'} (b) {$DEFINE LINUX} (c) {$IFDEF LINUX} (d) {$IFDEF UNIX_LINUX}

Answer (c) {$IFDEF LINUX} tests whether the LINUX symbol is defined, which is automatically set by the Free Pascal compiler when targeting Linux. {$IFDEF UNIX} is also true on Linux (and other Unix-like systems).

Q4. When calling a C library function from Pascal, the cdecl keyword specifies:

(a) That the function is thread-safe (b) That the function uses the C calling convention (c) That the function is defined in a DLL (d) That the function returns a C string

Answer (b) cdecl specifies the C calling convention: arguments pushed right-to-left, caller cleans up the stack. Without it, Pascal uses its own calling convention, which would corrupt the stack when calling C functions.

Q5. On Windows, application data (per-user settings) is conventionally stored in:

(a) C:\Program Files\AppName\ (b) C:\Windows\System32\ (c) %APPDATA%\AppName\ (d) %TEMP%\AppName\

Answer (c) %APPDATA% (typically C:\Users\Username\AppData\Roaming) is the standard location for per-user application data on Windows. Program Files is for installed executables, System32 is for system files, and TEMP is for temporary files.

Q6. What does FindClose(SR) do after a FindFirst/FindNext loop?

(a) Closes the file that was found (b) Releases the search handle and associated OS resources (c) Deletes the found files (d) Resets the search to the beginning

Answer (b) FindClose releases the search handle allocated by FindFirst. Failing to call it causes a resource leak — the OS search handle remains open until the program terminates.

Q7. To dynamically load a library at runtime, you use:

(a) external 'libname' (b) LoadLibrary and GetProcAddress from the DynLibs unit (c) {$LINKLIB libname} (d) uses LibraryName

Answer (b) The DynLibs unit provides LoadLibrary (loads the library), GetProcAddress (gets a function pointer), and UnloadLibrary (releases the library). This allows runtime discovery of libraries that may or may not be installed.

Section 2: True/False

Q8. {$IFDEF WINDOWS} code blocks are evaluated at runtime, allowing a single executable to run on both Windows and Linux.

Answer False. Conditional compilation is evaluated at compile time. Code inside a false {$IFDEF} block is completely excluded from the executable. You must compile separately for each platform.

Q9. The /proc filesystem on Linux contains real files stored on disk.

Answer False. /proc is a virtual filesystem — the files are generated dynamically by the kernel when you read them. They do not exist on disk and take no disk space. They provide a text interface to kernel and process information.

Q10. ParamStr(0) returns the first command-line argument passed by the user.

Answer False. ParamStr(0) returns the path to the executable itself. User-supplied arguments start at ParamStr(1). ParamCount returns the number of user-supplied arguments (not counting the executable).

Section 3: Short Answer

Q11. Write a function that opens a URL in the user's default web browser, working on Windows, Linux, and macOS. Use conditional compilation and TProcess.

Answer
procedure OpenURL(const URL: string);
var P: TProcess;
begin
  P := TProcess.Create(nil);
  try
    {$IFDEF WINDOWS}
    P.Executable := 'cmd.exe';
    P.Parameters.Add('/c'); P.Parameters.Add('start'); P.Parameters.Add(URL);
    {$ENDIF}
    {$IFDEF LINUX}
    P.Executable := 'xdg-open'; P.Parameters.Add(URL);
    {$ENDIF}
    {$IFDEF DARWIN}
    P.Executable := 'open'; P.Parameters.Add(URL);
    {$ENDIF}
    P.Options := [poNoConsole];
    P.Execute;
  finally
    P.Free;
  end;
end;
Windows uses 'start' (via cmd), Linux uses 'xdg-open', macOS uses 'open'. poNoConsole prevents a console window from flashing.

Q12. Explain why ForceDirectories is preferable to CreateDir when creating application data directories.

Answer CreateDir creates only the final directory in the path — it fails if any parent directory does not exist. ForceDirectories creates the entire directory tree, including all missing parent directories. For application data paths like ~/.config/pennywise/data/, ForceDirectories will create .config, pennywise, and data if any are missing. This is essential because you cannot assume any intermediate directories exist on a fresh installation.