DP Computer Science · HL / SL · B3 Object-oriented programming

B3.1 Fundamentals of OOP for a single class

Get started
Notes Quiz
Free preview 2/15
  1. Question 1

    A `LibraryBook` class is defined in Java with two private instance variables: `title` (String) and `pageCount` (int). Which of the following correctly instantiates a `LibraryBook` object named `novel` with the title `"Dune"` and 412 pages?
    No clue? Show me the answer
    Correct answerCorrect!Incorrect
    ALibraryBook novel = new LibraryBook("Dune", 412);

    Step-by-step walkthrough

    Choose a solution method

    Method #1Approach 1

    Step 1: Recall Java instantiation syntax

    In Java, creating an object requires the new keyword followed by the constructor call. The general pattern is: ClassName variableName = new ClassName(arguments);

    Step 2: Apply the pattern to the scenario

    Substituting the class name LibraryBook, variable name novel, and arguments "Dune" (String) and 412 (int), we get: LibraryBook novel = new LibraryBook("Dune", 412);

    Step 3: Confirm the correct answer

    This matches the first option exactly. The type declaration LibraryBook appears on the left, followed by the variable name, then the new keyword and constructor call on the right.

    Method #2Approach 2

    Step 1: Identify what is being tested

    The question tests knowledge of Java object instantiation syntax, specifically the use of the new keyword and correct variable declaration format.

    Step 2: Eliminate option B

    novel = LibraryBook("Dune", 412); omits both the type declaration (LibraryBook) on the left and the new keyword. This is Python syntax, not valid Java.

    Step 3: Eliminate option C

    new LibraryBook novel = LibraryBook("Dune", 412); incorrectly places new before the type declaration and uses the constructor call without new on the right — this is not valid Java syntax.

    Step 4: Eliminate option D

    LibraryBook("Dune", 412) = novel; reverses the assignment direction and omits new. Assignments in Java flow from right to left, and this is not valid syntax.

    Step 5: Select the correct answer

    Only LibraryBook novel = new LibraryBook("Dune", 412); uses the correct Java pattern: type declaration, variable name, =, new, constructor call.

  2. Question 2

    In the context of OOP, what is the primary purpose of a constructor?
    No clue? Show me the answer
    Correct answerCorrect!Incorrect
    BTo initialise the instance variables of an object when it is created

    Step-by-step walkthrough

    Choose a solution method

    Method #1Approach 1

    Step 1: Recall the definition of a constructor

    A constructor is a special method that is automatically called at the moment an object is instantiated. Its sole purpose is to set the initial state of the object by assigning values to its instance variables.

    Step 2: Apply the definition to the options

    The constructor does not define behaviours (that is the role of regular methods), does not allocate memory for the class, and does not copy objects. It specifically initialises attributes.

    Step 3: Confirm the correct answer

    "To initialise the instance variables of an object when it is created" directly matches the definition of a constructor.

    Method #2Approach 2

    Step 1: Identify what is being tested

    This question tests understanding of the role of a constructor in OOP, distinguishing it from other class components.

    Step 2: Eliminate option A

    "To define the methods that an object can perform" describes the purpose of regular method definitions within a class body, not the constructor specifically.

    Step 3: Eliminate option C

    "To declare the class and allocate memory for the class definition" describes the class declaration itself. Importantly, no memory is allocated for data when a class is merely defined — memory is allocated when objects are instantiated.

    Step 4: Eliminate option D

    "To return a copy of an existing object with the same attribute values" describes a copy constructor concept, which is a specialised pattern — not the general primary purpose of a constructor.

    Step 5: Select the correct answer

    "To initialise the instance variables of an object when it is created" is the only option that accurately describes a constructor's primary role.

Free preview

13 more questions in this topic

Next topic →B3.2 Fundamentals of OOP for multiple classes (HL only)
Koncepts

Learn it properly. Then practise like it's the real paper.

Start free

Features

  • Lessons
  • Past papers
  • Library
  • Homework Help
  • Duels

More

  • For parents
  • Compare
  • Plans & pricing
  • DP for students

Legal

  • Privacy
  • Terms
  • Account deletion

© 2026 Koncepts (product of PrepAiro, Inc). All rights reserved.
DP, IB, EE and TOK are terms of the International Baccalaureate Organization.

Made for IB DP students.