C Programming Questions and Answers – Initialization of Pointer Arrays – 1

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Initialization of Pointer Arrays – 1”.

Pre-requisite for C Pointer Arrays Initialization MCQ set: Video Tutorial on C Pointers.

advertisement

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
View Answer

Answer: a
Explanation: None.

2. What will be the output of the following C code?

advertisement
advertisement
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *a = {"p", "r", "o", "g", "r", "a", "m"};
  5.         printf("%s", a);
  6.     }

a) Output will be program
b) Output will be p
c) No output
d) Compile-time error
View Answer

Answer: b
Explanation: None.
advertisement

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
View Answer

Answer: d
Explanation: None.
advertisement

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         int i = 0;
  6.         for (i = 0;i < 10; i++)
  7.         printf("%s", *(a[i]));
  8.     }

a) segmentation fault
b) hi hello how followed by 7 null values
c) 10 null values
d) depends on compiler
View Answer

Answer: a
Explanation: None.
advertisement

5. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         int i = 0, j = 0;
  6.         a[0] = "hey";
  7.         for (i = 0;i < 10; i++)
  8.         printf("%s\n", a[i]);
  9.     }

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
View Answer

Answer: c
Explanation: None.

6. What will be the output of the following C code on a 32-bit system?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         printf("%d\n", sizeof(a));
  6.     }

a) 10
b) 13
c) Run time error
d) 40
View Answer

Answer: d
Explanation: 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?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         printf("%d\n", sizeof(a[1]));
  6.     }

a) 6
b) 4
c) 5
d) 3
View Answer

Answer: b
Explanation: 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?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char *a[10] = {"hi", "hello", "how"};
  5.         int i = 0;
  6.         for (i = 0;i < 10; i++)
  7.         printf("%s", a[i]);
  8.     }

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
View Answer

Answer: d
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.