B CompE Bachelor of Computer Engineering Bachelor of Computer Engineering C Language 4 — Questions and Answers
Question 1: In C, what is undefined behavior?
- Code that causes a compile-time error
- Code whose behavior is not specified by the C standard and may produce any result (Correct answer)
- Code that always crashes at runtime
- Code that triggers a hardware exception
Correct answer: Code whose behavior is not specified by the C standard and may produce any result
Undefined behavior (UB) means the C standard imposes no requirements on what the program does, so compilers may produce any output, including seemingly correct results.
Question 2: Which of the following is a correct way to swap two integers in C without a temporary variable using XOR?
- a = a ^ b; b = a ^ b; a = a ^ b; (Correct answer)
- a ^= b; a ^= b; b ^= a;
- b = a ^ b; a = a ^ b;
- a = b ^ a; b = b ^ a;
Correct answer: a = a ^ b; b = a ^ b; a = a ^ b;
The sequence `a^=b; b^=a; a^=b;` (written out fully) correctly swaps `a` and `b` using XOR without a temporary variable.
Question 3: What is the difference between `struct` and `union` in C?
- A `struct` stores members sequentially with individual storage; a `union` shares the same memory for all members (Correct answer)
- A `union` is a named `struct`
- `struct` allows only numeric types; `union` allows any type
- `union` members cannot be pointers
Correct answer: A `struct` stores members sequentially with individual storage; a `union` shares the same memory for all members
In a `struct`, each member has its own storage; in a `union`, all members share the same memory location, sized to the largest member.
Question 4: What is the output of: `int x = 10; printf("%d", x++);`?
- 11
- 10 (Correct answer)
- 9
- Undefined
Correct answer: 10
The post-increment operator `x++` evaluates to the current value of `x` (10) before incrementing, so `printf` prints 10.
Question 5: In C, which header file must be included to use `assert()`?
- <stdlib.h>
- <assert.h> (Correct answer)
- <debug.h>
- <error.h>
Correct answer: <assert.h>
The `assert()` macro is declared in `<assert.h>` and aborts the program with a diagnostic message if its argument evaluates to false.
Question 6: What is a memory leak in C?
- Accessing memory outside allocated bounds
- Allocating memory that is never freed, causing gradual resource exhaustion (Correct answer)
- Freeing memory that was never allocated
- Reading uninitialized memory values
Correct answer: Allocating memory that is never freed, causing gradual resource exhaustion
A memory leak occurs when dynamically allocated memory is never released with `free()`, causing the program's memory consumption to grow unboundedly.
Question 7: In C, what does the `const` keyword do when applied to a pointer declaration such as `const int *p`?
- Makes `p` itself unmodifiable
- Makes the integer value pointed to by `p` unmodifiable through `p` (Correct answer)
- Makes both the pointer and the value unmodifiable
- Has no effect on pointer declarations
Correct answer: Makes the integer value pointed to by `p` unmodifiable through `p`
`const int *p` means `p` points to a constant integer — you cannot modify the value via `p`, but you can reassign `p` to point elsewhere.
In C, what is undefined behavior?