Java MCQs constructors and garbage collection of Java Programming Language.
1. What is the return type of Constructors? Answer: d 2. Which keyword is used by the method to refer to the object that invoked it? Answer: d 3. Which of the following is a method having same name as that of its class? Answer: d 4. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed? Answer: d 5. Which function is used to perform some action when the object is to be destroyed? Answer: a 6. What will be the output of the following Java code? a) 100 7. What will be the output of the following Java code? a) compile time error Answer: a 8. What will be the output of the following Java code? a) 150 9. Which of the following statements are incorrect? Answer: c 10. What will be the output of the following Java code? a) 0 0
a) int
b) float
c) void
d) none of the mentioned
Clarification: Constructors does not have any return type, not even void.
a) import
b) catch
c) abstract
d) this
Clarification: this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.
a) finalize
b) delete
c) class
d) constructor
Clarification: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
a) delete
b) free
c) new
d) none of the mentioned
Clarification: Java handles deallocation of memory automatically, we do not need to explicitly delete an element. Garbage collection only occurs during execution of the program. When no references to the object exist, that object is assumed to be no longer needed, and the memory occupied by the object can be reclaimed.
a) finalize()
b) delete()
c) main()
d) none of the mentioned
Clarification: None.
class box
{
int width;
int height;
int length;
int volume;
box()
{
width = 5;
height = 5;
length = 6;
}
void volume()
{
volume = width*height*length;
}
}
class constructor_output
{
public static void main(String args[])
{
box obj = new box();
obj.volume();
System.out.println(obj.volume);
}
}
b) 150
c) 200
d) 250
Clarification: None.
output:
$ constructor_output.java
$ constructor_output
150
class San
{
San()throws IOException
{
}
}
class Foundry extends San
{
Foundry()
{
}
public static void main(String[]args)
{
}
}
b) run time error
c) compile and runs fine
d) unreported exception java.io.IOException in default constructor
Clarification: If parent class constructor throws any checked exception, compulsory child class constructor should throw the same checked exception as its parent, otherwise code won’t compile.
class box
{
int width;
int height;
int length;
int volume;
void finalize()
{
volume = width*height*length;
System.out.println(volume);
}
protected void volume()
{
volume = width*height*length;
System.out.println(volume);
}
}
class Output
{
public static void main(String args[])
{
box obj = new box();
obj.width=5;
obj.height=5;
obj.length=6;
obj.volume();
}
}
b) 200
c) Run time error
d) Compilation error
Clarification: None.
output:
$ javac Output.java
$ java Output
150
a) default constructor is called at the time of object declaration
b) constructor can be parameterized
c) finalize() method is called when a object goes out of scope and is no longer needed
d) finalize() method must be declared protected
Clarification: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.
class area
{
int width;
int length;
int area;
void area(int width, int length)
{
this.width = width;
this.length = length;
}
}
class Output
{
public static void main(String args[])
{
area obj = new area();
obj.area(5 , 6);
System.out.println(obj.length + " " + obj.width);
}
}
b) 5 6
c) 6 5
d) 5 5
Clarification: this keyword can be used inside any method to refer to the current object. this is always a reference to the object on which the method was invoked.
output:
$ javac Output.java
$ java Output
6 5