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

171/44 © 01 hour 09 ALL 21.

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

171/44 © 01 hour 09 ALL 21. (C++ Question) Concatenate two strings in C++ Suppose we have the following code (6) int main(){ string s1= "Hello"; string s2 = "World"; e Now we wish to concatenate these two strings. Which of the following is a valid method in C++ for concatenation of these 2 strings? Y Pick ONE option Vv | (eo) Add header #include and add the code: s3 = s1 + s2; | o | ( ; ) Add header #include and add the code: s3 = s1 + s2; | 22 | ( ; ) Add header #include and add the code: s3 = strcat(s1, s2); | 23 | ( ; ) Add header #include and add the code: s3 = strcat(strcat(s1, s2), '\O'); | Clear Selection G Saved! 24 --- [PAGE/IMAGE BREAK] --- d:18/44 © 01hour 07 mins @® G What is the output of the following code: ALL #include (6) using namespace std; class Base * { public: v Base(){ cout<<"Constructing Base\n";} Base(){ cout<<"Destroying Base\n";} 3 v class Derive: public Base { v public: Derive(){ cout<<"Constructing Derive\ } ~Derive(){ cout<<"Destroying Derive\n";} v 3 int main() 23 { Base *basePtr = new Derive(); delete basePtr; 24 return 0; } 25 Pick ONE option --- [PAGE/IMAGE BREAK] --- d:18/44 © 01hour 06 mins @® G class Derive: public Base ALL t public: Derive(){ cout<<"Constructing Derive\ (o) ~Derive(){ cout<<"Destroying Derive\n" 3 int main() { v Base xbasePtr = new Derive(); delete basePtr; return 0; v } v Pick ONE option v | ( ; ) Constructing Base Constructing Derive Destroying Derive | 23 | ( ; ) Constructing Derive Constructing Base Destroying Base | 24 | ( ; ) Constructing Derive Constructing Base Destroying Derive | 25 | (eo) Constructing Base Constructing Derive Destroying Base | ~ Clear Selection --- [PAGE/IMAGE BREAK] --- ALL 24 25 26 class Derived: public Base { 3 public: Derived() { cout << "Constructing Derived\n"; } ~Derived() { cout << "Destructing Derived\n" int main() { Derived *d = new Derived(); Base *b = d; delete b; return 0; d: 19/44 --- [PAGE/IMAGE BREAK] --- ALL 24 25 26 Pick ONE option —, Constructing Derived Constructing Base —\ Constructing Derived Constructing Base Constructing Derived Destructing Base Constructing Base Constructing Derived (eo) Destructing Derived Destructing Base Clear Selection G Saved! 9/44 - A) 01 hour 04 mins - B) G - C) 01 hour 04 mins - D) G

Choose one option.
Show answer & explanation
Answer: D. G

In C++, when a derived class object is deleted via a pointer to the base class, only the base class destructor is called unless the base class destructor is declared as 'virtual'. Since the Base destructor is not virtual, the behavior is a partial destruction (slicing), calling only the Base destructor.

Step-by-step Derivation:
Step 1: Analyze the class hierarchy. 'Derive' inherits from 'Base'.
Step 2: Trace the object creation: 'Base basePtr = new Derive();'. This invokes the Base constructor first, then the Derive constructor. Output: 'Constructing Base\nConstructing Derive\n'.
Step 3: Analyze the deletion: 'delete basePtr;'. The pointer type is 'Base
'.
Step 4: Check for virtual destructors. The Base class destructor 'Base()' is NOT marked as 'virtual'.
Step 5: According to C++ language specifications, deleting a derived object through a pointer to a base class that has a non-virtual destructor results in undefined behavior, but in most standard implementations, it statically binds to the Base destructor only.
Step 6: Therefore, only '
Base()' is executed. Output: 'Destroying Base\n'.
Step 7: Combine the sequence: Constructing Base -> Constructing Derive -> Destroying Base.