B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering C Language 5 — Questions and Answers
Question 1: What is the correct way to open a file for both reading and writing in binary mode in C?
- fopen("file", "rw")
- fopen("file", "r+b") (Correct answer)
- fopen("file", "rb")
- fopen("file", "w+")
Correct answer: fopen("file", "r+b")
The mode `"r+b"` opens an existing binary file for both reading and writing without truncating it.
Question 2: Which of the following C expressions evaluates the absolute value of an integer without using the standard library?
- x > 0 ? x : x
- x < 0 ? -x : x (Correct answer)
- x & 0x7FFFFFFF
- x ^ (x >> 31)
Correct answer: x < 0 ? -x : x
The ternary expression `x < 0 ? -x : x` returns `-x` when `x` is negative and `x` otherwise, correctly computing absolute value.
Question 3: In C, what is the effect of declaring a local variable inside a block `{ }` nested within a function?
- It has function scope and persists for the function's lifetime
- It has block scope and is destroyed when execution leaves the block (Correct answer)
- It becomes a global variable
- It is allocated on the heap automatically
Correct answer: It has block scope and is destroyed when execution leaves the block
Local variables declared inside a nested block have block scope; they are allocated on the stack when the block is entered and destroyed when it is exited.
Question 4: What does the `realloc` function do in C?
- Allocates a new block and frees the old one automatically while preserving existing data up to the smaller size (Correct answer)
- Always allocates a larger block than the original
- Frees the pointer and returns NULL
- Duplicates the memory block
Correct answer: Allocates a new block and frees the old one automatically while preserving existing data up to the smaller size
`realloc` resizes a previously allocated block, copies data up to the minimum of old and new sizes, and may return a new pointer if the block was moved.
Question 5: What is the role of the linker in C program compilation?
- Converts source code to assembly
- Translates assembly to machine code
- Resolves external references and combines object files into an executable (Correct answer)
- Performs macro substitution
Correct answer: Resolves external references and combines object files into an executable
The linker combines compiled object files, resolves symbol references (like function calls across files), and produces the final executable or library.
Question 6: In C, which standard library function searches for the first occurrence of a character in a string?
- strstr
- strchr (Correct answer)
- strsearch
- memchr
Correct answer: strchr
`strchr(s, c)` returns a pointer to the first occurrence of character `c` in the null-terminated string `s`, or NULL if not found.
Question 7: What is the purpose of the `extern` keyword in C?
- Allocates a variable in external (heap) memory
- Declares a variable or function defined in another translation unit, enabling cross-file access (Correct answer)
- Makes a variable constant across files
- Forces inline expansion of a function
Correct answer: Declares a variable or function defined in another translation unit, enabling cross-file access
`extern` tells the compiler that the variable or function is defined elsewhere, allowing multiple files to share a single definition without redefinition errors.
What is the correct way to open a file for both reading and writing in binary mode in C?