1. What is the primary purpose of the stdio.h header file in C?
Step-by-Step Explanation
stdio.h provides functions like printf(), scanf(), fopen(), fclose(), etc., for handling input and output operations.
Answer: a) Standard input/output functions. ✅
2. Which data type is used to store a single character in C?
Step-by-Step Explanation
char is specifically designed to store single characters (e.g., 'A', 'b', '9').
Answer: c) char ✅
3. What is the purpose of the & operator in C?
Step-by-Step Explanation
The & operator returns the memory address of a variable.
Answer: b) Address-of operator. ✅
4. Which keyword is used to define a constant in C?
Step-by-Step Explanation
The const keyword makes a variable read-only, preventing its value from being changed after initialization.
Answer: d) const ✅
5. What is the output of printf("%d", sizeof(int)); on a typical 32-bit system?
Step-by-Step Explanation
sizeof(int) returns the size of an integer in bytes, which is usually 4 bytes on 32-bit systems.
Answer: a) 4 ✅
6. Which function is used to dynamically allocate memory in C?
Step-by-Step Explanation
malloc() allocates a block of memory of a specified size and returns a pointer to the beginning of the allocated block.
Answer: b) malloc() ✅
7. What does the -> operator do in C?
Step-by-Step Explanation
The -> operator is used to access members of a structure or union when you have a pointer to that structure or union.
Answer: d) Accesses structure members using a pointer. ✅
8. Which loop in C is guaranteed to execute at least once?
Step-by-Step Explanation
The do-while loop checks the condition after executing the loop body, ensuring at least one iteration.
Answer: c) do-while loop ✅
9. What is the purpose of the static keyword when used with a local variable in a function?
Step-by-Step Explanation
A static local variable retains its value between function calls, acting as a persistent local variable.
Answer: a) Retains its value between function calls. ✅
10. Which header file is required for using strlen() function in C?
Step-by-Step Explanation
string.h contains functions for string manipulation, including strlen().
Answer: d) string.h ✅
11. What is the purpose of the typedef keyword in C?
Step-by-Step Explanation
typedef allows you to create custom names for existing data types, improving code readability.
Answer: b) To create an alias for a data type. ✅
12. What is the output of the following code?
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3};
printf("%d", *(arr + 1));
return 0;
}
Step-by-Step Explanation
arr + 1 points to the second element of the array, and *(arr + 1) dereferences it, giving the value 2.
Answer: c) 2 ✅
13. What is the purpose of the extern keyword in C?
Step-by-Step Explanation
extern is used to declare variables or functions that are defined in another source file, allowing them to be used in the current file.
Answer: a) To declare a variable or function defined in another file. ✅
14. Which function is used to read a string from standard input in C?
Step-by-Step Explanation
gets() is used to read a string from standard input, but it is generally avoided due to buffer overflow vulnerabilities. fgets() is a safer alternative.
Answer: d) gets() ✅
15. What is the purpose of the volatile keyword in C?
Step-by-Step Explanation
volatile tells the compiler that the variable's value can change in ways not controlled by the compiler, such as by hardware or other threads.
Answer: b) To indicate that a variable's value might be changed by external factors. ✅
16. What is the correct way to declare a function pointer in C?
Step-by-Step Explanation
This syntax correctly declares a pointer to a function that returns return_type and takes parameters.
Answer: a) return_type (*ptr_name)(parameters); ✅
17. What is the purpose of the #include preprocessor directive in C?
Step-by-Step Explanation
#include is used to insert the contents of a header file into the current source file.
Answer: c) To include the contents of a header file. ✅
18. Which function is used to release dynamically allocated memory in C?
Step-by-Step Explanation
free() releases the memory block that was previously allocated by malloc(), calloc(), or realloc().
Answer: b) free() ✅
19. What is the output of the following code?
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x++);
return 0;
}
Step-by-Step Explanation
x++ is a post-increment operator. It returns the current value of x (5) before incrementing it.
Answer: d) 5 ✅
20. What is the purpose of the enum keyword in C?
Step-by-Step Explanation
enum defines an enumerated type, which is a set of named integer constants.
Answer: b) To define a set of named integer constants. ✅