Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Similar documents
Ch02. True/False Indicate whether the statement is true or false.

Inheritance, and Polymorphism.

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Chapter 20 - C++ Virtual Functions and Polymorphism

Polymorphism Part 1 1

IS0020 Program Design and Software Tools Summer, 2004 August 2, 2004 in Class

Programming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor

Data Structures and Other Objects Using C++

Midterm Exam 5 April 20, 2015

Polymorphism. Arizona State University 1

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

Instantiation of Template class

UEE1303(1070) S12: Object-Oriented Programming Advanced Topics of Class

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Extending Classes (contd.) (Chapter 15) Questions:

Introduction Of Classes ( OOPS )

CS 162, Lecture 25: Exam II Review. 30 May 2018

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

COEN244: Class & function templates

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Concepts of Object Oriented Programming

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CS105 C++ Lecture 7. More on Classes, Inheritance

Chapter 19 C++ Inheritance

GEA 2017, Week 4. February 21, 2017

CS250 Final Review Questions

ECE 3574: Dynamic Polymorphism using Inheritance

Object Oriented Programming. Solved MCQs - Part 2

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

VIRTUAL FUNCTIONS Chapter 10

Lecture 5: Inheritance

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

CS304 Object Oriented Programming

C++ Programming: Polymorphism

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Midterm Review. PIC 10B Spring 2018

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

Function Overloading

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

CS

G52CPP C++ Programming Lecture 13

Programming in C++: Assignment Week 8

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

C++ Classes & Object Oriented Programming

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

Computer Science II CSci 1200 Lecture 24 C++ Inheritance and Polymorphism

private: // can only be used by the member functions of MyType and friends of MyType };

AN OVERVIEW OF C++ 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Short Notes of CS201

Absolute C++ Walter Savitch

EL2310 Scientific Programming

Praktikum: Entwicklung interaktiver eingebetteter Systeme

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Exam Duration: 2hrs and 30min Software Design

CS201 - Introduction to Programming Glossary By

The 10 Minute Guide to Object Oriented Programming

OBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2

1. the base class s (default) constructor is executed first, 2. followed by the derived class s constructor

Fast Introduction to Object Oriented Programming and C++

Object-Oriented Design (OOD) and C++

CSE 303: Concepts and Tools for Software Development

Polymorphism. Zimmer CSCI 330

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

IS 0020 Program Design and Software Tools

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

CS304- Object Oriented Programming LATEST SOLVED MCQS FROM FINALTERM PAPERS. MC

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

CPSC 427: Object-Oriented Programming

CS304 Object Oriented Programming Final Term

And Even More and More C++ Fundamentals of Computer Science

This examination has 11 pages. Check that you have a complete paper.

Data Abstraction. Hwansoo Han

Introduction to Programming

Object Oriented Design

6.096 Introduction to C++ January (IAP) 2009

Constructor - example

Interview Questions of C++

Chapter 19 - C++ Inheritance

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Object Oriented Software Design II

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

Overview. C++ Tutorial. Arrays. Pointers. Strings. Parameter Passing. Rob Jagnow

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

Object Oriented Software Design II

PIC10B/1 Winter 2014 Exam I Study Guide

CSCE 110 PROGRAMMING FUNDAMENTALS

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

/************************************************ * CSC 309/404 Review for Final -- Solutions * ************************************************/

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Transcription:

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with a redefined nonvirtual print function. Given the following statements, will the output of the two print function calls be identical? HourlyWorker h; Employee *eptr = &h; eptr->print(); eptr->employee::print(); a. No. b. It would depend on the implementation of the print function. c. Yes. d. Yes, if print is a static function. Q2: The main difference between a pure virtual function and a virtual function is: a. The return type. b. The member access specifier. c. That a pure virtual function cannot have an implementation. d. The location in the class. Q3: What mistake prevents the following class declaration from functioning properly as an abstract class? class Shape public: virtual double print() const; double area() const return base * height; private: double base; double height; ; a. There are no pure virtual functions. b. There is a non-virtual function. c. private variables are being accessed by a public function. d. Nothing, it functions fine as an abstract class. Q4: The relationship between function templates and function-template specializations is most similar to the relationship between: a. Classes and objects. b. Classes and functions. c. Functions and return types. 1

d. Headers and source files. Q5: Function-template specializations: a. Are identical to macros. b. Are generated at compile time. c. Have a maximum allowed number of type parameters. d. Are not more concise than the equivalent set of overloaded functions. Q6: A function template can be overloaded by: a. Using other function templates with the same function name and parameters. b. Using non-template functions with the same name and different parameters. c. Using non-template functions with a different name but the same parameters. d. Using other function templates with a different name but the same parameters. Q7: For a class template, the binary scope resolution operator (::) is needed: a. Only in the definitions of the member functions defined outside the class. b. Both in the prototype and definition of a member function. c. Only if multiple class-template specializations will be created from this class template. d. In neither the definition nor prototype of member functions. Q8: Function templates: a. Can include objects of template classes as parameters. b. Must have return type T. c. Do not need a separate template< typename type > statement if they take objects from a template class as a parameter. d. Do not need a separate template< typename type > statement. Q9: When an object of a derived class is instantiated, the constructor initializes the members. a. Derived class, base class. b. Base class, base class. c. Base class, derived class. d. Derived class, public. Q10: Which forms of inheritance are is-a relationships? a. Only public and private. b. Only public and protected. c. Only public. d. All forms of inheritance are is-a relationships. Q11: Problems using switch logic to deal with many objects of different types other than inheritance do not include: a. Forgetting to include an object in one of the cases. b. Not being able to implement separate functions on different objects. c. Having to update the switch statement whenever a new type of object is added. d. Having to track down every switch statement to do an update of object types. Q12: virtual functions must: a. Be declared virtual in the base class. b. Be overridden in every derived class. 2

c. Be declared virtual in every derived class. d. Have the same implementation in every derived class. Q13: protected base class members cannot be accessed by: a. Functions that are neither friends of the base class, derived-class member functions nor friends of a derived class. b. friends of the base class. c. Functions that are not derived-class member functions. d. friends of derived classes. Q14: Which of the following assignments would be a compilation error? a. Assigning the address of a base-class object to a base-class pointer. b. Assigning the address of a derived-class object to a base-class pointer. c. Assigning the address of a derived-class object to a derived-class pointer. d. Assigning the address of a base-class object to a derived-class pointer. Q15: From most restrictive to least restrictive, the access modifiers are: a. private, protected, public b. protected, private, public c. private, public, protected d. protected, public, private Q16: virtual destructors must be used when: a. The constructor in the base class is virtual. b. delete is used on a derived-class object. c. delete is used on a base-class pointer to a derived-class object. d. A constructor in either the base class or derived class is virtual. Q17: Suppose class B inherits from base class A. What is the order in which their constructors and destructors will be called when an object of class A is instantiated and then destroyed? a. B constructor, A constructor, A destructor, B destructor. b. B constructor, A constructor, B destructor, A destructor. c. A constructor, B constructor, A destructor, B destructor. d. A constructor, B constructor, B destructor, A destructor. Q18: If objects of all the classes derived from the same base class all need to draw themselves, the draw function would most likely be declared: a. private b. protected c. friend d. virtual Q19: The line: virtual double earnings() const = 0; appears in a class definition. You cannot deduce that: a. All classes that directly inherit from this class will override this method. b. This class is an abstract class. c. Any concrete class derived from this class will have an earnings function. d. This class will probably be used as a base class for other classes. 3

Q20: The line: virtual double earnings() const = 0; in a class definition indicates that the class is probably a: a. Base class. b. Derived class. c. Protected class. d. Library class. Section 2 True/False Questions (7 pts total, 1 pt each, please mark \/ or X in front of the questions): 1. Virtual function can use either static or dynamic binding, depending on the handles on which the functions are called. 2. A difference between function-template specializations and overloaded functions is that Function-template specializations cannot accept user-defined types. 3. Common attributes and behaviors should be factored out of closely related classes and placed into a base class from which the original classes can now inherit. 4. Some derived classes can have multiple base classes. 5. When deriving a class from a protected base class, the public members of the base class become protected and the protected members of the base class become protected. 6. Virtual functions must be overridden in every derived class. 7. Programmers should never intentionally instantiate any objects from abstract classes. Section 3 (23 pts) Question 1 (14 pts): (a) (7 pts) Draw an inheritance hierarchy for students at a university. Use Student as the base class of the hierarchy, then include classes UndergraduateStudent and GraduateStudent that derive from Student. Continue to extend the hierarchy to include Freshman, Sophomore, Junior and Senior, and DoctoralStudent and MastersStudent. 4

(b) (7 pts) After drawing the hierarchy, discuss the relationships that exist between the classes: what should be inherited, and which member functions should be virtual and pure virtual. Question 2 (9 pts): Correct the following code: The following code creates an object of type Y. Class Y inherits from class X. There are three errors in the code. Identify and correct them. #include <iostream> using namespace std; // class X definition class X public: // constructor X() cout << "X constructed!"; // end class X constructor ;//endclassx // class Y definition class Y public: // redefine inherited constructor X() cout << "Y created, not X!"; // end class Y constructor ;//endclassy int main() Y yobject(); //endmain 5

Section 4 (30 pts) Question 1. (10 pts) Convert the IntArray to a template version Array class so the class is not bounded to one type, but can be used for int, double, and char. #include <assert.h> // for assert() class IntArray private: int m_nlength; int *m_pndata; public: IntArray() m_nlength = 0; m_pndata = 0; IntArray(int nlength) m_pndata = new int[nlength]; m_nlength = nlength; ~IntArray() delete[] m_pndata; void Erase() delete[] m_pndata; m_pndata = 0; m_nlength = 0; int& operator[](int nindex) assert(nindex >= 0 && nindex < m_nlength); return m_pndata[nindex]; int GetLength(); ; int IntArray ::GetLength() return m_nlength; 6

Question 2. (20 pts) Write three classes: Animal, Dog, Cat, Mouse. The Animal class should be an abstract class. #include <iostream> #include "Animal.h" #include "Dog.h" #include "Cat.h" #include "Mouse.h" const int cnumanimals = 3; int main() Animal* panimal[cnumanimals] = new Dog, new Cat, new Mouse ; for (int i = 0 ; i < cnumanimals ; ++i) std::cout << panimal[i]->speak() << std::endl; delete panimal[i]; return 0; Output: Woof woof! meow meow! zzzzzzzzz! 7

8