ECE 462 Midterm Exam 1. 10:30-11:20AM, September 21, 2007

Similar documents
Containers in C++ and Java

ECE 462 Exam 1. 09:30-10:20AM, September 24, 2008

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

Inheritance, and Polymorphism.

ECE 462 Written Assignment 1. Due: 12:20PM, September 22, 2008

ECE462Exam1. 08:10-09:20AM, October 02, 2009

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

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

Classes, The Rest of the Story

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

ECE 462 Fall 2011, Third Exam

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

Polymorphism CSCI 201 Principles of Software Development

ECE 461 Fall 2011, Final Exam

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

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)

COEN244: Class & function templates

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

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

CE221 Programming in C++ Part 1 Introduction

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Friend Functions, Inheritance

Common Misunderstandings from Exam 1 Material

ECE Fall 20l2, Second Exam

Use the template below and fill in the areas in Red to complete it.

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

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

University of Swaziland Department OfComputer Science Main Examination December 2016

ECE 462 Midterm Exam 2. 10:30-11:20AM, October 19, 2007

CMSC 132: Object-Oriented Programming II

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

Java Object Oriented Design. CSC207 Fall 2014

Java Basic Syntax. Java vs C++ Wojciech Frohmberg / OOP Laboratory. Poznan University of Technology

Midterm Exam 5 April 20, 2015

ECE 3574: Dynamic Polymorphism using Inheritance

ECE 462 Object-Oriented Programming using C++ and Java Design Issues and Multiple Inheritance in C++

Linked List using a Sentinel

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CS197c: Programming in C++

Exam duration: 3 hours Number of pages: 10

CMSC 132: Object-Oriented Programming II

CS2141 Software Development using C/C++ C++ Basics

Assumptions. History

CMSC 202 Midterm Exam 1 Fall 2015

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

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

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

Lecture 15a Persistent Memory & Shared Pointers

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

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

Polymorphism Part 1 1

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Evolution of Programming Languages

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

CPSC 427: Object-Oriented Programming

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Lecture on pointers, references, and arrays and vectors

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

Where do we go from here?

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

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

System Design and Programming II

Lab 1: First Steps in C++ - Eclipse

Inheritance and Polymorphism

G52CPP C++ Programming Lecture 18

1. The term STL stands for?

PENN STATE UNIVERSITY Department of Economics

Lecture-5. STL Containers & Iterators

Homework 4. Any questions?

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

CSCE 110 PROGRAMMING FUNDAMENTALS

Ministry of Higher Education Colleges of Applied Sciences Final Exam Academic Year 2009/2010. Student s Name

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

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

ECE 462 Fall 2011, Second Exam

CS 376b Computer Vision

COMP 2355 Introduction to Systems Programming

Object Oriented Programming: Inheritance Polymorphism

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

Introduction to Programming Using Java (98-388)

Advanced Systems Programming

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CS 11 C++ track: lecture 1

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

Review Questions for Final Exam

Object Oriented Programming in C++

2 2

Introduction to Programming using C++

G52CPP C++ Programming Lecture 20

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Chapter 1: Object-Oriented Programming Using C++

Final Exam Solutions PIC 10B, Spring 2016

W3101: Programming Languages C++ Ramana Isukapalli

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Namespaces and Class Hierarchies

Object Oriented Programming. Java-Lecture 11 Polymorphism

Lecture 5: Inheritance

Transcription:

ECE 462 Midterm Exam 1 10:30-11:20AM, September 21, 2007 1 Template Classes and the STL Library 1.1 Container Classes Which statement is correct? Answer: B A. An element can be inserted anywhere in a stack. B. An element can be inserted anywhere in a list. C. In a map, the values must be unique and the keys can be the same. Thus, it can be a one-to-many mapping. D. In a map, the values and the keys must be unique. Thus, it must be a one-to-one mapping. E. Once a vector object is created, the first element cannot be deleted. 1.2 Container Classes Which statement is correct? Answer: C A. Elements can be inserted or removed only at the end of a list, not anywhere else. B. Elements can be inserted only at the end of a list and removed at the beginning of a list. C. In a Java list, duplicate elements are allowed. D. In a C++ list, duplicate elements are not allowed. The elements must be unique. E. Elements can be inserted or removed only at the beginning of a list. 1

1.3 Template in C++ Which statement is correct? Answer: E A. A template can be used in built-in container classes only. A programmer cannot create a class with a template. B. A template can be replaced by a primitive type (int, char, double...) only. C++ does not allow replacing a template by a class. C. A template can be replaced by a class only. C++ does not allow replacing a template by a primitive type. D. A class can have only one template and the placeholder must be called T. E. A class can have several templates and they can be replaced by different types. 2

1.4 Java Vector and Class Hierarchy What is the output of this program? Answer: B import java.util.*; class Base protected int b_val; public Base(int val) b_val = val; public void print() System.out.println("B " + b_val); class Derived extends Base public Derived(int val) super(val); public void print() System.out.println("D " + b_val); class Program public static void main( String[] args ) Vector<Base> objectvec = new Vector<Base>(); Base [] objects = new Base[4]; objects[0] = new Base(0); objects[1] = new Derived(1); objects[2] = new Base(2); objects[3] = new Derived(3); objectvec.add(objects[0]); objectvec.add(objects[1]); objectvec.add(objects[2]); objectvec.add(objects[3]); Iterator iter = objectvec.iterator(); Base iterobj; while ( iter.hasnext() ) iterobj = (Base) iter.next(); iterobj.print(); A. B0 B1 B2 B3 B. B0 D1 B2 D3 C. B0 D0 B0 D0 D. B1 D0 B3 D2 E. D0 D1 D2 D3 3

1.5 Container and Copy Constructor How many times is X (const X & xin) p = xin.p; cout << "X(const X&)" << endl; called in this program? Answer: E (12) #include <iostream> #include <vector> using namespace std; class X int p; X( int q ) p = q; cout << "X(int)" << endl; X (const X & xin) p = xin.p; cout << "X(const X&)" << endl; int getp() const return p; void changestate( int pp ) p = pp; ; void print( vector<x> ); int main() vector<x> vec; X x1( 2 ); X x2( 3 ); X x3( 5 ); vec.push_back( x1 ); vec.push_back( x2 ); vec.push_back( x3 ); print( vec ); x2.changestate( 1000 ); print( vec); return 0; void print( vector<x> v ) cout << "\nvector size is: " << v.size() << endl; vector<x>::iterator p = v.begin(); while ( p!= v.end() ) cout << (*p++).getp() << " "; cout << endl << endl; A. 0 B. 3 C. 6 D. 7 E. none of the above 4

1.6 C++ Set What is the output of this program? Answer: 4 c d f g (The letters must be ordered.) #include <iostream> #include <set> #include <string> using namespace std; int main(void) set<string> charset; set<string>::iterator iter; charset.insert("c"); charset.insert("g"); charset.insert("d"); charset.insert("f"); charset.insert("c"); cout << charset.size() << endl; for (iter = charset.begin();iter!= charset.end(); iter++) cout << *iter << " "; cout << endl; return 0; 5

2 Inheritance and Polymorphism 2.1 Inheritance and Class Which statement is correct? Answer: B A. A base class must not be an abstract class. B. A derived class has everything of the base class and probably additional attributes and methods. C. Polymorphism means objects can protect their private data. D. The only purpose of creating a class is to encapsulate data. E. In Java, a private attribute can be seen by derived classes; a protected attribute cannot be seen by derived classes. 2.2 Inheritance, Interface, and Implementation Answer: D Which statement is correct? A. An abstract class cannot have any attribute. B. If a C++ class has a virtual function, this class is abstract. C. If a class has no virtual function, this class cannot have derived classes. D. A class provides an interface through which messages can be sent. E. If an object X is not declared as an attribute inside a class Y, this object X is not allowed to send any message to any object of class Y. 2.3 Programming Assignment 1 When you create a derived class from JPanel and you create a new implementation of the paintcomponent method, which is the best description of the concept? Answer: A A. Polymorphism. Since this derived class overrides the implementation, this new implementation is called. B. Encapsulation. This paintcomponent method is not directly called anywhere by your code. Encapsulation means the caller of a function is invisible to a programmer. C. Inheritance. This paintcomponent method is inherited from the JPanel class. D. Abstraction. JPanel is an abstract class and no object can be created for JPanel. E. Containment. All painting is accomplished by this derived class. 6

2.4 Inheritance and Polymorphism What is the output of this program? Answer: D class X public void print() System.out.print("X"); class Y extends X public void print() System.out.print("Y"); class Z extends Y class prog public static void main (String[] args) X [] objs; objs = new X[3]; objs[0] = new X(); objs[1] = new Y(); objs[2] = new Z(); for (int ocnt = 0; ocnt < 3; ocnt ++) objs[ocnt].print(); A. This program does not compile. B. XXX C. YYY D. XYY E. XYZ 7

2.5 Inheritance and Polymorphism What is the output of this program? If the program cannot compile, write cannot compile. Answer: 1 2 4 #include <iostream> using namespace std; class BaseC int val; BaseC() val = 2; virtual void addobj(basec * obj) cout << (obj -> val) << " "; ; class DerivedC: public BaseC DerivedC() val = 1; virtual void addobj(basec * obj) cout << 2 * (obj -> val) << " "; virtual void addobj(derivedc * obj) cout << 3 * (obj -> val) << " "; ; int main(int argc, char * argv[]) BaseC * obj1 = new BaseC; BaseC * obj2 = new DerivedC; DerivedC * obj3 = new DerivedC; obj1 -> addobj (obj2); obj2 -> addobj (obj3); obj3 -> addobj (obj1); 2.6 Inheritance and Polymorphism Which statement is correct? Answer: A A. In C++, polymorphism is achieved using virtual functions. B. Polymorphism allows objects to send messages to each other. C. An object of the derived class has an object of the base class. D. Inheritance means an object s behavior may change based on whether this object is created for the base class or a derived class at run-time. E. In Java, polymorphism is achieved using abstract classes. 8

3 Object-Oriented Design and Programming 3.1 Abstract C++ Class Shape is an abstract class and it contains a public function area; the function takes no parameter and returns double. The area function must be overridden by derived classes. Write the class declaration for Shape Answer: class Shape virtual double area() = 0; ; 3.2 Prevent Inheritance in Java What word in Java can be added in front of the word class so that the class cannot have any derived class. Answer: final 3.3 Object Creation in C++ In C++, an object can be created in this way: AClassName anobject(parameters for constructor); How to create a C++ object using a pointer? Answer: AClassName * apointer = new AClassName(parameters for constructor); 3.4 Copy Constructor in C++ Write the copy constructor for class X Answer: X (const X & xin) class X int p; X( int q ) p = q; cout << "X(int)" << endl; 9

// what should be here for copy constructor? p = xin.p; cout << "X(const X&)" << endl; ; int getp() const return p; 3.5 Destructor in C++ Write the appropriate destructor to prevent memory leak Answer: X() delete [] p; class X int * p; int size; X(int s) size = s; p = new int[size]; // write the destructor ; 3.6 Java Set Fill in the missing line (or lines). Answer: Iterator iter = animals.iterator(); import java.util.*; class SetOps public static void main( String[] args ) Set<String> animals = new TreeSet<String>(); animals.add( "cheetah" ); animals.add( "lion" ); animals.add( "cat" ); animals.add( "elephant" ); animals.add( "cat" ); 10

System.out.println( animals ); System.out.println( animals.size() ); animals.remove( "lion" ); System.out.println( animals ); // missing line while ( iter.hasnext() ) System.out.println( iter.next() ); 3.7 UML Class Diagram What is correct about this UML class diagram? Answer: D A. The three methods are protected. B. Shape is an abstract class. C. The method move returns false if the shape cannot move. D. The class has an attribute called origin and it is private. E. The method display takes one parameter and it is of type boolean. 3.8 UML Use Case What is correct about this UML class diagram? Answer: B A. A use case specifies the interface of a class. B. Two people may manage the project; one is the Manager and the other is the Database Administrator. C. A use case shows the implementation of a class. D. An actor can use only one case. E. The system can be upgraded by both Programmer and System Administrator. 11

3.9 UML Class Diagram What is correct about this UML class diagram? Answer: E A. Rectangle does not support the move method. B. Shape is derived from Rectangle and Circle. C. Rectangle must override the display method. D. Circle has only one attribute called radius. E. Rectangle does not have the radius attribute. 3.10 UML Relationship Diagram What is correct about this UML relationship diagram? Answer: C 12

A. Employee is a derived class of Corporation B. Employee is the base class of Corporation C. A Corporation object may have zero Employee object. D. A Corporation object must have zero or one Employee object. E. A Corporation object must be created before any Employee object. 3.11 UML Sequence Diagram What is correct about this UML sequence diagram? Answer: C A. The AuctionList object is created after receiving the getcurrentmaxbid() message. 13

B. The selectitem() message may occur after the postbid() message. C. The notifyseller() message is sent only if minacceptbidexceeded condition is true. D. In the third step, the AuctionItem object sends the postbid() message to the Buyer object. E. The Seller object is created after sending the bidaccepted() message. 3.12 C++ Namespace What is the output of this program? Answer: 1:f 2:f 3:b #include <iostream> using namespace std; namespace Module1 class X void foo() cout << "1:f "; void bar() cout << "1:b "; ; namespace Module2 class X void foo() cout << "2:f "; void bar() cout << "2:b "; ; namespace Module3 using namespace Module1; class Z void foo() cout << "3:f "; void bar() cout << "3:b "; ; int main() using namespace Module1; X xobj1; xobj1.foo(); using namespace Module2; Module2::X xobj2; xobj2.foo(); Module3::Z zobj3; zobj3.bar(); return 0; 14

Contents 1 Template Classes and the STL Library 1 1.1 Container Classes........................................... 1 1.2 Container Classes........................................... 1 1.3 Template in C++........................................... 2 1.4 Java Vector and Class Hierarchy.................................. 3 1.5 Container and Copy Constructor.................................. 4 1.6 C++ Set............................................... 5 2 Inheritance and Polymorphism 6 2.1 Inheritance and Class........................................ 6 2.2 Inheritance, Interface, and Implementation............................ 6 2.3 Programming Assignment 1..................................... 6 2.4 Inheritance and Polymorphism................................... 7 2.5 Inheritance and Polymorphism................................... 8 2.6 Inheritance and Polymorphism................................... 8 3 Object-Oriented Design and Programming 9 3.1 Abstract C++ Class......................................... 9 3.2 Prevent Inheritance in Java..................................... 9 3.3 Object Creation in C++....................................... 9 3.4 Copy Constructor in C++...................................... 9 3.5 Destructor in C++.......................................... 10 3.6 Java Set................................................ 10 3.7 UML Class Diagram......................................... 11 3.8 UML Use Case............................................ 11 3.9 UML Class Diagram......................................... 12 3.10 UML Relationship Diagram..................................... 12 3.11 UML Sequence Diagram....................................... 13 3.12 C++ Namespace........................................... 14 15