Pointers Into Arrays(C++ Programming ) Questions and Answers
Question 1.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int arr[] = {4, 5, 6, 7}; 6. int *p = (arr + 1); 7. cout
12
5
13
error
Explanation:-
Answer: Option C. -> 13
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
Question 2.
What is the output of this program?
1. #include 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
10,20,30,40,50,
1020304050
compile error
runtime error
Explanation:-
Answer: Option A. -> 10,20,30,40,50,
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,
Question 3.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int arr[] = {4, 5, 6, 7}; 6. int *p = (arr + 1); 7. cout
4
5
address of arr
7
Explanation:-
Answer: Option C. -> address of arr
As we couted to print only arr, it will print the address of the array. Output: $ g++ point2.cpp $ a.out 0xbfb1cff
Question 4.
what is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int arr[] = {4, 5, 6, 7}; 6. int *p = (arr + 1); 7. cout
4
5
6
7
Explanation:-
Answer: Option B. -> 5
In this program, we are making the pointer point to next value and printing it. $ g++ point3.cpp $ a.out 5
Question 5.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int i; 6. char *arr[] = {"C", "C++", "Java", "VBA"}; 7. char *(*ptr)[4] = &arr; 8. cout
ava
java
c++
compile time error
Explanation:-
Answer: Option A. -> ava
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
Question 6.
What is size of generic pointer in C++ (in 32-bit platform) ?
2
4
8
0
Explanation:-
Answer: Option B. -> 4
Size of any type of pointer is 4 bytes in 32-bit platforms.
Question 7.
What is the output of this program?
1. #include 2. using namespace std; 3. int main() 4. { 5. int a[2][4] = {3, 6, 9, 12, 15, 18, 21, 24}; 6. cout
15 18 21
21 21 21
24 24 24
Compile time error
Explanation:-
Answer: Option B. -> 21 21 21
a[1][2] means 1 * (4)+2 = 6th element of an array staring from zero. Output: $ g++ point.cpp $ a.out 21 21 21
Question 8.
What is meaning of following declaration? int(*p[5])();
p is pointer to function.
p is array of pointer to function.
p is pointer to such function which return type is array.
p is pointer to array of function.
Explanation:-
Answer: Option B. -> p is array of pointer to function.
In the above declaration the variable p is array not pointer.