Quick Review Flashcards - Click to flip and test your knowledge!
Question
What is the defining characteristic of a constructor's name?
Answer
It must be identical to the name of the class in which it resides.
Question
What is the return type of a constructor?
Answer
A constructor does not have a return type, not even void.
Question
When is a constructor automatically called (invoked)?
Answer
It is invoked when an object is created using the new operator.
Question
Can a constructor be declared with the static or final modifiers?
Answer
No, a constructor cannot be static or final.
Question
What is the primary purpose of a constructor in Java?
Answer
It is used for initialising and constructing the data members of an object when it is created.
Question
Why are constructors necessary for initialising private data members?
Answer
To implement data hiding, private members cannot be initialised directly with values outside the class.
Question
In the syntax of a constructor, what follows the access specifier?
Answer
The class name.
Question
What are the two broad categories of constructors based on parameters?
Answer
Default constructors and parameterised constructors.
Question
Define a 'Default constructor'.
Answer
A constructor that takes no parameters.
Question
What action does the Java compiler take if no constructor is explicitly defined in a program?
Answer
The compiler automatically supplies a default constructor.
Question
To what value does the compiler-provided default constructor initialise integer variables?
Answer
Zero ($0$).
Question
To what value does the compiler-provided default constructor initialise floating-point variables?
Answer
$0.0$.
Question
To what value does the compiler-provided default constructor initialise String variables?
Answer
null.
Question
What is a 'Parameterised constructor'?
Answer
A constructor that takes one or more parameters or arguments.
Question
What occurs if a programmer fails to pass required arguments to a parameterised constructor during object creation?
Answer
The compiler generates an error message.
Question
Identify a similarity between constructors and member methods regarding overloading.
Answer
Both constructors and member methods can be overloaded.
Question
How do constructors and member methods differ regarding their name?
Answer
A constructor must match the class name, whereas a member method can have any name chosen by the user.
Question
How do constructors and member methods differ regarding their 'need'?
Answer
Constructors initialise variables, while methods are used to avoid repetition and increase code reusability.
Question
When is a constructor executed compared to a member method?
Answer
A constructor executes at the time of object creation, while a method executes repeatedly when called by name.
Question
Define 'Constructor Overloading'.
Answer
The existence of more than one constructor within a single class, each having a different signature.
Question
What distinguishes overloaded constructors within the same class?
Answer
Their parameter lists or signatures.
Question
What is the function of the `this` keyword within a constructor?
Answer
It is a reference used to refer to the current object that invokes the constructor.
Question
What does the compiler implicitly do to data members if the programmer does not use the `this` keyword?
Answer
The compiler normally prefixes `this.` to the data members.
Question
When is it necessary to use the `this` keyword explicitly?
Answer
To resolve ambiguity when instance variables and formal arguments share the same name.
Question
How does the `this` keyword assist in naming instance variables and local variables?
Answer
It helps avoid naming confusion between the two.
Question
What is stored by the `this` keyword?
Answer
The memory address of the currently calling object.
Question
Define 'Assigning an object as a reference variable to another'.
Answer
The process of giving the reference or memory address of one object to another object variable.
Question
If `Book b2 = b1;` is executed, what is the relationship between `b1` and `b2`?
Answer
Both variables refer to the same member memory location.
Question
What is the consequence of changing a value in an object that has been assigned to multiple reference variables?
Answer
The change affects all reference variables as they point to the same object.
Question
What is a 'Temporary Instance' in Java?
Answer
A process of using a memory location without assigning a name to it.
Question
How is a method called using a temporary instance?
Answer
By using the new operator followed by the constructor and the dot operator to call the method immediately.
Question
Define 'Constructor Chaining'.
Answer
Invoking one constructor from within another constructor within the same class using the `this` keyword.
Question
Where must the calling statement be placed when performing constructor chaining?
Answer
It must be the first statement within the constructor.
Question
A constructor is automatically called when the object is _____.
Answer
Created (or declared).
Question
When more than a single constructor exists within a single class, it is known as _____.
Answer
Constructor overloading.
Question
In constructor definition, the parameter list refers to _____ arguments.
Answer
Formal.
Question
A constructor generated by the compiler itself is called a _____ constructor.
Answer
Default.
Question
A simple constructor is created using the _____ operator followed by the name of the class.
Answer
new.
Question
Which keyword is used to refer to the current object within a constructor?
Answer
this.
Question
How many parameters does a default constructor take?
Answer
Zero parameters.
Question
True or False: A constructor can be invoked by the user like a normal method.
Answer
False; it cannot be invoked by the user like normal methods.
Question
Why must a constructor be declared public if accessed outside the class?
Answer
To ensure it is visible to other classes that need to instantiate it.
Question
According to ICSE 2005, identify one similarity between constructors and member methods regarding parameters.
Answer
Both can take parameters.
Question
What is the purpose of the `new` operator in the context of constructors?
Answer
It dynamically allocates memory for an object and returns a reference.
Question
Which unit of the class is called when an object is created?
Answer
The constructor.
Question
What is the default value assigned to a boolean data member by a compiler-generated constructor?
Answer
false.
Question
In the context of constructor chaining, what happens to the control after a constructor is invoked via `this()`?
Answer
Control moves to the other constructor until the data members are initialised and the object is fully constructed.
Question
Concept: Constructor Signature
Answer
Definition: The combination of the constructor name and its parameter list used to distinguish overloaded constructors.
Question
How many parameters can a parameterised constructor take?
Answer
There is no limitation to the number of parameters.
Question
What is the order of execution for constructors in a class with constructor chaining?
Answer
The called constructor (via `this`) executes first before the rest of the calling constructor's code.
Question
In Program 4.6, how is the `display()` method called on a temporary instance of `Room1`?
Answer
`new Room1(12,11,11).display();`
Question
A constructor that takes exactly one parameter is a type of _____ constructor.
Answer
Parameterised.
Question
What is the primary role of a constructor in implementing abstraction?
Answer
It allows the creation of objects with valid initial states without exposing the underlying data members directly.
Question
Can a constructor be declared private?
Answer
Yes, it can be declared private if it is only meant to be accessed within the same class.
Question
Code Snippet: `Book b1 = new Book(190);` invokes which type of constructor?
Answer
A parameterised constructor.
Question
How does the compiler distinguish between an instance variable and a local variable in the statement `this.bookno = bookno;`?
Answer
`this.bookno` refers to the instance variable, while `bookno` refers to the local variable/argument.
Question
When using `this()` for constructor chaining, what error occurs if it is not the first line?
Answer
A compiler error stating 'call to this must be first statement in constructor'.
Question
What value is stored in a reference variable after an object is assigned to it?
Answer
The memory address of the object.
Question
How long does a temporary instance stay in the referenced memory?
Answer
Only as long as it is being used; it is removed later.
Question
In the ICSE 2013 example, write a Java statement to create an object `mp4` of class `digital`.
Answer
`digital mp4 = new digital();`