Machine-translated by Google. Questions are authored in English.
Top

Memory Allocation Questions and Answers

91.

Where are initialised global variables stored in a C program ?

Answer: C

Correct answer: The initialised data segment

Globals given an explicit non-zero value at compile time occupy the initialised data segment, and their values are part of the executable file. Globals left uninitialised go instead to the BSS, which the loader clears to zero.

Please log in or register to report this question.

92.

Which of the following statement is correct prototype of the malloc() function in c ?

Answer: D

Correct answer: void* malloc(size_t);

The prototype takes a size_t - an unsigned type large enough to hold the size of any object - and returns void *.

Both halves matter. The void * return is what allows one function to serve every type, and size_t rather than int is what allows allocations larger than an int can express on a 64-bit machine. It also means a negative size argument is converted to a very large positive one, so the allocation simply fails rather than misbehaving.

Please log in or register to report this question.

93.

The type of linked list in which the node does not contain any pointer or reference to the previous node:

Answer: B

Correct answer: Singly linked list

In a singly linked list each node holds one pointer, to its successor. Nothing refers backwards, so traversal runs in one direction only and deleting a node requires a reference to the one before it.

Adding a second pointer to the predecessor gives a doubly linked list, which costs memory per node but allows backward traversal and deletion given only the target node. Whether a list is circular is a separate question, concerning only whether the last node points back to the first.

Please log in or register to report this question.

94.

malloc() returns a NULL if it fails to allocate the requested memory.

Answer: A

Correct answer: TRUE

When malloc cannot satisfy a request it returns a null pointer. That is the only way it reports failure - there is no error code and nothing is set beyond, on most systems, errno.

Checking the return value is therefore the caller's responsibility, and omitting the check is a common fault: the program proceeds to dereference a null pointer and crashes at a line some distance from the real cause. Note that malloc(0) may legitimately return either null or a unique pointer.

Please log in or register to report this question.

Answer: A

Correct answer: allocated memory is never released and its pointer is lost

A leak is memory that remains allocated while the program has no pointer left with which to free it. The block stays reserved for the life of the process, and in a long-running program repeated leaks exhaust the heap.

Please log in or register to report this question.

96.

Which of the following will return a result most quickly for searching a given key?

Answer: D

Correct answer: Binary Search Tree

A balanced binary search tree finds a key in \( O(\log n) \) comparisons, halving the search space at each step.

An unsorted array and a sorted linked list both require \( O(n) \), the list because it cannot be indexed and so cannot be bisected however well ordered it is. A sorted array also permits binary search at \( O(\log n) \), so the two are comparable on lookup - but the tree keeps that performance while supporting insertion and deletion in \( O(\log n) \), where the array would need elements shifted.

Please log in or register to report this question.

Answer: C

Correct answer: Preprocessor, Compiler, Assembler, Linking

The order follows what each stage needs from the last. The preprocessor runs first, handling #include, #define and conditional compilation to produce a single translation unit of pure C.

The compiler turns that into assembly, the assembler turns assembly into an object file of machine code with unresolved references, and the linker joins the object files and libraries into an executable, resolving those references. A missing function is a link error precisely because the linker is the first stage that looks across files.

Please log in or register to report this question.

Answer: B

Correct answer: It is for the type-casting

The cast converts the void * that malloc returns to an int *. It tells the compiler how to treat the value; malloc itself never sees it and learns nothing about the intended type.

In C the cast is unnecessary, since void * converts implicitly to any object pointer type, and it can do harm: if stdlib.h has been forgotten, the compiler assumes a return type of int, and the cast silences the very warning that would have exposed the problem. In C++ it is required.

Please log in or register to report this question.

99.

Point out the error in the following program.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *a[3];
    a = (int*) malloc(sizeof(int)*3);
    free(a);
    return 0;
}

 

Answer: B

Correct answer: Error: We cannot store address of allocated memory in a

int *a[3] declares an array of three pointers. An array name is not a modifiable lvalue, so the compiler rejects any assignment to a.

What the code appears to intend is int *a;, a single pointer able to hold the returned address. The parentheses make all the difference in such declarations, and reading them from the identifier outwards - a is an array of 3 pointers to int - is the reliable technique.

Please log in or register to report this question.

100.

Specify the 2 library functions to dynamically allocate memory?

Answer: C

Correct answer: malloc() and calloc()

malloc and calloc are the two allocation functions in standard C. malloc takes a single total size and leaves the block uninitialised; calloc takes a count and an element size and zeroes the result.

The standard library also provides realloc to resize an existing block and free to release one, so four functions in all - but only two of them allocate fresh storage. The others named in the options are not standard C at all.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Memory Allocation Questions and Answers || Memory Allocation MCQ Questions and Answers || Memory Allocation GK Questions and Answers || Memory Allocation GK MCQ Questions || Memory Allocation Multiple Choice Questions and Answers || Memory Allocation GK || GK on Memory Allocation || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming