C++ Programming Questions and Answers – Dereferencing

This section on C question bank focuses on “Dereferencing”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C question bank comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C question bank on “Dereferencing” along with answers, explanations and/or solutions:

1. Which is used to tell the computer that where a pointer is pointing to?
a) dereference
b) reference
c) heap operations
d) binary operations
View Answer

Answer: a
Explanation: dereference is used to tell the computer where a pointer is pointing to it.

2. Which is used to do the dereferencing?
a) pointer without asterix
b) value without asterix
c) pointer with asterix
d) value with asterix
View Answer

Answer: c
Explanation: Dereferencing is using a pointer with asterix. For example, *(abc).

3. Pick out the correct option.
a) References automatically dereference without needing an extra character
b) References automatically dereference with an extra character
c) Reference will not dereference
d) Reference automatically dereference with extra space and character
View Answer

Answer: a
Explanation: References automatically dereference without needing an extra character.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a, b;
  6.         int* c;
  7.         c = &a;
  8.         a = 200;
  9.         b = 200;
  10.         *c = 100;
  11.         b = *c;
  12.         cout << *c << " " << b;
  13.         return 0;
  14.     }

a) 100 200
b) 100 0
c) 200 200
d) 100 100
View Answer

Answer: d
Explanation: In this program, We are making the assignments and invoking the both b and c values as 100 by dereference operator.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ def.cpp
$ a.out
100 100

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int x;
  6.         int *p;
  7.         x = 5;
  8.         p = &x;
  9.         cout << *p;
  10.         return 0;
  11.     }

a) 5
b) 10
c) memory address
d) 15
View Answer

Answer: a
Explanation: In this program, we are copying the memory location of x into p and then printing the value in the address.
Output:

advertisement
$ g++ def1.cpp
$ a.out
5

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main ()
  4.     {
  5.         int a;
  6.         int * ptr_b;
  7.         int ** ptr_c;
  8.         a = 1;
  9.         ptr_b = &a;
  10.         ptr_c = &ptr_b;
  11.         cout << a << "\n";
  12.         cout << *ptr_b << "\n";
  13.         cout << *ptr_c << "\n";
  14.         cout << **ptr_c << "\n";
  15.         return 0;
  16.     }

a)

   1
   1
   0xbffc9924
   1

b)

   1
   1
   1
   0xbffc9924

c)

   1
   0xbffc9924
   1
   &1

d) 0xbffc9924
View Answer

Answer: a
Explanation: In this program, We are printing the values and memory address
by using the pointer and dereference operator.
Output:

$ g++ def2.cpp
$ a.out
1
1
0xbffc9924
1

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main() 
  4.     {
  5.         int x = 9;
  6.         int* p = &x;
  7.         cout << sizeof(p);
  8.         return 0;
  9.     }

a) 4
b) 2
c) Depends on compiler
d) 8
View Answer

Answer: c
Explanation: The size of a data type mainly depends on compiler only.
Output:

$ g++ def3.cpp
$ a.out
4

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

  1.     #include  <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         double arr[] = {5.0, 6.0, 7.0, 8.0};
  6.         double *p = (arr+2);
  7.         cout << *p << endl;   
  8.         cout << arr << endl;  
  9.         cout << *(arr+3) << endl;
  10.         cout << *(arr) << endl;  
  11.         cout << *arr+9 << endl;  
  12.         return 0;
  13.     }

a)

7
0xbf99fc98
8
5
14

b)

7
8
0xbf99fc98
5
14

c) 0xbf99fc98
d) 14
View Answer

Answer: a
Explanation: In this program, We are printing the values that are pointed by pointer and also the dereference operator.
Output:

$ g++ def5.cpp
$ a.out
7
0xbf99fc98
8
5
14

9. What does the dereference operator will return?
a) rvalue equivalent to the value at the pointer address
b) lvalue equivalent to the value at the pointer address
c) it will return nothing
d) it will return boolean values
View Answer

Answer: b
Explanation: It operates on a pointer variable, and returns an l-value equivalent to the value at the pointer address.

10. Pick out the correct statement.
a) the null pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to null
b) the null pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to the memory address
c) rvalue equivalent to the value at the pointer address
d) null pointer will not return anything
View Answer

Answer: a
Explanation: The null pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to null.

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.