Q.Given the following class Test and assuming all necessary header file(s) included, answer the questions that follow the code: class Test { int Marks; char TName[20]; public: Test(int M) //Function 1 { Marks = M; } Test(char S[]) //Function 2 { strcpy(TName,S); } Test(char S[], int M) //Function 3 { Marks = M; strcpy(TName,S); } Test(Test &T) //Function 4 { Marks = T.Marks + 10; strcpy(TName,T.TName); } }; void main() { Test T1(10); //Statement I Test T2(70); //Statement II Test T3(30, "PRACTICAL"); //Statement III _______________; //Statement IV }
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The class Test has four constructors, but Statement III passes arguments in the wrong order — it should be Test T3("PRACTICAL", 30);. Function 4 is a copy constructor, and Statement IV should call it by passing an existing object, e.g., Test T4(T1);.
Let's understand the problem step by step.
The class Test has two data members: an integer Marks and a character array TName of size 20. It defines four constructors — each is a different overloaded version.
1. Understanding each constructor
Function 1: Test(int M) — takes a single integer. It sets Marks = M but leaves TName uninitialized (garbage).
Function 2: Test(char S[]) — takes a single C-string. It copies the string into TName but leaves Marks uninitialized.
Function 3: Test(char S[], int M) — takes a C-string first, then an integer. It sets both members.
Function 4: Test(Test &T) — takes a reference to another Test object. It copies TName from T, but sets Marks = T.Marks + 10 (not a direct copy). This is a copy constructor because it takes a reference to the same class type.
2. Checking each statement in main()
Statement I: Test T1(10);
This matches Function 1 (single integer). Correct.
Statement II: Test T2(70);
Again matches Function 1. Correct.
Statement III: Test T3(30, "PRACTICAL");
Here the first argument is an integer 30, and the second is a string "PRACTICAL". But Function 3 expects (char[], int) — string first, integer second. So the order is reversed.
C++ will try to match:
- Function 3 expects
(char[], int)— doesn't match because30is not a string. - No other constructor takes two arguments. So this statement is incorrect.
A common mistake is to assume the order of arguments doesn't matter — but C++ is strict about parameter types and order. Test T3(30, "PRACTICAL") will cause a compilation error because no constructor matches (int, char[]).
Statement IV: It's blank — we need to fill it so that Function 4 (the copy constructor) executes.
3. What is Function 4?
Function 4 is Test(Test &T) — a constructor that takes a reference to an existing object of the same class. This is the copy constructor. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.