1. C++ allows pointers to stack-dynamic variables. Consider the following C++ function: See image.

Will the above function compile in C++? Is it correct? If not, under what circumstances will it fail? Consider one other language that has pointers. Does that language have the same problem? Explain.

2. Examine the following C++ program, in which a wrapper Int class is defined that contains an overloaded += operator. What is the output of this program? See image.

Notice that the overloaded += operator returns a reference. Change the += operator to return a value instead and determine what the output would be in that case. Explain the difference in behavior and why the ability to return by reference is essential to be able to implement such operators.

3. Consider the following C++ template class. See image.

The class SortedList cannot be instantiated for any arbitrary type. For example, consider the following instantiation for a wrapper integer class. See image.

Explain why the second implementation fails. What must be added to that class so this program will compile? Suppose this program were written in Java. Explain how Java allows the constraints on a generic type parameter to be specified and how they would be specified in this case

Java does have one limitation, however. Although wrapper classes can be used to instantiate generic type parameters, primitive types cannot. Explain why.

4. What is the output of the following C++ program? See image.

Modify the program so that the put function is virtual. What is the output after that change? Does Java allow both virtual and nonvirtual methods? If not, which does it allow? Rewrite this program in Java and identify at least four differences between the programs in the two languages.

5. Consider the following C++ program? See image.

This program contains an example of object slicing. On what line does it occur? Why must it happen? Explain why this never happens in Java. Do some investigating and determine how C# avoids this problem.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.