Object Oriented Programming using Java Questions and Answers – this Keyword

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “this Keyword”.

1. What will be the output of the following Java code?

public class Main
{
    String s="ABC";
    Main(String s)
    {
        this.s = s;
    }
    void print()
    {
        System.out.println(this.s);
    }
    public static void main(String[] args)
    {
        Main a = new Main("Sanfoundry");
        a.print();
    }
}

a) Compilation Error
b) Sanfoundry
c) Runtime Error
d) ABC
View Answer

Answer: b
Explanation: The this keyword is used to refer to the object that is calling that function. Here this is used to set the value of the string s to Sanfoundry and then to print it.
Output:

$ javac Main.java
$ java Main
Sanfoundry

2. What will be the output of the following Java code?

advertisement
advertisement
public class Main
{
    String s;
    Main(String s)
    {
        this.s = s;
    }
    void print()
    {
        System.out.println(this.s);
    }
    public static void main(String[] args)
    {
        Main a = new Main("San");
        Main b = new Main("foundry");
        a.print();
        b.print();
    }
}

a) Sanfoundry
b) Compilation Error
c)

San
foundry

d) Runtime Error
View Answer

Answer: c
Explanation: The this keyword is use to refer to the object calling the respective function. Both a and b have a different value of s, which is called using the this keyword.
Output:

advertisement
$ javac Main.java
$ java Main
San
foundry

3. What will be the output of the following Java code?

advertisement
public class Main
{
    int a=100, b=200;
    void calculate(int a, int b)
    {
        System.out.println(a+b);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.calculate(20, 30);
    }
}

a) 300
b) 50
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: In the function calculate, if this keyowrd is not used, it refers to the parameters passed into the function due to the scope of the variables.
Output:

$ javac Main.java
$ java Main
50

4. What will be the output of the following Java code?

public class Main
{
    int a=500, b=300;
    void sum(int a, int b)
    {
        System.out.println(this.a+this.b);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.sum(45, 60);
    }
}

a) 105
b) 800
c) Runtime Error
d) Compilation Error
View Answer

Answer: b
Explanation: In the sum function, this keyword is used, hence it refers to the global variables in the print statement.
Output:

$ javac Main.java
$ java Main
800

5. What will be the output of the following Java code?

public class Main
{
    int breadth, height;
    void Area(int breadth, int height)
    {
        System.out.println(this.breadth*this.height);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.Area(20, 30);
    }
}

a) 0
b) 50
c) 600
d) Compilation Error
View Answer

Answer: a
Explanation: When an object is created, all the variables are initialised to their respective default values. For integers, the default value is 0. In the Area function, the this keyword refers to the global variables, and as both are 0, it prints 0.
Output:

$ javac Main.java
$ java Main
0

6. What will be the output of the following Java code?

 public class Main
{
    String s;
    Main(String s)
    {
        this.s=s;
    }
    void position(char ch)
    {
        System.out.println(this.s.indexOf(ch));
    }
    public static void main(String[] args)
    {
        Main a = new Main("Sanfoundry");
        a.position('n');
    }
}

a) 2, 6
b) 2
c) 3, 7
d) 3
View Answer

Answer: b
Explanation: The indexOf function returns the index of the first instance of the character in the string calling it. Indexing starts from 0, so the nth position in the string is the (n-1)th index.
Output:

$ javac Main.java
$ java Main
2

7. What will be the output of the following Java code?

public class Main
{
    String s;
    Main(String s)
    {
        this.s=s;
    }
    void className()
    {
        System.out.println(this.s.getClass());
        System.out.println(this.s.getClass().getSuperclass());
    }
    public static void main(String[] args)
    {
        Main a = new Main("this");
        a.className();
    }
}

a)

String
Object 

b)

class java.io.String
class java.io.Object

c)

class java.lang.String
class java.lang.Object

d)

class java.util.String
class java.util.Object
View Answer
Answer: c
Explanation: Strings are a subclass of the class Object of the lang package in java. Since getClass and getSuperclass are used on a string, it prints out the classes.
Output:

$ javac Main.java
$ java Main
class java.lang.String
class java.lang.Object

8. What will be the output of the following Java code?

public class Main
{
    static class A
    {
        void print()
        {
            System.out.println(this.getClass());
            System.out.println(this.getClass().getSuperclass());
        }
    }
    public static void main(String[] args)
    {
        Main.A a = new Main.A();
        a.print();
    }
}

a)

class A
class Main

b)

class Main$A
class Main

c)

class Main$A
class java.lang.Object

d) Compilation Error
View Answer

Answer: c
Explanation: The object a is an object of the class A, but since A is a subclass of Main, it retains the class name as Main$A. The Main class is a subclass of the java Object class of the java package.
Output:

$ javac Main.java
$ java Main
class Main$A
class java.lang.Object

9. What will be the output of the following Java code?

public class Main
{
    String s;
    Main(String s)
    {
        this.s=s;
    }
    void character(int a)
    {
        System.out.println(this.s.charAt(a));
    }
    public static void main(String[] args)
    {
        Main a = new Main("this keyword");
        a.character(6);
    }
}

a) e
b) y
c) k
d) Compilation Error
View Answer

Answer: a
Explanation: The charAt function returns the character at the specified index of the string. As indexing starts from 0, the nth position is the (n-1)th index.
Output:

$ javac Main.java
$ java Main
e

10. What will be the output of the following Java code?

public class Main
{
    int a=10;
    static class A
    {
        int a=20;
        void display(int a)
        {
            System.out.println(this.a);
            System.out.println(a);
        }
    }
    public static void main(String[] args)
    {
        Main.A a = new Main.A();
        a.display(30);
    }
}

a)

10
20

b)

20
30

c)

10
30

d) Compilation Error
View Answer

Answer: b
Explanation: When this keyword is used, it refers to the object of the class calling that function. Here, the this keyword refers to the class A, hence the value of a is 20.
Output:

$ javac Main.java
$ java Main
20
30

11. What will be the output of the following Java code?

public class Main
{
    int a = 10;
    String s = "Sanfoundry"
    void display()
    {
        System.out.println(this);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.display();
    }
}

a) 10, “Sanfoundry”
b) 10 Sanfoundry
c) Main@2a139a55
d) Compilation Error
View Answer

Answer: c
Explanation: When an object is printed, it is in the format ClassName@HashCode. Here, this refers to the object of the class calling it, which is Main.
Output:

$ javac Main.java
$ java Main
Main@2a139a55

12. What will be the output of the following Java code?

public class Main
{
    int a = 2;
    void Shift(int a)
    {
        System.out.println(this.a<<a);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.Shift(4);
    }
}

a) 32
b) 16
c) 8
d) Compilation Error
View Answer

Answer: a
Explanation: The left shift operator shifts the bits to the left by adding 0s at the end. Here this.a refers to the value of the global variable, which is 2. This is shifted by 4 units, which is the same as multiplying it by 24.
Output:

$ javac Main.java
$ java Main
32

13. What will be the output of the following Java code?

public class Main
{
    char ch = 'a';
    void print(char ch)
    {
        System.out.println((int)ch);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.print('c');
    }
}

a) 97
b) 99
c) 67
d) 65
View Answer

Answer: b
Explanation: When a character is converted to int, it changes to it’s ascii value. As the this keyword is not used, it refers to the parameter passed to the function, which is ‘c’. The ascii value of ‘c’ is 99, while that of ‘C’ is 67.
Output:

$ javac Main.java
$ java Main
99

14. What will be the output of the following Java code?

public class Main
{
    int a = 102;
    void display(int a)
    {
        System.out.println((char)this.a);
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        a.display(105);
    }
}

a) f
b) i
c) F
d) I
View Answer

Answer: a
Explanation: When an integer is converted to a character, it is changed based on the ascii value of the integer. The ascii value of ‘f’ is 102, and that of ‘i’ is 105. The this keyword refers to the global variable, hence ‘f’ is printed.
Output:

$ javac Main.java
$ java Main
f

15. What will be the output of the following Java code?

public class Main
{
    String s="";
    void set(String s)
    {
        this.s = s;
    }
    void compare()
    {
        System.out.println(this.s.equals("Sanfoundry"));
    }
    public static void main(String[] args)
    {
        Main a = new Main();
        Main b = new Main();
        a.set("Sanfoundry");
        b.compare();
    }
}

a) true
b) Compilation Error
c) false
d) Runtime Error
View Answer

Answer: c
Explanation: The value of s is set to “Sanfoundry” for object a, but not for object b. Hence when compare is called, the value of s is “” and thus prints false.
Output:

$ javac Main.java
$ java Main
false

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.