Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
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++?
- In C++, "this" is a pointer that is used to point virtual functions.
- In C++, "this" is a pointer that contains the reference of the current object.
- In C++, "this" pointer can be used within the class only.
- In C++, "this" pointer can be used inside as well as outside the class.
Options:
- A and C
- B and C
- A, B and D
- A, B, and C
Correct Answer - 2
B and C
Explanation:
Statements B and C are correct about "this" pointer.
2) Can we pass the reference of the current object using "this" pointer?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, we can pass the reference of the current object using "this" pointer.
3) Can we call member function in a cascaded manner using "this" pointer?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, we can call member function in a cascaded manner using "this" pointer.
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:
- 10
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error. Because "this" is a pointer then we need to use the referential operator "->" to access the members.
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:
- 10
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 2
Garbage value
Explanation:
The above code will print garbage value.
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:
- 10
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 1
10
Explanation:
The above code will print "10" on the console screen.
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:
- 10 20 30 30
- 10 20 0 30
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax 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:
- 10 20 30 30
- 10 20 0 30
- Syntax error
- Runtime error
Correct Answer - 1
10 20 30 30
Explanation:
The code print "10 20 30 30" on the console screen.
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:
- fun1 fun2 fun3
- Syntax error
- Runtime error
- Garbage value
Correct Answer - 2
Syntax error
Explanation:
The above code will generate a syntax error.
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:
- fun1 fun2 fun3
- Syntax error
- Runtime error
- Garbage value
Correct Answer - 1
fun1 fun2 fun3
Explanation:
The above code will print "fun1 fun2 fun3 " on the console screen.