1. What will happen in the following C++ code snippet?
int a = 100, b = 200;
int *p = &a, *q = &b;
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
Explanation: Assigning to reference changes the object to which the reference is bound.
2. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, c = 15;
int *arr[ ] = {&a, &b, &c};
cout << arr[1];
return 0;
}
a) 5
b) 10
c) 15
d) it will return some random number
Explanation: Array element cannot be address of auto variable. It can be address of static or extern variables.
3. 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)
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**).
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char arr[20];
int i;
for(i = 0; i < 10; i++)
*(arr + i) = 65 + i;
*(arr + i) = '\0';
cout << arr;
return(0);
}
a) ABCDEFGHIJ
b) AAAAAAAAAA
c) JJJJJJJJ
d) AAAAAAJJJJ
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.
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int main()
{
char *ptr;
char Str[] = "abcdefg";
ptr = Str;
ptr += 5;
cout << ptr;
return 0;
}
a) fg
b) cdef
c) defg
d) abcd
Explanation: Pointer ptr points to string ‘fg’. So it prints fg.
Output:
fg
6. Which of the following correctly declares an array?
a) int array[10];
b) int array;
c) array{10};
d) array array[10];
Explanation: Because array variable and values need to be declared after the datatype only.
7. What is the index number of the last element of an array with 9 elements?
a) 9
b) 8
c) 0
d) Programmer-defined
Explanation: Because the first element always starts at 0. So it is on 8 position.
8. What is the correct definition of an array?
a) An array is a series of elements of the same type in contiguous memory locations
b) An array is a series of element
c) An array is a series of elements of the same type placed in non-contiguous memory locations
d) An array is an element of the different type
Explanation: Correct definition of an array is An array is a series of elements of the same type in contiguous memory locations.
9. Which of the following accesses the seventh element stored in array?
a) array[6];
b) array[7];
c) array(7);
d) array;
Explanation: The array location starts from zero, So it can accessed by array[6].
10. Which of the following gives the memory address of the first element in array?
a) array[0];
b) array[1];
c) array(2);
d) array;
Explanation: To get the address of ith index of an array, we use following syntax (arr + i). So as we need address of first index we will use (arr + 0) equivalent to arr.