C++ strings (C-style) — predict the concatenated message built by the constructor with default parameters and print via Display(). #include<iostream.h> #include<string.h> class CuriousTabString { char x[50]; char y[50]; char z[50]; public: CuriousTabString() { } CuriousTabString(char* xx) { strcpy(x, xx); strcpy(y, xx); } CuriousTabString(char* xx, char* yy = " C++", char* zz = " Programming!") { strcpy(z, xx); strcat(z, yy); strcat(z, zz); } void Display(void) { cout << z << endl; } }; int main() { CuriousTabString objStr("Learn", " Java"); objStr.Display(); return 0; } What is the exact output?

Difficulty: Easy

Correct Answer: Learn Java Programming!

Explanation:


Introduction / Context:
This item checks understanding of C-style string operations (strcpy, strcat) and how default parameters work when some but not all constructor arguments are provided. The Display() method prints the buffer z built by the three-argument constructor.


Given Data / Assumptions:

  • Constructor signature: (char* xx, char* yy = " C++", char* zz = " Programming!").
  • Call: CuriousTabString objStr("Learn", " Java"); so xx = "Learn", yy = " Java", zz takes the default " Programming!".
  • Buffers are large enough for the concatenation in this example.


Concept / Approach:
The constructor copies xx into z, then concatenates yy, then concatenates zz. Because yy and zz include leading spaces, they create word spacing naturally in the result.


Step-by-Step Solution:

Start with z = "Learn".Concatenate yy: z = "Learn Java".Concatenate default zz: z = "Learn Java Programming!".Display() prints the final string.


Verification / Alternative check:
Changing the call to ("Learn") would use the one-parameter constructor and not initialize z; Display() would then print an uninitialized buffer (undefined). The two-parameter constructor is the correct usage here.


Why Other Options Are Wrong:

  • Other choices either omit the Learn prefix or use the wrong defaults ordering.


Common Pitfalls:
Forgetting that default arguments keep their specified leading spaces; omitting them causes words to touch.


Final Answer:
Learn Java Programming!

More Questions from Functions

Discussion & Comments

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