Java Multiple Choice Questions & Answers on Constructors & Garbage Collection for Freshers

https://www.computersprofessor.com/2017/11/java-multiple-choice-questions-answers_26.html
1. What is the return type of Constructors?
a) int
b) float
c) void
d) None of the mentioned
Answer: d
Explanation: Constructors does not have any return type, not even void.
2. Which keyword is used by method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
Answer: d
Explanation: 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.
3. Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
Answer: d
Explanation: 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.
4. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?
a) delete
b) free
c) new
d) None of the mentioned
Answer: d
Explanation: 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.
5. Which function is used to perform some action when the object is to be destroyed?
a) finalize()
b) delete()
c) main()
d) None of the mentioned
Answer: a
6. What is the output of this program?
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);
}
}
a) 100
b) 150
c) 200
d) 250
b) 150
c) 200
d) 250
Answer: b
7. What is the output of this program?
class San
{
San()throws IOException
{
}
}
class Foundry extends San
{
Foundry()
{
}
public static void main(String[]args)
{
}
}
a) compile time error
b) run time error
c) compile and runs fine
d) unreported exception java.io.IOException in default constructor
b) run time error
c) compile and runs fine
d) unreported exception java.io.IOException in default constructor
Answer: a
Explanation: 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.
8. What is the output of this program?
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();
}
}
a) 150
b) 200
c) Run time error
d) Compilation error
b) 200
c) Run time error
d) Compilation error
Answer: a
9. Which of the following statements are incorrect?
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
Answer: c
Explanation: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.
10. What is the output of this program?
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);
}
}
a) 0 0
b) 5 6
c) 6 5
d) 5 5
b) 5 6
c) 6 5
d) 5 5
Answer: c
Explanation: 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.