Java Questions & Answers – Constructors & Garbage Collection

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: d
Explanation: 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
View Answer

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
View Answer

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

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
View Answer

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: None.
output:

advertisement
$ constructor_output.java
$ constructor_output
150

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

advertisement
  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
View Answer

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 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
View Answer

Answer: a
Explanation: 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
View Answer

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

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

$ javac Output.java
$ java Output
6 5

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.