C++ Programming Questions and Answers – Pointers into Arrays

Q 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

Show Answer Answer:-B. p is array of pointer to function
Explanation In the above declaration the variable p is the array, not the pointer.

Q 2. What is size of generic pointer in C++ (in 32-bit platform)?
A. 2
B. 4
C. 8
D. 0

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

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

#include <iostream>

using namespace std;

int main()

{

int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24};

cout << *(a[1] + 2) << *(*(a + 1) + 2) << 2[1[a]];

return 0;

}

A. 15 18 21
B. 21 21 21
C. 24 24 24
D. Compile time error

Show Answer Answer:-B. 21 21 21
Explanation a[1][2] means 1 * (4)+2 = 6th element of an array starting from zero. Output: $ g++ point.cpp $ a.out 21 21 21

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

#include <iostream>

using namespace std;

int main()

{

int i;

const char *arr[] = {“C”, “C++”, “Java”, “VBA”};

const char *(*ptr)[4] = &arr;

cout << ++(*ptr)[2];

return 0;

}

A. java
B. ava
C. c++
D. compile time error

Show Answer Answer:-B. ava
Explanation In this program we are moving the pointer from first position to second position and printing the remaining value. Output: $ g++ point1.cpp $ a.out ava

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

#include <iostream>

using namespace std;

int main()

{

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *p;

return 0;

}

A. 4
B. 5
C. 6
D. 7

Show Answer Answer:-B. 5
Explanation In this program, we are making the pointer point to next value and printing it. $ g++ point3.cpp $ a.out 5

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

#include <iostream>

using namespace std;

int main()

{

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << arr;

return 0;

}

A. 4
B. 5
C. address of arr
D. 7

Show Answer Answer:-C. address of arr
Explanation As we counted to print only arr, it will print the address of the array. Output: $ g++ point2.cpp $ a.out 0xbfb1cff

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

#include <iostream>

using namespace std;

int main ()

{

int numbers[5];

int * p;

p = numbers; *p = 10;

p++; *p = 20;

p = &numbers[2]; *p = 30;

p = numbers + 3; *p = 40;

p = numbers; *(p + 4) = 50;

for (int n = 0; n < 5; n++)

cout << numbers[n] << “,”;

return 0;

}

A. 10,20,30,40,50,
B. 1020304050
C. compile error
D. runtime error

Show Answer Answer:-A. 10,20,30,40,50,
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,

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

#include <iostream>

using namespace std;

int main()

{

int arr[] = {4, 5, 6, 7};

int *p = (arr + 1);

cout << *arr + 9;

return 0;

}

A. 10
B. 12
C. 13
D. error

Show Answer Answer:-C. 13
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

Comments

Leave a Reply

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

You cannot copy content of this page