C++ this Pointer Aptitude Questions and Answers

C++ this Pointer Aptitude: This section contains C++ this Pointer Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 18, 2021

1) There are the following statements that are given below, which of them are correct about "this" keyword in C++?

  1. In C++, "this" is a pointer that is used to point virtual functions.
  2. In C++, "this" is a pointer that contains the reference of the current object.
  3. In C++, "this" pointer can be used within the class only.
  4. In C++, "this" pointer can be used inside as well as outside the class.

Options:

  1. A and C
  2. B and C
  3. A, B and D
  4. A, B, and C

2) Can we pass the reference of the current object using "this" pointer?

  1. Yes
  2. No

3) Can we call member function in a cascaded manner using "this" pointer?

  1. Yes
  2. No

4) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Sample {
    int VAL;

public:
    Sample(int VAL)
    {
        VAL = this.VAL;
    }
    void Print()
    {
        cout << VAL;
    }
};

int main()
{
    Sample S(10);

    S.Print();
}

Options:

  1. 10
  2. Garbage value
  3. Syntax error
  4. Runtime error

5) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    int VAL;

public:
    Sample(int VAL)
    {
        VAL = this->VAL;
    }
    void Print()
    {
        cout << VAL;
    }
};

int main()
{
    Sample S(10);

    S.Print();
}

Options:

  1. 10
  2. Garbage value
  3. Syntax error
  4. Runtime error

6) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    int VAL;

public:
    Sample(int VAL)
    {
        this->VAL = VAL;
    }
    void Print()
    {
        cout << VAL;
    }
};

int main()
{
    Sample S(10);

    S.Print();
}

Options:

  1. 10
  2. Garbage value
  3. Syntax error
  4. Runtime error

7) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    int VAL;

public:
    Sample()
    {
        this->VAL = 0;
    }

    Sample(int VAL)
    {
        this->VAL = VAL;
    }

    Sample Sum(Sample S1, Sample S2)
    {
        this->VAL = S1.VAL + S2.VAL;

        return this;
    }

    void Print()
    {
        cout << VAL << " ";
    }
};

int main()
{
    Sample S1(10);
    Sample S2(20);

    Sample S3;

    Sample S4;

    S4 = S3.Sum(S1, S2);

    S1.Print();
    S2.Print();
    S3.Print();
    S4.Print();
}

Options:

  1. 10 20 30 30
  2. 10 20 0 30
  3. Syntax error
  4. Runtime error

8) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
    int VAL;

public:
    Sample()
    {
        this->VAL = 0;
    }

    Sample(int VAL)
    {
        this->VAL = VAL;
    }

    Sample Sum(Sample S1, Sample S2)
    {
        this->VAL = S1.VAL + S2.VAL;

        return *this;
    }

    void Print()
    {
        cout << VAL << " ";
    }
};

int main()
{
    Sample S1(10);
    Sample S2(20);

    Sample S3;

    Sample S4;

    S4 = S3.Sum(S1, S2);

    S1.Print();
    S2.Print();
    S3.Print();
    S4.Print();
}

Options:

  1. 10 20 30 30
  2. 10 20 0 30
  3. Syntax error
  4. Runtime error

9) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
public:
    Sample* fun1()
    {
        cout << "fun1 ";
        return this;
    }
    Sample* fun2()
    {
        cout << "fun2 ";
        return this;
    }
    Sample* fun3()
    {
        cout << "fun3 ";
        return this;
    }
};

int main()
{
    Sample S;

    S->fun1()->fun2()->fun3();
}

Options:

  1. fun1 fun2 fun3
  2. Syntax error
  3. Runtime error
  4. Garbage value

10) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Sample {
public:
    Sample fun1()
    {
        cout << "fun1 ";
        return *this;
    }
    Sample fun2()
    {
        cout << "fun2 ";
        return *this;
    }
    Sample fun3()
    {
        cout << "fun3 ";
        return *this;
    }
};

int main()
{
    Sample S;

    S.fun1().fun2().fun3();
}

Options:

  1. fun1 fun2 fun3
  2. Syntax error
  3. Runtime error
  4. Garbage value





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.