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

Memory Allocation Questions and Answers

81.

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.

82.

Assume integer is 2 bytes wide. What will be the output of the following code?

#include <stdio.h>
#include <stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    printf("%d, %d\n", sizeof(p), sizeof(*p));
    return 0;
}

 

Answer: A

Correct answer: 2, 8

Two different quantities are being measured. sizeof(p) is the size of the pointer itself, which on the 16-bit platform assumed here is 2 bytes.

sizeof(*p) is the size of what it points to - an array of MAXCOL integers:

\[ 4 \times 2 = 8\ \mathrm{bytes} \]

The pair illustrates why sizeof on a pointer almost never gives what a beginner expects: it reports the size of the address, and the pointed-to type must be dereferenced to be measured.

Please log in or register to report this question.

83.

What is the output of this program?

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int *p;
  p = (int *)malloc(40);
  printf("%d", sizeof(p));
  free(p);
  return 0;
}

 

Answer: C

Since p is the pointer variable of integer type, thus the size of integer type is 4.

Please log in or register to report this question.

84.

Can we increase the size of statically allocated array?

Answer: B

Correct answer: No

A statically allocated array has its size fixed at compile time, in the stack frame or the data segment, and there is no mechanism to change it while the program runs.

The only way to get a larger array is to allocate a new one dynamically and copy the contents across - which is precisely what realloc does for heap blocks. This inflexibility is the central reason dynamic allocation exists, and it is why growable structures are always built on the heap.

Please log in or register to report this question.

85.

What function should be used to free the memory allocated by calloc() ?

Answer: C

Correct answer: free();

Memory from calloc is released by free, exactly as memory from malloc or realloc is. There is one deallocation function for all three.

That is possible because all three obtain their storage from the same allocator; the difference between them lies in how the block is sized and whether it is zeroed, not in where it comes from. The other names offered are not part of standard C.

Please log in or register to report this question.

86.

Point out the error in the following program.

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

int main()
{
    char *ptr;
    *ptr = (char)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return 0;
}

 

Answer: B

Correct answer: Error: in *ptr = (char)malloc(30);

The line assigns to *ptr - a single character - rather than to ptr, and casts the returned address to (char), which throws away everything but its lowest byte.

It should read ptr = malloc(30);. The strcpy that follows is then writing through a pointer that holds no valid address, so it is the line most likely to crash - but the cause lies in the allocation line above it, which is what the question asks for.

Please log in or register to report this question.

87.

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.

88.

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.

89.

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.

90.

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.

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