Consider a structure: struct Point { int x; int y; }; If p is a pointer to this structure and we want to access member x, which notation is INCORRECT?
Ap->x
B(*p).x
Cp.x
DAll are correct when used appropriately
Correct Answer:
C. p.x
EXPLANATION
When p is a pointer to a structure, we cannot use p.x (dot notation) directly. The correct ways are: p->x (arrow operator) or (*p).x (dereference then dot operator). The notation p.x would be used only if p were a structure variable, not a pointer. Therefore, p.x is incorrect when p is a pointer.
If arr[5] = {1, 2, 3, 4, 5}, what will be the value of *(arr + 3)?
A3
B4
C5
D2
Correct Answer:
B. 4
EXPLANATION
In array notation, arr is a pointer to the first element. *(arr + 3) dereferences the pointer that points to the 4th element (index 3). Since array indexing is 0-based: arr[0]=1, arr[1]=2, arr[2]=3, arr[3]=4. Therefore, *(arr + 3) = 4.
What is the primary difference between malloc() and calloc() functions in C?
Amalloc() allocates memory while calloc() deallocates it
Bmalloc() doesn't initialize memory, while calloc() initializes it to zero
Ccalloc() can only allocate fixed-size blocks
Dmalloc() requires two arguments while calloc() requires one
Correct Answer:
B. malloc() doesn't initialize memory, while calloc() initializes it to zero
EXPLANATION
Both malloc() and calloc() allocate dynamic memory. The key difference is that malloc() allocates uninitialized memory (contains garbage values), while calloc() allocates memory and initializes all bytes to zero. calloc() also takes two arguments (number of elements and size of each element), while malloc() takes one (total bytes needed).
Which loop construct will execute at least once even if the condition is false?
Awhile loop
Bfor loop
Cdo-while loop
DAll of the above
Correct Answer:
C. do-while loop
EXPLANATION
The do-while loop executes the body first and then checks the condition, ensuring at least one execution. while and for loops check the condition first.