C++ Questions and Answers Part-19

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

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.

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

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.

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 = NULL;
cout<< q;
return 0;
}
a) NULL
b) 0
c) Address of NULL
d) error

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.

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

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.

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

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

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

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.

7. The void pointer can point to which type of objects?
a) int
b) float
c) double
d) all of the mentioned

Answer: d
Explanation: Because it doesn’t know the type of object it is pointing to, So it can point to all objects.

8. When does the void pointer can be dereferenced?
a) when it doesn’t point to any value
b) when it cast to another type of object
c) using delete keyword
d) using shift keyword

Answer: b
Explanation: By casting the pointer to another data type, it can be dereferenced from the void pointer.

9. The pointer can point to any variable that is not declared with which of these?
a) const
b) volatile
c) both const & volatile
d) static

Answer: c
Explanation: Pointer can point to any variable that is not declared with const & volatile.

10. A void pointer cannot point to which of these?
a) methods in c++
b) class member in c++
c) methods & class member in c++
d) none of the mentioned

Answer: d
Explanation: A void pointer can point to methods & class member in c++.