Discussion
What is the output of this C code?
int main()
{
if (7 & 8)
printf("Honesty");
if ((~7 & 0x000f) == 8)
printf("is the best policy\n");
}
Answer: C
Correct answer: is the best policy
Take the two conditions separately.
7 & 8: \(0111 \;\&\; 1000 = 0000\). They share no set bit, so the value is 0 and Honesty is not printed.~7 & 0x000f: \(\sim 0111 = \ldots 11111000\); masking with \(1111\) leaves \(1000 = 8\), which does equal 8.
So only the second printf runs, and the output is is the best policy.
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