Programming in C MCQs set 2 for Bahria University Entry Test computer science — 20 solved questions.
Q1. What is the value of `x` after `int x = 5; x = x++ + ++x;`?
Answer: Undefined behavior
Explanation: The expression involves multiple increments of x between sequence points, leading to undefined behavior.
Q2. Which of the following is NOT a valid identifier in C?
Answer: 2var
Explanation: Identifiers cannot start with a digit. 2var is correct because it matches what the question requires. NET computer science MCQs often test this core concept.
Q3. What is the output of `int arr[5] = 1, 2, 3, 4, 5; printf('%d', *(arr + 2));`?
Answer: 3
Explanation: Array indexing is equivalent to pointer arithmetic; *(arr + 2) accesses the third element.
Q4. What does `static` mean for a variable inside a function?
Answer: It retains its value between function calls
Explanation: Static variables inside functions retain their values between calls. NET computer science MCQs often test this core concept.
Q5. What is the purpose of `extern` keyword?
Answer: To declare a global variable
Explanation: Extern is used to declare a global variable or function that is defined elsewhere.
Q6. What is the output of `printf('%d %d', sizeof(int), sizeof(char));`?
Answer: 4 1
Explanation: Typical sizes for int and char are 4 and 1 bytes, respectively.
Q7. What is the effect of `typedef int myInt;`?
Answer: Creates an alias for int
Explanation: Typedef creates an alias for an existing type. NET computer science MCQs often test this core concept.
Q8. What is the output of `printf('%d', 3 & 2);`?
Answer: 1
Explanation: Bitwise AND operation between 3 (11) and 2 (10) results in 2 (10), which is incorrect; correct result is 2.
Q9. What is the purpose of `#include` directive?
Answer: To include a file for preprocessing
Explanation: The #include directive includes a file during the preprocessing stage. NET computer science MCQs often test this core concept.
Q10. What is the output of `int arr[3] = 1, 2; printf('%d', arr[2]);`?
Answer: 0
Explanation: Partially initialized arrays have remaining elements initialized to zero. NET computer science MCQs often test this core concept.
Q11. What is the effect of `const int x = 5; x = 10;`?
Answer: Compilation error
Explanation: Assigning to a const variable results in a compilation error. NET computer science MCQs often test this core concept.
Q12. What is the result of `sizeof(void *)`?
Answer: Size of a pointer
Explanation: Void pointer size is typically the size of a pointer on the system.
Q13. What is the output of `printf('%d', 5 ^ 3);`?
Answer: 6
Explanation: Bitwise XOR operation between 5 (101) and 3 (011) results in 6 (110).
Q14. What is the purpose of `register` keyword?
Answer: To hint the compiler to store a variable in a register
Explanation: The register keyword is a hint to the compiler to store the variable in a register for faster access.
Q15. What is the output of the expression `printf('%d', sizeof('a'))` in C?
Answer: 4
Explanation: In C, the size of a character literal is equal to the size of an int, typically 4 bytes.
Q16. What is the output of `printf('%d %d', 5/2, -5/2)`?
Answer: 2 -3
Explanation: Integer division truncates towards zero, so 5/2 = 2 and -5/2 = -2.
Q17. The `static` keyword in C is used to
Answer: retain the value of a variable between function calls
Explanation: Static variables retain their values between function calls. NET computer science MCQs often test this core concept.
Q18. What is the result of the expression `sizeof(arr) / sizeof(arr[0])` for an array `int arr[5]`?
Answer: 5
Explanation: This expression calculates the number of elements in the array. NET computer science MCQs often test this core concept.
Q19. The function `malloc()` is used to
Answer: allocate memory on the heap
Explanation: malloc() dynamically allocates memory on the heap. allocate memory on the heap is correct because it matches what the question requires. NET computer science MCQs often test this core concept.
Q20. What is the output of `int x = 10; printf('%d', x >> 1)`?
Answer: 5
Explanation: Right shift operation divides the number by 2. NET computer science MCQs often test this core concept.