References(C++ Programming ) Questions and Answers

Question 1.


What will be the output of the following program?


#include
enum xyz
{
a, b, c
};
int main()
{
int x = a, y = b, z = c;
int &p = x, &q = y, &r = z;
p = z;
p = ++q;
q = ++p;
z = ++q + p++;
cout
  1.    2 3 6
  2.    4 4 7
  3.    4 5 8
  4.    3 4 6
Explanation:-
Answer: Option B. -> 4 4 7



Question 2.


Which of the following statement is correct about the program given below?


#include
int main()
{
int arr[] = {1, 2 ,3, 4, 5};
int &zarr = arr;
for(int i = 0; i
  1.    The program will print the output 1 2 3 4 5.
  2.    The program will print the output 2 4 6 8 10.
  3.    The program will print the output 1 1 1 1 1.
  4.    It will result in a compile time error.
Explanation:-
Answer: Option D. -> It will result in a compile time error.



Question 3.


Which of the following statement is correct about the program given below?


#include
enum xyz
{
a, b, c
};
int main()
{
int x = a, y = b, z = c;
int &p = x, &q = y, &r = z;
p = ++x;
q = ++y;
r = ++c;
cout
  1.    The program will print the output 1 2 3.
  2.    The program will print the output 2 3 4.
  3.    The program will print the output 0 1 2.
  4.    It will result in a compile time error.
Explanation:-
Answer: Option D. -> It will result in a compile time error.



Question 4.


Which of the following statement is correct about the program given below?


#include
int main()
{
int m = 2, n = 6;
int &x = m++;
int &y = n++;
m = x++;
x = m++;
n = y++;
y = n++;
cout
  1.    The program will print output 3 7.
  2.    The program will print output 4 8.
  3.    The program will print output 5 9.
  4.    The program will print output 6 10.
  5.    It will result in a compile time error.
Explanation:-
Answer: Option E. -> It will result in a compile time error.



Question 5.


Which of the following statement is correct about the program given below?


#include
int main()
{
int m = 2, n = 6;
int &x = m;
int &y = n;
m = x++;
x = m++;
n = y++;
y = n++;
cout
  1.    The program will print output 2 6.
  2.    The program will print output 3 7.
  3.    The program will print output 4 8.
  4.    The program will print output 5 9.
  5.    The program will print output 6 10.
Explanation:-
Answer: Option C. -> The program will print output 4 8.



Question 6.

Identify the correct sentence regarding inequality between reference and pointer.


  1.    we can not create the array of reference.
  2.    we can create the Array of reference.
  3.    we can use reference to reference.
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> we can not create the array of reference.

None.



Question 7.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
void print (char * a)
4.
{
5.
cout
  1.    Hello world
  2.    Hello
  3.    world
  4.    compile time error
Explanation:-
Answer: Option A. -> Hello world

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



Question 8.


What is the output of this program?


1.
#include
2.
using namespace std;
3.
int main()
4.
{
5.
int a = 9;
6.
int & aref = a;
7.
a++;
8.
cout
  1.    9
  2.    10
  3.    error
  4.    11
Explanation:-
Answer: Option B. -> 10

The value is declared and it is post incremented, so it's value is 10.
$ g++ ref1.cpp
$ a.out
10




Question 9.

What does a reference provide?


  1.    Alternate name for the class
  2.    Alternate name for the variable
  3.    Alternate name for the pointer
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> Alternate name for the variable

Because we are pointing memory address using temp variable



Question 10.


What is the output of this program?


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
  1.    In swap 105 In main 105
  2.    In swap 105 In main 510
  3.    In swap 510 In main 105
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> In swap 105 In main 105

As we are calling by reference the values in the address also changed. So the main and swap 

values also changed.
Output:
$ g++ ref.cpp
$ a.out
In swap 105 In main 105