Mastering C++ Pointers: Comprehensive MCQs for Enhanced Understanding
Delve into essential C++ pointer concepts with this 30-question MCQ set. Topics include pointers’ basics, pointer variables, arithmetic, arrays, and function pointers—ideal for students and programmers aiming to deepen their skills.
1. What does a pointer store in C++?
2. Which symbol is used to declare a pointer variable in C++?
@$&*3. What is the output of *ptr if ptr is a pointer to an integer variable x?
xxxx4. How do you declare a pointer to an integer in C++?
int *ptr;int ptr*;int &ptr;int ptr&;5. Which operator is used to access the address of a variable in C++?
*&@#6. If ptr is a pointer to int, what does ptr++ do?
ptr by 1ptr by 17. What will happen if a pointer with NULL value is dereferenced?
8. Which of the following is the correct way to initialize a pointer variable in C++?
int *ptr = NULL;int ptr = NULL;int &ptr = NULL;int ptr;9. If ptr points to an integer variable, what is the correct way to change the value of the variable it points to?
ptr = 20;*ptr = 20;&ptr = 20;ptr* = 20;10. What is the term used when a pointer points to the memory that is no longer allocated?
11. How do pointers and arrays relate in C++?
12. If arr is an array, which of the following expressions is equivalent to arr[2]?
*(arr+2)arr*2arr(2)*arr[2]13. Which type of pointer points to the first element of an array?
14. What is pointer arithmetic in C++?
15. Which operation is valid for pointer arithmetic?
16. What does the expression *(ptr + i) represent when ptr points to the start of an array?
ith value in the arrayith element17. How do you define a pointer to a function that takes no arguments and returns an integer?
int (*fptr)();int fptr();int *fptr();int fptr(*)();18. Which of the following statements is true about pointers to functions?
19. What is the purpose of a const pointer?
20. Which keyword is used to create a pointer that cannot be changed?
conststaticvoidnew21. What is the output of the following code if ptr points to an array int arr[] = {1, 2, 3}; and cout << *ptr; is executed?
01arr22. Which of the following types of pointers are automatically assigned to avoid dereferencing issues?
23. When is a pointer said to be uninitialized?
24. What will be the result if ++ptr is used on a pointer pointing to an array?
25. Which statement correctly defines a void pointer?
void *ptr;int void *ptr;*void ptr;void ptr;26. How do you call a function using a function pointer in C++?
*fptr();fptr->();(*fptr)();*fptr;27. Which of the following statements about pointers is true?
NULL&28. How can you declare a pointer to an array in C++?
int *arr_ptr[];int (*arr_ptr)[];int arr_ptr[];int arr_ptr;29. Which of the following is a valid usage of delete in C++?
delete ptr[];delete ptr;delete *ptr;delete ptr; ptr = NULL;30. Which of the following expressions is used to assign the address of variable a to pointer ptr?
ptr = a;ptr = &a;ptr = *a;ptr = *&a;| QNo | Answer |
|---|---|
| 1 | B. The address of a variable |
| 2 | D. * |
| 3 | B. The value stored in x |
| 4 | A. int *ptr; |
| 5 | B. & |
| 6 | C. Moves the pointer to the next integer position |
| 7 | C. It causes a segmentation fault |
| 8 | A. int *ptr = NULL; |
| 9 | B. *ptr = 20; |
| 10 | B. Dangling pointer |
| 11 | B. An array name acts like a constant pointer |
| 12 | A. *(arr+2) |
| 13 | D. Pointer to an array |
| 14 | B. Performing operations on addresses stored in pointers |
| 15 | B. Subtracting an integer from a pointer |
| 16 | A. ith value in the array |
| 17 | A. int (*fptr)(); |
| 18 | C. A function pointer can store the address of a function |
| 19 | D. Both B and C |
| 20 | A. const |
| 21 | B. 1 |
| 22 | B. Null pointers |
| 23 | B. When it points to an unknown memory location |
| 24 | A. Pointer moves to the next array element |
| 25 | A. void *ptr; |
| 26 | C. (*fptr)(); |
| 27 | C. The address of a pointer can be obtained using & |
| 28 | B. int (*arr_ptr)[]; |
| 29 | B. delete ptr; |
| 30 | B. ptr = &a; |