C++ (dynamic allocation and character arithmetic) — determine the printed value from a constructor that sums an int and a character code.\n\n#include<iostream.h>\nclass CuriousTab {\n int *p;\npublic:\n CuriousTab(int xx, char ch) {\n p = new int();\n *p = xx + int(ch);\n cout << *p;\n }\n ~CuriousTab() { delete p; }\n};\nint main() {\n CuriousTab objCuriousTab(10, 'B');\n return 0;\n}\n\nWhat value is printed?

Difficulty: Easy

Correct Answer: The program will print the output 76.

Explanation:


Introduction / Context:
This snippet allocates an int on the heap, computes a sum using an integer literal and the numeric code of a character, and prints the result. Understanding how chars convert to int in C++ is the key here.


Given Data / Assumptions:

  • xx = 10.
  • ch = 'B', whose ASCII code is 66.
  • The constructor does *p = xx + int(ch) and prints *p.


Concept / Approach:
In C and C++, characters are small integer types. Converting a char to int yields its code point (commonly ASCII in this context). Therefore the arithmetic expression 10 + 66 evaluates to 76, which is then printed. Memory is correctly freed in the destructor, avoiding leaks.


Step-by-Step Solution:

1) Evaluate int(ch) for 'B' → 66.2) Compute 10 + 66 → 76.3) Store in *p and stream via cout → prints 76.


Verification / Alternative check:
Replace 'B' with 'a' (97) to see 107. Replace 10 with 0 to print the character code itself.


Why Other Options Are Wrong:

  • 108: Would correspond to 42 + 66 or 10 + 98; not our inputs.
  • Garbage value / compile error: Code is correct and deterministic.


Common Pitfalls:
Expecting character concatenation semantics (like strings) instead of integer arithmetic with char.


Final Answer:
The program will print the output 76.

More Questions from Constructors and Destructors

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion