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

Memory Allocation Questions and Answers

Answer: C

Correct answer: It allocates a new block, copies the contents and frees the old one

When there is not enough adjacent free space, realloc() obtains a fresh block, copies the existing data across, releases the old block and returns the new address. This is why the returned pointer must always be captured rather than assumed unchanged.

Please log in or register to report this question.

62.

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.

Answer: B

Correct answer: Until it is explicitly freed or the program ends

Heap memory has no automatic lifetime: it persists until free() is called or the process exits. This is precisely why a function can allocate a block and return its address safely, whereas returning the address of a local variable cannot work.

Please log in or register to report this question.

Answer: A

Correct answer: points to memory that has already been freed

Calling free() releases the block but leaves the pointer holding its old address, and using it afterwards is undefined behaviour. Setting the pointer to NULL immediately after freeing it is the standard guard against this.

Please log in or register to report this question.

65.

If malloc() successfully allocates memory it returns the number of bytes it has allocated.

#include <stdio.h>
#include <stdlib.h>
void fun(int *a)
{
    a = (int*)malloc(sizeof(int));
}
int main()
{
    int *p;
    fun(p);
    *p = 6;
    printf("%d\n",*p);
    return(0);
}

Answer: A

The program is not valid. Try replacing "int *p;" with "int *p = NULL;" and it will try to dereference a null pointer. This is because fun() makes a copy of the pointer, so when malloc() is called, it is setting the copied pointer to the memory location, not p. p is pointing to random memory before and after the call to fun(), and when you dereference it, it will crash. If you want to add memory to a pointer from a function, you need to pass the address of the pointer (ie. double pointer).

Please log in or register to report this question.

66.

During preprocessing, the code #include gets replaced by the contents of the file stdio.h. Which is true?

Answer: B

Correct answer: Yes

The statement is correct: #include is handled by the preprocessor, which runs before compilation and replaces the directive with the entire contents of the named file.

The result is a single translation unit containing the program text and every header it pulled in. What the header supplies is declarations - types and function signatures - so that the compiler can check calls. The functions themselves live in the library and are joined at link time, which is a later and quite separate stage.

Please log in or register to report this question.

Answer: A

As defined Use of fflush(stdin) in C : fflush() is typically used for output stream only. Its purpose is to clear (or flush) the output buffer and move the buffered data to console (in case of stdout) or disk (in case of file output stream).

Please log in or register to report this question.

Answer: B

Correct answer: allocates the specified number of bytes and initializes them to zero

calloc does two things where malloc does one: it allocates the storage and sets every byte of it to zero.

It also takes its size as a count and an element size rather than a single total, and it is required to detect overflow in the product of the two - a check a hand-written malloc(m * n) does not get. Resizing an existing block is realloc's work, described by the third option.

Please log in or register to report this question.

Answer: C

Correct answer: Preprocessor -> Compiler -> Assembler -> Linking

Each stage consumes what the previous one produced. The preprocessor handles #include, #define and conditional compilation, emitting a single translation unit of pure C.

The compiler turns that into assembly, the assembler into an object file holding machine code with unresolved external references, and the linker joins the object files and libraries into an executable. A call to a function that was declared but never defined is a link error precisely because the linker is the first stage to look across files.

Please log in or register to report this question.

70.

Which header file should be included to use functions like malloc() and calloc()?

Answer: B

Correct answer: stdlib.h

stdlib.h declares malloc, calloc, realloc and free, together with size_t.

Omitting it is more dangerous than it looks. Without the prototype an older compiler assumes malloc returns int, and where a pointer is wider than an int the address is silently truncated. A cast on the result hides exactly the warning that would have revealed it, which is the main argument against casting in C.

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