C++ Programming MCQ – References

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

1. What are the references in C++?
a) An alternative name for already existing variables
b) A pointer to a variable
c) A new type of variables
d) A new type of constant variable
View Answer

Answer: a
Explanation: References are an alternative name for an already defined variable. They are different from pointers.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int &q = 5;
	cout<<q;
	return 0;
}

a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error
View Answer

Answer: d
Explanation: References require are other names for variables not for a constant literal. No such assignment are allowed in C++.
advertisement
advertisement

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int &p;
	int a = 5;
	&p = a;
	cout<<p;
	return 0;
}

a) 5
b) 55
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: Every reference should be initialized during its declaration but as p is not initialized here therfore the program gives error.
advertisement

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int &p = a;
	cout<<p;
	return 0;
}

a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error
View Answer

Answer: a
Explanation: In this program, every thing is correct so the program runs perfectly and prints the 5 as output.
advertisement

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int *p = &a;
	int &q = p;
	cout<<p;
	return 0;
}

a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error
View Answer

Answer: d
Explanation: A pointer cannot be directly assigned to references, because types of pointer(int*) and reference(int) are different here. You need to think before assigning two variable of different types otherwise the program throws error.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int *p = &a;
	int *(&q) = p;
	cout<<q;
	return 0;
}

a) 5
b) Address of pointer a
c) Address of pointer p
d) Error
View Answer

Answer: b
Explanation: The program is correct so the the program runs perfectly. It is way to assign pointers to references. The program prints the address of a because it is an alias for pointer p and pointer p stores the address of a therefore answer is address of a.

7. What is the difference between references and pointers?
a) References are an alias for a variable whereas pointer stores the address of a variable
b) References and pointers are similar
c) References stores address of variables whereas pointer points to variables
d) Pointers are an alias for a variable whereas references stores the address of a variable
View Answer

Answer: a
Explanation: References are an alias/another name for a variable whereas pointer stores the address of a variable. Pointers need to be deference before use whereas references need not.

8. Pick the correct statement about references in C++.
a) References stores the address of variables
b) References and variables both have the same address
c) References use dereferencing operator(*) to access the value of variable its referencing
d) References were also available in C
View Answer

Answer: b
Explanation: References and variable it is referring to shares the same address. References do not consume extra address. References do not store the address of other variables. No dereferencing operator required while using references. References are not available in C++.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int *p = &a;
	int &q = a;
	cout<<p<<endl;
	cout<<q<<endl;
	return 0;
}

a) Address of a followed by 5 in next line
b) Address of p followed by 5 in next line
c) Address of a followed by Address of a in next line
d) Address of p followed by Address of q in next line
View Answer

Answer: a
Explanation: Pointer p stores the address of variable whereas q is alias for variable a therefore when p is printed it prints the address of a and when q is printed value of a is printed.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int *p = &a;
	int &q = a;
	cout<<*p<<endl;
	cout<<*q<<endl;
	return 0;
}

a) Address of a followed by 5 in next line
b) Address of p followed by 5 in next line
c) Run time error
d) Compile time error
View Answer

Answer: d
Explanation: References uses no * operator to access the value of variables it is refering to therefore no program gives error as we are using * operator.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int a = 5;
	int &q = a;
	cout<<&a<<endl;
	cout<<&q<<endl;
	return 0;
}

a)

5
5

b) Address of p followed by 5 in next line
c) 5 followed by Address of a in next line
d) Address of a followed by Address of a in next line
View Answer

Answer: d
Explanation: Both variable and reference shares the same addres so the output will be two times the address of a, because references are other name for same variable not a new variable with separate memory.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
int main(int argc, char const *argv[])
{
	int &q = NULL;
	cout<<q;
	return 0;
}

a) NULL
b) 0
c) Address of NULL
d) Error
View Answer

Answer: d
Explanation: NULL cannot be assigned to references therefore the program gives error. Here it is an int reference and NULL is not an int therefore cannot be assigned to this reference.

13. Pick the correct statement about references.
a) References can be assigned value NULL
b) References once assigned cannot be changed to refer another variable
c) Reference should not be initialized when created
d) Reference is the same as pointers
View Answer

Answer: b
Explanation: References are should be initialized during its creation and once assigned cannot be changed to refer another variable. References cannot be assigned NULL value. References and pointers are two different concepts.

14. Which of the following operator is used while declaring references?
a) *
b) &
c) ^
d) ->
View Answer

Answer: b
Explanation: & operator is used for assigning references.

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

#include <iostream>
#include <string>
#include <cstdlib>
 
using namespace std;
 
void func(const int &a)
{
	int temp = 10;
	a = temp;
	cout<<a;
}
 
int main(int argc, char const *argv[])
{
	int a = 5;
	func(a);
	return 0;
}

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

Answer: c
Explanation: As we are passing a as const reference to function therefore its value cannot be changes inside the function. So the program gives error.

More References MCQs in C++ Programming:

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

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.