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 = 10; int y = 5; int z = x / y + x % y; printf("%d", z); return 0; }

Explanation: Integer division gives x / y = 10 / 5 = 2 and the modulo gives x % y = 10 % 5 = 0, so z = 2 + 0 = 2. The program prints 2.

Q2. 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.

Q3. What is the purpose of the !!__attribute__((__packed__))!! directive in C?

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.

Q4. What is the purpose of the 'volatile' keyword in C when used with a global variable?

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.

Q5. What is the effect of the "static" keyword when applied to a function in C?

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.

Q6. What is the purpose of the `scanf` format specifier `%p` in C?

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.

Q7. What is the purpose of the "__attribute__((packed))" directive in C?

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.

Q8. What is the purpose of the "static" keyword when used with a function in C?

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.

Q9. In C, what is the purpose of the `__attribute__((packed))` attribute when applied to a 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.

Q10. Given a C function that uses the `longjmp` function to implement a non-local jump, what is the purpose of the `setjmp` function?

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.

That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.

Sign Up Free
C MCQ Practice Test | Mind Hustle