Discussion
What will be the output of the following C code?
#include <stdio.h>
int main()
{
int a = 2;
if (a >> 1)
printf("%d\n", a);
}
Answer: B
Correct answer: 2
a >> 1 computes a value; it does not modify a. Only the compound form a >>= 1 would assign back.
So the test evaluates \( 2 \gg 1 = 1 \), which is non-zero and therefore true, and the printf then prints a - still 2, exactly as it was declared. The distractor 1 is the value of the shift expression, which is tested but never stored, and "No Output" would require the test to be false.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development