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?

Beginner
A The superclass object B The current object C The current method D The current class
Show 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?

Beginner
A To call another constructor in the same class B To create a new object C To access static variables D To call a constructor in the superclass
Show 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?

Beginner
A Only if the method is final B Only if the method is private C No, never D Yes, always
Show 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.

Beginner
A True B False
Show 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.

Beginner
A True B False
Show 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(); } } ```

Intermediate
A Runtime error B Compilation error C 10 D 0
Show 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?

Intermediate
A `this` can be used to refer to instance variables. B `this` can be reassigned to refer to another object. C `this` can be used in static methods. D `this` is a keyword that can be used as an identifier.
Show 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?

Intermediate
A To create a new instance of the class B To call a method of the current object C To call a constructor of the superclass D To call another constructor of the same class
Show 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(); } } ```

Intermediate
A Default constructor Parameterized constructor: 10 B Parameterized constructor: 10 C Parameterized constructor: 10 Default constructor D Default constructor
Show 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`?

Intermediate
A Passing `this` as an argument to a method B Returning `this` from a method C Using `this` to invoke an instance method D Using `this` to access a static variable
Show 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(); } } ```

Intermediate
A 10 B Compilation error C 5 D 0
Show 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); } } ```

Intermediate
A Compilation error B Prints false C Runtime error D Prints true
Show 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?

Intermediate
A `this` can be used to access static methods. B `this` can be used to differentiate between instance variables and parameters with the same name. C `this` can be passed as an argument to a method. D `this` can be used to invoke another constructor of the same class.
Show 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(); } } ```

Intermediate
A Runtime error B 10 C 0 D Compilation error
Show 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?

Intermediate
A this->methodName(); B this.methodName(); C this::methodName(); D this.methodName;
Show 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?

Intermediate
A `this` is a reference to the current object. B `this` is a keyword that can be used as a variable name. C `this` is a reference to the superclass object. D `this` can be used to access static variables.
Show 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(); } } ```

Advanced
A 4 B 5 C 6 D 3
Show answer & explanation

5

Local variable `x` is 2. `this.x` is set to 3. So `x + this.x` is 2 + 3 = 5.

Make your own

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