C++ Programming Questions and Answers – References – 3

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “References – 3”.

1. What will be the output of the following C++ code?

advertisement
#include <iostream>  
using namespace std;
int f(int &x, int c) 
{
   c  = c - 1;
   if (c == 0) return 1;
   x = x + 1;
   return f(x, c) * x;
} 
int main(int argc, char const *argv[])
{
	int a = 4;
	cout<<f(a,a);
	return 0;
}

a) 343
b) 336
c) 120
d) 840
View Answer

Answer: a
Explanation: In this program as one parametere is passed by value and other is passed by reference so after 4 calls when c == 0, then the value of x = 7 and as x is passed by reference so all the changes will be reflected back in all the previous calls hence the answer 1*7*7*7 = 343.

2. Which of the following is incorrect?
a) References cannot be NULL
b) A reference must be initialized when declared
c) Once a reference is declared, it cannot be modified later to reference another object i.e. it cannot be reset
d) References cannot refer to a constant value
View Answer

Answer: d
Explanation: C++ allows references to refer to a constant value by making constant references. For example:
const int a = 5;
const int &ref = a;
is an example of that.

3. Which of the following function must use reference.
a) Assignment operator function
b) Copy Constructor
c) Destructor
d) Parameterized constructor
View Answer

Answer: b
Explanation: We don’t need references in case of assignment, destructor or constructor. But in case of a copy constructor, we need to call copy constructor because if we use pass by value then as copy constructor itself is a function. So if we pass an argument bypass by value method in a copy constructor, a call to copy constructor would be made to again call copy constructor which becomes an endless chain of calls. Therefore compiler doesn’t allow parameters to be passed by value.
advertisement

4. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
 
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}

a) 30
b) 10
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: A function returning value by reference can be used as lvalue i.e. it can be used on the left side of an expression. Here when we are doing fun() = 30 then we are changing the value of x(i.e. value returning) to and as x is static therefore it will not be initialized again so the value of x becomes 30 hence the output is 30.

5. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
 
int &fun()
{
    int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}
 
#include<iostream>
using namespace std;
 
int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}

a) 30
b) 10
c) Error
d) Segmentation fault
View Answer

Answer: d
Explanation: In this case we are trying to assign 30 to a local variable which is returned form the function func() which will be destroyed after the function call hence next time this assignmnet is not correct hence segmentation fault.
advertisement

6. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
 
int main()
{
   int x = 10;
   int& ref = x;
   ref = 20;
   cout << x << endl ;
   x = 30;
   cout << ref << endl;
   return 0;
}

a)

20
30

b)

10
10
advertisement

c)

10
20

d)

10 
30
View Answer
Answer: a
Explanation: As we know references are alias for a variable so the value of a variable can be changed using alias hence both ref and x are same therefore changing the value of one effects the value of other.
 
 

7. How a reference is different from a pointer?
a) A reference cannot be null
b) A reference once established cannot be changed
c) The reference doesn’t need an explicit dereferencing mechanism
d) All of the mentioned
View Answer

Answer: d
Explanation: References can never be NULL. It is not allowed to change a reference once allocated. Referencing does not need an explicit referencing operator.

8. Which of the following statement(s) is/are correct?
a) * operator is used to declare a reference
b) A reference variable defined to refer a particular variable can refer to any other variable also
c) References must always be initialized inside classes
d) A variable can have more than one references
View Answer

Answer: d
Explanation: A variable can have multiple references as references are nothing just another name for a variable hence a variable can more than one references.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn