OA. free
Free
TCS Data Structures & Algorithms Data Structures & Algorithms Medium

Question 15: What will be the output of the following code?

TCS technical mcq question, verified with a worked answer. Free to practise - no sign-up.

What will be the output of the following code? #include <stdio.h> int main() { int a=64; double b=256; int *x=&a; double *y=&b; printf("x and y are %d and %d",sizeof(x),sizeof(y)); return 0; } Ans:- B) x and y are 8 and 8 - A) Compilation error - B) x and y are 8 and 8 - C) x and y are 1 and 2 - D) Runtime error

Choose one option.
Show answer & explanation
Answer: B. x and y are 8 and 8

In C, the size of a pointer is determined by the architecture of the system (e.g., 64-bit or 32-bit) and is independent of the data type it points to. On a 64-bit system, all pointers (int*, double*, etc.) typically occupy 8 bytes.

Step-by-step Derivation:
Step 1: Analyze the variable declarations. 'a' is an integer and 'b' is a double. 'x' is a pointer to an integer (int*) and 'y' is a pointer to a double (double*).
Step 2: Evaluate the sizeof operator. The sizeof operator returns the size in bytes of the object or type. For pointers, it returns the size required to store a memory address.
Step 3: Determine pointer size based on architecture. In modern 64-bit environments (which is the standard for current assessment contexts), a memory address is 64 bits wide, which equals 8 bytes.
Step 4: Compare x and y. Since both x and y are pointers, regardless of whether they point to an int (4 bytes) or a double (8 bytes), the pointers themselves are the same size.
Step 5: Conclusion. sizeof(x) = 8 and sizeof(y) = 8. The printf statement will output 'x and y are 8 and 8'.