What is printed by: char s[] = "hello"; printf("%zu", sizeof(s));?
A5
B6
C4
DUndefined
Correct Answer:
B. 6
Explanation:
sizeof(s) includes the null terminator '\0'. "hello" has 5 characters + 1 null terminator = 6 bytes. If s was a pointer, sizeof would give pointer size.
In a struct, how is memory allocated for union members?
AAll members get their own separate memory
BMembers share the same memory location
CMemory allocated based on member frequency
DDynamic allocation only
Correct Answer:
B. Members share the same memory location
Explanation:
In a union, all members share the same memory location. The size of the union equals the size of the largest member. Only one member can hold a value at a time.
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.