Sample questions
Question 1Easy
What is the output?
#include <stdio.h>
int main(void) {
int x = 010, y = 0x1F;
printf("%d %d\n", x, y);
return 0;
}
- 10 31
- 10 1F
- 8 31
- 8 15
Answer: C. 8 31
Explanation
A leading 0 makes 010 an octal constant (8 decimal); 0x1F is hexadecimal 1*16 + 15 = 31.
Step-by-step solution
- 010 starts with 0, so it is octal: 1 × 8 + 0 = 8.
- 0x1F is hexadecimal: 1 × 16 + 15 = 31.
- Printf prints 8 then 31, so the output is 8 31, which is option C.
Why the other options are wrong
- A. A leading 0 means octal, so 010 is 8, not 10.
- B. 010 is 8, not 10, and 0x1F is 31 in decimal, not the letters 1F.
- D. 0x1F is 1 × 16 + 15 = 31, not 15.
Tip: A leading 0 means octal and 0x means hexadecimal in C; 0x1F is 31.
Question 2Easy
What is the output?
#include <stdio.h>
int main(void) {
int a = (int)3.99;
int b = (int)-3.99;
printf("%d %d\n", a, b);
return 0;
}
- 3 -4
- 3 -3
- 4 -3
- 4 -4
Answer: B. 3 -3
Explanation
Converting a floating value to an integer type discards the fractional part (truncation toward zero), so 3.99 -> 3 and -3.99 -> -3.
Step-by-step solution
- (int)3.99 drops the fraction, giving 3.
- (int)-3.99 truncates toward zero, giving -3, not -4.
- Printf prints 3 then -3, so the output is 3 -3, which is option B.
Why the other options are wrong
- A. -3.99 becomes -3 when converted to int, not -4, because conversion truncates.
- C. 3.99 becomes 3, not 4; the fraction is dropped, not rounded.
- D. 3.99 becomes 3 and -3.99 becomes -3; both are wrong here.
Tip: Converting a float to an integer type truncates toward zero; it never rounds.
Question 3Easy
In C, what is the value of sizeof(char)?
- Always 1, by definition
- 1 or 2 depending on the compiler
- Equal to sizeof(int)
- Always 2
Answer: A. Always 1, by definition
Explanation
The standard defines sizeof(char) as 1; a byte in C is whatever size a char has (CHAR_BIT bits, at least 8).
Step-by-step solution
- The C standard defines sizeof(char) as 1.
- So sizeof(char) is always 1, whatever the compiler.
- This matches option A: Always 1, by definition.
Why the other options are wrong
- B. sizeof(char) is fixed at 1 by the standard; it does not vary between compilers.
- C. sizeof(char) is 1, while sizeof(int) is larger than 1 on common systems, so they are not equal.
- D. sizeof(char) is 1, not 2.
Tip: In C, sizeof(char) is always 1; a byte is CHAR_BIT bits, which is at least 8.
Question 4Easy
Assume unsigned int is 32 bits. What is the output?
#include <stdio.h>
int main(void) {
unsigned int x = 1u << 31;
printf("%u\n", x);
return 0;
}
- 4294967296
- -2147483648
- 1073741824
- 2147483648
Answer: D. 2147483648
Explanation
Shifting 1 left by 31 sets the top bit: 2^31 = 2147483648,
which fits in a 32-bit unsigned int.
Step-by-step solution
- 1 << 31 equals 2147483648.
- Unsigned int can hold 2^32 values from 0 to 4294967295.
- 2147483648 is within the range of a 32-bit unsigned int.
- Printf with %u prints the decimal value of x as 2147483648.
- Final answer: 2147483648.
Why the other options are wrong
- A. 4294967296 is 2^32, too big for unsigned int; the value here is 2^31.
- B. -2147483648 is negative; unsigned int cannot hold negative values.
- C. 1073741824 is 2^30, half of the value that 1u << 31 gives.
Tip: Remember that shifting 1 left by n gives 2^n, and it fits in an unsigned int if within 32 bits.
Question 5Medium
What is the output?
#include <stdio.h>
int main(void) {
float f = 0.1f;
double d = 0.1;
if (f == d)
printf("equal\n");
else
printf("not equal\n");
return 0;
}
- Depends on the value of CHAR_BIT
- not equal
- Compilation error
- equal
Answer: B. not equal
Explanation
0.1 cannot be represented exactly; the float approximation 0.1f differs from the more precise double d, and f is converted to double for the comparison, so they are unequal.
Step-by-step solution
- 0.1f is rounded to float precision.
- 0.1 as double is more precise.
- Comparing them gives not equal.
Why the other options are wrong
- A. CHAR_BIT is irrelevant.
- C. It compiles.
- D. Rounded values differ.
Tip: Float vs double differ.
Question 6Medium
Assume double is 8 bytes and int is 4 bytes. What is the output?
#include <stdio.h>
int main(void) {
printf("%zu\n", sizeof(1 + 2.5));
return 0;
}
- 12
- 4
- 8
- 3
Answer: C. 8
Explanation
In 1 + 2.5 the int is converted to double (usual arithmetic conversions), so the expression has type double and sizeof gives 8.
Step-by-step solution
- Expression 1 + 2.5 uses usual arithmetic conversions, so 1 becomes double.
- Result of 1 + 2.5 is a double, so sizeof(double) is calculated.
- sizeof(double) is 8 bytes as per the question's assumption.
- Printf with %zu prints the size in bytes correctly.
- Final output is 8.
Why the other options are wrong
- A. 12 is wrong; the expression has type double, which is 8 bytes, not 12.
- B. 4 is the size of int, but 1 + 2.5 is a double, so its size is 8.
- D. 3 is not a size; sizeof gives the byte size of the double result.
Tip: Remember: when mixing int and double, int converts to double for calculation.
Question 7Medium
What is the output?
#include <stdio.h>
int main(void) {
int i = -1;
unsigned int u = 1;
if (i < u)
printf("less\n");
else
printf("not less\n");
return 0;
}
- not less
- Compilation error
- Nothing is printed
- less
Answer: A. not less
Explanation
In i < u the int is converted to unsigned int, so -1 becomes UINT_MAX, which is greater than 1. The comparison is false.
Step-by-step solution
- I is -1, u is 1.
- -1 is converted to unsigned int, becoming UINT_MAX (which is large).
- UINT_MAX is greater than 1, so i < u is false.
- The else block runs, printing 'not less'.
- Final answer: not less.
Why the other options are wrong
- B. The code compiles fine; the comparison is valid C, so there is no compilation error.
- C. Something is printed, since one branch always runs, so nothing is printed is wrong.
- D. Less is wrong because -1 becomes a huge unsigned value, so i < u is false.
Tip: Compare signed and unsigned types carefully; negative numbers become large positives.
Question 8Medium
Assume unsigned int is 32 bits. What is the output?
#include <limits.h>
#include <stdio.h>
int main(void) {
printf("%u\n", UINT_MAX + 1u);
return 0;
}
- 4294967296
- 0
- Undefined behaviour
- -1
Answer: B. 0
Explanation
Unsigned arithmetic is performed modulo 2^32, so UINT_MAX + 1 wraps to 0.
Unlike signed overflow, this is fully defined.
Step-by-step solution
- With 32-bit unsigned int, UINT_MAX is 2^32 − 1.
- UINT_MAX + 1u is unsigned arithmetic, so it wraps around modulo 2^32.
- (2^32 − 1) + 1 = 2^32, which wraps to 0.
- Printf with %u prints 0, which is option B.
Why the other options are wrong
- A. The value wraps around to 0; it does not reach 4294967296 because that is outside unsigned int.
- C. Unsigned overflow is defined in C, so it is not undefined behaviour.
- D. An unsigned value cannot be negative; the result is 0.
Tip: Unsigned integer arithmetic wraps modulo 2^n; signed integer overflow is undefined.
Question 9Hard
Assume char is 8 bits and int is 32 bits. What is the output?
#include <stdio.h>
int main(void) {
unsigned char a = 200, b = 100;
int s = a + b;
unsigned char r = a + b;
printf("%d %d\n", s, r);
return 0;
}
- 44 300
- 44 44
- 300 44
- 300 300
Answer: C. 300 44
Explanation
Both operands are promoted to int before adding, so a + b is 300. Storing 300 in an unsigned char reduces it modulo 256, giving 44.
Step-by-step solution
- A and b are promoted to int, so a + b = 200 + 100 = 300.
- S is int, so s = 300.
- R is unsigned char, so 300 mod 256 = 44.
- Printf prints s and r as %d, so output is 300 44.
Why the other options are wrong
- A. Int addition gives 300 for s, so 44 is wrong for the first value.
- B. The first value is 300, not 44, because the sum is computed as an int.
- D. r is unsigned char, so it holds 44 not 300; the second value is 44.
Tip: Remember that unsigned char wraps around when storing large values.
Question 10Hard
Assume float uses IEEE-754 single precision (24-bit significand). What is the output?
#include <stdio.h>
int main(void) {
float f = 16777216.0f;
f = f + 1.0f;
printf("%.1f\n", f);
return 0;
}
- 16777217.0
- 16777218.0
- 0.0
- 16777216.0
Answer: D. 16777216.0
Explanation
2^24 = 16777216 is the point where float can no longer represent every integer; 16777217 rounds back to 16777216 (round to nearest even).
Step-by-step solution
- F starts at 16777216, which is 2^24.
- Above 2^24, a single-precision float cannot store every integer; between 2^24 and 2^25 it steps by 2.
- 16777216 + 1 = 16777217 is not representable; it rounds to the even neighbour 16777216.
- F stays 16777216, and %.1f prints 16777216.0, which is option D.
Why the other options are wrong
- A. 16777217 cannot be stored exactly as a float here, so the value stays 16777216.
- B. 16777218 is not the rounded value; the sum rounds down to the even neighbour 16777216.
- C. Adding 1 does not make the value 0; the value stays near 16777216.
Tip: A single-precision float holds every integer exactly only up to 2^24 = 16777216.
Practise all 29 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →