C++ Questions and Answers Part-16

1. What will be the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int array1[] = {1200, 200, 2300, 1230, 1543};
int array2[] = {12, 14, 16, 18, 20};
int temp, result = 0;
int main()
{
for (temp = 0; temp < 5; temp++)
{
result += array1[temp];
}
for (temp = 0; temp < 4; temp++)
{
result += array2[temp];
}
cout << result;
return 0;
}
a) 6553
b) 6533
c) 6522
d) 12200

Answer: b
Explanation: In this program we are adding the every element of two arrays. Finally we got output as 6533.

2. What will be the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main ()
{
int array[] = {0, 2, 4, 6, 7, 5, 3};
int n, result = 0;
for (n = 0; n < 8; n++)
{
result += array[n];
}
cout << result;
return 0;
}
a) 25
b) 26
c) 27
d) 21

Answer: c
Explanation: We are adding all the elements in the array and printing it. Total elements in the array is 7, but our for loop will go beyond 7 and add a garbage value.

3. What will be the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, c = 15;
int arr[3] = {&a, &b, &c};
cout << *arr[*arr[1] - 8];
return 0;
}
a) 15
b) 18
c) garbage value
d) compile time error

Answer: d
Explanation: The conversion is invalid in this array. So it will arise error. The following compilation error will be raised: cannot convert from ‘int *’ to ‘int’ This is because &a, &b and &c represent int* whereas the array defined is of int type.

4. What will be the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
char str[5] = "ABC";
cout << str[3];
cout << str;
return 0;
}
a) ABC
b) ABCD
c) AB
d) AC

Answer: a
Explanation: We are just printing the values of first 3 values.

5. What will be the output of the following C++ code?
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int array[] = {10, 20, 30};
cout << -2[array];
return 0;
}
a) -15
b) -30
c) compile time error
d) garbage value

Answer: b
Explanation: It’s just printing the negative value of the concern element.

6. 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

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

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

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

8. 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

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

9. 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) ava
b) java
c) c++
d) compile time error

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

10. 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

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