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 answer
Correct!
IncorrectStep-by-step walkthrough
Choose a solution method
Method #1Approach 1Step 1: Recall Java instantiation syntax
In Java, creating an object requires the
newkeyword 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 namenovel, and arguments"Dune"(String) and412(int), we get:LibraryBook novel = new LibraryBook("Dune", 412);Step 3: Confirm the correct answer
This matches the first option exactly. The type declaration
LibraryBookappears on the left, followed by the variable name, then thenewkeyword and constructor call on the right.Method #2Approach 2Step 1: Identify what is being tested
The question tests knowledge of Java object instantiation syntax, specifically the use of the
newkeyword and correct variable declaration format.Step 2: Eliminate option B
novel = LibraryBook("Dune", 412);omits both the type declaration (LibraryBook) on the left and thenewkeyword. This is Python syntax, not valid Java.Step 3: Eliminate option C
new LibraryBook novel = LibraryBook("Dune", 412);incorrectly placesnewbefore the type declaration and uses the constructor call withoutnewon the right — this is not valid Java syntax.Step 4: Eliminate option D
LibraryBook("Dune", 412) = novel;reverses the assignment direction and omitsnew. 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.Question 2
In the context of OOP, what is the primary purpose of a constructor?No clue? Show me the answer
Correct answer
Correct!
IncorrectStep-by-step walkthrough
Choose a solution method
Method #1Approach 1Step 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 2Step 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.