worksheet 1 constructor
worksheet 1 constructor
3. Which keyword is used to call a constructor from the parent class in Java?
a) this
b) super
c) parent
d) base
Answer: b) super
Explanation: The super keyword is used to call the constructor of the parent class from the child
class.
java
Copy code
class MyClass {
MyClass() {
System.out.println("Constructor called!");
a) Compile-time error
b) Runtime error
c) "Constructor called!"
d) No output
9. What will happen if you define a constructor with parameters but not a default constructor in
Java?
csharp
Copy code
a) `void MyClass()`
b) `MyClass MyClass()`
c) `MyClass()`
d) `MyClass(void)`
**Answer:** c) `MyClass()`
**Explanation:** A constructor does not have a return type, not even `void`, and it has the same
name as the class.