this Keyword MCQs
What does the this keyword refer to in Java?
A. The class itself
B. The current instance of the class
C. The parent class
D. The static method in the class
Which of the following is a valid use of the this keyword?
A. To call a static method
B. To refer to instance variables and methods
C. To access private members of another class
D. To refer to the superclass constructor
How can the this keyword be used in a constructor?
A. To refer to another constructor in the same class (constructor chaining)
B. To call a method from a static context
C. To access variables in a static method
D. To refer to a superclass constructor
What happens if you use this in a static method?
A. It refers to the current instance of the class
B. It cannot be used in static methods
C. It refers to the static variables of the class
D. It refers to the parent class instance
Consider the following code snippet:
java
Copy code
class Example {
int x;
Example(int x) {
this.x = x;
}
void printValue() {
System.out.println(this.x);
}
}
What does this.x refer to in the constructor?
A. The parameter x
B. The instance variable x
C. A local variable x
D. A static variable x
Which statement about the this keyword is true?
A. this can be used to call private methods in the same class
B. this can refer to a method in another class
C. this can be used to call static methods
D. this can be used to access variables from a superclass
How can this be used to resolve name conflicts?
A. By referring to static variables
B. By referring to instance variables when their names conflict with parameter names
C. By calling superclass methods
D. By accessing local variables
In which of the following scenarios is the use of this keyword mandatory?
A. To call a superclass constructor
B. To access instance variables when there are local variables with the same name
C. To access static variables
D. To call static methods
What is the output of the following code?
java
Copy code
class MyClass {
int x = 10;
void setValue(int x) {
this.x = x;
}
void display() {
System.out.println(x);
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.setValue(20);
obj.display();
}
}
A. 10
B. 20
C. 0
D. Compilation error
Can this be used to access methods of the superclass?
A. Yes, if the methods are not overridden
B. No, this cannot be used to access superclass methods
C. Yes, this can be used in a subclass
D. No, this is only for current instance methods
What will happen if this is used inside an instance method to refer to a static variable?
java
Copy code
class Test {
static int count = 5;
void display() {
System.out.println(this.count);
}
}
A. It will print the static variable value correctly
B. It will result in a compilation error
C. It will print null
D. It will throw a runtime exception
Which of the following statements correctly demonstrates constructor chaining using this?
java
Copy code
class Demo {
Demo() {
this(10); // Calls parameterized constructor
}
Demo(int x) {
System.out.println(x);
}
}
A. Yes, this demonstrates constructor chaining
B. No, constructor chaining cannot be done with this
C. No, this cannot be used in a constructor
D. Yes, but it will result in a compilation error
Can this be used to refer to the current instance in a static context?
A. Yes, if the instance is created inside the static method
B. No, this cannot be used in a static context
C. Yes, but only if this is explicitly passed as a parameter
D. No, because this is only for instance methods
What is the purpose of using this keyword in method chaining?
java
Copy code
class Chain {
Chain method1() {
System.out.println("Method1");
return this;
}
Chain method2() {
System.out.println("Method2");
return this;
}
}
A. To return the current instance for chaining method calls
B. To access static methods
C. To access methods of other classes
D. To call superclass methods
What does the following code print?
java
Copy code
class Person {
String name;
Person(String name) {
this.name = name;
}
void showName() {
System.out.println(this.name);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("John");
p.showName();
}
}
A. John
B. name
C. null
D. Compilation error
What is the result of using this in an inner class?
java
Copy code
class Outer {
int x = 10;
class Inner {
void display() {
System.out.println(Outer.this.x);
}
}
}
A. It will print 10
B. It will print 0
C. It will result in a compilation error
D. It will print null
How does this differ from super keyword?
A. this is used to refer to the current instance, super refers to the superclass
B. this is used to call static methods, super is for instance methods
C. this refers to static variables, super to instance variables
D. this is used to access superclass methods, super is for instance variables
What is the result of the following code snippet?
java
Copy code
class Test {
void display() {
System.out.println(this);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.display();
}
}
A. Prints the memory address of the Test object
B. Prints null
C. Prints Test
D. Prints the class name only
Which statement is true about using this in a constructor?
A. this can be used to call another constructor in the same class
B. this can call constructors in other classes
C. this must be the first statement in a constructor
D. this cannot be used in constructors
Can this be used to access private members of a class?
A. Yes, this can access private members within the same class
B. No, this cannot access private members
C. Yes, but only if this is used in a different class
D. No, this can only access public members
Answers:
B. The current instance of the class
B. To refer to instance variables and methods
A. To refer to another constructor in the same class (constructor chaining)
B. It cannot be used in static methods
B. The instance variable x
A. this can be used to call private methods in the same class
B. By referring to instance variables when their names conflict with parameter names
B. By referring to instance variables when there are local variables with the same name
B. 20
C. Yes, this can be used in a subclass
A. It will print the static variable value correctly
A. Yes, this demonstrates constructor chaining
B. No, this cannot be used in a static context
A. To return the current instance for chaining method calls
A. John
A. It will print 10
A. this is used to refer to the current instance, super refers to the superclass
A. Prints the memory address of the Test object
A. this can be used to call another constructor in the same class
A. Yes, this can access private members within the same class
No comments:
Post a Comment