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

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

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

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

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

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

CE221 Programming in C++ Part 1 Introduction

ECE462Exam2. 08:10-09:20AM, October 30, 2009

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

CS 211 Winter 2004 Sample Final Exam (Solutions)

2 2

ECE 462 Exam 3. 11:30AM-12:20PM, November 17, 2010

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

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

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

Program template-smart-pointers-again.cc

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

ECE 461 Fall 2011, Final Exam

Polymorphism Part 1 1

C:\Temp\Templates. Download This PDF From The Web Site

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Do not turn to the next page until the start of the exam.

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

Inheritance, and Polymorphism.

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

ECE 462 Fall 2011, Third Exam

Lab 1: First Steps in C++ - Eclipse

Lecture on pointers, references, and arrays and vectors

Midterm Exam 5 April 20, 2015

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

CSE 333 Midterm Exam Cinco de Mayo, 2017 (May 5) Name UW ID#

Exam duration: 3 hours Number of pages: 10

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

CSCE 110 PROGRAMMING FUNDAMENTALS

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

Midterm Practice Exam

Advanced Systems Programming

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

ECE Fall 2018, Test 3

CS 376b Computer Vision

ECE 462 Fall 2011, Second Exam

ECE Fall 2018, Test 2

Assumptions. History

ECE264 Fall 2013 Exam 1, September 24, 2013

CSCE 206: Structured Programming in C++

CSE 333 Midterm Exam 7/22/12

PENN STATE UNIVERSITY Department of Economics

COMP 2355 Introduction to Systems Programming

Exam Duration: 2hrs and 30min Software Design

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Common Misunderstandings from Exam 1 Material

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

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

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

ECE 2036 Lab 1: Introduction to Software Objects

Unified Modeling Language a case study

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

Function Overloading

Linked List using a Sentinel

Review Questions for Final Exam

CSE 333 Final Exam 3/19/14

System Design and Programming II

Fast Introduction to Object Oriented Programming and C++

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

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

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

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

C++ Lab 03 - C++ Functions

C++ Important Questions with Answers

ECE Fall 20l2, Second Exam

Program template-smart-pointers.cc

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

COEN244: Class & function templates

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

Implementing an ADT with a Class

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

CS 1337 Computer Science II Page 1

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

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

CSCI 102L - Data Structures Midterm Exam #2 Spring 2011

Inheritance and Polymorphism

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

Programming in C++: Assignment Week 8

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

PIC 10A. Lecture 17: Classes III, overloading

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

ECE264 Fall 2013 Exam 3, November 20, 2013

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

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

Unit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

1. The term STL stands for?

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

CPSC 427: Object-Oriented Programming

Comp151. Inheritance: Initialization & Substitution Principle

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

Transcription:

ECE 462 Exam 1 6:30-7:30PM, September 22, 2010 I will not receive nor provide aid to any other student for this exam. Signature: You must sign here. Otherwise, the exam is not graded. This exam is printed double sides. Write your answers next to the questions. If you need more space, you can use the two blank pages. This is an open-book, open-note exam. You can use any book or note or program printouts. Please turn off your cellular phone now. Two outcomes are tested in this exam. To pass each outcome, you must receive 50% of the points. Outcome 3: an understanding of the concepts of inheritance and polymorphism. Outcome 4: an ability to use template classes and libraries in C++ and Java. You need to obtain 50% of the points in each outcome to pass the outcome. If a program has a syntax error, point out which line causes the error. If a question has a numeric answer, you can write the procedure without the final result. For example, you can write 1 + 2 instead of 3. 1

Contents Passed Outcomes: 3 4 Total Score: 2

This page is blank. 3

This page is blank. 4

1 Abstract Class (1 point) Which statement is correct? You may choose multiple answers. Answer: A A. If a Java class has an abstract member function (i.e. method), this class is abstract. B. If a Java class is abstract, all methods of this class must be abstract. C. If a C++ class has a pure virtual function, all methods must be virtual. D. If a C++ class has a pure virtual function, all methods must be pure virtual. E. If a Java class has an abstract method, this class must not have any private attribute. All attributes must be protected. 5

2 C++ Inheritance and Polymorphism (Outcome 3, 4 points) Answer the questions based on the following class hierarchy. There are 16 answers, 0.25 point for each answer. // question31.h #ifndef QUESTION3_H #define QUESTION3_H #include <iostream> using namespace std; class Base public: void f1() cout << "B:f1 "; f2(); // CAUTION: call f2 void f2() cout << "B:f2"<< endl; virtual void f3() cout << "B:f3"<< endl; ; class Derived1: public Base public: void f1() cout << "D1:f1 "; f2(); // CAUTION: virtual void f2() cout << "D1:f2"<< endl; virtual void f4() cout << "D1:f4"<< endl; ; all f2 class Derived2: public Derived1 public: void f2() cout << "D2:f2"<< endl; void f3() cout << "D2:f3"<< endl; void f5() cout << "D2:f5 "; f2(); // CAUTION: call f2 ; #endif // end of question31.h // // question31.cpp // #include "question31.h" int main(int argc, char * argv[]) // Which lines cause compile-time errors? Base * b2d1 = new Derived1; Base * b3d2 = new Derived2; 6

Derived1 * d1b1 = new Base; Derived1 * d1d1 = new Derived1; Derived1 * d1d2 = new Derived2; Derived2 * d2d2 = new Derived2; // Which lines below cause compile-time errors? // If a line has no error, write the outputs. // Please write your answers next to the corresponding lines. // If a variable has an error earlier, ignore the variable below. // For example, if d1b1 has an error, ignore everything about d1b1. b2d1 -> f3(); b3d2 -> f2(); b3d2 -> f1(); b3d2 -> f5(); d1d1 -> f1(); d1d2 -> f4(); d2d2 -> f1(); d2d2 -> f2(); d2d2 -> f4(); d2d2 -> f5(); return 0; Answer: errors: Derived1 * d1b1 = new Base; b3d2 -> f5(); b2d1 -> f3(); B:f3 b3d2 -> f2(); B:f2 b3d2 -> f1(); B:f1 B:f2 b3d2 -> f5(); error 7

d1d1 -> f1(); D1:f1 D1:f2 d1d2 -> f4(); D1:f4 d2d2 -> f1(); D1:f1 D2:f2 d2d2 -> f2(); D2:f2 d2d2 -> f4(); D1:f4 d2d2 -> f5(); D2:f5 D2:f2 8

3 Java Inheritance and Polymorphism (Outcome 3, 2 points) Answer the questions based on the following class hierarchy. There are 5 answers, 0.4 point for each answer. class Base public void f1() System.out.print("B:f1 "); f2(); public void f2() System.out.println("B:f2"); public void f3() System.out.println("B:f3"); class Derived1 extends Base public void f1() System.out.print("D1:f1 "); f2(); public void f2() System.out.println("D1:f2"); public void f4() System.out.println("D1:f4"); class Derived2 extends Derived1 public void f2() System.out.println("D2:f2"); public void f3() System.out.println("D2:f3"); public void f5() System.out.print("D2:f5 "); f2(); class Question public static void main(string [] args) Base b2d1 = new Derived1(); Base b3d2 = new Derived2(); Derived1 d1d1 = new Derived1(); Derived1 d1d2 = new Derived2(); Derived2 d2d2 = new Derived2(); // Write the outputs next to the corresponding lines. b3d2.f2(); b3d2.f1(); d1d1.f1(); d2d2.f1(); 9

d2d2.f5(); Answer: no compile-time error b3d2.f2(); D2:f2 b3d2.f1(); D1:f1 D2:f2 d1d1.f1(); D1:f1 D1:f2 d2d2.f1(); D1:f1 D2:f2 d2d2.f5(); D2:f5 D2:f2 10

4 C++ Template (Outcome 4, 2 point) Use arrays to implement a map. A map stores (key, value) pairs. The keys must be distinct. This map can handle different types, including int and string; therefore, you should use template. #include <iostream> #include <string> using namespace std; // ---> FIX ME <--- // create generic map using template // a map stores (key, value) pairs class ECE462MAP public: ECE462MAP() size = 10; item = 0; karray = new KEY[size]; varray = new VALUE[size]; bool contains(key k) // ---> FIX ME <--- // does the key already exist? // return true, if it does // return false, if it does not void insert(key k, VALUE v) // check whether the key has already been seen for (int i = 0; i < item; i ++) if (karray[i] == k) // replace the value varray[i] = v; return; // new key if (item == size) // need more space size *= 2; 11

KEY * karray2 = new KEY[size]; VALUE * varray2 = new VALUE[size]; // copy elements for (int i = 0; i < item; i ++) karray2[i] = karray[i]; varray2[i] = varray[i]; delete [] karray; delete [] varray; karray = karray2; varray = varray2; karray[item] = k; varray[item] = v; item ++; // ---> FIX ME <--- // write the return type and the argument type of the "get" function // assume the map contains this key for (int i = 0; i < item; i ++) if (karray[i] == k) return varray[i]; // should not reach here return varray[0]; void remove(key k) // assume the set contains this key for (int i = 0; i < item; i ++) if (karray[i] == k) karray[i] = karray[item - 1]; varray[i] = varray[item - 1]; item --; return; // don t worry about shrinking the arrays 12

// should not reach here int getsize() return item; virtual ECE462MAP() // ---> FIX ME <--- // release memory for the key and value arrays private: KEY * karray; VALUE * varray; int size; int item; ; int main(int argc, char * argv[]) // create a map string -> integer ECE462MAP<string,int> mapsi; mapsi.insert("ece", 4); mapsi.insert("purdue", 6); mapsi.insert("lafayette", 2); cout << mapsi.get("lafayette") << endl; // 2 mapsi.insert("lafayette", 9); cout << mapsi.get("lafayette") << endl; // 9 cout << mapsi.getsize() << endl; // 3 mapsi.remove("purdue"); cout << mapsi.getsize() << endl; // 2 cout << mapsi.contains("ece") << endl; // 1 // create a map integer -> string ECE462MAP<int, string> mapis; mapis.insert(2, "WEST"); cout << mapis.getsize() << endl; // 1 cout << mapis.get(2) << endl; // WEST // create a map string -> string ECE462MAP<string,string> mapss; mapss.insert("hello", "Purdue"); cout << mapss.get("hello") << endl; // Purdue return 0; Answer: 13

#include <iostream> #include <string> using namespace std; template<class KEY, class VALUE> class ECE462MAP public: ECE462MAP() size = 10; item = 0; karray = new KEY[size]; varray = new VALUE[size]; bool contains(key k) for (int i = 0; i < item; i ++) if (karray[i] == k) return true; return false; void insert(key k, VALUE v) for (int i = 0; i < item; i ++) if (karray[i] == k) varray[i] = v; return; if (item == size) size *= 2; KEY * karray2 = new KEY[size]; VALUE * varray2 = new VALUE[size]; for (int i = 0; i < item; i ++) karray2[i] = karray[i]; varray2[i] = varray[i]; delete [] karray; 14

delete [] varray; karray = karray2; varray = varray2; karray[item] = k; varray[item] = v; item ++; VALUE get(key k) for (int i = 0; i < item; i ++) if (karray[i] == k) return varray[i]; return varray[0]; void remove(key k) for (int i = 0; i < item; i ++) if (karray[i] == k) karray[i] = karray[item - 1]; varray[i] = varray[item - 1]; item --; return; int getsize() return item; virtual ECE462MAP() delete [] karray; delete [] varray; private: KEY * karray; VALUE * varray; 15

int size; int item; ; int main(int argc, char * argv[]) // create a map string -> integer ECE462MAP<string,int> mapsi; mapsi.insert("ece", 4); mapsi.insert("purdue", 6); mapsi.insert("lafayette", 2); cout << mapsi.get("lafayette") << endl; // 2 mapsi.insert("lafayette", 9); cout << mapsi.get("lafayette") << endl; // 9 cout << mapsi.getsize() << endl; // 3 mapsi.remove("purdue"); cout << mapsi.getsize() << endl; // 2 cout << mapsi.contains("ece") << endl; // 1 // create a map integer -> string ECE462MAP<int, string> mapis; mapis.insert(2, "WEST"); cout << mapis.getsize() << endl; // 1 cout << mapis.get(2) << endl; // WEST // create a map string -> string ECE462MAP<string,string> mapss; mapss.insert("hello", "Purdue"); cout << mapss.get("hello") << endl; // Purdue return 0; 16

5 C++ Iterator (Outcome 4, 2 point) Write the print function using an iterator. #include <iostream> #include <vector> using namespace std; void print(vector<int> vec) /* ---> FIX ME <--- */ /* iterate through the elements and print the elements one by one */ cout << endl; int main() int data[] = 11, 12, 23, 34; int size = sizeof( data ) / sizeof( data[0] ); vector<int> vec; for (int i = 0; i < size; i ++) vec.push_back(data[i]); print( vec ); return 0; Answer: void print(vector<int> vec ) vector<int>::iterator p = vec.begin(); while ( p < vec.end() ) cout << *p++ << " "; cout << endl; 17

6 Java List and Class Hierarchy (Outcome 4, 2 point) What is the output of this program? There are four answers, 0.5 point for each. import java.util.*; class Base public void f1() System.out.print("B:f1 "); f2(); public void f2() System.out.println("B:f2"); class Derived1 extends Base public void f1() System.out.print("D1:f1 "); f2(); class Derived2 extends Derived1 public void f2() System.out.println("D2:f2"); class Question public static void main(string [] args) List<Base> blist = new ArrayList<Base>(); Base b1b = new Base(); Base b2d1 = new Derived1(); Base b3d2 = new Derived2(); Derived1 d1d2 = new Derived2(); blist.add(b1b); blist.add(b2d1); blist.add(b3d2); blist.add(d1d2); ListIterator iter = blist.listiterator(); while ( iter.hasnext() ) Base b = (Base) iter.next(); b.f1(); Answer: B:f1 B:f2 18

D1:f1 B:f2 D1:f1 D2:f2 D1:f1 D2:f2 19

7 C++ Constructor and Destructor (2 point) Write down the output of this program. Make sure you draw the separators (-----). Answer: B1::B1 D11::D11 ----- B2::B2 D21::D21 D22::D22 ----- B1:: B1 ----- D22:: D22 D21:: D21 B2:: B2 #include <iostream> using namespace std; class B1 public: B1() cout << "B1::B1" << endl; B1() cout << "B1:: B1" << endl; ; class D11: public B1 public: D11() cout << "D11::D11" << endl; D11() cout << "D11:: D11" << endl; ; class D12: public D11 public: D12() cout << "D12::D12" << endl; data = new int[100]; D12() 20

cout << "D12:: D12" << endl; delete [] data; private: int * data; ; class B2 public: B2() cout << "B2::B2" << endl; virtual B2() cout << "B2:: B2" << endl; ; class D21: public B2 public: D21() cout << "D21::D21" << endl; data = new int[100]; D21() cout << "D21:: D21" << endl; delete [] data; int * data; ; class D22: public D21 public: D22() cout << "D22::D22" << endl; D22() cout << "D22:: D22" << endl; ; int main(int argc, char * argv[]) B1 * b1 = new D11; cout << "-----" << endl; B2 * b2 = new D22; cout << "-----" << endl; delete b1; cout << "-----" << endl; 21

delete b2; return 0; 22