In C, which function is used to read a string from input with space handling?
Ascanf("%s", str);
Bfgets(str, sizeof(str), stdin);
Cgets(str);
Dgetline(str);
Correct Answer:
B. fgets(str, sizeof(str), stdin);
Explanation:
fgets() is the safe way to read strings including spaces. scanf("%s") stops at whitespace, gets() is deprecated and unsafe due to buffer overflow risks.
What is the purpose of the 'static' keyword when used with a variable inside a function?
ATo make the variable global
BTo preserve the variable's value between function calls
CTo prevent the variable from being modified
DTo allocate memory on the stack
Correct Answer:
B. To preserve the variable's value between function calls
Explanation:
A static variable inside a function is initialized only once and retains its value between successive function calls. It has memory allocated in the data segment, not the stack.
Which of the following correctly represents a structure declaration in C?
Astruct {int a; char b;} s;
Bstruct Name {int a; char b;};
CBoth A and B are correct
DNeither A nor B is correct
Correct Answer:
C. Both A and B are correct
Explanation:
Both are valid syntax. Option A declares an anonymous structure with immediate variable instantiation. Option B declares a named structure type that can be used later to create variables.
AThe variable can be modified externally at any time
BThe variable cannot be modified
CThe variable is stored in a register
DThe variable has global scope
Correct Answer:
A. The variable can be modified externally at any time
Explanation:
The volatile keyword tells the compiler that a variable's value may change unexpectedly (e.g., due to hardware, interrupts, or other threads), preventing compiler optimizations that assume the value doesn't change.
What will be the final value of x: int x = 5; x += 3 * 2; printf("%d", x);
A16
B11
C13
D36
Correct Answer:
A. 16
Explanation:
Step 1:x = 5. Step 2: 3 * 2 = 6 (multiplication has higher precedence). Step 3: x += 6 means x = x + 6 = 5 + 6 = 11. Wait, let me recalculate: x = 5; x += 6 gives x = 11. The answer should be B (11), not A. Correction: The correct answer is B.
Which of the following operators has the highest precedence in C?
A* (multiplication)
B++ (increment)
C+ (addition)
D= (assignment)
Correct Answer:
B. ++ (increment)
Explanation:
The increment (++) and decrement (--) operators have the highest precedence among all operators in C, followed by unary operators, then multiplicative, additive, and so on.
Which of the following is true about the 'break' statement in C?
AIt skips the current iteration
BIt exits the loop or switch statement
CIt terminates the program
DIt has no effect in loops
Correct Answer:
B. It exits the loop or switch statement
Explanation:
The 'break' statement is used to exit or terminate a loop or switch statement immediately. It transfers control to the statement following the loop or switch.
What will be printed: int i = 0; while(i < 3) { printf("%d ", i); i++; }
A0 1 2
B1 2 3
C0 1 2 3
DNothing is printed
Correct Answer:
A. 0 1 2
Explanation:
The while loop starts with i=0. It prints 0, then increments i to 1, prints 1, increments to 2, prints 2, increments to 3, and the condition i<3 becomes false, so the loop terminates. Output: 0 1 2