What happens when you use the strcpy() function without bounds checking?
AIt prevents buffer overflow automatically
BIt may cause buffer overflow if source string is longer than destination
CIt returns an error code
DIt truncates the source string automatically
Correct Answer:
B. It may cause buffer overflow if source string is longer than destination
Explanation:
strcpy() does not perform bounds checking. If the source string is longer than the destination buffer, it will write beyond the buffer boundary, causing a buffer overflow. This is a security vulnerability. Using strncpy() is safer.
What is the output of the following C code?
int arr[] = {10, 20, 30};
int *ptr = arr;
printf("%d", *(ptr + 1));
A10
B20
C30
DAddress of second element
Correct Answer:
B. 20
Explanation:
ptr points to arr[0]. ptr + 1 points to arr[1]. *(ptr + 1) dereferences to get the value at arr[1] which is 20. Pointer arithmetic adds sizeof(int) to the address for each increment.
Which of the following correctly describes the scope of a static variable declared inside a function?
ALocal to the function, retains value between function calls
BGlobal scope, initialized once
CLocal to the file only
DCreates a new instance on each function call
Correct Answer:
A. Local to the function, retains value between function calls
Explanation:
A static variable declared inside a function has local scope (visible only within that function) but persists for the entire program lifetime. Its value is retained between function calls and is initialized only once.
What is the output of the following C code?
#include
int main() {
int a = 5;
printf("%d %d %d", a++, ++a, a);
return 0;
}
A5 7 7
B6 7 7
C5 6 6
DUndefined behavior
Correct Answer:
D. Undefined behavior
Explanation:
This code contains undefined behavior because variable 'a' is modified multiple times (a++, ++a) without intervening sequence points in the same expression. The order of evaluation is unspecified, making the result compiler-dependent.
What is the correct way to declare a constant pointer to a constant integer?
Aconst int * const ptr;
Bconst int const *ptr;
Cint const * const ptr;
DBoth A and C
Correct Answer:
D. Both A and C
Explanation:
Both declarations are equivalent. 'const int * const ptr' and 'int const * const ptr' declare a constant pointer to a constant integer. The first const makes the integer constant, the second const makes the pointer constant.
Consider the following C code. What will be printed?
int arr[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int *ptr = (int *)arr;
printf("%d", *(ptr + 5));
A5
B6
C8
D9
Correct Answer:
B. 6
Explanation:
A 2D array is stored in row-major order in memory: 1,2,3,4,5,6,7,8,9. When ptr is cast to int*, ptr+5 points to the 6th element (0-indexed), which is 6.
What is the difference between struct and union in C?
Astruct members share memory, union members have separate memory
Bunion members share memory, struct members have separate memory
CNo difference in memory allocation
Dstruct is faster than union
Correct Answer:
B. union members share memory, struct members have separate memory
Explanation:
In a struct, each member has its own memory allocation, so the total size is the sum of all members. In a union, all members share the same memory location, so the size equals the largest member. Only one member can hold a value at a time in a union.