Test 1 Solutions

It is important that you work on the questions on your own before looking at these solutions.

1. (b) [a = -1, b= -1]
Many students have written (c) which is not correct.
Of course the value of a is changed to 0 however the next statement e = f; makes both e and f point to b and now since e also points to b cout « *e; will print -1 because e now points to b and its value is -1. f already points to b whose value is -1.

Run this code online to verify if the answer is correct
A slightly modified code using the same question - make sure you understand the output

2. (c)
(b) is not the correct answer since in a class even if you do not write a constructor you can still create an object of it because the compiler will supply the default constructor automatically.

3. (c) - Hello is printed two times and Bye is printed two times
To answer this question correctly you need to understand a destructor and when it will be called. In the code you can see two objects h1 and h2 are created . h1 is created statically and the object pointed by h2 is created using dynamic memory allocation. In case of h2 the destructor is called when delete h2; statement is executed . In case of h1 destructor is also called when h1 is going out of scope i.e main has finished executing

Run this code to verify the answer

4. (b)

5. (b)

6. (b)
Since Fruit is the base class you cannot assign an instance of Fruit to the DryFruit which is a derived class of Fruit unless you cast it - like the statements in (c) and (d) in which casting is used explicitly to convert the Fruit to DryFruit

7. The Point class - view and run online
To overload the operator == you should return boolean (bool in C++) not a Point object
showXY() - should show x and y values

8. The Triangle class - view and run online

9. The sumOfArray() and the sumOfArrayUsingPointer() functions
View and run the program with the two sum of array functions

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License