Here is a listing of C quiz on “Initialization of Pointer Arrays” along with answers, explanations and/or solutions:
1. Which of the following is the correct syntax to declare a 3 dimensional array using pointers?
a) char *a[][];
b) char **a[];
c) char ***a;
d) all of the mentioned
Answer: a
Clarification: None.
2. What will be the output of the following C code?
-
#include
-
int main()
-
{
-
char *a = {"p", "r", "o", "g", "r", "a", "m"};
-
printf("%s", a);
-
}
a) Output will be program
b) Output will be p
c) No output
d) Compile-time error
Answer: b
Clarification: None.
3. An array of strings can be initialized by _________
a) char *a[] = {“Hello”, “World”};
b) char *a[] = {“Hello”, “Worlds”};
c)
char *b = "Hello"; char *c = "World"; char *a[] = {b, c};
d) all of the mentioned
Answer: d
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *a[10] = {"hi", "hello", "how"};
-
int i = 0;
-
for (i = 0;i < 10; i++)
-
printf("%s", *(a[i]));
-
}
a) segmentation fault
b) hi hello how followed by 7 null values
c) 10 null values
d) depends on compiler
Answer: a
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *a[10] = {"hi", "hello", "how"};
-
int i = 0, j = 0;
-
a[0] = "hey";
-
for (i = 0;i < 10; i++)
-
printf("%sn", a[i]);
-
}
a) hi hello how Segmentation fault
b) hi hello how followed by 7 null values
c) hey hello how Segmentation fault
d) depends on compiler
Answer: c
Clarification: None.
6. What will be the output of the following C code on a 32-bit system?
-
#include
-
void main()
-
{
-
char *a[10] = {"hi", "hello", "how"};
-
printf("%dn", sizeof(a));
-
}
a) 10
b) 13
c) Run time error
d) 40
Answer: d
Clarification: If the system is 32-bit system, then the size of pointer will be 4 bytes. For such a system, the size of array a will be 4×10 = 40 bytes. The size of pointer is 8 bytes on a 64 bit system. For the given array a of 10 elements, it will be 8×10 = 80 bytes.
7. What will be the output of the following C code on a 32-bit system?
-
#include
-
void main()
-
{
-
char *a[10] = {"hi", "hello", "how"};
-
printf("%dn", sizeof(a[1]));
-
}
a) 6
b) 4
c) 5
d) 3
Answer: b
Clarification: Array element a[1] is storing an address of character pointer. For a 32-bit systems its 4 bytes and for a 64-bit system, its 8 bytes.
8. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char *a[10] = {"hi", "hello", "how"};
-
int i = 0;
-
for (i = 0;i < 10; i++)
-
printf("%s", a[i]);
-
}
a) hi hello how Segmentation fault
b) hi hello how null
c) hey hello how Segmentation fault
d) hi hello how followed by 7 nulls
Answer: d
Clarification: None.