C++ Programming MCQ – Pointers

This section on C++ aptitude questions and answers focuses on “Pointers”. One shall practice these aptitude questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams, aptitude tests 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++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ aptitude questions on “Pointers” along with answers, explanations and/or solutions:

1. What does the following statement mean?

int (*fp)(char*)

a) pointer to a pointer
b) pointer to an array of chars
c) pointer to function taking a char* argument and returns an int
d) function taking a char* argument and returning a pointer to int
View Answer

Answer: c
Explanation: The (*fn) represents a pointer to a function and char* as arguments and returning int from the function. So according to that, the above syntax represents a pointer to a function taking a char* as an argument and returning int.
advertisement
advertisement

2. The operator used for dereferencing or indirection is ____
a) *
b) &
c) ->
d) –>>
View Answer

Answer: a
Explanation: * is used as dereferencing operator, used to read value stored at the pointed address.

3. Choose the right option.

string* x, y;
Note: Join free Sanfoundry classes at Telegram or Youtube

a) x is a pointer to a string, y is a string
b) y is a pointer to a string, x is a string
c) both x and y are pointers to string types
d) y is a pointer to a string
View Answer

Answer: a
Explanation: * is to be grouped with the variables, not the data types.

4. Which one of the following is not a possible state for a pointer.
a) hold the address of the specific object
b) point one past the end of an object
c) zero
d) point to a type
View Answer

Answer: d
Explanation: A pointer can be in only 3 states a, b and c.
advertisement

5. Which of the following is illegal?
a) int *ip;
b) string s, *sp = 0;
c) int i; double* dp = &i;
d) int *pi = 0;
View Answer

Answer: c
Explanation: dp is initialized int value of i.

6. What will happen in the following C++ code snippet?

advertisement
  1.    int a = 100, b = 200;
  2.    int *p = &a, *q = &b;
  3.    p = q;

a) b is assigned to a
b) p now points to b
c) a is assigned to b
d) q now points to a
View Answer

Answer: b
Explanation: Assigning to reference changes the object to which the reference is bound.

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         int a = 5, b = 10, c = 15;
  6.         int *arr[ ] = {&a, &b, &c};
  7.         cout << arr[1];
  8.         return 0;
  9.     }

a) 5
b) 10
c) 15
d) it will return some random number
View Answer

Answer: d
Explanation: Array element cannot be address of auto variable. It can be address of static or extern variables.

8. The correct statement for a function that takes pointer to a float, a pointer to a pointer to a char and returns a pointer to a pointer to a integer is ____________
a) int **fun(float**, char**)
b) int *fun(float*, char*)
c) int **fun(float*, char**)
d) int ***fun(*float, **char)
View Answer

Answer: c
Explanation: Function that takes pointer to a float, a pointer to a pointer to a char and returns a pointer to a pointer to a integer is int **fun(float*, char**).

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char arr[20];
  6.         int i;
  7.         for(i = 0; i < 10; i++)
  8.             *(arr + i) = 65 + i;
  9.         *(arr + i) = '\0';
  10.         cout << arr;
  11.         return(0);
  12.     }

a) ABCDEFGHIJ
b) AAAAAAAAAA
c) JJJJJJJJ
d) AAAAAAJJJJ
View Answer

Answer: a
Explanation: Each time we are assigning 65 + i. In first iteration i = 0 and 65 is assigned. So it will print from A to J.
$ g++ point1.cpp
$ a.out
ABCDEFGHIJ

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     int main()
  4.     {
  5.         char *ptr;
  6.         char Str[] = "abcdefg";
  7.         ptr = Str;
  8.         ptr += 5;
  9.         cout << ptr;
  10.         return 0;
  11.     }

a) fg
b) cdef
c) defg
d) abcd
View Answer

Answer: a
Explanation: Pointer ptr points to string ‘fg’. So it prints fg.
Output:

$ g++ point.cpp
$ a.out
fg

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.

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.