1

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;
} 
A
3024
B
6561
C
55440
D
161051
References    
Discuss it


Question 1 Explanation: 
Since c is passed by value and x is passed by reference, all functions will have same copy of x, but different copies of c. f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4 Since x is incremented in every function call, it becomes 9 after f(x, 2) call. So the value of expression x^4 becomes 9^4 which is 6561. 1
Question 2
Which of the following is FALSE about references in C++
A
References cannot be NULL
B
A reference must be initialized when declared
C
Once a reference is created, it cannot be later made to reference another object; it cannot be reset.
D
References cannot refer to constant value
References    
Discuss it


Question 2 Explanation: 
We can create a constant reference that refers to a constant. For example, the following program compiles and runs fine.
#include<iostream>
using namespace std;

int main()
{
  const int x = 10;
  const int& ref = x;

  cout << ref;
  return 0;
}
Question 3
Which of the following functions must use reference.
A
Assignment operator function
B
Copy Constructor
C
Destructor
D
Parameterized constructor
References    
Discuss it


Question 3 Explanation: 
A copy constructor is called when an object is passed by value. Copy constructor itself is a function. So if we pass argument by value in a copy constructor, a call to copy constructor would be made to call copy constructor which becomes a non-terminating chain of calls. Therefore compiler doesn’t allow parameters to be pass by value. See http://geeksquiz.com/copy-constructor-in-cpp/ for details.
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;
}
A
Compiler Error: Function cannot be used as lvalue
B
10
C
30
References    
Discuss it


Question 4 Explanation: 
When a function returns by reference, it can be used as lvalue. Since x is a static variable, it is shared among function calls and the initialization line "static int x = 10;" is executed only once. The function call fun() = 30, modifies x to 30. The next call "cout << fun()" returns the modified value.
Question 5
#include<iostream>
using namespace std;

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

A
May cause runtime error
B
May cause compiler error
C
Always works fine.
D
0
References    
Discuss it


Question 5 Explanation: 
Since we return reference to a local variable, the memory location becomes invalid after function call is over. Hence it may result in segmentation fault runtime error.
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;
}
A
x = 20
ref = 30
B
x = 20
ref = 20
C
x = 10
ref = 30
D
x = 30
ref = 30
References    
Discuss it


Question 6 Explanation: 
ref is an alias of x, so if we change either of them, we can see the change in other as well.
There are 6 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice