C Standard Library Functions MCQ

1. What does the 'printf' function do in C?

a) Reads input from the user
b) Prints output to the console
c) Initializes a variable
d) Ends the program

Answer:

b) Prints output to the console

Explanation:

The 'printf' function is used in C to print output to the console. It can format strings, numbers, and other data types.

2. Which function is used to read a formatted input in C?

a) printf
b) scanf
c) gets
d) fgets

Answer:

b) scanf

Explanation:

The 'scanf' function is used to read formatted input from the standard input (console).

3. Which header file must be included to use the 'printf' and 'scanf' functions?

a) <math.h>
b) <string.h>
c) <stdio.h>
d) <stdlib.h>

Answer:

c) <stdio.h>

Explanation:

The 'printf' and 'scanf' functions are part of the standard input/output library in C, and they require the inclusion of the <stdio.h> header file.

4. What does the 'strcpy' function do in C?

a) Concatenates two strings
b) Compares two strings
c) Copies a string
d) Searches for a character in a string

Answer:

c) Copies a string

Explanation:

The 'strcpy' function is used to copy one string into another in C.

5. Which standard library function allocates memory dynamically in C?

a) malloc
b) free
c) sizeof
d) realloc

Answer:

a) malloc

Explanation:

The 'malloc' function is used to allocate memory dynamically at runtime in C.

6. How do you compare two strings in C?

a) using ==
b) with the strcmp function
c) with the strcat function
d) using =

Answer:

b) with the strcmp function

Explanation:

The 'strcmp' function is used to compare two strings in C. It returns 0 if the strings are equal.

7. Which function is used to calculate the length of a string in C?

a) strlen
b) strlength
c) length
d) size

Answer:

a) strlen

Explanation:

The 'strlen' function is used to calculate the length of a string in C, excluding the null terminator.

8. What does the 'free' function do in C?

a) Allocates memory
b) Frees dynamically allocated memory
c) Initializes memory
d) Copies memory

Answer:

b) Frees dynamically allocated memory

Explanation:

The 'free' function is used to release memory that was previously allocated dynamically (e.g., using malloc or calloc).

9. Which function is used to read a line of text from a file in C?

a) fscanf
b) fread
c) fgets
d) getc

Answer:

c) fgets

Explanation:

The 'fgets' function is used to read a line of text from a file or other stream in C.

10. How can you open a file in C?

a) Using the open function
b) With the fopen function
c) By calling the file function
d) Using the openfile function

Answer:

b) With the fopen function

Explanation:

The 'fopen' function is used to open a file in C, returning a pointer to the file.

11. Which function is used to close a file in C?

a) fclose
b) close
c) closefile
d) fexit

Answer:

a) fclose

Explanation:

The 'fclose' function is used to close a file in C. It takes a file pointer as its argument.

12. What is the purpose of the 'qsort' function in C?

a) To search an array
b) To sort elements in an array
c) To query the size of an array
d) To partition an array

Answer:

b) To sort elements in an array

Explanation:

The 'qsort' function is used for sorting elements in an array. It implements the quicksort algorithm.

13. Which function is used to convert a string to an integer in C?

a) stoi
b) str2int
c) atoi
d) itoa

Answer:

c) atoi

Explanation:

The 'atoi' function converts a string to an integer in C.

14. How is memory allocated for an array of integers in C?

a) Using the array function
b) With the malloc function
c) By declaring an array variable
d) Through the new operator

Answer:

b) With the malloc function

Explanation:

The 'malloc' function can be used to allocate memory dynamically for an array of integers in C, based on the required number of elements.

15. What does the 'memset' function do in C?

a) Sets a memory block with a specific value
b) Clears a memory block
c) Compares two memory blocks
d) Copies a memory block

Answer:

a) Sets a memory block with a specific value

Explanation:

The 'memset' function is used to fill a block of memory with a specific value in C. It is often used for initialization.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top