Showing 1–10 of 18 questions
in Data Types & Variables
What happens when a volatile variable is declared in a multi-threaded C program?
A
It becomes thread-safe automatically
B
Compiler won't optimize it away and will fetch value from memory each time
C
It can only be accessed by one thread
D
It doubles in size for redundancy
Correct Answer:
B. Compiler won't optimize it away and will fetch value from memory each time
EXPLANATION
volatile tells compiler not to optimize variable accesses and to fetch fresh value from memory, useful for hardware registers and shared memory, but doesn't guarantee thread-safety without synchronization.
What will be the result of the following expression:
float x = 0.1 + 0.2;
if(x == 0.3) printf("Equal"); else printf("Not Equal");
A
Equal
B
Not Equal
C
Undefined behavior
D
Compilation error
Correct Answer:
B. Not Equal
EXPLANATION
Due to floating-point precision limitations, 0.1 + 0.2 does not exactly equal 0.3 in binary representation. Direct comparison with == fails.
What happens in this code: int arr[5]; int *p = &arr[0]; printf("%d", sizeof(p));
A
20 (5 integers)
B
4 or 8 (pointer size)
C
5
D
Compilation error
Correct Answer:
B. 4 or 8 (pointer size)
EXPLANATION
p is a pointer variable, so sizeof(p) returns the size of the pointer itself (4 bytes on 32-bit, 8 bytes on 64-bit systems), not the array.
What is the primary purpose of the 'restrict' keyword introduced in C99?
A
Restrict variable scope
B
Allow compiler optimizations by promising pointer exclusivity
C
Make variables read-only
D
Reduce memory allocation
Correct Answer:
B. Allow compiler optimizations by promising pointer exclusivity
EXPLANATION
The 'restrict' qualifier tells the compiler that a pointer is the only way to access that object, enabling optimizations.
Which of the following statements about 'register' keyword is TRUE?
A
It guarantees variable storage in CPU register
B
It's a hint to compiler for optimization
C
It prevents taking address of variable
D
Both B and C
Correct Answer:
D. Both B and C
EXPLANATION
Register is a hint only; compiler may ignore it. You cannot use & operator on register variables.
Which of the following correctly declares a constant pointer to a constant integer?
A
const int * const p;
B
int const * const p;
C
const int const * p;
D
Both A and B
Correct Answer:
D. Both A and B
EXPLANATION
Both 'const int * const p' and 'int const * const p' are equivalent and declare constant pointer to constant integer.
What is printed by: int x = 5; printf("%d %d", x++, ++x);?
A
5 7
B
6 7
C
Undefined behavior
D
7 7
Correct Answer:
C. Undefined behavior
EXPLANATION
Modifying variable x twice without intervening sequence point causes undefined behavior.
What is the difference between 'const int *p' and 'int * const p'?
A
No difference
B
First allows changing pointer, second allows changing value
C
First prevents changing value, second prevents changing pointer
D
Both prevent all modifications
Correct Answer:
C. First prevents changing value, second prevents changing pointer
EXPLANATION
'const int *p' - pointer can change but value cannot. 'int * const p' - value can change but pointer cannot.
What is the purpose of the 'restrict' keyword in modern C?
A
To prevent a variable from being modified
B
To tell the compiler that a pointer has exclusive access to data
C
To limit variable scope
D
To make variable read-only
Correct Answer:
B. To tell the compiler that a pointer has exclusive access to data
EXPLANATION
restrict (C99) informs the compiler that a pointer is the sole accessor to the object, enabling optimization. It doesn't prevent modification.
In the declaration 'int *p, q;', what are the data types?
A
p is int, q is pointer to int
B
p is pointer to int, q is int
C
Both are pointers to int
D
Both are integers
Correct Answer:
B. p is pointer to int, q is int
EXPLANATION
The '*' applies only to the immediately following variable. So 'int *p' declares p as pointer to int, and 'q' is just an int.