Constructors
A. Answer the
following Question
1. Which of the
following is not applicable for a constructor function?
a. It has the same
name as the class.
b. It has no
return-type
c. It is usually
used for initialisation.
d. It can be
invoked using an object like any other member function.
Ans.
d. It can be invoked using an object like any other member
function.
2. If the name of a
class is ‘Number’, what can be the possible name for its constructor?
a. Number b. number
c. No d. No
Ans.
a. Number
3. Which among the
following is a type of constructor?
a. Parameterised
constructor b. Non-parameterised constructor
c. Both a and b d.
None of these
Ans.
c. Both a and b
4. If constructors
are overloaded, what differentiates it?
a. Parameter list
b. Return type
c. Both a and b d.
None of these
5. What access
specifier for a constructor allows you to create an object only within the
class?
a. public b.
private
c. protected d.
default
Ans.
b. private
6. Name the type of
constructor that gets invoked when an object is created, which is initialised
with the content of
another object.
a. Copy constructor
b. Default constructor
c. Overloaded
constructor d. None of these
Ans. a. Copy constructor
7. Categorise the
type of object that can be created without any name or identifier.
a. Temporary object
b. Anonymous object
c. Both a and b d.
None of these
Ans.
c. Both a and b
8. Predict the
output of the following program:
class T
{
int t = 20;
T()
{
t = 40;
}
public static void
main(String args[])
{
T t1 = new T();
System.out.println(t1.t);
}
}
a. 20 b. 40
c. Compiler Error
d. None of these
Ans.
b. 40
9. The following
code contains one compilation error, find it?
public class Test {
Test() { } // line
1
static void Test()
{ this(); } // line 2
public static void
main(String[] args) { // line 3
Test(); // line 4
}}
a. At line 1,
constructor Tester must be marked public like its class
b. At line 2,
constructor call
c. At line 3,
compilation error, ambiguity problem, compiler can’t determine whether a
constructor
d. At line 4
Ans.
b. At line 2, constructor call
10. Which of the
following is not true for static block?
a. It is used to
initialise static variables.
b. It gets executed when a class gets loaded in the memory.
c. It can print the
content of instance variables.
d. It begins with
the static keyword.
Ans. c. It can print the content of instance variables.
11. A Java constructor
is like a method without ___.
A) statements
B) return type
C) argument list
D) None
Answer [=]
B
12. The name of a
constructor and the name of a class are ___.
A) Same
B) Different
C) -
D) -
Answer [=]
A
13. The placement of a
constructor inside a class should be ___.
A) Always at the
beginning of class
B) Always at the end
of class
C) Anywhere in the
class
D) None
Answer [=]
C
14. The purpose of a
Java constructor is ___.
A) Initialization of
variables with passed data
B) Writing custom code
C) Accepting other
objects as inputs
D) All the above
Answer [=]
D
15. Memory is allocated
to an object once the execution of ___ is over in Java language.
A) main method
B) constructor
C) destructor
D) None
Answer [=]
B
16. What is the output
of the below Java program?
public class
TestingConstructor
{
void TestingConstructor()
{
System.out.println("Amsterdam");
}
TestingConstructor()
{
System.out.println("Antarctica");
}
public static void main(String[] args)
{
TestingConstructor tc = new
TestingConstructor();
}
}
A) Antarctica
B) Amsterdam
C) No output
D) Compiler error
Answer [=]
A
Explanation:
Here the constructor
is TestingConstructor() without return type.
17. In Java, a
constructor with no parameters or no arguments is called ___ constructor.
A) Default constructor
B) User-defined
constructor
C) -
D) -
Answer [=]
A
18. In Java, a
constructor with one or more arguments or parameters is called a ___
constructor.
A) Default constructor
B) User-defined
constructor or Non-default constructor
C) -
D) -
Answer [=]
B
19. The compiler adds a
default no-argument constructor to a class if it ___.
A) does not define a
constructor at all.
B) defines at least
one constructor with arguments
C) -
D) -
Answer [=]
A
20. Overloading of
constructors in Java means adding more than ___ constructors with the different
argument list.
A) 1
B) 2
C) 3
D) 8
Answer [=]
A
21. What is the output
of the below Java program with constructors?
public class
Constructor2
{
int count=10;
Constructor2(int count)
{
System.out.println("Count=" +
count);
}
public static void main(String[] args)
{
Constructor2 con = new Constructor2();
}
}
A) Count=0
B) Count=10
C) Compiler error
D) None of the above
Answer [=]
C
Explanation:
If you write a
constructor with arguments, the default constructor is not added by the
compiler. You should add it explicitly.
22. A constructor can
call another overloaded constructor using the ___ keyword in Java.
A) super
B) local
C) con
D) this
Answer [=]
D
23. In Java, you can
pass __ variables from one constructor to another overloaded constructor.
A) local variables
B) static variables
C) non-static
variables
D) local and static
variables
Answer [=]
D
B. Write TRUE or FALSE
for the following statements
Question 1: The compiler supplies a special
constructor in a class that does not have any constructor.
Ans.: True
Question 2: A constructor is not defined with
any return type.
Ans.: True
Question 3: Every class must have all types of
constructors.
Ans.: False
Question 4: A constructor is a member function
of a class.
Ans.: True
Question 5: Constructor is used to initialize
the data members of a class.
Ans.: True
Question 6: A constructor may have different
name than the class name.
Ans.: False
Question 7: A constructor is likely to be
defined after the class declaration.
Ans.: False
Question 8: Copy constructor copies functions
from one object to another.
Ans.: False
C. Answer the
following
1. What is a constructor? Why is it needed in a program?
Ans.: A constructor in Java is a block of code similar to a method that is called when an instance of an
object is created. A constructor is needed to initialise data members with legal initial values.
2. State the characteristics of a constructor.
Ans.: The characteristics of a constructor are:
a. It has the same name as the class-name.
b. It does not have any return type.
c. It follows the usual rules of accessibility as other members of a class and therefore access
modifiers can be applied to it.
d. It gets called automatically, whenever an object is created.
3. How are constructors invoked?
Ans.:Constructor function gets called (invoked) automatically whenever an object is created.
4. Why do we need a constructor as a class member?
Ans.: A constructor is a special member method which will be called by the JVM implicitly for placing
user/programmer defined values instead of placing default values. Constructors are meant for
initializing the object.
5. State the difference between function and constructor.
Ans.: Following are the difference between constructor and method.
a. Constructor is used to initialize an object whereas method is used to exhibits functionality
of an object.
b. Constructors are invoked implicitly whereas methods are invoked explicitly.
c. Constructor does not return any value where the method may/may not return a value.
d. In case constructor is not present, a default constructor is provided by java compiler. In the
case of a method, no default method is provided.
e. Constructor should be of the same name as that of class. Method name should not be of
the same name as that of class.
6. How are private constructors called?
Ans.:Private constructors allows an object to be created only within the methods of the class where
it is private.
7. What are the different types of constructors available in Java?
Ans.: Parameterised constructor and Non-parameterised constructor.
8. What is a default constructor?
Ans.: When we do not explicitly define a constructor for a class, then java creates a default constructor
for the class. It is essentially a non-parameterized constructor, i.e. it doesn’t accept any
arguments. The default constructor’s job is to call the super class constructor and initialize all
instance variables.
9. Point out the difference between implicit and explicit default constructors.
Ans.: Implicit default constructor is the default constructor created by java if the user do not create a
constructor. Explicit default constructor is a non-parameterised constructor defined by the user
to initialise data members with legal initial values.
10. What are temporary objects? How are they created, explain with the help of an example?
Ans.: Temporary or anonymous objects or instances are the ones that live in the memory as long as it
is being used or referenced in an expression and after that it dies. Temporary objects are created
by explicit call to a constructor, preceded with the new command. For example,
public class temp
{
int a,b;
temp(int x, int y)
{
a=x;
b=y;
}
void show()
{
System.out.println(a+“,”+b);
}
static void test( )
{
new temp(1,2).show( );
}
}
Here the statement new temp(1,2) of the statement new temp(1,2).show(); creates an
anonymous temporary object and lives in the memory as long as show() of the statement new
temp(1,2).show(); is being executed. Upon completion of the execution of the show() function,
the temporary object gets wiped out of the memory.
11. What is a destructor? Is destructor function necessary in Java? If no, explain why?
Ans.: A destructor is a special method called automatically during the destruction of an object. In java
a destructor is not necessary because Java is a garbage collected language you cannot predict
when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor.
12. What is the implicit return type of a constructor function?
Ans.: The implicit return type is the class itself.
13. What is the default initial value of a boolean variable data type?
Ans.: the default initial value is ‘false’.
Question 14: What is meant by a constructor?
Ans.: A constructor is a member method that is
written with the same name as the class name and is used to initialize the data
members or instance variables. It is invoked at the time of creating any object
of the class. For example:
Employee emp = new Employee();
Here, Employee() is invoking a default
constructor.
Question 15: Name the different types of
constructors used in a class program.
Ans.:
1.
Parameterised
constructor
2.
Non-parameterised
constructor
Question 16: Why do we need a constructor as a
class member?
Ans.: A constructor is used to initialize the
objects of the class with a legal initial value.
Question 17: Explain the following terms:
Ans.:
(a) Constructor with default argument
Java specification doesn't support default
arguments in methods so Constructor with default argument cannot be written in
Java.
(b) Parameterised constructor
A Parameterised constructor receives
parameters at the time of creating an object and initializes the object with
the received values.
(c) Copy constructor
A constructor used to initialize the instance
variables of an object by copying the initial values of the instance variables
from another object is known as Copy Constructor.
(d) Constructor overloading
The process of using a number of constructors
with the same name but different types of parameters is known as Constructor
overloading.
Question 18: Why is an object not passed to a
constructor by value? Explain.
Ans.: Constructors are special member methods of the
class. Objects are non-primitive data types so they are passed by reference and
not by value to constructors. If objects were passed by value to a constructor
then to copy the objects from actual arguments to formal arguments, Java would
again invoke the constructor. This would lead to an endless circular loop of
constructor calls.
Question 19: State the difference between
constructor and method.
Ans.: Constructor
§
It is a block of code
that initializes a newly created object.
§
It has the same name
as class name.
§
It has no return type
§
It is called
implicitly at the time of object creation
§
If a constructor is
not present, a default constructor is provided by Java
§
It is not inherited by
subclasses.
Method
§
It is a group of
statements that can be called at any point in the program using its name to
perform a specific task.
§
It should have a
different name than class name.
§
It needs a valid
return type if it returns a value otherwise void
§
It is called
explicitly by the programmer by making a method call
§
In case of a method,
no default method is provided.
§
It may or may not be
inherited depending upon its access specifier.
Question 20:Explain two features of a
constructor.
Ans.:
1.
A constructor has the
same name as that of the class.
2.
A constructor has no
return type, not even void.
Question 8:Differentiate between the following
statements:
abc p = new abc();
abc p = new abc(5,7,9);
Ans.: The first statement abc p = new abc(); is calling a
non-parameterised constructor to create and initialize an object p of class
abc. The second statement abc p = new abc(5,7,9); is calling a parameterised
constructor which accepts three arguments to create and initialize an object p
of class abc.
Question 21: What are the temporary instances
of a class?
Ans.: If we don't assign an object created by the
new operator to a variable then it is known as a temporary instance or an
anonymous object. If we want to use an object only once, then we can use it as
a temporary instance. The below example illustrates this:
class Sum {
int x;
int y;
public Sum(int a, int b) {
x = a;
y = b;
}
public void computeSum() {
int s = x + y;
System.out.println("Sum = " + s);
}
public static void main(String[] args) {
new Sum(3, 5).computeSum();
}
}
In the main method, the object of class Sum is
not assigned to any variable. We use it just to print the sum of 3 and 5 after
which it is destroyed.
0 Comments