C++ Programming Questions and Answers – Dereferencing

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.

C question bank on “Dereferencing” along with answers, explanations and/or solutions:

Q 1. Which is used to tell the computer that where a pointer is pointing to?
A. reference
B. dereference
C. heap operations
D. binary operations

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

Q 2. Which is used to do the dereferencing?
A. pointer without asterix
B. value without asterix
C. pointer with asterix
D. value with asterix

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

Q 3. Pick out the correct option.
A. Reference will not dereference
B. References automatically dereference with an extra character
C. References automatically dereference without needing an extra character
D. Reference automatically dereference with extra space and character

Show Answer Answer:-C. References automatically dereference without needing an extra character
Explanation References automatically dereference without needing an extra character.

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

#include <iostream>

using namespace std;

int main()

{

int a, b;

int* c;

c = &a;

a = 200;

b = 200;

*c = 100;

b = *c;

cout << *c << ” ” << b;

return 0;

}

A. 100 200
B. 100 100
C. 200 200
D. 100 0

Show Answer Answer:-B. 100 100
Explanation In this program, We are making the assignments and invoking the both b and c values as 100 by dereference operator. Output: $ g++ def.cpp $ a.out 100 100

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

#include <iostream>

using namespace std;

int main()

{

int x;

int *p;

x = 5;

p = &x;

cout << *p;

return 0;

}

A. 5
B. 10
C. 15
D. memory address

Show Answer Answer:-A. 5
Explanation In this program, we are copying the memory location of x into p and then printing the value in the address. Output: $ g++ def1.cpp $ a.out 5

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

#include <iostream>

using namespace std;

int main ()

{

int a;

int * ptr_b;

int ** ptr_c;

a = 1;

ptr_b = &a;

ptr_c = &ptr_b;

cout << a << “\n“;

cout << *ptr_b << “\n“;

cout << *ptr_c << “\n“;

cout << **ptr_c << “\n“;

return 0;

}

A. 1
1
0xbffc9924
1

B. 1
1
1
0xbffc9924

C. 1
0xbffc9924
1
&1

D. 0xbffc9924

Show Answer Answer:-A. 1 1 0xbffc9924 1
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

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

#include <iostream>

using namespace std;

int main()

{

int x = 9;

int* p = &x;

cout << sizeof(p);

return 0;

}

A. 2
B. 4
C. 8
D. Depends on compiler

Show Answer Answer:-D. Depends on compiler
Explanation The size of a data type mainly depends on compiler only. Output: $ g++ def3.cpp $ a.out 4

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

#include <iostream>

using namespace std;

int main()

{

double arr[] = {5.0, 6.0, 7.0, 8.0};

double *p = (arr+2);

cout << *p << endl;

cout << arr << endl;

cout << *(arr+3) << endl;

cout << *(arr) << endl;

cout << *arr+9 << endl;

return 0;

}

A. 7
0xbf99fc98
8
5
14

B.780xbf99fc98514

C. 0xbf99fc98
D. 14

Show Answer Answer:-A. 7 0xbf99fc98 8 5 14
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

Q 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

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

Q 10. Pick out the correct statement.
A. rvalue equivalent to the value at the pointer address
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. the null pointer dereference occurs where a pointer that is expected to be a valid address but instead is equal to null
D. null pointer will not return anything

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


Posted

in

by

Comments

Leave a Reply

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

You cannot copy content of this page