





































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 compilation process translates source code into an intermediate representation before actual machine code generation? A) Preprocessing B) Assembling C) Linking D) Compiling Answer: D Explanation: The compiler parses the source, performs syntax/semantic analysis, and creates an intermediate representation (e.g., assembly) that later stages turn into machine code. Question 2. In a C program, which header file must be included to use the printf function? A) B) C) D) Answer: B Explanation: printf is declared in ``; omitting it leads to an implicit-function warning. Question 3. Which of the following is a correct identifier name according to C naming rules? A) 2ndValue B) float C) _max_speed D) my-value Answer: C Explanation: Identifiers may start with a letter or underscore and contain letters, digits, or underscores. float is a keyword; 2ndValue starts with a digit; hyphens are illegal. Question 4. What is the lifetime of a variable declared inside a function without the static keyword? A) Entire program execution
B) Until the function returns C) Until the next call to the function D) Until the block ends Answer: B Explanation: Automatic (non-static) local variables exist only while the function is executing; they are destroyed when the function returns. Question 5. Which of the following literals is of type double? A) 3.14f B) 3.14 C) 3 D) '3' Answer: B Explanation: A floating-point literal without a suffix defaults to double; the f suffix forces float. Question 6. Which type modifier creates a signed integer that can store values larger than a regular int? A) short B) long C) unsigned D) char Answer: B Explanation: long int (or just long) typically provides a wider range than int. Question 7. What does the following statement do? float result = (float)5 / 2; A) Performs integer division, then casts to float B) Casts 5 to float, then performs floating-point division C) Causes a compilation error D) Returns 2.0 as an integer Answer: B
Answer: A Explanation: The modulus operator returns the remainder; 7 divided by 3 leaves a remainder of 1. Question 12. Which compound assignment operator is equivalent to x = x * 5;? A) x *= 5; B) x += 5; C) x /= 5; D) x %= 5; Answer: A Explanation: *= multiplies the variable by the right-hand operand and assigns the result. Question 13. In the expression ++i, when is the increment performed relative to the value being used? A) After the current value is used (post-increment) B) Before the current value is used (pre-increment) C) At the end of the statement only D) It depends on compiler optimization Answer: B Explanation: Prefix ++ increments i first, then yields the new value. Question 14. Which logical operator has the highest precedence in C? A) && B) || C) ! D) == Answer: C
Explanation: The logical NOT operator ! binds tighter than && and ||. Question 15. What does the relational expression a != b evaluate to when a and b are equal? A) 0 (false) B) 1 (true) C) Undefined behavior D) Compilation error Answer: A Explanation: != yields true when operands differ; if they are equal, the result is false (0). Question 16. Which bitwise operator in C performs an exclusive OR operation? A) & B) | C) ^ D) ~ Answer: C Explanation: ^ sets each bit to 1 only if the corresponding bits of the operands differ. Question 17. What is the effect of the expression value << 2 on an unsigned integer value? A) Divides value by 4 B) Multiplies value by 4 C) Rotates bits left by 2 positions D) Performs logical right shift Answer: B Explanation: Left shift by n positions multiplies the value by 2ⁿ (provided no overflow). Question 18. Which control structure is most appropriate for checking a variable against many constant values?
Question 22. How many elements are validly indexed in an array declared as char name[20];? A) 19 B) 20 C) 21 D) 0 Answer: B Explanation: Array indices run from 0 to 19 inclusive, giving 20 accessible positions. Question 23. Which of the following statements about multi-dimensional arrays is true? A) int matrix[3][4]; creates 4 rows and 3 columns B) The total number of elements equals the product of dimensions C) Only the first dimension can be omitted in a function parameter D) All dimensions must be constant expressions Answer: B Explanation: For int matrix[3][4]; there are 3 × 4 = 12 elements. Question 24. What is the result of accessing arr[5] when arr was declared as int arr[5];? A) The first element B) The sixth element (out-of-bounds) C) Compile-time error D) Undefined behavior at runtime Answer: D Explanation: Index 5 is beyond the valid range 0-4; the behavior is undefined. Question 25. Which operator yields the memory address of a variable? A) * B) & C) #
Answer: B Explanation: The address-of operator & returns a pointer to its operand. Question 26. Given int *p; int x = 10; p = &x;, what does *p = 20; accomplish? A) Changes the value of p to 20 B) Changes x to 20 C) Causes a segmentation fault D) Allocates new memory for p Answer: B Explanation: Dereferencing p accesses the object it points to (x), assigning 20 to x. Question 27. Which statement correctly allocates memory for an array of 15 doubles on the heap? A) double *arr = malloc(15); B) double *arr = malloc(15 * sizeof(double)); C) double arr = malloc(15 * sizeof(double)); D) double arr[15] = malloc(); Answer: B Explanation: malloc requires the total size in bytes; multiplying by sizeof(double) gives the correct amount. Question 28. After allocating memory with malloc, which function must eventually be called to avoid memory leaks? A) calloc B) realloc C) free D) delete Answer: C Explanation: free releases dynamically allocated memory back to the heap.
C) void func(float a, float b); D) int func(float a, int b); Answer: A Explanation: The return type is int; both parameters are float. Question 33. In C, passing an argument by reference is typically achieved by: A) Using the ref keyword B) Passing a pointer to the variable C) Declaring the parameter as const D) Using global variables only Answer: B Explanation: C has no native pass-by-reference; a pointer to the variable allows the function to modify the original object. Question 34. What is the purpose of a function prototype? A) To define the function body B) To allocate memory for the function C) To inform the compiler of the function’s return type and parameters before its use D) To execute the function at compile time Answer: C Explanation: Prototypes allow calls to functions that are defined later in the source file. Question 35. Which statement correctly declares a function that does not return a value? A) void display(); B) int display(); C) char display(); D) float display(); Answer: A Explanation: void indicates the function returns no value.
Question 36. Which of the following is a compilation error? A) int main() { return 0; } B) float x = 3.5; printf("%d", x); C) int a = 5; a = a + 2; D) char c = 'A'; Answer: B Explanation: %d expects an int; passing a float mismatches the format specifier, causing a compile-time warning/error. Question 37. Which of these statements about the preprocessor is true? A) It generates machine code directly. B) It can replace macros before compilation. C) It runs after linking. D) It executes at runtime. Answer: B Explanation: The preprocessor performs textual substitution (e.g., #define) before the compiler processes the code. Question 38. What does the linker do during the build process? A) Checks syntax errors B. Converts source code to assembly C) Resolves external symbol references and creates the final executable D) Allocates memory for variables Answer: C Explanation: The linker combines object files, resolves symbols, and produces the executable binary. Question 39. Which error type occurs when a program accesses memory that it was not allocated? A) Compilation error B) Runtime error C) Linker error D) Preprocessor warning
Question 43. Which of the following statements about unsigned int is correct? A) It can store negative numbers. B) It has the same range as int but only positive values. C) Its range starts at 0 and goes up to 2ⁿ-1, where n is the number of bits. D) It occupies twice the memory of a signed int. Answer: C Explanation: Unsigned integers represent values from 0 to the maximum representable unsigned number. Question 44. What does the expression a & b compute? A) Logical AND of a and b B) Bitwise AND of a and b C) Boolean OR of a and b D) Exclusive OR of a and b Answer: B Explanation: & performs a bitwise AND, combining each bit position. Question 45. Which of the following is true about the do-while loop? A) The condition is evaluated before the first iteration. B) The loop body may never execute. C) The loop guarantees at least one execution of the body. D) It cannot contain a break statement. Answer: C Explanation: do { … } while(condition); executes the body first, then checks the condition. Question 46. In the statement for (int i = 0; i < 10; ++i), what is the value of i after the loop terminates? A) 9 B) 10 C) 11 D) Undefined
Answer: B Explanation: The loop exits when i becomes 10; at that point the condition i < 10 is false. Question 47. Which of the following correctly swaps two integers using pointers? A) void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } B) void swap(int a, int b) { int temp = a; a = b; b = temp; } C) void swap(int &a, int &b) { int temp = a; a = b; b = temp; } D) void swap(int *a, int *b) { a = b; b = a; } Answer: A Explanation: Dereferencing the pointers accesses the original integers, allowing their values to be exchanged. Question 48. Which of the following is a correct way to define a macro that squares a number x? A) #define SQUARE(x) ((x) * (x)) B) #define SQUARE(x) (x * x) C) #define SQUARE x * x D) #define SQUARE(x) x*x Answer: A Explanation: Parenthesizing both the parameter and the whole expression avoids precedence problems when the macro is used with complex arguments. Question 49. What is the output of the following code?
int a = 5; printf("%d", a++);A) 5 B) 6
Explanation: By definition, sizeof(char) is always 1 byte. Question 53. Which of the following statements about enum types is correct? A) Enumerators are automatically assigned values starting at 1. B) An enum can contain duplicate values. C) The underlying type of an enum is always int. D) Enumerators must be strings. Answer: B Explanation: Enumerators can be given explicit values, and different names may share the same value. Question 54. Which of these functions can be used to concatenate two strings safely, given a destination buffer size n? A) strcat(dest, src); B) strncat(dest, src, n); C) memcpy(dest, src, n); D) sprintf(dest, "%s%s", dest, src); Answer: B Explanation: strncat appends at most n characters, preventing overflow when n is the buffer capacity minus current length. Question 55. What is the result of the expression (~0) >> 1 on a 32-bit signed integer? A) 0 B) - C) 2147483647 D) Undefined behavior Answer: C Explanation: ~0 yields all bits set (0xFFFFFFFF). Right-shifting a signed negative implementation-defined, but on two’s-complement arithmetic with arithmetic shift, the result becomes 0x7FFFFFFF (2147483647).
Question 56. Which of the following is the correct syntax to declare a variable that can hold the result of malloc without a cast in C? A) int *p = malloc(sizeof(int)); B) int *p = (int *)malloc(sizeof(int)); C) int p = malloc(sizeof(int)); D) int *p = malloc; Answer: A Explanation: In C, void* returned by malloc is implicitly convertible to any object pointer; casting is unnecessary and discouraged. Question 57. What does the continue statement do inside a for loop? A) Terminates the loop entirely. B) Skips the rest of the current iteration and proceeds to the next iteration step. C) Jumps to the loop’s condition test without executing the increment expression. D) Restarts the loop from the beginning. Answer: B Explanation: continue bypasses the remaining statements in the loop body and proceeds with the next iteration (including the increment expression in a for loop). Question 58. Which of the following format specifiers is used to print a pointer address? A) %p B) %x C) %d D) %s Answer: A Explanation: %p expects a void* and prints it in an implementation-defined format (usually hexadecimal). Question 59. Which statement correctly initializes a two-dimensional array of integers with 3 rows and 2 columns? A) int a[3][2] = {{1,2},{3,4},{5,6}}; B) int a[3,2] = {1,2,3,4,5,6};
Explanation: The function calls itself with n-1 until the base case n==0 is reached, correctly implementing recursion. Question 63. Which of the following statements about the volatile qualifier is correct? A) It tells the compiler that the variable’s value may change unexpectedly, preventing certain optimizations. B) It makes a variable read-only. C) It forces the variable to be stored in cache. D) It is only useful for global variables. Answer: A Explanation: volatile prevents the compiler from assuming the value stays unchanged between accesses, which is essential for hardware registers or signal handlers. Question 64. What does the expression sizeof(arr) / sizeof(arr[0]) compute for an array arr? A) The size of the first element in bytes B) The number of elements in the array C) The total memory used by the array in kilobytes D) The address of the last element Answer: B Explanation: Dividing total array size by element size yields the element count. Question 65. Which of the following statements about enum constants is false? A) They are of type int unless a different underlying type is specified (C99). B) They can be used wherever an integer constant expression is required. C) Their values are automatically assigned sequentially starting at 0. D) They can be assigned string literals. Answer: D Explanation: Enumerators must be integer constant expressions; strings are not allowed.
Question 66. Which of the following is a valid way to declare a function pointer named handler that points to a function taking no arguments and returning void? A) void (*handler)(); B) void handler*(); C) void handler(); D) void *handler(void); Answer: A Explanation: The syntax void (*handler)(); declares handler as a pointer to a function with the specified signature. Question 67. Which of the following statements about malloc failure is correct? A) It returns NULL when the system cannot allocate the requested memory. B) It throws a C++ exception. C) It aborts the program automatically. D) It returns a negative integer. Answer: A Explanation: On allocation failure, malloc returns a null pointer; the program must check for this condition. Question 68. Which of the following statements about typedef is true? A) It creates a new distinct type that is not compatible with the original. B) It creates an alias for an existing type, improving readability. C) It can only be used with primitive types. D) It can change the size of the underlying type. Answer: B Explanation: typedef introduces a new name for an existing type; the underlying representation remains unchanged. Question 69. What is the output of the following code?