C++ Questions and Answers Part-18

1. Identify the correct sentence regarding inequality between reference and pointer.
a) we can not create the array of reference
b) we can create the Array of reference
c) we can use reference to reference
d) we can use variable

Answer: a
Explanation: It is not allowed in C++ to make an array of references. To test check following array:
int &arr[] = {&a, &b, &c};
This will give an error.

2. 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

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

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 &q = 5;
cout<< q;
return 0;
}
a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error

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

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 &p;
int a = 5;
&p = a;
cout<< p;
return 0;
}
a) 5
b) 55
c) error
d) Segmentation fault

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

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;
cout<< p;
return 0;
}
a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error

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

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<< p;
return 0;
}
a) 5
b) Run-time error
c) Segmentation fault
d) Compile-time error

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.

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

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.

8. 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

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.

9. 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

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++.

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

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.