C Programming Questions and Answers – Storage Management – 2

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Storage Management – 2”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

1. Which of the following will return a result most quickly for searching a given key?
a) Unsorted Array
b) Sorted Array
c) Sorted linked list
d) Binary Search Tree
View Answer

Answer: d
Explanation: None.

2. On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to.
a) NULL
b) Other dynamically allocated memory
c) The same deallocated memory location
d) It points back to the location it was initialized with
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

3. Which of the following should be used for freeing the memory allocated in the following C code?

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         struct p *next;
  5.         int x;
  6.     };
  7.     int main()
  8.     {
  9.         struct p *p1 = (struct p*)malloc(sizeof(struct p));
  10.         p1->x = 1;
  11.         p1->next = (struct p*)malloc(sizeof(struct p));
  12.         return 0;
  13.     }

a)

advertisement
   free(p1);
   free(p1->next)

b)

advertisement
   free(p1->next);
   free(p1);

c) free(p1);
d) all of the mentioned
View Answer

Answer: b
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         struct p *next;
  5.         int x;
  6.     };
  7.     int main()
  8.     {
  9.         struct p *p1 = calloc(1, sizeof(struct p));
  10.         p1->x = 1;
  11.         p1->next = calloc(1, sizeof(struct p));
  12.         printf("%d\n", p1->next->x);
  13.         return 0;
  14.     }

a) Compile time error
b) 1
c) Somegarbage value
d) 0
View Answer

Answer: d
Explanation: None.

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

  1.      #include <stdio.h>
  2.     struct p
  3.     {
  4.         struct p *next;
  5.         int x;
  6.     };
  7.     int main()
  8.     {
  9.         struct p* p1 = malloc(sizeof(struct p));
  10.         p1->x = 1;
  11.         p1->next = malloc(sizeof(struct p));
  12.         printf("%d\n", p1->next->x);
  13.         return 0;
  14.     }

a) Compile time error
b) 1
c) Somegarbage value
d) 0
View Answer

Answer: c
Explanation: None.

6. calloc() initialize memory with all bits set to zero.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
View Answer

Answer: a
Explanation: None.

7. What if size is zero in the following C statement?

realloc(ptr, size)

a) Allocate a memory location with zero length
b) Free the memory pointed to by ptr
c) Undefined behaviour
d) Doesn’t do any reallocation of ptr i.e. no operation
View Answer

Answer: b
Explanation: None.
.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement

Leave a Comment

Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.