CONSTRUCTORS AND GARBAGE COLLECTOR IN JAVA (MCQ s & Answers)
1) What is the return type of Constructors?
a) int
b) float
c) void
d) None of the mentioned
2)Which keyword is used by method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
3)Which of the following is a method having same name as that of its class?
a) finalize
b) delete
c) class
d) constructor
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
05) 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
6.What is the output of this
program?
1. class box {
2. int width;
3. int height;
4. int length;
5. int volume;
6. box() {
7. width = 5;
8. height = 5;
9. length = 6;
10. }
11. void volume() {
12. volume = width*height*length;
13. }
14. }
15. class constructor_output {
16. public static void main(String args[])
17. {
18. box obj = new box();
19. obj.volume();
20.
System.out.println(obj.volume);
}
21. }
22. }
a) 100
b) 150
c) 200
d) 250
b) 150
c) 200
d) 250
7. What is the output of this program?
1. class equality {
2. int x;
3. int y;
4. boolean isequal() {
5. return(x == y);
6. }
7. }
8. class Output {
9. public static void main(String args[])
10. {
11. equality obj = new equality();
12. obj.x = 5;
13. obj.y = 5;
14. System.out.println(obj.isequal); }
15. }a) false
b) true
c) 0
d) 1
8. What is the output of this program?
1. class box {
2. int width;
3. int height;
4. int length;
5. int volume;
6. void finalize() {
7. volume = width*height*length;
8. System.out.println(volume);
9. }
10. protected void volume() {
11. volume = width*height*length;
12. System.out.println(volume);
13. }
14. }
15. class Output {
16. public static void main(String args[])
17. {
18. box obj = new box();
19. obj.volume();
20. }
21. }
a) 150
b) 200
c) Runtime error
d) Compilation error
b) 200
c) Runtime error
d) Compilation error
9. Which
of the folowing stements are incorrect?
a) Default constructor is called at the time of declaration of the object if a constructor has not been defined.
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.
a) Default constructor is called at the time of declaration of the object if a constructor has not been defined.
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.
1. class area {
2. int width;
3. int length;
4. int area;
5. void area(int width, int length) {
6. this.width = width;
7. this.length = length;
8. }
9.
10. }
11. class Output {
12. public static void main(String args[])
13. {
14. area obj = new area();
15. obj.area(5 , 6);
16. System.out.println(obj.length + " " + obj.width);
17. }
18. }
b) 5 6
c) 6 5
d) 5 5
ANSWERS
1. Answer: d
Explanation: Constructors does not have any return type, not even void.
2. 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. 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. 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. Answer: a
6. Answer: b
7. Answer: b
8. Answer: a
9. Answer: c
Explanation: finalize() method is called just prior to garbage collection. it is not called when object goes out of scope.
10. 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.
Comments
Post a Comment