Test Series - cpp

Test Number 15/102

Q: Which of the following is illegal?
A. int *ip;
B. string s, *sp = 0;
C. int i; double* dp = &i;
D. int *pi = 0;
Solution: dp is initialized int value of i.
Q: 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
Solution: Assigning to reference changes the object to which the reference is bound.
Q: 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)
Solution: 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**).
Q: Which of the following correctly declares an array?
A. int array[10];
B. int array;
C. array{10};
D. array array[10];
Solution: Because array variable and values need to be declared after the datatype only.
Q: What is the index number of the last element of an array with 9 elements?
A. 9
B. 8
C. 0
D. Programmer-defined
Solution: Because the first element always starts at 0. So it is on 8 position.
Q: 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
Solution: Correct definition of an array is An array is a series of elements of the same type in contiguous memory locations.
Q: Which of the following accesses the seventh element stored in array?
A. array[6];
B. array[7];
C. array(7);
D. array;
Solution: The array location starts from zero, So it can accessed by array[6].
Q: 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;
Solution: 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.
Q: 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
Solution: In the above declaration the variable p is the array, not the pointer.
Q: What is size of generic pointer in C++ (in 32-bit platform)?
A. 2
B. 8
C. 4
D. 0
Solution: Size of any type of pointer is 4 bytes in 32-bit platforms.

You Have Score    /10