This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “References – 2”.
1. Which value can we not assign to reference?
a) integer
b) floating
c) unsigned
d) null
View Answer
Explanation: If it can be assigned with a null value means, it is a copy of the pointer.
2. Identify the incorrect statement.
a) Reference is the alternate name of the object
b) A reference value once defined can be reassigned
c) A reference value once defined cannot be reassigned
d) Reference is the alternate name of the variable
View Answer
Explanation: Reference is a thing which points to the valid memory address, so it can’t be redesigned.
3. Which reference modifier is used to define the reference variable?
a) &
b) $
c) #
d) @
View Answer
Explanation: & aka ‘ampersand’ used to define a reference variable.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void swap(int &a, int &b);
int main()
{
int a = 5, b = 10;
swap(a, b);
cout << "In main " << a << b;
return 0;
}
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
cout << "In swap " << a << b;
}
a) In swap 105 In main 105
b) In swap 105 In main 510
c) In swap 510 In main 105
d) In swap 510 In main 510
View Answer
Explanation: As the function is called by reference i.e. all the changes are done directly into the memories of a and b. Therefore changes made to a and b in swap function is reflected back to main function. Hence the values of a and b in swap as well as in main function is changed.
Output:
$ g++ ref.cpp $ a.out In swap 105 In main 105
5. What does a reference provide?
a) Alternate name for the class
b) Alternate name for the variable
c) Alternate name for the pointer
d) Alternate name for the object
View Answer
Explanation: Because we are pointing memory address using the temp variable.
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 9;
int & aref = a;
a++;
cout << "The value of a is " << aref;
return 0;
}
a) 9
b) 10
c) error
d) 11
View Answer
Explanation: The value is declared and it isincrementedrement, so it’s value is 10.
$ g++ ref1.cpp $ a.out 10
7. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void print (char * a)
{
cout << a << endl;
}
int main ()
{
const char * a = "Hello world";
print(const_cast<char *> (a) );
return 0;
}
a) Hello world
b) Hello
c) world
d) compile time error
View Answer
Explanation: In this program we used the concept of constant casting to cast the variable and printing it.
Output:
$ g++ ref2.cpp $ a.out Hello world
8. Identify the correct sentence regarding inequality between reference and pointer.
a) we can not create the array of reference
b) we can create the Array of reference
c) we can use reference to reference
d) we can use variable
View Answer
Explanation: It is not allowed in C++ to make an array of references. To test check following array:
int &arr[] = {&a, &b, &c};
This will give an error.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Programming MCQs
- Check Computer Science Books
- Check Programming Books
- Apply for C++ Internship
- Check C++ Books