B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering C Language 2 — Questions and Answers
Question 1: What is the result of the expression `5 >> 1` in C?
- 10
- 2 (Correct answer)
- 1
- 4
Correct answer: 2
Right-shifting 5 (binary 101) by 1 position yields 2 (binary 010).
Question 2: Which storage class specifier in C limits a variable's visibility to the file in which it is declared?
- extern
- register
- static (Correct answer)
- auto
Correct answer: static
A file-scope variable declared with `static` has internal linkage, making it invisible outside that translation unit.
Question 3: In C, what does the `volatile` qualifier tell the compiler?
- The variable is read-only
- Do not cache the variable in a register; always read from memory (Correct answer)
- The variable is thread-local
- The variable may be optimized away
Correct answer: Do not cache the variable in a register; always read from memory
`volatile` prevents the compiler from optimizing accesses, ensuring every read/write goes to actual memory.
Question 4: What is the output of: `printf("%d", sizeof('A'));` in a typical 32-bit C implementation?
- 1
- 2
- 4 (Correct answer)
- 8
Correct answer: 4
In C, character literals have type `int`, so `sizeof('A')` equals `sizeof(int)`, which is 4 on most 32-bit platforms.
Question 5: Which function is used to release memory allocated by `malloc`?
- delete
- dealloc
- free (Correct answer)
- release
Correct answer: free
`free()` is the standard C library function to deallocate memory previously allocated by `malloc`, `calloc`, or `realloc`.
Question 6: What is a dangling pointer in C?
- A pointer that has never been assigned a value
- A pointer that points to memory that has been freed or gone out of scope (Correct answer)
- A pointer to a null address
- A pointer larger than the address space
Correct answer: A pointer that points to memory that has been freed or gone out of scope
A dangling pointer references memory that is no longer valid, often because `free()` was called or the pointed-to local variable was destroyed.
Question 7: In C, which of the following correctly declares a pointer to a function that takes an `int` and returns `double`?
- double *fp(int);
- double (*fp)(int); (Correct answer)
- double (fp*)(int);
- *double fp(int);
Correct answer: double (*fp)(int);
`double (*fp)(int);` declares `fp` as a pointer to a function with an `int` parameter returning `double`.
What is the result of the expression `5 >> 1` in C?