Exercise: Questions on References


Questions on References : Question 1 :
What is the return value of f(p, p) if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f(int &x, int c) {
   c  = c - 1;
   if (c == 0) return 1;
   x = x + 1;
   return f(x, c) * x;
} 

3024
6561
55440
161051
Show Answer
Questions on References : Question 2 :
Which of the following is FALSE about references in C++

References cannot be NULL
A reference must be initialized when declared
Once a reference is created, it cannot be later made to reference another object; it cannot be reset.
References cannot refer to constant value
Show Answer
Questions on References : Question 3 :
Which of the following functions must use reference.

Assignment operator function
Copy Constructor
Destructor
Parameterized constructor
Show Answer
Questions on References : Question 4 :
Predict the output of following C++ program.
#include<iostream>
using namespace std;

int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}

Compiler Error: Function cannot be used as lvalue
10
30
Show Answer
Questions on References : Question 5 :
#include<iostream>
using namespace std;

int &fun()
{
    int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}


May cause runtime error
May cause compiler error
Always works fine.
0
Show Answer
Questions on References : Question 6 :
Output of following C++ program?
#include<iostream>
using namespace std;

int main()
{
  int x = 10;
  int& ref = x;
  ref = 20;
  cout << "x = " << x << endl ;
  x = 30;
  cout << "ref = " << ref << endl;
  return 0;
}

x = 20
ref = 30
x = 20
ref = 20
x = 10
ref = 30
x = 30
ref = 30
Show Answer