C++ References Interview Preparation Guide

Refine your C++ References interview skills with our 16 critical questions. These questions are specifically selected to challenge and enhance your knowledge in C++ References. Perfect for all proficiency levels, they are key to your interview success. Dont miss out on our free PDF download, containing all 16 questions to help you succeed in your C++ References interview. Its an invaluable tool for reinforcing your knowledge and building confidence.
Tweet Share WhatsApp

16 C++ References Questions and Answers:

1 :: What is the difference between pointer and reference?

When a reference is created, it can’t reference another object. This can be done with pointers. References cannot be null whereas pointers can be. References cannot be uninitialized and it is not possible to refer directly to a reference object after it is defined.

Example:
Syntax of reference:
<Type> & <Name>
int& rA = A; rA is a reference to int.
Download PDFRead All C++ References Questions

2 :: What means pass by value?

The callee function receives a set of values that are to be received by the parameters. All these copies of values have local scope, i.e., they can be accessed only by the callee function. The simplicity and guarantee of unchanging of values passed are the advantages of pass by value.

3 :: What means pass by reference?

The callee function receives a set of references which are aliases to variables. If a change is made to the reference variable, the original value (passed by the caller function) will also be changed. All the references are handled by the pointers. Multiple values modification can be done by passing multiple variables.

4 :: What means pass by pointer?

The callee function receives a pointer to the variable. The value of the pointer in the caller function can then be modified. The advantages of this process are that the changes are passed back to the caller function and multiple variables can be changed.

5 :: What is reference variable?

A reference variable is just like pointer with few differences. It is declared using & operator. A reference variable must always be initialized. The reference variable once defined to refer to a variable can’t be changed to point to other variable. You can't create an array of references the way it is possible with pointer.
Download PDFRead All C++ References Questions

6 :: What is a local reference?

A reference which has a local scope i.e., in a method or in a block or in a function is known as local reference.

7 :: What are References in C++?

A restricted type of pointer in C++ is known as a reference. A reference can be assigned only once and can not have a null value.

8 :: Can you please explain the difference between pass by value and pass by reference?

In pass by value approach, the called function creates another copies of the variables passes as arguments. In this approach, the values of the original variables remain unchanged. However, we come across situations where we need to change the values of the original variables. Then the values may b passed by reference.

9 :: 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) none of the mentioned

c) A reference value once defined cannot be reassigned

10 :: What is the output of this program?

#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) none of the mentioned

a) In swap 105 In main 105
Download PDFRead All C++ References Questions