0% found this document useful (0 votes)
17 views7 pages

Java Mcqs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Java Mcqs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

A.

finalize() Method – [5 MCQs]

1. What is the purpose of the finalize() method in Java?

A. To initialize an object​
B. To release system resources before garbage collection​
C. To finalize class definition​
D. To compile a class

Answer: B​
Explanation: finalize() is called by the garbage collector before reclaiming the memory
occupied by the object.

2. In which package is the finalize() method defined?

A. java.io​
B. java.lang.Object​
C. java.util​
D. java.gc

Answer: B​
Explanation: finalize() is a protected method in the Object class in the java.lang
package.

3. What is the return type of the finalize() method?

A. int​
B. void​
C. boolean​
D. Object

Answer: B​
Explanation: finalize() has void return type and cannot return any value.

4. Can finalize() be called explicitly?


A. No, only the JVM can call it​
B. Yes, like any other method​
C. Only during runtime​
D. Only once per object

Answer: B​
Explanation: Though discouraged, you can call finalize() manually, but it won’t trigger
garbage collection.

5. What happens if an exception is thrown inside finalize()?

A. JVM terminates​
B. Garbage collector stops​
C. JVM ignores the exception​
D. JVM retries garbage collection

Answer: C​
Explanation: If an exception is thrown in finalize(), it is ignored, and garbage collection
proceeds.

✅ B. this Keyword – [5 MCQs]


6. What does the this keyword refer to in Java?

A. Current class​
B. Parent class​
C. Current object​
D. Static object

Answer: C​
Explanation: this refers to the current object inside a method or constructor.

7. Which of the following is a valid use of this keyword?

A. Calling another constructor​


B. Referring to the current object​
C. Passing current object as parameter​
D. All of the above

Answer: D​
Explanation: this is versatile and can be used in all the mentioned ways.

8. What is the output of this code?


java
CopyEdit
class Demo {
int x;
Demo(int x) {
this.x = x;
}
}

A. Compiler error​
B. Runtime error​
C. Correctly assigns value to the class variable​
D. Infinite recursion

Answer: C​
Explanation: this.x = x distinguishes class variable x from parameter x.

9. Can this() be used to call a constructor from another constructor?

A. Yes, it must be the first statement​


B. Yes, it can be anywhere​
C. No, it's invalid​
D. Only in static methods

Answer: A​
Explanation: this() must be the first line in a constructor if used to call another constructor.

10. What will be the output of this code?


java
CopyEdit
class Demo {
Demo() {
this(10);
System.out.print("A ");
}

Demo(int x) {
System.out.print("B ");
}

public static void main(String[] args) {


new Demo();
}
}

A. B A​
B. A B​
C. A​
D. B

Answer: A​
Explanation: Constructor chaining using this(10) prints "B", then continues and prints "A".

✅ C. Method Overloading – [5 MCQs]


11. What is method overloading in Java?

A. Redefining a method in subclass​


B. Defining multiple methods with same name but different parameters​
C. Having the same method in multiple classes​
D. Writing duplicate code

Answer: B​
Explanation: Method overloading allows methods to share a name but differ in type or
number of parameters.
12. Which of the following is a valid overloaded method?

A. void sum(int a) and void sum(int a, int b)​


B. int sum() and String sum()​
C. void sum(int a) and int sum(int a)​
D. All of the above

Answer: D​
Explanation: All are valid as overloading is based on method signature, not just return type.

13. Can overloaded methods have different return types?

A. No​
B. Yes, if parameter list is different​
C. Only if return type is void​
D. Only in static methods

Answer: B​
Explanation: Return type can be different, but overloading is decided by parameter list only.

14. What is the output of the following code?


java
CopyEdit
class Test {
void display(int x) {
System.out.println("int");
}

void display(double x) {
System.out.println("double");
}

public static void main(String[] args) {


Test t = new Test();
t.display(10.5);
}
}
A. int​
B. double​
C. Compile error​
D. float

Answer: B​
Explanation: 10.5 is a double literal. So display(double) is called.

15. Which is not true about method overloading?

A. It helps in code readability​


B. It supports dynamic binding​
C. It occurs at compile time​
D. It can differ by number or type of arguments

Answer: B​
Explanation: Overloading uses compile-time binding, not dynamic (which is for overriding).

✅ D. Garbage Collection – [5 MCQs]


16. Which method is used to request garbage collection in Java?

A. destroy()​
B. free()​
C. System.gc()​
D. clear()

Answer: C​
Explanation: System.gc() suggests JVM to start garbage collection, but it's not
guaranteed.

17. When does garbage collection happen in Java?

A. At fixed intervals​
B. After main() completes​
C. When JVM decides it's necessary​
D. Manually by programmer
Answer: C​
Explanation: GC is automatic and happens when JVM decides based on memory pressure.

18. Which of these can make an object eligible for GC?

A. Setting reference to null​


B. Reassigning object reference​
C. Object going out of scope​
D. All of the above

Answer: D​
Explanation: All listed ways make the object unreachable, thus eligible for GC.

19. Which class is the root for all Java classes and defines finalize()?

A. Object​
B. System​
C. Runtime​
D. Class

Answer: A​
Explanation: Object is the superclass of all Java classes, and defines the finalize()
method.

20. What happens if there are no more references to an object?

A. It is deleted immediately​
B. JVM crashes​
C. It becomes eligible for garbage collection​
D. It is archived

Answer: C​
Explanation: If no reference points to an object, it is eligible for GC, but not guaranteed to be
collected instantly.

You might also like