MCQ on Memory allocation and compilation process

1. What will be the output of the following code?

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

int main()
{
    union test {
        int i;
        float f;
        char c;
    };
    union test* t;
    t = (union test*)malloc(sizeof(union test));
    t->f = 20.60f;
    printf("%f", t->f);
    return 0;
}

(a) Garbage value
(b) 20.600000
(c) Syntax Error
(d) 20.60

Explanation :
Unions provide an efficient way of using the same memory location. All member of Union uses same memory location which has maximum space.
Here, float f is used which has

f = 20.60f = 20.600000

. So, option (b) is correct.

2. Which is the correct sequence of compilation process?
(a) Assembler → Compiler → Preprocessor → Linking
(b) Compiler → Assenbler → Preprocessor → Linking
(c) Preprocessor → Compiler → Assembler → Linking
(d) Assembler → Compiler → Linking → Preprocessor

Explanation :
Correct order is :


Option (c) is correct.

3. During preprocessing, the code #include gets replaced by the contents of the file stdio.h. Which is true?
(a) During linking the code #include replaces by stdio.h
(b) Yes
(c) During execution the code #include replaces by stdio.h
(d) During editing the code #include replaces by stdio.h

Explanation :
Preprocessing enlarges and boosts the C programming language by replacing preprocessing directive #include with the content of the file stdio.h.
Option (b).

4. Why to use fflush() library function?
(a) To flush all streams and specified streams
(b) To flush only specified stream
(c) To flush input/output buffer
(d) Invalid library function

Explanation :
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).
Option (a) is correct.

5. Point out the error ->


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

(a) Error: in strcpy() statement.
(b) Error: in *ptr = (int *) malloc(30);
(c) Error: in free(ptr);
(d) No error

Explanation :
Assignment makes integer from pointer without a cast. Option (b) is true.

6. Why is calloc() function used for?
(a) allocates the specified number of bytes
(b) allocates the specified number of bytes and initializes them to zero
(c) increases or decreases the size of the specified block of memory and reallocates it if needed
(d) calls the specified block of memory for execution.

Explanation :

  • malloc() : allocates the specified number of bytes.
  • realloc() : increases or decrease the size of the specified block of memory. Reallocates it if needed.
  • calloc() : allocates the specified number of bytes and initializes them to zero.
  • free() : releases the specified block of memory back to the system.

So, option (b) is correct.

This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.