0% found this document useful (0 votes)
3 views

worksheet 1 constructor

The document provides a quiz on constructors in Java, covering their definition, characteristics, and usage. Key points include that constructors initialize objects, have the same name as the class, and can be parameterized. The document also explains that if no constructor is defined, a default constructor is provided by the Java compiler.

Uploaded by

vanuradha064
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

worksheet 1 constructor

The document provides a quiz on constructors in Java, covering their definition, characteristics, and usage. Key points include that constructors initialize objects, have the same name as the class, and can be parameterized. The document also explains that if no constructor is defined, a default constructor is provided by the Java compiler.

Uploaded by

vanuradha064
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

is a constructor in Java?

a) A method used to destroy objects


b) A special method used to initialize objects
c) A method used to perform operations on objects
d) A method to compare two objects

Answer: b) A special method used to initialize objects


Explanation: A constructor is a special method that is automatically called when an object is created,
and it initializes the object.

2. Which of the following is true about constructors in Java?

a) Constructors have the same name as the class


b) Constructors can have a return type
c) Constructors are called explicitly using the new keyword
d) Constructors are inherited by subclasses

Answer: a) Constructors have the same name as the class


Explanation: Constructors must have the same name as the class and do not have a return type.

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.

4. What happens if a class does not have a constructor?

a) The program will not compile


b) A default constructor is provided by the Java compiler
c) The object cannot be created
d) The class will use the constructor of another class

Answer: b) A default constructor is provided by the Java compiler


Explanation: If no constructor is defined, the Java compiler automatically provides a default
constructor with no parameters.

5. What will be the output of the following code?

java

Copy code
class MyClass {

MyClass() {

System.out.println("Constructor called!");

public class Main {

public static void main(String[] args) {

MyClass obj = new MyClass();

a) Compile-time error
b) Runtime error
c) "Constructor called!"
d) No output

Answer: c) "Constructor called!"


Explanation: The constructor is called automatically when the object obj is created, and it prints the
message.

6. Which of the following statements is true about parameterized constructors?

a) They must have a void return type


b) They do not allow arguments
c) They allow passing parameters to initialize objects
d) They can only be defined once in a class

Answer: c) They allow passing parameters to initialize objects


Explanation: Parameterized constructors allow you to pass arguments to initialize object attributes.

7. Which of the following statements is false about constructors in Java?

a) Constructors can be overloaded


b) A constructor can return a value
c) A constructor initializes an object
d) Constructors are called when an object is created

Answer: b) A constructor can return a value


Explanation: Constructors do not return values, not even void.

8. What is the role of the this() keyword in a constructor?


a) It calls the parent class constructor
b) It refers to the current instance of the class
c) It calls another constructor in the same class
d) It accesses static members of the class

Answer: c) It calls another constructor in the same class


Explanation: this() is used to call another constructor in the same class.

9. What will happen if you define a constructor with parameters but not a default constructor in
Java?

a) The compiler will automatically generate a default constructor


b) The class will not compile
c) You cannot create an object without passing arguments
d) The constructor will behave as a default constructor

Answer: c) You cannot create an object without passing arguments


Explanation: If only a parameterized constructor is defined, the default constructor is not provided
by the compiler. You must pass arguments when creating an object.

10. Which of the following is a valid constructor declaration 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy