

























































































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 of the following is the correct way to include the standard input‑output header in a C program? A) #include B) #include "stdio.h" C) import D) #include stdio.h Answer: A Explanation: The angle‑bracket form #include tells the preprocessor to search the system include directories for the header file. Question 2. What is the result of the expression sizeof(char) on any conforming C implementation? A) 0 B) 1 C) 2 D) Implementation‑defined Answer: B Explanation: By definition, sizeof(char) is always 1 byte. Question 3. Which literal represents an unsigned long integer with the value 5000? A) 5000U B) 5000UL C) 5000L D) 5000ULL Answer: B Explanation: The suffix UL designates an unsigned long constant.
Question 4. Which format specifier should be used with printf to print a double value in scientific notation? A) %e B) %f C) %g D) %lf Answer: A Explanation: %e prints a floating‑point number in scientific (exponential) notation. Question 5. Which escape sequence represents a horizontal tab character? A) \n B) \t C) \b D) \r Answer: B Explanation: \t is the escape sequence for a horizontal tab. Question 6. In C, which of the following identifiers is illegal? A) _temp B) temp C) 1temp D) temp_var Answer: C Explanation: Identifiers cannot begin with a digit.
Question 10. Which operator has the highest precedence in C? A) + (addition) B) * (multiplication) C) ++ (prefix increment) D) = (assignment) Answer: C Explanation: Prefix increment has higher precedence than multiplication and addition, and much higher than assignment. Question 11. What is the result of 5 / 2 in C when both operands are of type int? A) 2. B) 2 C) 3 D) 0 Answer: B Explanation: Integer division discards the fractional part, yielding 2. Question 12. Which of the following expressions is equivalent to a = a + b;? A) a += b; B) a =+ b; C) a = b + a; D) a ++ b; Answer: A Explanation: The compound assignment += adds b to a and stores the result in a.
Question 13. What is the value of x after executing int x = 10; x *= 3 + 2;? A) 30 B) 50 C) 60 D) 70 Answer: B Explanation: The expression 3 + 2 evaluates to 5; then x *= 5 results in x = 10 * 5 = 50. Question 14. Which relational operator tests for inequality? A) == B) != C) <> D) !== Answer: B Explanation: != returns true when the two operands are not equal. Question 15. What does the logical expression !(a && b) evaluate to when a is true and b is false? A) true B) false C) undefined D) compilation error Answer: A Explanation: a && b is false (since b is false); applying ! yields true.
Answer: A Explanation: Converting a float to an int discards the fractional portion. Question 19. What is the output of the following code? printf("%d", (int)3.99); A) 3 B) 4 C) 3. D) Compilation error Answer: A Explanation: Casting to int truncates the decimal part, yielding 3. Question 20. Which of the following is a correct way to declare a function that returns a pointer to an integer? A) int* func(); B) int func*(); C) *int func(); D) int func(); Answer: A Explanation: int* func(); declares func as returning a pointer to int. Question 21. In a switch statement, what happens if a break statement is omitted after a case block? A) The program terminates. B) Execution continues into the next case (fall‑through). C) The next case is skipped. D) Compilation error.
Answer: B Explanation: Without break, control falls through to the subsequent case labels. Question 22. Which loop guarantees that its body is executed at least once? A) while B) do‑while C) for D) none of the above Answer: B Explanation: The do‑while loop evaluates its condition after executing the body. Question 23. What is the purpose of the continue statement inside a loop? A) Exit the loop entirely. B) Skip the rest of the current iteration and evaluate the loop condition again. C) Restart the loop from the beginning. D) Pause execution until user input. Answer: B Explanation: continue jumps to the loop’s condition check, starting the next iteration. Question 24. Evaluate the ternary expression: int a = 5, b = 10; int c = (a > b)? a : b;. What is the value of c? A) 5 B) 10 C) 0 D) Undefined
Answer: A Explanation: The address‑of operator & yields the memory address of x. Question 28. What does the following declaration mean? int *p; A) p is an integer pointer. B) p is a pointer to a pointer. C) p is an integer array. D) p is a function returning int. Answer: A Explanation: int *p; declares p as a pointer to int. Question 29. After executing int a = 10; int *p = &a; *p = 20;, what is the value of a? A) 10 B) 20 C) Undefined D) 0 Answer: B Explanation: Dereferencing p and assigning 20 changes the object a points to. Question 30. Which of the following statements about NULL is true? A) It is the integer 0. B) It is a macro representing a null pointer constant. C) It can be assigned to any variable type. D) It is equivalent to false. Answer: B
Explanation: NULL is defined as a null pointer constant, typically (void *)0. Question 31. What is the result of the pointer arithmetic expression p + 3 where p is of type int *? A) Increments the pointer by 3 bytes. B) Increments the pointer by 3 integers. C) Adds 3 to the integer value pointed to. D) Causes a compilation error. Answer: B Explanation: Adding 3 to an int * advances it by three int‑sized steps. Question 32. Given int arr[5] = {0,1,2,3,4};, what does the expression *(arr + 2) evaluate to? A) 0 B) 1 C) 2 D) 3 Answer: C Explanation: arr decays to a pointer to its first element; adding 2 moves to arr[2], which is 2. Question 33. Which statement correctly explains the relationship between an array name and a pointer? A) The array name is a modifiable pointer variable. B) The array name is a constant pointer to its first element. C) The array name stores the size of the array. D) The array name can be assigned to another pointer.
Answer: A Explanation: The syntax struct Point { int x, y; }; defines a structure type. Question 37. How do you access member y of a structure variable p of type struct Point? A) p->y B) p.y C) (p).y D) p#y Answer: B Explanation: For a structure variable (not a pointer), the dot operator selects members. Question 38. If struct Point *ptr = &p; where p is a struct Point, how do you set x to 5 using the pointer? A) ptr.x = 5; B) (ptr).x = 5; C) ptr->x = 5; D) Both B and C are correct. Answer: D Explanation: Both (*ptr).x and the shorthand ptr->x access the member via a pointer. Question 39. Which storage class gives a variable internal linkage and static duration? A) auto B) static C) extern D) register
Answer: B Explanation: static at file scope makes the variable visible only within the translation unit and retains its value between calls. Question 40. What is the effect of the register storage class specifier? A) Forces the variable to be stored in a CPU register if possible. B) Makes the variable globally accessible. C) Allocates memory on the heap. D) Declares a constant variable. Answer: A Explanation: register hints to the compiler that the variable will be heavily used and should be kept in a register. Question 41. Which function allocates memory for an array of 10 integers and initializes them to zero? A) malloc(10 * sizeof(int)); B) calloc(10, sizeof(int)); C) realloc(NULL, 10 * sizeof(int)); D) malloc(sizeof(int[10])); Answer: B Explanation: calloc allocates and zero‑initializes the memory. Question 42. After allocating memory with malloc, which function must eventually be called to avoid a memory leak? A) calloc() B) realloc()
B) Undefined behavior C) The value of the last evaluated expression D) Compiler error Answer: B Explanation: Falling off the end of a non‑void function yields undefined behavior. Question 46. Which of the following correctly passes an array to a function so that the function can modify its elements? A) void modify(int a[10]); B) void modify(int *a); C) void modify(int a); D) Both A and B are correct. Answer: D Explanation: In function parameters, int a[10] decays to int *a; both declarations allow modification of the original array. Question 47. How does passing an argument by value differ from passing by reference? A) By value copies the argument; by reference passes its address. B) By value passes the address; by reference copies the value. C) Both copy the argument. D) Both pass the address. Answer: A Explanation: int x passes a copy; int *p (or int & in C++) passes a reference to the original. Question 48. Which of the following is a proper recursive function base case for computing factorial?
A) if (n == 0) return 1; B) if (n == 1) return n; C) if (n < 0) return n; D) No base case needed. Answer: A Explanation: The factorial of 0 is defined as 1, providing a terminating condition. Question 49. What will the preprocessor do with the following code? #ifdef DEBUG printf("Debug"); #endif when DEBUG is not defined? A) Print "Debug". B) Remove the line entirely. C) Cause a compilation error. D) Treat #ifdef as a comment. Answer: B Explanation: The block is excluded from the translation unit when DEBUG is undefined. Question 50. Which macro definition correctly creates a function‑like macro that squares its argument? A) #define SQUARE(x) ((x)(x)) B) #define SQUARE x ((x)(x)) C) #define SQUARE(x) (xx) D) #define SQUARE(x) xx Answer: A Explanation: Parentheses around the parameter and the whole expression prevent precedence problems.
Explanation: goto may transfer control to any label in the same function scope. Question 54. What is the result of the expression (~0U) >> 1 on a 32‑bit unsigned integer? A) 0x7FFFFFFF B) 0xFFFFFFFF C) 0x D) 0x Answer: A Explanation: ~0U yields all 1 bits (0xFFFFFFFF). Right‑shifting by 1 gives 0x7FFFFFFF. Question 55. Which of the following correctly declares a pointer to a function that takes two int arguments and returns a float? A) float (*fp)(int, int); B) float fp(int, int); C) float fp(int, int); D) float (fp)(int, int); Answer: A Explanation: float (*fp)(int, int); declares fp as a pointer to such a function. Question 56. In the expression a = b = c = 0;, how many assignments are performed? A) One B) Two C) Three D) Four Answer: C
Explanation: The expression associates right‑to‑left, performing three assignments. Question 57. Which of the following format specifiers is used to read a single character with scanf? A) %c B) %s C) %d D) %hhd Answer: A Explanation: %c reads a character, including whitespace. Question 58. What is the output of the following code? int a = 5; printf("%#x", a); A) 0x B) 5 C) 0X D) 0x Answer: A Explanation: The # flag adds the 0x prefix for hexadecimal output. Question 59. Which of the following statements about enum types is correct? A) Enumerators must be strings. B) The underlying type is always int. C) Enumerators are integer constants that can be used as integral values. D) enum cannot be used with switch. Answer: C