programming
beginner
10 sample questions
C MCQ Practice Test
Basics of syntax
Q1. What will be the output of the following C code snippet?
c
#include <stdio.h>
int main() {
int x = 5;
int y = 10;
int z = x + y / 2;
printf("%d", z);
return 0;
}
Explanation: The expression `x + y / 2` is evaluated according to operator precedence. Division has higher precedence than addition. Therefore, `y / 2` (10 / 2 = 5) is calculated first, and then added to `x` (5 + 5 = 10). The program will output 10.
Q2. What is the purpose of the !!__attribute__((__packed__))!! directive in C?
-
A. It forces the compiler to align struct members to the natural size of their types
-
B. It enables the compiler to generate optimized code for the target platform
-
C. It instructs the compiler to pack struct members tightly, without padding ✓
-
D. It allows the programmer to specify custom memory allocation for variables
Explanation: The !!__attribute__((__packed__))!! directive is used to inform the compiler that it should not insert padding bytes between struct members, resulting in a more compact memory layout.
Q3. What is the purpose of the 'volatile' keyword in C when used with a global variable?
-
A. To ensure thread safety in a multi-threaded environment
-
B. To prevent the compiler from optimizing away the variable ✓
-
C. To indicate that the variable is used in a signal handler
-
D. To specify the variable's storage class
Explanation: The 'volatile' keyword in C is used to inform the compiler that a variable may change in ways that are not visible to the compiler, such as being modified by hardware or another thread. This prevents the compiler from optimizing away accesses to the variable, ensuring that the program behaves as expected.
Q4. What is the effect of the "static" keyword when applied to a function in C?
-
A. It makes the function thread-safe.
-
B. It limits the function's scope to the current file. ✓
-
C. It causes the function to be called only once during the program's execution.
-
D. It makes the function's local variables retain their values between function calls.
Explanation: When the "static" keyword is applied to a function, it gives the function internal linkage, meaning its scope is limited to the file in which it is defined. This prevents the function from being accessed or called from other files in the program.
Q5. What is the purpose of the `scanf` format specifier `%p` in C?
-
A. Reads a pointer value in hexadecimal format. ✓
-
B. Reads a pointer to a character.
-
C. Reads a pointer to an integer.
-
D. Reads a floating-point number.
Explanation: The `%p` format specifier in `scanf` is used to read a pointer value from the input stream. It expects the input to be in a hexadecimal format, typically the address of a memory location.
Q6. What is the purpose of the "__attribute__((packed))" directive in C?
-
A. "__attribute__((packed))" is used to specify that a structure should be aligned on a 4-byte boundary.
-
B. "__attribute__((packed))" is used to specify that a structure should not have any padding bytes. ✓
-
C. "__attribute__((packed))" is used to specify that a function should be called with a specific calling convention.
-
D. "__attribute__((packed))" is used to specify that a variable should be initialized with a specific value.
Explanation: "__attribute__((packed))" is a GNU extension that tells the compiler to pack the structure members tightly, without any padding bytes. This can be useful for reducing the size of the structure, but it can also make the code less efficient due to the lack of alignment.
Q7. What is the purpose of the "static" keyword when used with a function in C?
-
A. To make the function thread-safe
-
B. To prevent the function from being inlined
-
C. To make the function's address unavailable ✓
-
D. To make the function's call faster
Explanation: The "static" keyword when used with a function in C makes the function's address unavailable, meaning it cannot be called from outside the file where it is defined. This is because the function is not exported and its address is not added to the symbol table.
Q8. In C, what is the purpose of the `__attribute__((packed))` attribute when applied to a struct?
-
A. It forces the compiler to use a specific alignment for the struct.
-
B. It allows the compiler to optimize the struct layout for better performance.
-
C. It tells the compiler to pack the struct members tightly without padding. ✓
-
D. It enables the compiler to generate debugging information for the struct.
Explanation: The `__attribute__((packed))` attribute instructs the compiler to pack the struct members tightly without adding padding bytes, which can be useful for binary compatibility or when working with legacy systems.
Q9. Given a C function that uses the `longjmp` function to implement a non-local jump, what is the purpose of the `setjmp` function?
-
A. To set up a non-local jump buffer
-
B. To restore the state of the program after a longjmp
-
C. To save the current state of the program for a later longjmp ✓
-
D. To perform a non-local jump without using setjmp
Explanation: The `setjmp` function is used to save the current state of the program, including the program counter and any registers, so that it can be restored later using `longjmp`. This allows a function to jump out of a nested call stack and return to a previous point in the program.
Q10. In C, what is the purpose of the `*` operator in the following code snippet: `*p = (int *)malloc(sizeof(int));`
-
A. It is used to cast the result of malloc to a pointer to an integer.
-
B. It is used to assign a null value to the pointer p.
-
C. It is used to allocate memory for an integer on the heap.
-
D. It is used to dereference the pointer p and assign the allocated memory address to the memory location it points to. ✓
Explanation: The `*` operator is used to dereference the pointer `p`. This means it accesses the memory location that `p` points to. The `(int *)malloc(sizeof(int))` part allocates memory on the heap for an integer and casts the returned `void*` to an `int*`. The dereferenced pointer `*p` then receives the address of the allocated memory.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free