250+ TOP MCQs on References – 1 and Answers

This section on C++ interview questions and answers on “References”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “References” along with answers, explanations and/or solutions:

1. Which value can we not assign to reference?
a) integer
b) floating
c) unsigned
d) null
Answer: d
Clarification: 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
Answer: b
Clarification: 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) @
Answer: a
Clarification: & aka ‘ampersand’ used to define a reference variable.

4. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     void swap(int &a, int &b);
  4.     int main()
  5.     {
  6.         int a = 5, b = 10;
  7.         swap(a, b);
  8.         cout << "In main " << a << b;
  9.         return 0;
  10.     }
  11.     void swap(int &a, int &b)
  12.     {
  13.         int temp;
  14.         temp = a;
  15.         a = b;
  16.         b = temp;
  17.         cout << "In swap " << a << b;
  18.     }

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
Answer: a
Clarification: 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
Answer: b
Clarification: Because we are pointing memory address using the temp variable.

6. What will be the output of the following C++ code?

  1.     #include 
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 9;
  6.         int & aref = a;
  7.         a++;
  8.         cout << "The value of a is " << aref;
  9.         return 0;
  10.     }

a) 9
b) 10
c) error
d) 11
Answer: b
Clarification: 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?

  1.     #include 
  2.     using namespace std;
  3.     void print (char * a)
  4.     {
  5.         cout << a << endl;
  6.     }
  7.     int main ()
  8.     {
  9.         const char * a = "Hello world";
  10.         print(const_cast<char *> (a) );
  11.         return 0;
  12.     }

a) Hello world
b) Hello
c) world
d) compile time error
Answer: a
Clarification: 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
Answer: a
Clarification: 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.

  250+ TOP MCQs on Exception Handling – 1 and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top