Java OOP: 17 MCQs with answers
All 17 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 5 questions
Q1. What does the `this` keyword refer to in Java?
BeginnerShow answer & explanation
The current object
`this` is a reference to the current object whose method or constructor is being invoked.
Q2. Which of the following is a valid use of the `this` keyword?
BeginnerShow answer & explanation
To call another constructor in the same class
`this()` can be used to invoke another constructor of the same class. Calling a superclass constructor is done with `super()`.
Q3. Can `this` be used inside a static method?
BeginnerShow answer & explanation
No, never
Static methods belong to the class, not to any instance, so `this` is not available.
Q4. The `this` keyword can be used to refer to the current object's instance variables.
BeginnerShow answer & explanation
True
`this` is commonly used to access instance variables, especially when they are shadowed by local variables or parameters.
Q5. The `this` keyword can be used in a static method.
BeginnerShow answer & explanation
False
Static methods are not associated with any instance, so `this` cannot be used inside them.
Building up · 11 questions
Q6. What is the output of the following code? ```java class Test { int x; Test(int x) { this.x = x; } void display() { System.out.println(x); } public static void main(String[] args) { Test t = new Test(10); t.display(); } } ```
IntermediateShow answer & explanation
10
The constructor parameter `x` shadows the instance variable `x`. Using `this.x = x` assigns the parameter value to the instance variable. So `x` becomes 10.
Q7. Which statement about `this` is true?
IntermediateShow answer & explanation
`this` can be used to refer to instance variables.
`this` refers to the current object and is commonly used to access instance variables. It cannot be used in static contexts and cannot be reassigned.
Q8. What is the purpose of `this()` in a constructor?
IntermediateShow answer & explanation
To call another constructor of the same class
`this()` is used to invoke another constructor within the same class, enabling constructor chaining.
Q9. What is the output of the following code? ```java class A { A() { this(10); System.out.println("Default constructor"); } A(int x) { System.out.println("Parameterized constructor: " + x); } public static void main(String[] args) { new A(); } } ```
IntermediateShow answer & explanation
Parameterized constructor: 10 Default constructor
The default constructor calls `this(10)`, which invokes the parameterized constructor first. After it completes, the default constructor continues and prints its message.
Q10. Which of the following is NOT a valid use of `this`?
IntermediateShow answer & explanation
Using `this` to access a static variable
`this` refers to the current object, so it cannot be used to access static members (which belong to the class, not an object).
Q11. What is the output of the following code? ```java class Test { int x = 5; void method() { int x = 10; System.out.println(this.x); } public static void main(String[] args) { new Test().method(); } } ```
IntermediateShow answer & explanation
5
`this.x` refers to the instance variable `x`, which is 5. The local variable `x` (10) is shadowing it, but `this.x` accesses the instance variable.
Q12. What does the following code do? ```java class Test { Test get() { return this; } public static void main(String[] args) { Test t = new Test().get(); System.out.println(t instanceof Test); } } ```
IntermediateShow answer & explanation
Prints true
The `get()` method returns the current object (`this`). The returned object is an instance of `Test`, so `t instanceof Test` is true.
Q13. Which of the following statements about `this` is false?
IntermediateShow answer & explanation
`this` can be used to access static methods.
`this` cannot be used to access static methods because static methods are not associated with any particular instance.
Q14. What is the output of the following code? ```java class Test { int x; Test() { this(10); } Test(int x) { this.x = x; } void display() { System.out.println(x); } public static void main(String[] args) { new Test().display(); } } ```
IntermediateShow answer & explanation
10
The default constructor calls `this(10)`, which sets `x` to 10. Then `display()` prints 10.
Q15. Which of the following is a valid way to use `this` to call a method?
IntermediateShow answer & explanation
this.methodName();
In Java, `this.methodName()` is the correct syntax to call an instance method on the current object.
Q16. Which of the following is true about `this` in Java?
IntermediateShow answer & explanation
`this` is a reference to the current object.
`this` is a reference to the current object. It cannot be used as a variable name, and it does not refer to the superclass.
Topper level · 1 questions
Q17. What is the output of the following code? ```java class Test { int x = 1; void method() { int x = 2; this.x = 3; System.out.println(x + this.x); } public static void main(String[] args) { new Test().method(); } } ```
AdvancedShow answer & explanation
5
Local variable `x` is 2. `this.x` is set to 3. So `x + this.x` is 2 + 3 = 5.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages