C Programming Multiple choice Questions and Answers on Storage Management (Dynamic Memory Allocation) for Freshers

1. The function ____ obtains block of memory dynamically.

a) calloc
b) malloc
c) Both calloc & malloc
d) free
Answer: c
2. void * malloc(size_t n) returns

a) Pointer to n bytes of uninitialized storage
b) NULL if the request can be satisfied
c) Nothing
d) None of the mentioned
Answer: a
3. calloc() returns a storage that is initialized to.

a) Zero
b) Null
c) Nothing
d) One
Answer: a
4. In function free(p), p is a

a) int
b) Pointer returned by malloc()
c) Pointer returned by calloc()
d) Pointer returned by malloc() & calloc()
Answer: d
5. What is the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         char *p = calloc(100, 1);
  5.         p = "welcome";
  6.         printf("%s\n", p);
  7.     }
a) Segmentation fault
b) Garbage
c) Error
d) welcome
Answer: d
6. Memory allocation using malloc() is done in?

a) Static area
b) Stack area
c) Heap area
d) Both Stack & Heap area
Answer: c
7. Why do we write (int *) before malloc?
    int *ip = (int *)malloc(sizeof(int));

a) It is for the syntax correctness
b) It is for the type-casting
c) It is to inform malloc function about the data-type expected
d) None of the mentioned
Answer: b
8. Which one is used during memory deallocation in C?

a) remove(p);
b) delete(p);
c) free(p);
d) terminate(p);
Answer: c
9. 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
Answer: d
10. 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 location it was initialized with
Answer: c
11. For the following program, Which of the following should be used for freeing the memory allocated?
  1.     #include 
  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) free(p1);
    free(p1->next)
b) free(p1->next);
    free(p1);
c) free(p1);
d) all of the mentioned
Answer: b
12. What is the output of this C code?
  1.     #include 
  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
Answer: d
13. What is the output of this C code?
  1.     #include 
  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
Answer: c
14. calloc initialises memory with all bits set to zero.

a) True
b) False
c) Depends on the compiler
d) Depends on the standard
Answer: a
15. realloc(ptr, size), where size is zero means

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

Related

Linux Questions & Answers on Shell Redirection for Freshers

1. The redirection 2> abc implies a) Write file 2 to file abcb) Write standard output to abcc) Write standard error to abcd) None of the mentioned Answer: c 2. cmd 2>&1 > abc will a...

C# Questions & Answers on Writing Console Output Operations for Freshers

1. Select the objects of the class TextWriter which is/are not used to perform the write operations to the console? a) Write()b) WriteLine()c) WriteError()d) All of the mentioned Answer: c Explan...

Linux Questions & Answers on Shell Environment for Freshers

1. To feed standard output of one command to standard input of another in a single shell session a) IO redirection can be usedb) Named pipes can be usedc) The pipe operator provided by the shell ca...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item