Here is a listing of C Objective Questions on “Storage Management” along with answers, explanations and/or solutions:
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
Answer: d
Clarification: 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
Answer: c
Clarification: None.
3. Which of the following should be used for freeing the memory allocated in the following C code?
-
#include
-
struct p
-
{
-
struct p *next;
-
int x;
-
};
-
int main()
-
{
-
struct p *p1 = (struct p*)malloc(sizeof(struct p));
-
p1->x = 1;
-
p1->next = (struct p*)malloc(sizeof(struct p));
-
return 0;
-
}
a)
b)
free(p1->next); free(p1);
c) free(p1);
d) all of the mentioned
Answer: b
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
struct p
-
{
-
struct p *next;
-
int x;
-
};
-
int main()
-
{
-
struct p *p1 = calloc(1, sizeof(struct p));
-
p1->x = 1;
-
p1->next = calloc(1, sizeof(struct p));
-
printf("%dn", p1->next->x);
-
return 0;
-
}
a) Compile time error
b) 1
c) Somegarbage value
d) 0
Answer: d
Clarification: None.
5. What will be the output of the following C code?
-
#include
-
struct p
-
{
-
struct p *next;
-
int x;
-
};
-
int main()
-
{
-
struct p* p1 = malloc(sizeof(struct p));
-
p1->x = 1;
-
p1->next = malloc(sizeof(struct p));
-
printf("%dn", p1->next->x);
-
return 0;
-
}
a) Compile time error
b) 1
c) Somegarbage value
d) 0
Answer: c
Clarification: 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
Answer: a
Clarification: None.
7. What if size is zero in the following C statement?
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
Clarification: None.
.