C++ Programming Questions and Answers – Pointers into Arrays

This section on C++ language interview questions and answers focuses on “Pointers into Arrays”. One shall practice these interview 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++ language interview questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ language interview questions “Pointers into Arrays” along with answers, explanations and/or solutions:

1. What is the meaning of the following declaration?

int(*p[5])();

a) p is pointer to function
b) p is array of pointer to function
c) p is pointer to such function which return type is the array
d) p is pointer to array of function
View Answer

Answer: b
Explanation: In the above declaration the variable p is the array, not the pointer.

2. What is size of generic pointer in C++ (in 32-bit platform)?
a) 2
b) 4
c) 8
d) 0
View Answer

Answer: b
Explanation: Size of any type of pointer is 4 bytes in 32-bit platforms.
advertisement
advertisement

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

  1.    #include <iostream>
  2.    using namespace std;
  3.    int main()
  4.    {
  5.        int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};
  6.        cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];
  7.        return 0;
  8.    }

a) 15 18 21
b) 21 21 21
c) 24 24 24
d) Compile time error
View Answer

Answer: b
Explanation: a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero.
Output:

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

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

advertisement
  1.    #include <iostream>
  2.    using namespace std;
  3.    int main()
  4.    {
  5.        int i;
  6.        const char *arr[] = {"C", "C++", "Java", "VBA"};
  7.        const char *(*ptr)[4] = &arr;
  8.        cout << ++(*ptr)[2];
  9.        return 0;
  10.    }

a) ava
b) java
c) c++
d) compile time error
View Answer

Answer: a
Explanation: In this program we are moving the pointer from first position to second position and printing the remaining value.
Output:

advertisement
$ g++ point1.cpp
$ a.out
ava

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

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

a) 4
b) 5
c) 6
d) 7
View Answer

Answer: b
Explanation: In this program, we are making the pointer point to next value and printing it.

$ g++ point3.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 arr[] = {4, 5, 6, 7};
  6.        int *p = (arr + 1);
  7.        cout << arr;
  8.        return 0;
  9.    }

a) 4
b) 5
c) address of arr
d) 7
View Answer

Answer: c
Explanation: As we counted to print only arr, it will print the address of the array.
Output:

$ g++ point2.cpp
$ a.out
0xbfb1cff

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

  1.    #include <iostream>
  2.    using namespace std;
  3.    int main ()
  4.    {
  5.        int numbers[5];
  6.        int * p;
  7.        p = numbers;  *p = 10;
  8.        p++;  *p = 20;
  9.        p = &numbers[2];  *p = 30;
  10.        p = numbers + 3;  *p = 40;
  11.        p = numbers;  *(p + 4) = 50;
  12.        for (int n = 0; n < 5; n++)
  13.            cout << numbers[n] << ",";
  14.        return 0;
  15.    }

a) 10,20,30,40,50,
b) 1020304050
c) compile error
d) runtime error
View Answer

Answer: a
Explanation: In this program, we are just assigning a value to the array and printing it and immediately dereferencing it.
Output:

$ g++ point4.cpp
$ a.out
10,20,30,40,50,

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

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

a) 12
b) 5
c) 13
d) error
View Answer

Answer: c
Explanation: In this program, we are adding the value 9 to the initial value of the array, So it’s printing as 13.
Output:

$ g++ point5.cpp
$ a.out
13

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.