Font size
WorksheetsConstructors_crt
Total questions: 15
Worksheet time: 10mins
What is the purpose of a constructor in Java?
To destroy objects
To initialize objects
To display output
To perform calculations
A constructor has the same name as the ______.
Package
Method
Class
Variable
Which type of constructor takes no parameters?
Parameterized constructor
Default constructor
Copy constructor
Static constructor
Which constructor accepts arguments?
Default constructor
Empty constructor
Final constructor
Parameterized constructor
How many constructors can a class contain?
Only one
Two
Three
Multiple Constructors
Which keyword refers to the current object?
super
static
this
final
Which constructor is automatically provided by Java when no constructor is defined?
Default constructor
Parameterized constructor
Copy constructor
Static constructor
What will be the output?
class Test{
Test(){
System.out.println("Constructor");
}
public static void main(String args[]){
Test t = new Test();
}
}
Error
Constructor
Test
Output
What is the output?
class Demo{
int x;
Demo(){
x = 50;
}
public static void main(String args[]){
Demo d = new Demo();
System.out.println(d.x);
}
}
0
10
50
Error
What is the output?
class Student{
Student(){
System.out.println("Hello");
}
Student(int a){
System.out.println("Java");
}
public static void main(String args[]){
Student s = new Student(5);
}
}
Hello
Java
Hello Java
Hello
Hello
What is the output?
class Demo{
Demo(){
this(10);
System.out.println("Default");
}
Demo(int a){
System.out.println("Parameterized");
}
public static void main(String args[]){
new Demo();
}
}
Default Parameterized
Parameterized Default
Error
No output
constructor is automatically invoked when an (a) is created.
class A{
A(){
System.out.println("Hello");
}
public static void main(String args[]){
A obj = (a) A();
}
}
class Demo{
int x;
Demo(int x){
(a) .x = x;
}
}
A constructor having parameters is called a (a) constructor.
