Preview

10 - this Keyword

 1. The keyword "this" can be used in a class to refer to the _____________.

  current calling object.

  instance variable.

  declared method.

  constructor parameters.

 2. In the following Class Person, when we create an object p1 and call the constructor or p1.setEmail(), the word "this" refers to ____.
public class Person
{
   // instance variables
    private String name;
    private String email;
    private String phoneNumber;

   // constructor
   public Person(String theName)
   {
      this.name = theName;
   }

   // accessor methods - getters
   public String getName() { return this.name;}
   public String getEmail() { return this.email;}
   public String getPhoneNumber() { return this.phoneNumber;}

   // mutator methods - setters
   public void setName(String theName) { this.name = theName;}
   public void setEmail(String theEmail) {this.email = theEmail;}
   public void setPhoneNumber(String thePhoneNumber) { this.phoneNumber = thePhoneNumber;}
   public String toString()
   {
      return this.name + " " + this.email + " " + this.phoneNumber;
   }

   // main method for testing
   public static void main(String[] args)
   {
      Person p1 = new Person("Jonathan");
      System.out.println(p1);
      Person p2 = new Person("Peter");
      p2.setEmail("[email protected]");
      p2.setPhoneNumber("404 899-9955");
      System.out.println(p2);
   }
}

  theName

  this.name

  p1

  Person

 3. Referring to the code above, if we make the same method calls with object p2, "this" refers to ____.

  this

  theName

  p1

  p2

 4. this.variable (e.g. this.name or this.email) is a way to indicate that we are referring to the instance variables of this object instead of a __________.

  global variable

  local method

  local variable

  variable passed by reference

 5. Static methods can directly refer to this or instance variables because they are not called with the classname.

  TRUE

  FALSE

 6. The keyword this is sometimes used by programmers to distinguish between variables.
Additional information: Programmers can give the parameter variables the same names as the instance variables and this can distinguish them and avoid a naming conflict.

  TRUE

  FALSE

 7. In the code below, both the instance variable and the parameter variable are called _____ in the code below.
// instance variables
private String name;

// constructor
public Person(String name)
{
   // Set this object's instance variable name to the parameter variable name
   this.name = name;
}

  this

  this.name

  String

  name

 8. The this variable can be used anywhere you would use an object variable.

  TRUE

  FALSE

 9. The this variable can never be passed to another method as an argument.

  TRUE

  FALSE

 10. The image below shows how this and myPay and p all refer to ___________________________.
Image Source: https://runestone.academy/
thiskeyword_APComp_intermediate.png

  a different object in memory.

  an object variable that is identical to the constructor variable.

  an instance variable.

  the same object in memory.

 11. What is the output of the following code?
public class Pay
{
 private double pay;

 public Pay(double p)
 {
     pay = p;
 }

 public double getPay()
 {
     return pay;
 }

 public void calculatePayWithOvertime()
 {
     // this Pay object is passed to the Overtime constructor
     Overtime ot = new Overtime(this);
     pay = ot.getOvertimePay();
 }

 public static void main(String[] args)
 {
     Pay myPay = new Pay(100.0);
     myPay.calculatePayWithOvertime();
     System.out.println(myPay.getPay());
 }
}

class Overtime
{
 private double payWithOvertime;

 public Overtime(Pay p)
 {
     payWithOvertime = p.getPay() * 1.5;
 }

 public double getOvertimePay()
 {
     return payWithOvertime;
 }
}

  Answer: 150.0

  Answer: 100

  Answer: 120.0

  Answer: Error

 12. Within a non-static method or a constructor, the keyword this is a reference to the current object, the object whose method or __________________.

  constructor is being called.

  object is being created.

  attribute is being changed in scope.

  attribute is ambiguous.

 13. Static methods always have a this reference.

  TRUE

  FALSE

 14. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

  FALSE

  TRUE

 15. The output of the following code is:
public class MyClass {
   
    int num;
    public void setNum(int num)
    {
        this.num = num;
    }
    
    public static void main(String[] args) {
        
        MyClass obj = new MyClass();
        obj.setNum(5);
        
        System.out.println("Num value : "+obj.num);
 
    }  
}

  2

  5

  3

  6