

















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















































































Question 1. Which phase of the C compilation process translates source code directives like #include and #define into a form the compiler can understand? A) Preprocessor B) Assembler C) Linker D) Runtime loader Answer: A Explanation: The preprocessor handles macro expansion, file inclusion, and conditional compilation before actual compilation. Question 2. In the statement int *p = &x;, what does the & operator produce? A) The value of x B) The address of x C) A pointer to the pointer p D) The size of x Answer: B Explanation: The address‑of operator & yields the memory address of its operand. Question 3. Which of the following format specifiers is correct for printing a double with printf? A) %f B) %lf C) %d D) %c Answer: A Explanation: In printf, %f is used for both float and double. The l length modifier is ignored.
Question 4. What is the result of the expression 5 > 3 && 2 < 4? A) 0 B) 1 C) 5 D) Undefined Answer: B Explanation: Both relational expressions are true, and && yields true (1) when both operands are true. Question 5. Which storage class gives a variable internal linkage, making it visible only within its translation unit? A) extern B) static C) auto D) register Answer: B Explanation: static at file scope restricts the variable’s visibility to the file it is defined in. Question 6. Which of the following statements about const qualified pointers is true? A) int * const p makes the pointed value constant. B) const int * p makes the pointer constant. C) int const * p makes the pointed value constant. D) int * p const is valid syntax. Answer: C Explanation: int const * p (or const int * p) declares a pointer to a constant integer; the pointer itself can change.
Question 10. Which of the following prototypes correctly declares a function that returns a pointer to an integer and takes a const char * argument? A) int * func(const char *); B) int func(const char *); C) int * const func(const char *); D) int * func(char const); Answer: A Explanation: int * func(const char *); returns int * and accepts a pointer to constant characters. Question 11. In C, which of the following correctly allocates memory for an array of 10 double elements and initializes them to zero? A) double *p = malloc(10 * sizeof(double)); B) double *p = calloc(10, sizeof(double)); C) double *p = realloc(NULL, 10 * sizeof(double)); D) double *p = malloc(10); Answer: B Explanation: calloc allocates and zero‑initializes the requested memory. Question 12. What does the expression *(p + 3) mean when p is a pointer to int? A) The fourth element of the array that p points to. B) The address of the third element. C) The value stored at p plus three. D) Undefined behavior. Answer: A
Explanation: Pointer arithmetic adds the element size; p + 3 points to the fourth int, and dereferencing yields its value. Question 13. Which of the following statements about sizeof is correct? A) sizeof can be applied to a function. B) sizeof yields the size of the expression after evaluation. C) sizeof is evaluated at compile time for most cases. D) sizeof returns the number of bits. Answer: C Explanation: sizeof is a compile‑time operator that yields the size in bytes of its operand, unless applied to a VLA. Question 14. Which of the following is a valid way to prevent multiple inclusion of a header file named utils.h? A) #pragma once only. B) #ifndef UTILS_H #define UTILS_H … #endif C) #include "utils.h" inside utils.h. D) No protection is needed. Answer: B Explanation: The classic include guard uses #ifndef, #define, and #endif to ensure the header’s contents are processed once. Question 15. What is the output of the following code?
int a = 5; printf("%d %d", a++, ++a); Explanation: Subtracting 1 flips all bits up to the least‑significant 1; ANDing with the original clears that bit. **Question 18.** Which of the following statements about `enum` types is false? A) Enumerators are of type `int` by default. B) An enumerator can be assigned a negative value. C) The size of an `enum` is guaranteed to be one byte. D) You can explicitly set the value of an enumerator. Answer: C Explanation: The C standard does not guarantee the size of an `enum`; it is implementation‑defined, often the size of `int`. **Question 19.** What does the following macro compute? ```c #define SQR(x) ((x)*(x)) A) The square of x with correct precedence. B) The square of x but may evaluate x twice. C) The cube of x. D) It causes a compilation error. Answer: B Explanation: The macro expands x twice, so side effects in x (e.g., i++) are evaluated twice, leading to unexpected results.
Question 20. Which of the following statements about the static keyword when applied to a function parameter is correct? A) It makes the parameter retain its value between calls. B) It is illegal; static cannot be used on parameters. C) It forces the parameter to be passed by reference. D) It changes the parameter’s storage class to global. Answer: B Explanation: static may be used in function definitions for array parameters (e.g., int a[static 10]) but not to give parameters static storage. Question 21. What is the effect of the -Wall flag when compiling with gcc? A) Enables all warnings. B) Enables all optimizations. C) Links the program statically. D) Generates debugging information. Answer: A Explanation: -Wall instructs GCC to enable most common warning messages. Question 22. Which of the following is true about malloc when the requested size is zero? A) It always returns NULL. B) It returns a unique pointer that must be freed. C) Its behavior is implementation‑defined; it may return NULL or a non‑NULL pointer. D) It crashes the program. Answer: C Explanation: The C standard leaves the result of malloc(0) implementation‑defined. Question 23. Which of the following correctly opens a file for reading binary data?
B) A function named cmp_f. C) An array of function pointers. D) A structure containing function pointers. Answer: A Explanation: cmp_f becomes an alias for the specified function‑pointer type. Question 27. Which statement about the assert macro is false? A) It is removed when compiling with NDEBUG defined. B) It aborts the program if the expression evaluates to false. C) It can be used for runtime error handling in production code. D) It prints the failed expression, file, and line number. Answer: C Explanation: assert is intended for debugging; it should not be relied upon for production error handling. Question 28. In the following code, what is the value of *p after execution?
int a = 10; int *p = &a; *p = *p + 5; A) 5 B) 10 C) 15
D) Undefined Answer: C Explanation: *p refers to a; adding 5 makes a (and thus *p) equal to 15. Question 29. Which of the following statements about argv is correct? A) argv[0] always contains the string "main". B) argv[argc] is guaranteed to be NULL. C) argv[1] holds the program’s executable name. D) argv is an array of integers. Answer: B Explanation: By convention, argv[argc] is a null pointer marking the end of the argument list. Question 30. What is the result of the expression (~0U) >> 1 on a typical 32‑bit unsigned integer? A) 0x7FFFFFFF B) 0xFFFFFFFF C) 0x80000000 D) 0x00000000 Answer: A Explanation: ~0U yields all 1 bits (0xFFFFFFFF). Right‑shifting by 1 produces 0x7FFFFFFF. Question 31. Which of the following is a correct way to define a macro that safely computes the maximum of two values without evaluating arguments multiple times? A) #define MAX(a,b) ((a) > (b)? (a) : (b)) B) static inline int max(int a, int b) { return a > b? a : b; } C) #define MAX(a,b) (a > b? a : b) D) #define MAX(a,b) ((a) > (b)? (a) : (b))
D) Flushes the file buffer. Answer: B Explanation: SEEK_END with offset 0 positions the file pointer at the end of the file. Question 35. Which of the following C standards introduced the _Bool type and the <stdbool.h> header? A) C89/C B) C C) C D) C Answer: B Explanation: The _Bool type and the stdbool.h header were added in the C99 revision. Question 36. Which of the following statements about restrict qualifier is correct? A) It guarantees that a pointer is never NULL. B) It tells the compiler that the pointed-to object is accessed only through that pointer for the lifetime of the pointer. C) It forces the pointer to be stored in a register. D) It is only applicable to function return types. Answer: B Explanation: restrict provides aliasing information, enabling better optimization. Question 37. What is the output of the following code on a little‑endian machine?
unsigned int x = 0x11223344; printf("%c", *((char *)&x)); ## ``` A) `0x44` (character with ASCII code 68) B) `0x11` C) `0x22` D) Undefined behavior Answer: A Explanation: On little‑endian, the least‑significant byte (`0x44`) is stored first; casting to `char*` and dereferencing yields that byte. **Question 38.** Which of the following statements about `malloc` and `free` is false? A) Memory obtained by `malloc` must be released with `free`. B) Passing a pointer not obtained from `malloc` to `free` results in undefined behavior. C) `free(NULL)` is a no‑op. D) After `free(p)`, the pointer `p` becomes automatically `NULL`. Answer: D Explanation: `free` does not modify the pointer variable; it remains dangling unless manually set to `NULL`. **Question 39.** In which situation is a `static` local variable useful? A) When the variable must be shared across translation units. B) When the variable’s value must persist between function calls. C) When the variable should be stored in a register. D) When the variable should be allocated on the heap. Answer: B Explanation: A `static` local variable retains its value across multiple invocations of the function. C) `char *ptr;` D) `generic *ptr;` Answer: A Explanation: `void *` is a generic pointer that can be converted to/from any object pointer type. **Question 43.** What is the purpose of the `fflush(stdout);` call? A) Closes the standard output stream. B) Forces any buffered output for `stdout` to be written immediately. C) Clears the contents of the screen. D) Rewinds the file pointer to the beginning. Answer: B Explanation: `fflush` forces buffered data to be written to the underlying file descriptor. **Question 44.** Which of the following statements about the `strncpy` function is true? A) It always null‑terminates the destination string. B) It copies exactly `n` characters, padding with null bytes if necessary. C) It returns a pointer to the destination string. D) It cannot be used for overlapping memory regions. Answer: B Explanation: `strncpy` copies up to `n` characters; if the source is shorter, it pads the remainder with `'\0'`. It does not guarantee null‑termination if the source length ≥ `n`. **Question 45.** Which of the following best describes the effect of the `-Werror` flag in GCC? A) Treats all warnings as errors, causing compilation to fail. B) Disables all warning messages. C) Enables extra optimization levels. D) Generates debugging symbols. Answer: A Explanation: `-Werror` converts all warnings into errors, halting compilation. **Question 46.** In the context of a `for` loop, which part is executed *first*? A) The condition expression B) The iteration expression C) The initialization expression D) The loop body Answer: C Explanation: The initialization runs once before the first condition check. **Question 47.** Which of the following statements about `int main(void)` is correct? A) It indicates that `main` takes no arguments. B) It is equivalent to `int main()` in all contexts. C) It forces the program to exit with status 0. D) It is illegal in ISO C. Answer: A Explanation: `void` in the parameter list explicitly specifies that the function takes no arguments. **Question 48.** Which of the following is true about the `calloc` function compared to `malloc`? A) `calloc` takes a single argument specifying total size. B) `calloc` initializes allocated memory to zero. C) `calloc` can only allocate memory for arrays of `char`. D) `calloc` never fails. D) It causes the variable to be allocated on the stack. Answer: B Explanation: At file scope, `static` restricts the variable’s linkage to the current source file. **Question 52.** What does the `sizeof` operator return when applied to a function type? A) The size of a function pointer. B) The size of the function’s code. C) It is illegal; applying `sizeof` to a function type is a compile‑time error. D) Zero. Answer: C Explanation: The operand of `sizeof` must be an object type; a function type is not allowed. **Question 53.** Which of the following is a correct way to define a macro that concatenates two tokens `a` and `b`? A) `#define CAT(a,b) a ## b` B) `#define CAT(a,b) a + b` C) `#define CAT(a,b) a ## b ##` D) `#define CAT(a,b) a b` Answer: A Explanation: The token‑pasting operator `##` concatenates two tokens during macro expansion. **Question 54.** Which of the following statements about the `enum` underlying type is guaranteed by the C standard? A) It is always `int`. B) It is large enough to hold all enumerator values. C) It is always `unsigned int`. D) It is implementation‑defined and may be smaller than `int`. Answer: B Explanation: The standard requires the underlying type to be able to represent all enumerator values; its exact size is implementation‑defined. **Question 55.** Which of the following is the correct way to declare a function that takes a variable number of arguments? A) `int sum(int count, ...);` B) `int sum(...);` C) `int sum(int count, int args[]);` D) `int sum(int count, int *args);` Answer: A Explanation: The ellipsis `...` after at least one named parameter indicates a variadic function. **Question 56.** Which of the following is true about the `memcpy` function? A) It can safely copy overlapping memory regions. B) It copies `n` bytes from source to destination regardless of type. C) It stops copying when it encounters a null byte. D) It returns the number of bytes copied. Answer: B Explanation: `memcpy` copies a specified number of bytes; overlapping regions lead to undefined behavior (use `memmove` instead). **Question 57.** Which of the following statements about `static` functions is correct? A) They can be called from other translation units. B) They have internal linkage, making them invisible outside the source file. C) They must be declared before `main`. D) They are automatically inlined.