Question 3: What is the output of the following code?
TCS technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What is the output of the following code? #include
Show answer & explanation
The code uses the left shift operator (<<), which shifts the bits of the integer to the left by the specified number of positions. Shifting an integer left by 1 position is mathematically equivalent to multiplying the number by 2^1.
Step-by-step Derivation:
Step 1: Identify the variable and operation. The variable digit is initialized to 8. The operation is digit << 1.
Step 2: Convert the decimal value 8 to binary. 8 in binary is 00001000 (representing 8 in an 8-bit simplified view).
Step 3: Perform the left shift operation by 1 position. Shifting 00001000 left by 1 results in 00010000.
Step 4: Convert the resulting binary 00010000 back to decimal. 2^4 = 16.
Step 5: The printf function outputs the resulting integer value, which is 16.