Here is a listing of C programming questions on “Pointers Vs. Multi-dimensional Arrays” along with answers, explanations and/or solutions:
1. Which is true for a, if a is defined as “int a[10][20];”?
a) a is true two-dimensional array
b) 200 int-sized locations have been set aside
c) The conventional rectangular subscript calculation 20 * row + col is used to find the element a[row, col].
d) All of the mentioned
Answer: d
Clarification: None.
2. Which is true for b, if b is defined as “int *b[10];”?
a) The definition only allocates 10 pointers and does not initialize them
b) Initialization must be done explicitly
c) The definition only allocates 10 pointers and does not initialize them & Initialization must be done explicitly
d) Error
Answer: c
Clarification: None.
3. What will be the output of the following C code?
-
#include -
void main()
-
{ -
char a[10][5] = {"hi", "hello", "fellows"};
-
printf("%s", a[2]);
-
}
a) fellows
b) fellow
c) fello
d) fell
Answer: c
Clarification: Since every row in the array a[10][5] can contain only 5 characters, the a[2] element will hold “fello” i.e. 5 characters. There will not be any null character in a[2]. Since, the array is completely intialized, other rows (row a[3] will have only null characters. Hence, printf() using %s specifier will display fello only.
4. What will be the output of the following C code?
-
#include -
void main()
-
{ -
char a[10][5] = {"hi", "hello", "fellows"};
-
printf("%pn", a);
-
printf("%p", a[0]);
-
}
a) same address is printed
b) different address is printed
c) hello
d) hi hello fello
Answer: a
Clarification: None.
5. What will be the output of the following C code?
-
#include -
void main()
-
{ -
char a[10][5] = {"hi", "hello", "fellows"};
-
printf("%d", sizeof(a[1]));
-
}
a) 2
b) 4
c) 5
d) 10
Answer: c
Clarification: None.
6. What will be the output of the following C code?
-
#include -
int main()
-
{ -
char a[1][5] = {"hello"};
-
printf("%s", a[0]);
-
return 0;
-
}
a) Compile time error
b) hello
c) Undefined behaviour
d) hellon
Answer: c
Clarification: None.
7. What will be the output of the following C code?
-
#include -
int main()
-
{ -
char *a[1] = {"hello"};
-
printf("%s", a[0]);
-
return 0;
-
}
a) Compile time error
b) hello
c) Undefined behaviour
d) hellon
Answer: b
Clarification: None.
8. Which of the following statements are true?
P. Pointer to Array
Q. Multi-dimensional array
a) P are static, Q are static
b) P are static, Q are dynamic
c) P are dynamic, Q are static
d) P are dynamic, Q are dynamic
Answer: c
Clarification: None.
