Java - Interview Questions and Answers for 'This keyword' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'This keyword' - 6 question(s) found - Order By Newest

 Q1. Why can't we use this in static context ?Core Java
Admin
info@buggybread.com
Ans. Static methods can be called using instance references wherein this would have made sense but static method can also be called using Class name wherein this would mean nothing and hence forbidden.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   static methods   this keyword

Try 2 Question(s) Test


 Q2. Can we use "this" within static method ? Why ?Core Java
Admin
info@buggybread.com
Ans. No. Even though "this" would mean a reference to current object id the method gets called using object reference but "this" would mean an ambiguity if the same static method gets called using Class name.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   static   this keyword   this   yes no   why questions      intermediate


 Q3. In majority of the situations it won't make much sense whether you append this with the instance method call.Can you tell a situation wherein this keyword would make sense in a instance method ? Core Java
admin
info@buggybread.com
Ans. Within instance method of the parent class that has other multiple methods that have been overridden by the derived classes.

In such case a simple method call from the common method will always be made to the method definition in the parent class. But If we use this.methodCall , this will be polymorphic and will be made to the respective derived object overriding method.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   this keyword   java keywords


 Q4. Which of the following is not the use of this keyword ?

a. Passing itself to another method
b. To call the static method
c. Referring to the instance variable when local variable has the same name
d. Calling another constructor in constructor chaining
Anonymous
Ans. To call the static method

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     this   this keyword   java


 Q5. Does compiler treats it differently if we don't prefix this to the member element and its implicit as its the only variable available with that name ?2016-05-29 10:18:38

Ans. It makes no difference whether we add this to the variable or not. The only use of this is in the cases where there are multiple variables with the same name and we want to distinguish between the member variable and local variable. In case this is not added ,this is automatically added by the compiler in the byte code.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     this keyword  member elements  member variables


 Q6. What will be the output of following code ?

public class BuggyBread {

   private int x;

   private BuggyBread(int x){
      x = x;
   };

   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread(5);
      System.out.println(buggybread.x);
   }
}
Core Java
a. compilation error
b. undefined
c. 0
d. 5

Ans.c. 0



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: