Quiz — Chapter 16: Introduction to Object-Oriented Programming

Instructions: Select the best answer for each question. Each question has exactly one correct answer.


Question 1. What is the relationship between a class and an object?

A) A class is a specific instance; an object is the blueprint. B) A class is a blueprint; an object is a specific instance created from that blueprint. C) Classes and objects are interchangeable terms for the same concept. D) A class exists in memory; an object exists only in source code.


Question 2. In Object Pascal, what happens when you declare a variable of a class type?

var
  Student: TStudent;

A) An object is created on the stack with default field values. B) An object is created on the heap and assigned to Student. C) A reference variable is created, initialized to nil — no object exists yet. D) The compiler allocates memory for all the class's fields on the stack.


Question 3. Which of the following correctly creates an object and ensures it is freed?

A)

var S: TStudent;
begin
  S := TStudent.Create('Alice', 90);
  S.Display;
  S.Free;
end;

B)

var S: TStudent;
begin
  S := TStudent.Create('Alice', 90);
  try
    S.Display;
  finally
    S.Free;
  end;
end;

C) Both A and B create and free the object, but B is the recommended pattern because it ensures cleanup even if an exception occurs.

D) Neither — you do not need to free objects in Pascal.


Question 4. What is the conventional name for a constructor in Object Pascal?

A) Init B) New C) Create D) Build


Question 5. What should the first statement in most constructors be?

A) Self := nil; B) inherited Create; C) Free; D) Initialize;


Question 6. What should the last statement in a destructor be?

A) inherited Create; B) Self.Free; C) inherited Destroy; D) Dispose(Self);


Question 7. What does the private visibility specifier mean in Free Pascal's default mode?

A) Accessible only within the class itself — nowhere else. B) Accessible within the class and within the same unit, but not from other units. C) Accessible within the class and all its descendants. D) Accessible from anywhere in the program.


Question 8. Which visibility specifier would you use for a field that should be accessible to descendant classes but not to unrelated code in other units?

A) private B) public C) protected D) strict private


Question 9. What is the output of this program?

var
  A, B: TCounter; { TCounter has FValue: Integer, Increment adds 1 }
begin
  A := TCounter.Create(0);
  B := A;
  A.Increment;
  B.Increment;
  WriteLn(A.Value);
  A.Free;
end.

A) 0 B) 1 C) 2 D) Compile error


Question 10. What is the fundamental difference between record assignment and class assignment?

A) Records are assigned by reference; classes are assigned by value. B) Records are assigned by value (data is copied); classes are assigned by reference (only the pointer is copied). C) There is no difference — both copy all data. D) Records cannot be assigned; classes can.


Question 11. What is a property in Object Pascal?

A) Another name for a public field. B) A member that provides controlled access to data, potentially using getter and setter methods behind a field-like syntax. C) A constant value defined within a class. D) A method that takes no parameters.


Question 12. Given this property declaration, what happens when external code writes Student.Grade := 105?

property Grade: Integer read FGrade write SetGrade;

procedure TStudent.SetGrade(AValue: Integer);
begin
  if (AValue < 0) or (AValue > 100) then
    raise Exception.Create('Invalid grade');
  FGrade := AValue;
end;

A) FGrade is set to 105. B) SetGrade(105) is called, which raises an exception because 105 > 100. C) The compiler rejects the assignment at compile time. D) FGrade is silently clamped to 100.


Question 13. Which of these correctly declares a read-only property?

A) property Name: string read FName write FName; B) property Name: string read FName; C) property Name: string write FName; D) property Name: string read GetName write SetName;


Question 14. What is Self in the context of an instance method?

A) A reference to the class definition. B) An implicit reference to the object on which the method was called. C) A reference to the parent class. D) A keyword that creates a new instance of the current class.


Question 15. What is wrong with this code?

var
  A, B: TStudent;
begin
  A := TStudent.Create('Alice', 90);
  B := TStudent.Create('Bob', 85);
  A := B;
  A.Free;
  B.Free;
end.

A) Nothing — this is correct. B) After A := B, the original Alice object is leaked (never freed), and then A.Free and B.Free both try to free the same Bob object (double free). C) A := B copies all of Bob's data into Alice's object. D) The compiler will not allow assigning one class variable to another.


Question 16. What does TStudent = class implicitly mean in Object Pascal?

A) TStudent is a record type. B) TStudent is a class that descends from TObject. C) TStudent is an abstract class that cannot be instantiated. D) TStudent is a standalone class with no parent.


Question 17. Which of the following is NOT a benefit of encapsulation?

A) External code cannot put an object into an invalid state. B) The internal implementation can change without affecting code that uses the class. C) Programs run faster because private fields use less memory. D) Classes enforce their own invariants through validation in methods and setters.


Question 18. What is the difference between a class method and an instance method?

A) A class method can access instance fields; an instance method cannot. B) A class method belongs to the class itself and does not need an object instance; an instance method operates on a specific object. C) Instance methods are always private; class methods are always public. D) There is no difference — the terms are synonymous.


Question 19. Why is it important that TBudget.Destroy frees FExpenses before calling inherited Destroy?

A) It is not important — the order does not matter. B) Because inherited Destroy deallocates the object's memory, so after it runs, FExpenses would be inaccessible. You must clean up owned resources first, then let the parent class clean up its own. C) Because inherited Destroy sets all fields to zero. D) Because the compiler requires it syntactically.


Question 20. A procedural programmer says: "I can do everything with records and procedures that you can do with classes and methods." Is this true?

A) Yes — OOP is purely syntactic sugar and adds no capabilities. B) Technically yes (any computation can be expressed procedurally), but OOP provides organizational benefits: encapsulation, data protection, composability, and extensibility through inheritance and polymorphism, making large programs much easier to design, maintain, and extend. C) No — there are computations that classes can perform that procedural code cannot. D) No — records cannot store the same types of data that class fields can.


Answer Key

  1. B — A class is a blueprint that defines fields and methods; an object is a concrete instance created from that blueprint, occupying memory at runtime.

  2. C — Declaring a class variable creates a reference initialized to nil. No object exists until you call the constructor.

  3. C — Both create and free the object. A works in the happy path, but B is recommended because try...finally guarantees cleanup even if Display raises an exception.

  4. C — By convention, Pascal constructors are named Create. This is not enforced by the language, but it is the universal convention in the Object Pascal ecosystem.

  5. Binherited Create calls the parent class's constructor, ensuring the base TObject infrastructure is initialized.

  6. Cinherited Destroy should be the last statement in a destructor, called after you have cleaned up your own resources.

  7. B — In Free Pascal's default mode, private means accessible within the class and within the same unit. Use strict private for class-only access.

  8. Cprotected members are accessible to the class itself and any descendant classes.

  9. CB := A copies the reference, so both A and B point to the same object. Two increments (one via A, one via B) result in Value = 2.

  10. B — Record assignment copies all data (value semantics). Class assignment copies only the reference (reference semantics), so both variables point to the same object.

  11. B — A property provides controlled access to data through getter/setter methods while presenting a field-like syntax to calling code.

  12. B — Writing to the Grade property calls SetGrade(105), which raises an exception because 105 exceeds the valid range.

  13. B — A property with only a read clause and no write clause is read-only. External code can read the value but cannot assign to it.

  14. BSelf is an implicit parameter referencing the object on which the current method was invoked.

  15. BA := B overwrites A's reference to Alice (leaking Alice) and makes A point to Bob. Then A.Free and B.Free both free the same object — a double free error.

  16. B — Every class in Object Pascal implicitly descends from TObject unless another parent class is specified.

  17. C — Encapsulation does not affect memory usage or performance. Its benefits are about safety, maintainability, and abstraction, not speed.

  18. B — A class method is called on the class itself (TMyClass.MyMethod) and cannot access instance fields. An instance method is called on an object and operates on that object's data.

  19. B — After inherited Destroy runs, the object's memory may be in the process of being deallocated. You must free owned resources before calling inherited Destroy.

  20. B — Procedural code is Turing-complete and can express any computation. But OOP provides organizational and structural advantages that make complex programs dramatically easier to manage. The benefit is in design, not in computational capability.