Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ Reference Variable Aptitude Questions and Answers
C++ Reference Variable Aptitude: This section contains C++ Reference Variable Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 21, 2021
1) There are the following statements that are given below, which of them are correct about reference variables in C++?
- Reference variables are used to provide the new name to the existing variables.
- The size of the reference variable is fixed, any type of reference variable occupies 4 bytes in memory.
- If we made any changes in the reference variable, it will also reflect in the original variable.
- The reference variables do not occupy space in memory.
Options:
- A and B
- A and C
- A, B, and C
- A, C, and D
Correct Answer - 4
A, C, and D
Explanation:
Statements A, C, and D are correct about the reference variable in C++.
2) Which of the following symbol is used to create a reference variable in C++?
- $
- *
- &
- @
Correct Answer - 3
&
Explanation:
The "&" is used to create a reference variable.
3) Can we create multiple reference variables of an existing variable?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
Yes, we can create multiple reference variables of an existing variable.
4) Can we bind a reference variable with multiple existing variables?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, we can bind a reference variable with only one existing variable.
5) Which of following is correct syntax, if we want to create a reference variable of an existing integer variable?
- int a=10; int r = &a;
- int a=10; int &r = a;
- int a=10; int &r = &a;
- int a=10; int &r; r = a;
Correct Answer - 2
int a=10; int &r = a;
Explanation:
The 2nd option is correct.
6) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int& r1 = a;
int& r2 = b;
cout << r1 << " " << r2 << endl;
return 0;
}
Options:
- 10 20
- 20 10
- Compile-time error
- Runtime error
Correct Answer - 1
10 20
Explanation:
The above code will print "10 20" on the console screen.
7) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int& r1 = a;
int& r2 = b;
r1 = 30;
r2 = 40;
cout << b << " " << a << endl;
return 0;
}
Options:
- 30 40
- 40 30
- Compile-time error
- Runtime error
Correct Answer - 2
40 30
Explanation:
The above code will print "40 30" on the console screen.
8) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int& r1 = a;
r1 = b;
cout << r1 << endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.
9) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
int a=10;
int b=20;
int &r1 = a;
&r1 = b;
r1 =30;
cout << a <<" "<<b<< endl;
return 0;
}
Options:
- 10 30
- 30 20
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a syntax error because we cannot bind a reference variable with an existing variable.
10) What is the correct output of the given code?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int* p;
p = &a;
int& r = p;
r = 20;
cout << a << " " << *p << " " << r << endl;
return 0;
}
Options:
- 20 20 20
- 20 20
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error.
11) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int* p;
p = &a;
int& r = *p;
r = 20;
cout << a << " " << *p << " " << r << endl;
return 0;
}
Options:
- 20 20 20
- 20 20
- Compile-time error
- Runtime error
Correct Answer - 1
20 20 20
Explanation:
The above code will print "20 20 20" on the console screen.
12) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
short a = 10;
short* p;
p = &a;
long& r = *p;
r = 20;
cout << a << " " << *p << " " << r << endl;
return 0;
}
Options:
- 20 20 20
- 20 20
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate a compile-time error. Because long type reference variable cannot bind with a short type variable.
13) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
int main()
{
short a = 10;
short& r = a;
if (&r == &a)
cout << "Same address";
else
cout << "Different address";
return 0;
}
Options:
- Same address
- Different address
- Compile-time error
- Runtime error
Correct Answer - 1
Same address
Explanation:
The above code will print "Same address" on the console screen.
14) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
void fun(short& X)
{
X = 20;
}
int main()
{
short a = 10;
fun(a);
cout << a << endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.
15) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
void fun(static short& X)
{
X = 20;
}
int main()
{
static short a = 10;
fun(a);
cout << a << endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 3
Compile-time error
Explanation:
The above code will generate an error.
16) What is the correct output of given code snippets?
#include <iostream>
using namespace std;
void fun(short& X)
{
X = 20;
}
int main()
{
static short a = 10;
fun(a);
cout << a << endl;
return 0;
}
Options:
- 10
- 20
- Compile-time error
- Runtime error
Correct Answer - 2
20
Explanation:
The above code will print "20" on the console screen.