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.
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
BDeclares ptr and initializes it to NULL (no memory address)
CCauses a compilation error
DAllocates memory for ptr
Correct Answer:
B. Declares ptr and initializes it to NULL (no memory address)
Explanation:
This declaration creates a pointer to an integer and initializes it to NULL, which means it doesn't point to any valid memory address. This is a safe initialization practice.
Since both 5 and 2 are integers, integer division is performed: 5/2 = 2. The %f format specifier then prints this integer value (2) as a floating-point number: 2.000000
What does the modulus operator (%) return when applied to negative numbers?
AAlways positive result
BThe remainder with the sign of dividend
CThe remainder with the sign of divisor
DZero
Correct Answer:
B. The remainder with the sign of dividend
Explanation:
In C, the modulus operator (%) returns a remainder that has the same sign as the dividend. For example, -7 % 3 = -1 (not 2), because -7 is the dividend and it's negative.
Which of the following statements about pointers is correct?
AA pointer stores the value of a variable
BA pointer stores the memory address of a variable
CA pointer can only point to integers
DPointers cannot be modified after declaration
Correct Answer:
B. A pointer stores the memory address of a variable
Explanation:
A pointer is a variable that stores the memory address of another variable. You can access the value at that address using the dereference operator (*), and pointers can point to any data type.
What is the correct way to declare a constant in C?
Aconstant int x = 10;
Bconst int x = 10;
Cint const x = 10;
DBoth B and C
Correct Answer:
D. Both B and C
Explanation:
Both 'const int x = 10;' and 'int const x = 10;' are valid ways to declare a constant in C. The const keyword can be placed before or after the type specifier, and both have the same meaning.
How many times will the following loop execute: for(int i=0; i
A2 times
B3 times
C4 times
D5 times
Correct Answer:
B. 3 times
Explanation:
The loop executes for i=0, i=1, and i=2. When i=3, the break statement is encountered, which immediately terminates the loop. Therefore, the loop executes exactly 3 times.