C++ Programming MCQ – References

Q 1. What are the references in C++?
A. A new type of variables
B. A pointer to a variable
C. An alternative name for already existing variables
D. A new type of constant variable

Show Answer Answer:-C. An alternative name for already existing variables
Explanation References are an alternative name for an already defined variable. They are different from pointers.

Q 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. Compile-time error
D. Segmentation fault

Show Answer Answer:-C. Compile-time error
Explanation References require are other names for variables not for a constant literal. No such assignment are allowed in C++.

Q 3. 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 &p;
	int a = 5;
	&p = a;
	cout<<p;
	return 0;
}

A. 5
B. 55
C. Error
D. Segmentation fault

Show Answer Answer:-C. Error
Explanation Every reference should be initialized during its declaration but as p is not initialized here therfore the program gives error.

Q 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

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

Q 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

Show Answer Answer:-D. Compile-time error
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.

Q 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 p
C. Address of pointer a
D. Error

Show Answer Answer:-C. Address of pointer a
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.

Q 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

Show Answer Answer:-A. References are an alias for a variable whereas pointer stores the address of a variable
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.

Q 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

Show Answer Answer:-B. References and variables both have the same address
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++.

Q 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 p followed by Address of q 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 a followed by 5 in next line

Show Answer Answer:-D. Address of a followed by 5 in next line
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.

Q 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

Show Answer Answer:-D. Compile time error
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.

Q 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.55

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

Show Answer Answer:-D. Address of a followed by Address of a in next line
Explanation Both variable and reference shares the same address 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.

Q 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

Show Answer Answer:-D. Error
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.

Q 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

Show Answer Answer:-B. References once assigned cannot be changed to refer another variable
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.

Q 14. Which of the following operator is used while declaring references?
A. *
B. &
C. ^
D. ->

Show Answer Answer:-B. &
Explanation & operator is used for assigning references.

Q 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

Show Answer Answer:-C. Error
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page