Sample questions
Question 1Medium
What will be the value of d in the following program?
#include <stdio.h>
int main()
{
int a = 10, b = 5, c = 5;
int d;
d = b + c == a;
printf("%d", d);
}
- Syntax error
- 1
- 5
- 10
Answer: B. 1
Explanation
In the Statement
d = b + c == a;
here (==) is the relational Operator. this Operator returns boolean value.
So this use in if loop is very important to perform the result Because if is a decision making loop.
Step-by-step solution
a = 10, b = 5, c = 5.B + c = 5 + 5 = 10.d = b + c == a = 10 == 10 = 1.
Why the other options are wrong
- A. No syntax errors in the code.
- C. B + c equals 10, not 5.
- D. D is assigned a boolean value, not 10.
Tip: Remember that '==' checks for equality, not assignment.
Question 2Medium
Are logical operators sequence points?
- True
- False
- Depends on the compiler
- Depends on the standard
Answer: A. True
Explanation
To stop the Unnecessary Execution, Logical Operator is Sequence Point.
Step-by-step solution
- In C, the logical operators
&& and || are sequence points, along with the comma operator and ?:. - A sequence point means everything on the left is finished before the next part is evaluated.
- This rule is set by the C standard, so it is the same on all compilers.
- So the answer is True.
Why the other options are wrong
- B. Logical operators are sequence points, so False is wrong.
- C. The rule is defined by the standard, so it does not depend on the compiler.
- D. The standard defines this rule, so it does not depend on which standard is used.
Tip: Sequence points include && and || in C.
Question 3Medium
What is the output of this C code?
#include <stdio.h>
void main()
{
int a = 3;
int b = ++a + a++ + --a;
printf("Value of b is %d", b);
}
- Value of x is 12
- Value of x is 13
- Value of x is 10
- Undefined behavior
Answer: D. Undefined behavior
Explanation
a is modified three times in one expression (++a, a++, --a) with no sequence point, so the behaviour is undefined in C. The printed text is also 'Value of b', not 'Value of x'.
Step-by-step solution
- The expression
++a + a++ + --a changes a three times. - There is no sequence point between these modifications.
- This is undefined behaviour, so no fixed output is guaranteed.
Why the other options are wrong
- A. Output says 'Value of b', and 12 is not guaranteed.
- B. Output says 'Value of b', and 13 is not guaranteed.
- C. Output says 'Value of b', and 10 is not guaranteed.
Tip: Multiple unsequenced changes to one variable in a single expression mean undefined behaviour.
Question 4Medium
Which of the following is not an arithmetic operation?
a *= 10;a /= 10;a != 10;a %= 10;
Answer: C. a != 10;
Explanation
The '!=' operator is not an arithmetic operation; it's a relational operator used to check if two values are unequal. The other options (a *= 10, a /= 10, and a %= 10) are all arithmetic operations that perform mathematical calculations on variables.
Step-by-step solution
- The arithmetic operations are addition, subtraction, multiplication, division and remainder.
- A *= 10, a /= 10 and a %= 10 are compound assignments for multiplication, division and remainder.
- A != 10 is a relational operator: it tests whether a is not equal to 10 and does not change a.
- So the answer is a != 10;
Why the other options are wrong
- A. A *= 10 multiplies a by 10, which is arithmetic.
- B. A /= 10 divides a by 10, which is arithmetic.
- D. A %= 10 takes the remainder after division by 10, which is arithmetic.
Tip: Compound assignments such as *= and %= are arithmetic; != and == are comparisons.
Question 5Medium
What is the output of this C code?
#include <stdio.h>
void main()
{
int x = 4, y, z;
y = --x;
z = x--;
printf("%d%d%d", x, y, z);
}
- 3 2 3
- 2 3 3
- 3 2 2
- 2 3 4
Answer: B. 2 3 3
Explanation
The difference between pre and post increment expressions is just like it sounds.
Pre-incrementation means the variable is incremented before the expression is set or evaluated. Post-incrementation means the expression is set or evaluated, and thenthe variable is altered.
Step-by-step solution
- Start with
x = 4. y = --x decrements x first, so x becomes 3 and y becomes 3.z = x-- gives z the current value 3, then decrements x, so x becomes 2.Printf prints x, y, z in order with no spaces, which gives 2 3 3.- The output is 2 3 3.
Why the other options are wrong
- A. This needs x to stay 3, but x-- lowers it to 2.
- C. This needs z to be 2, but z takes the value 3 before x is decremented.
- D. This needs z to be 4, but z takes the value 3.
Tip: Pre-decrement changes the variable before use; post-decrement uses the old value first.
Question 6Medium
What is the result of following code?
main()
{
int a=14,b=8;
printf("%o\t",a<<2);
printf("%x\t",b<<3);
printf("%d\t",a&b);
printf("%d",a^b);
getch();
}
- 42 62 8 4
- 70 40 8 6
- 48 40 8 4
- 62 48 8 5
Answer: B. 70 40 8 6
Explanation
printf("%o",a<<2);
The value of a is 14.
in this statement %o is used to print the octal value of a no.
a is 14 and a<<2 is left shifted by 2. So value becomes a 54 in decimal and Octal no of 54 is 70.
same in variable b so decimal no is 64 and the Hexadecimal no of 64 is 40.
In Next statement a&b is shows that the bitwise AND is used in a and b.a is 1110 and b is 1000 so result is 1000 so equivalent decimal no is 8.
At last BITWISE OR is used so result is 0110 Equivalent decimal no is 6.
Step-by-step solution
- a = 14 is 1110 in binary and b = 8 is 1000 in binary.
- A<<2 = 14×4 = 56, which is 70 in octal (7×8 + 0).
- B<<3 = 8×8 = 64, which is 40 in hex (4×16 + 0).
- A&b = 1110 AND 1000 = 1000 = 8, and a^b = 1110 XOR 1000 = 0110 = 6.
- So the output is: 70 40 8 6.
Why the other options are wrong
- A. 42 is not the octal form of 56, which is 70; a^b is 6, not 4.
- C. A<<2 is 56, which prints as 70 in octal, not 48; a^b is 6, not 4.
- D. A<<2 prints as 70 in octal, not 62; b<<3 prints as 40 in hex, not 48; a^b is 6, not 5.
Tip: Shift left by n multiplies by 2 to the power n; %o prints octal and %x prints hex.
Question 7Medium
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 0;
int x = i++, y = ++i;
printf("%d % d", x, y);
return 0;
}
- 0, 2
- 0, 1
- 1, 2
- Undefined
Answer: A. 0, 2
Explanation
++ is a increment Operator. But there is two way to use that. Pre-increment and Post increment.
x store postincreated of i. that is 1.and y store preincremented of i. so it store 2.
Step-by-step solution
x = i++ gives x the value 0, and then i becomes 1.y = ++i first makes i 2, so y gets the value 2.- So
x is 0 and y is 2, and the printf prints both values. - So the answer is 0, 2.
Why the other options are wrong
- B. That would need y to be 1, but ++i makes i 2 before it is used.
- C. That would need i to start at 1, but it starts at 0.
- D. Each declarator ends with a sequence point, so the code is not undefined.
Tip: Work through i++ and ++i one step at a time, in the order they appear.
Question 8Medium
Result of a logical or relational expression in C is
- True or False
- 0 or 1
- 0 if expression is false and any positive number i
- None of the mentioned
Answer: B. 0 or 1
Explanation
In C, a relational or logical expression gives 1 when it is true and 0 when it is false.
Step-by-step solution
- In C, relational (==, <, >) and logical (&&, ||, !) expressions produce an int result.
- The result is 1 when the expression is true.
- The result is 0 when the expression is false.
- So the result is 0 or 1.
Why the other options are wrong
- A. C has no True or False result; the value is the int 0 or 1.
- C. A true expression gives 1, not any positive number.
- D. Option B is correct, so None of the mentioned is wrong.
Tip: In C, true is 1 and false is 0 for every comparison.
Question 9Medium
What is the output of this C code?
#include <stdio.h>
int main()
{
int i = 10;
int *p = &i;
printf("%d", *p++);
}
- 10
- 11
- Garbage value
- Address of i
Answer: A. 10
Explanation
p=&i;
This statement is use to store the address of i into the p.so *p contain value of i that is 10;
So output is 10.++ is post increment operator so output is 10.
Step-by-step solution
- P points to i, so *p is the value of i, which is 10.
- P++ is a postfix increment, which uses the old value of p and then moves p.
- So *p++ reads the value at the old pointer position, which is i.
- Printf prints 10.
- The answer is 10.
Why the other options are wrong
- B. 11 would need ++*p or (*p)++; here the pointer moves, not the value.
- C. The pointer points to a valid variable, so there is no garbage value.
- D. %d prints the value, not the address; the address would need %p.
Tip: In *p++, the ++ moves the pointer after the old value is read.
Question 10Medium
What is the output of this C code?
#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf("%d%d%d%d", a,b,c,d);
}
- 6 -6 0 0
- 6 -5 0 1
- -6 -6 0 1
- 6 -6 0 1
Answer: D. 6 -6 0 1
Explanation
d= ++ a && ++b || ++c.
This statement contains two sort circuit operator && and ||.Compiler optimize this expression like ( ++5&& ++ -7 )||++0, after optimization of this code output is (6 && -6)|| 0,Both of operands of && operator is non zero, this operator return 1 if and only if both operands value is non zero and if value of 1st operand is zero && operator return 0 without optimizing second operand so && return 1. || operator return one 1 if one of the its operands value non zero, if value 1st operand is non zero it returns 1 without executing the next operand, so above statement value of c variable never increment.
Step-by-step solution
A is 5, so ++a makes a = 6, which is true (non-zero).- Because the left side of
&& is true, ++b runs: b changes from -7 to -6, which is also true. - So the
&& part is 1 and the || short-circuits, so ++c is not evaluated and c stays 0. - d = 1, so the values are a = 6, b = -6, c = 0 and d = 1.
- These values match option D: 6 -6 0 1. Printed without separators they would run together, but option D is the only option with these values.
Why the other options are wrong
- A. D is 1, not 0, because the && part is true; the option gives d = 0.
- B. B is -6, not -5, because ++b increases -7 to -6.
- C. A is 6, not -6, because ++a increases 5 to 6.
Tip: In && and ||, the right side runs only when needed; track short-circuiting.
Practise all 18 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 →