• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ References

Question 1

Which of the following functions must use reference.
  • Assignment operator function
  • Copy Constructor
  • Destructor
  • Parameterized constructor

Question 2

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

Question 3

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

Question 4

Predict the output of following C++ program. C
#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

Question 5

CPP
#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

Question 6

Output of following C++ program? CPP
#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
    

There are 6 questions to complete.

Last Updated :
Take a part in the ongoing discussion