Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

A brief question to prep for final exam, Exams of Computer Science

Programming 2, do this to understand the concept of OOP and master it

Typology: Exams

2023/2024

Uploaded on 06/09/2025

minh-hoang-38
minh-hoang-38 🇻🇳

1 document

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 274—Object Oriented Programming with C++
Final Exam
December 16th
(2) 1. TRUE or FALSE: Namespaces allow you to have multiple memory locations with
the same variable name.
(6) 2. Explain the error in the following groups of function declarations or write “No Error”
(a) void f(int x, string y);
int f(int x, string y);
(b) float g(int x, float y = 2.31, string z);
(c) void h(int x);
void h();
(2) 3. Explain the difference between the keywords struct and class.
(6) 4. Consider the following class definition:
class TwoPoint{
private:
int x, y;
public:
void set(int x, int y);
};
Provide the definition of the method set. Write the method outside the class and do not
change the prototype of the method.
(4) 5. Give two reasons to pass an object by reference.
(3) 6. Refer back to the TwoPoint class in #4 above. Write a default constructor for the class
that does not have an empty parameter list.
(2) 7. Give one reason why a class would need a destructor.
(2) 8. Why can’t the constant pointer this be used inside a static method?
(4) 9. Assume the necessary operators are overloaded:
(a) Write the following in operator form: b.operator=(a.operator+(c));
(b) Write the following in functional form: z = x + w*y;
(4) 10. Consider a class Complex in which addition has been overloaded. Let the variable z
represent an object of type Complex. What two things need to be true in order for the
expression 2 + z to be legal?
(3) 11. Why can’t << be overloaded as a method and still retain its standard functionality (ie,
cout << x, where x is an object type)?
conflicting declaration
Overloading only work with diff parameters,
but here they are same parameters but diff return type
Error because default variable?
no error
struct is an ADT and can have both
variables and methods, but by default
members are public."
class is struct with method
TwoPoint coord;
coord.set(2,6);
1. Reduce space, no copy is needed
2. Modify directly to the parameter without dereference
TwoPoint(int _x, int _y_) : x = _x, y = _y{}
To free up memory
pf3

Partial preview of the text

Download A brief question to prep for final exam and more Exams Computer Science in PDF only on Docsity!

CS 274—Object Oriented Programming with C++ Final Exam December 16 th

(2) 1. TRUE or FALSE: Namespaces allow you to have multiple memory locations with the same variable name.

(6) 2. Explain the error in the following groups of function declarations or write “No Error”

(a) void f(int x, string y); int f(int x, string y);

(b) float g(int x, float y = 2.31, string z);

(c) void h(int x); void h();

(2) 3. Explain the difference between the keywords struct and class.

(6) 4. Consider the following class definition:

class TwoPoint{ private: int x, y; public: void set(int x, int y); };

Provide the definition of the method set. Write the method outside the class and do not change the prototype of the method.

(4) 5. Give two reasons to pass an object by reference.

(3) 6. Refer back to the TwoPoint class in #4 above. Write a default constructor for the class that does not have an empty parameter list.

(2) 7. Give one reason why a class would need a destructor.

(2) 8. Why can’t the constant pointer this be used inside a static method?

(4) 9. Assume the necessary operators are overloaded: (a) Write the following in operator form: b.operator=(a.operator+(c)); (b) Write the following in functional form: z = x + w*y;

(4) 10. Consider a class Complex in which addition has been overloaded. Let the variable z represent an object of type Complex. What two things need to be true in order for the expression 2 + z to be legal?

(3) 11. Why can’t << be overloaded as a method and still retain its standard functionality (ie, cout << x , where x is an object type)?

(2) 12. TRUE or FALSE: The assignment operator must be overloaded as a top-level function.

(4) 13. Consider the following inheritance hierarchy: class A{ protected: int x, y; public: int z; }

class B: public A{ private: int a, b, c; } (a) How many data members does B have? (b) How many of B’s data members are visible in B?

(4) 14. Why is protected accessibility needed?

(3) 15. Give an example of a case where virtual inheritance is necessary (ie, a case

where you need virtual base classes in a multiple inheritance hierarchy.)

(5) 16. Consider the following inheritance hierarchy:

class A{ public: int x; }

class B: public A{ private: int x; public: void set(int a, int b); }

Write the definition for the method set that assigns the inputs a and b to B’s data members.

(2) 17. TRUE or FALSE: If a base class has a default constructor, then none of the constructors in the derived class need to explicitly call a base class constructor.

(2) 18. TRUE or FALSE: If a base class has a parameterized constructor but no default constructor, then derived class objects cannot be instanced unless the derived class constructor explicitly calls one of the base class constructors.

(2) 19. TRUE or FALSE: If a base class has no constructors whatsoever, then a derived class object cannot be instanced.

(3) 20. With which type of binding is the vtable used?