250+ TOP MCQs on Constructors & Garbage Collection and Answers

Java MCQs constructors and garbage collection of Java Programming Language.

1. What is the return type of Constructors?
a) int
b) float
c) void
d) none of the mentioned

Answer: d
Clarification: Constructors does not have any return type, not even void.

2. Which keyword is used by the method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this

Answer: d
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.

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
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.

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
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.

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
Clarification: None.

6. What will be the output of the following Java code?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         box() 
  8.         {
  9.             width = 5;
  10.             height = 5;
  11.             length = 6;
  12.         }
  13.         void volume() 
  14.         {
  15.              volume = width*height*length;
  16.         } 
  17.     }    
  18.     class constructor_output 
  19.     {
  20.         public static void main(String args[])
  21.         {
  22.             box obj = new box();
  23.             obj.volume();
  24.             System.out.println(obj.volume);
  25.         }
  26.    }

a) 100
b) 150
c) 200
d) 250

Answer: b
Clarification: None.
output:

$ constructor_output.java
$ constructor_output
150

7. What will be the output of the following Java code?

  1. class San
  2. {
  3.      San()throws IOException
  4.      {
  5.  
  6.      } 
  7.  
  8. }
  9. class Foundry extends San
  10. {
  11.      Foundry()
  12.      {
  13.  
  14.      }
  15.      public static void main(String[]args)
  16.      {
  17.  
  18.      }
  19. }

a) compile time error
b) run time error
c) compile and runs fine
d) unreported exception java.io.IOException in default constructor

Answer: a
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.

8. What will be the output of the following Java code?

  1.     class box 
  2.     {
  3.         int width;
  4.         int height;
  5.         int length;
  6.         int volume;
  7.         void finalize() 
  8.         {
  9.             volume = width*height*length;
  10.             System.out.println(volume);
  11.         }
  12.         protected void volume() 
  13.        {
  14.             volume = width*height*length;
  15.             System.out.println(volume);
  16.        } 
  17.     }    
  18.     class Output 
  19.     { 
  20.         public static void main(String args[])
  21.         {
  22.             box obj = new box();
  23.             obj.width=5;
  24.             obj.height=5;
  25.             obj.length=6;
  26.             obj.volume();
  27.         } 
  28.     }

a) 150
b) 200
c) Run time error
d) Compilation error

Answer: a
Clarification: None.
output:

$ javac Output.java
$ java Output
150

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
Clarification: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.

10. What will be the output of the following Java code?

  1.     class area 
  2.     {
  3.         int width;
  4.         int length;
  5.         int area;
  6.         void area(int width, int length) 
  7.         {
  8.             this.width = width;
  9.             this.length = length;
  10.         }
  11.  
  12.     }    
  13.     class Output 
  14.     {
  15.         public static void main(String args[])
  16.         {
  17.             area obj = new area();
  18.             obj.area(5 , 6);
  19.             System.out.println(obj.length + " " + obj.width);        
  20.         } 
  21.     }

a) 0 0
b) 5 6
c) 6 5
d) 5 5

Answer: c
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

Leave a Reply

Your email address will not be published. Required fields are marked *