IGDTUW 2026 — Computer PYQ
IGDTUW | Computer | 2026What is the output of the following C++ code?
Choose the correct answer:
- A.
0
- B.
1
- C.
7
(Correct Answer) - D.
Compilation Error
7
Explanation
The correct option is (b) 1.
Step-by-Step Explanation:
Variable Initialization: An integer variable a is initialized with the decimal value 7.
int a=7;
Understanding the Bitwise AND Operator (
&): The expression(a & 1)utilizes the bitwise AND operator. This operator compares corresponding bits of two numbers and returns 1 if both bits are 1, otherwise it returns 0.Binary Conversion and Evaluation: To perform the operation, convert both numbers into their binary representations:
7 in binary: 000001112
1 in binary: 000000012
Now, align the bits and apply the logical AND operation column-by-column:
000001112 & 000000012000000012(7)(1)(1)
The result of this operation is 000000012, which corresponds to the decimal value:
1
Programming Insight: Performing a bitwise AND with 1 (
number & 1) evaluates only the least significant bit (LSB) of the integer. If a number is odd, its LSB is always 1, yielding a result of 1. If it is even, its LSB is 0, yielding a result of 0. Since 7 is an odd number, the expression evaluates to 1.
Therefore, the program outputs:
1
