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

Similar documents
ECE462Exam3. 08:10-09:20AM, November 20, 2009

ECE Fall 20l2, Second Exam

ECE 461 Fall 2011, Final Exam

ECE 462 Fall 2011, Second Exam

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

ECE Fall 2014, Final Exam

ECE 462 Fall 2011, Third Exam

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

CPSC 427: Object-Oriented Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

C++ Exception Handling 1

CPSC 427: Object-Oriented Programming

Overloading Operators in C++

Multithreaded OO. Questions:

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

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Classes, The Rest of the Story

G52CPP C++ Programming Lecture 16

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

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

Object Oriented Design

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

Constructor - example

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Programming in C++: Assignment Week 8

CS3157: Advanced Programming. Outline

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

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

Polymorphism CSCI 201 Principles of Software Development

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

C++_ MARKS 40 MIN

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

W3101: Programming Languages C++ Ramana Isukapalli

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

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

IS 0020 Program Design and Software Tools

Modern Programming Languages. Lecture Java Programming Language. An Introduction

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Polymorphism Part 1 1

Friend Functions, Inheritance

Module Contact: Dr Anthony J. Bagnall, CMP Copyright of the University of East Anglia Version 2

Exam Number/Code : 1Z Exam Name: Name: Java Standard Edition 6. Demo. Version : Programmer Certified Professional Exam.

1 Shyam sir JAVA Notes

Threads Chate Patanothai

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CSC 330 Object-Oriented Programming. Exception Handling CSC 330

Part IV Other Systems: I Java Threads

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Java Threads. COMP 585 Noteset #2 1

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

Full file at Chapter 2 - Inheritance and Exception Handling

Selected Questions from by Nageshwara Rao

CS102 C++ Exception Handling & Namespaces

Laboratorio di Tecnologie dell'informazione

Evolution of Programming Languages

Introduction to Programming Using Java (98-388)

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

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

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

COM S 213 PRELIM EXAMINATION #2 April 26, 2001

Interview Questions of C++

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

2 ADT Programming User-defined abstract data types

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

CS 247: Software Engineering Principles. ADT Design

Exception with arguments

1. (25 pts) Short Answer. Provide brief (1-3 sentence) answers to the following:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

ECE Fall 2017, Third Exam

IS 0020 Program Design and Software Tools

Functions and Methods. Questions:

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

G52CPP C++ Programming Lecture 17

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

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Midterm Review. PIC 10B Spring 2018

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Intermediate Programming, Spring 2017*

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

C and C++ I. Spring 2014 Carola Wenk

Exceptions, Case Study-Exception handling in C++.

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

22316 Course Title : Object Oriented Programming using C++ Max. Marks : 70 Time: 3 Hrs.

COP 3223H Fall 2017 Final Exam. Last Name:, First Name: Directions: Please answer questions 1-5 in C, and questions 6-10 in C++.

Lab Instructor : Jean Lai

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

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

STRUCTURING OF PROGRAM

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Chapter 3 Function Overloading

Transcription:

ECE 462 Midterm Exam 2 10:30-11:20AM, October 19, 2007 1 Overloading and Overriding 1.1 Overloading in C++ and Java Answer: E A. In C++, if a function is overloaded, it cannot be overridden in a derived class. B. In Java, if a function is overloaded, it cannot be overridden in a derived class. C. Overloaded functions in C++ must use primitive types; objects cannot be used as parameters in overloaded functions. D. In Java, overloaded functions can be distinguished by both the argument types and the return types. E. In Java, overloaded functions can use objects as parameters. 1.2 Overloading in C++ Answer: A A. In C++, an integer cannot be promoted to string automatically. B. In C++, only primitive types can distinguish which version of the overloaded function. Objects cannot decide the overloaded function. C. In C++, a function can be overloaded only if it is a method in a class. D. In C++, if a function is overloaded, a derived class must override all versions of the overloaded function. E. In C++, a string argument may be automatically converted to double to match an overloaded function. 1

1.3 Overloading in Java Answer: A A. In Java, a program cannot compile if ambiguity occurs in deciding which version of an overloaded function to choose. B. In Java, a user can give hint during program execution to decide which version of an overloaded function to choose by specifying input. C. In Java, if an exact match is unavailable, an object of class X may match to an object of class Y, here Y is derived from X (class Y extends X). D. In Java, overloading must be distinguished using primitive types; objects are not allowed. E. In Java, all versions of an overloaded function must have the same number of arguments; these arguments have different types. 1.4 Overloading in Java What is the output of this program? Answer: E class X public void print() System.out.println("()"); public void print(int v1) System.out.println("(int)"); public void print(int v1, int v2) System.out.println("(int, int)"); class prog public static void main (String[] args) X obj; obj = new X(); obj.print(); obj.print(1); obj.print(1, 2); A. This program does not compile. B. () () () 2

C. (int) (int) (int) D. (int, int) (int, int) (int, int) E. () (int) (int, int) 1.5 Overloading and Overriding and Class Hierarchy in Java What is the output of this program? If the program cannot compile, write cannot compile. Answer: X() X(X, X) X(X, X) X(X, X) class X public void print() System.out.println("X()"); public void print(x v1) System.out.println("X(X)"); public void print(x v1, X v2) System.out.println("X(X, X)"); class Y extends X public void print(y v1, Y v2) System.out.println("Y(Y, Y)"); class prog public static void main (String[] args) X obj0, obj1; obj0 = new X(); obj1 = new Y(); obj0.print(); obj0.print(obj0, obj0); obj0.print(obj0, obj1); obj1.print(obj1, obj0); 1.6 Overloading and Overriding What is the output of this program? You can write the steps how val is changed without doing the calculation. For example, you can write 1 + 2 instead of 3. If the program cannot compile, write cannot compile. 3

Answer: cannot compile. cannot find symbol symbol : method foo(java.lang.string) location: class MyBaseClass mobj.foo(str); class MyBaseClass protected int val; public MyBaseClass() val = 1; public void foo() val += 2; public void foo(int i) val += 3; public int getval() return val; class MyDerivedClass extends MyBaseClass public MyDerivedClass () val = 4; public void foo() val += 5; public void foo(string str) val += 6; class Test public static void main(string[] args) MyBaseClass mobj = new MyDerivedClass(); String str = new String("hello"); mobj.foo(); mobj.foo(str); mobj.foo(4); System.out.println("val = " + mobj.getval()); 4

2 Operator Overloading 2.1 Operator Overloading and Object-Oriented Programming Answer: E A. Operator overloading is supported in C++ and Java. B. If operator overloading is not supported, inheritance cannot be achieved. C. C++ allows redefining the precedence of operators. D. In C++, an operator can be overloaded for primitive types. In Java, an operator can be overloaded only for user-defined classes. E. Operator overloading is not essential for object-oriented programming. 2.2 Operator Overloading in C++ Answer: C A. If operators < and == are overloaded, it is unnecessary to overload <=. Calling <= will be automatically converted to calling < and ==. B. Operator - can be overloaded only for binary operation. C. A binary operator can be implemented as a member function. D. A programmer can change the precedence of operators in C++. E. Overloaded operators must be member functions. 5

2.3 Operator Overloading as Global Functions Which statement is correct, assuming correct code segments are filled into the appropriate locations. Answer: C #include <iostream> using namespace std; class MyComplex double re, im; public: MyComplex( double r, double i ) : re(r), im(i) double getreal() const return re; double getimag() const return im; ; MyComplex operator+ /* A */ double d1 = arg1.getreal() + arg2.getreal(); double d2 = arg1.getimag() + arg2.getimag(); return MyComplex( d1, d2 ); MyComplex operator- (MyComplex arg1, MyComplex arg2 ) double d1 = arg1.getreal() - arg2.getreal(); double d2 = arg1.getimag() - arg2.getimag(); return MyComplex( d1, d2 ); ostream& operator << /* B */ double d1 = arg.getreal(); double d2 = arg.getimag(); os << "(" << d1 << ", " << d2 << ")" << endl; /* C */ int main() MyComplex first(3, 4); MyComplex second(2, 9); cout << first; cout << second; cout << first + second; cout << first - second; /* D */ return 0; A. This program has compile-time error. B. /* A */ should be replaced by (const MyComplex arg2 ). C. /* B */ should be replaced by ( ostream & os, const MyComplex & arg ). D. /* C */ should be replaced by return MyComplex( d1, d2 );. E. /* D */ can be replaced by cout << - second + first;. 6

2.4 Operator Overloading as Global and Member Functions Which statement is correct, assuming correct code segments are filled into the appropriate locations. Answer: D #include <iostream> using namespace std; class MyComplex double re, im; public: MyComplex( double r, double i ) : re(r), im(i) MyComplex operator-() const; /* A */ double getreal() const return re; double getimag() const return im; friend ostream& operator<< ( ostream&, const MyComplex& ); ; MyComplex MyComplex::operator-() const return MyComplex( -re, -im ); /* B */ MyComplex operator+( const MyComplex arg1, const MyComplex arg2 ) double d1 = arg1.getreal() + arg2.getreal(); double d2 = arg1.getimag() + arg2.getimag(); return MyComplex( d1, d2 ); MyComplex operator-( MyComplex arg1, MyComplex arg2 ) double d1 = arg1.getreal() - arg2.getreal(); /* C */ double d2 = arg1.getimag() - arg2.getimag(); return MyComplex( d1, d2 ); ostream& operator<< ( ostream& os, const MyComplex& c ) os << "(" << c.re << ", " << c.im << ")" << endl; return os; int main() MyComplex c(3, 4); MyComplex z = -c; /* D */ cout << z << endl; MyComplex first(3, 4); MyComplex second(2, 9); cout << first; cout << second; cout << first + second; cout << first - second; cout << - second + first; /* E */ return 0; 7

A. /* A */ causes compile-time error because there is no argument. B. /* B */ causes compile-time error because re and im are private. C. /* C */ can be replaced by double d1 = arg1.re - arg2.re;. D. /* D */ calls MyComplex MyComplex::operator-() const. E. /* E */ calls MyComplex operator-( MyComplex arg1, MyComplex arg2) and MyComplex operator+( const MyComplex arg1, const MyComplex arg2 ). 8

2.5 Overloading Operator << Please fill in the place marked by /* here */ so that the program can compile. If nothing is needed, please write nothing. Answer: friend #include <iostream> using namespace std; class MyComplex double re, im; public: MyComplex( double r, double i ) : re(r), im(i) MyComplex operator+( MyComplex) const; MyComplex operator-( MyComplex) const; /* here */ ostream& operator<< ( ostream&, const MyComplex& ); ; MyComplex MyComplex::operator+( const MyComplex arg ) const double d1 = re + arg.re; double d2 = im + arg.im; return MyComplex( d1, d2 ); MyComplex MyComplex::operator-( const MyComplex arg ) const double d1 = re - arg.re; double d2 = im - arg.im; return MyComplex( d1, d2 ); ostream& operator<< ( ostream& os, const MyComplex& c ) os << "(" << c.re << ", " << c.im << ")" << endl; return os; int main() MyComplex first(3, 4); MyComplex second(2, 9); cout << first; // (3, 4) cout << second; // (2, 9) cout << first + second; // (5, 13) cout << first - second; // (1, -5) return 0; 9

2.6 Overloading Operator << Please fill in the place marked by /* here */. If nothing is needed, please write nothing. Answer: ostream& operator<< ( ostream& os, const MyComplex& arg ) #include <iostream> using namespace std; class MyComplex double re, im; public: MyComplex( double r, double i ) : re(r), im(i) double getreal() const return re; double getimag() const return im; ; MyComplex operator-( const MyComplex arg ) return MyComplex( -arg.getreal(), -arg.getimag() ); /* here */ double d1 = arg.getreal(); double d2 = arg.getimag(); os << "(" << arg.getreal() << ", " << arg.getimag() << ")" << endl; return os; int main() MyComplex c(3, 4); cout << c; // (3, 4) cout << -c; // (-3, -4) return 0; 10

3 Exception Handling 3.1 Exception in C++ Answer: B A. In C++, a function may throw at most one type of exception. B. In C++, an integer can be passed when an exception is thrown. C. In C++, a function that may throw an exception must not call another function that may throw a different type of exception. D. An exception must be caught by the immediate caller; otherwise the program terminates. E. In C++, if a function may throw an exception, the function must be called within a try-catch block. 3.2 Exception in Java Answer: E A. In Java, an integer can be passed when an exception is thrown. B. In Java, a function may throw at most one type of exception. C. If an exception is not caught, the program causes memory leak. D. In Java, an exception can pass only a primitive type (such as integer, char, or float). E. In Java, if a function may throw an exception, this must be declared at the beginning of the function. 3.3 Exception in Java Answer: A class MyException /* A */ String message; public MyException( String mess ) message = mess; 11

class Test static void f() /* B */ throw new MyException( "Hello from f()" ); public static void main( String[] args ) /* C */ try f(); catch( MyException e ) /* D */ System.out.println( e.message ); A. /* A */ can be replaced by extends Exception. B. /* A */ can be replaced by extends Exception or left empty; either can work. C. /* B */ can be replaced by throws String;. D. /* C */ can call f(); E. /* D */ anything between catch...... can be removed. 3.4 Exception in C++ Answer: B #include <iostream> #include <cstdlib> using namespace std; class Err ; void f( int ); /* A */ int main() try f(0); /* B */ catch ( Err ) cout << "caught Err" << endl; exit(0); return 0; 12

void f(int j) /* C */ cout << "function f invoked with j = " << j << endl; if (j == 3) throw Err(); f( ++j ); /* D */ A. /* A */ must be replaced by throw(int);. B. If /* B */ f(0); is replaced by f(5); no exception will be thrown but the program will eventually run out of call stack. C. /* A */ must be replaced by throw(err);. D. /* C */ must be replaced by throw(err);. E. /* D */ must put f(++j); inside a try-catch block. 3.5 Exception in C++ What should be /* here */ to handle an exception created by f? If nothing is needed, please write nothing. Answer: catch( MyException ex ) #include <iostream> #include <string> using namespace std; class MyException string message; public: MyException( string m ) message = m; string getmessage() return message; ; void f() throw( MyException ); int main() try f(); /* here */ cout << ex.getmessage(); return 0; void f() throw( MyException ) throw MyException( "hello there" ); 13

3.6 Exception in Java What should be /* here */ so that the catch block prints Hi? If nothing is needed, please write nothing. Answer: throw new MyException( "Hi" ); class MyException extends Exception String message; public MyException( String mess ) message = mess; class Test static void f() throws MyException /* here */ public static void main( String[] args ) try f(); catch( MyException e ) System.out.println( e.message ); // Hi 14

4 Multiple Thread and Synchronization 4.1 Thread States in Java A Java thread may be in one of several states. Which one is not included? Answer: B A. running B. Indiana C. waiting D. sleeping E. born 4.2 Thread in Java Answer: D A. Calling the run method of a thread object (for example obj.run()) will invoke the object s start method. B. Creating a thread (calling new) makes the thread execute immediately. C. A thread can be created by instantiating a class that is derived (extends) from Runnable. D. After creating a thread, call start to start the thread s execution. E. If a thread is created (new) first, the thread always finishes first. 15

4.3 Thread Execution Order Answer: C class HelloThread extends Thread String message; HelloThread( String message ) this.message = message; public void run() int sleeptime = (int) ( Math.random() * 3000 ); try sleep( sleeptime ); catch( InterruptedException e ) System.out.print( message ); public static void main( String[] args ) HelloThread ht1 = new HelloThread( " Good" ); HelloThread ht2 = new HelloThread( " morning" ); HelloThread ht3 = new HelloThread( " to" ); ht1.start(); ht2.start(); ht3.start(); int sleeptime = (int) ( Math.random() * 3000 ); try sleep( sleeptime ); catch( InterruptedException e ) System.out.println( " you!" ); A. The output is always Good morning to you!. B. The word you! appears last but the order of Good, morning, and to may change in each execution. C. The order of Good, morning, to, and you! may change in each execution. D. This program may cause deadlock. E. The statements ht1.start(); ht2.start(); ht3.start(); are unnecessary for printing the words. 4.4 Java wait-notify Answer: E 16

A. If a synchronized method calls wait, the effect is equivalent to calling abort. B. If a synchronized method calls wait, the effect is equivalent to calling return. C. A synchronized method must not call wait; otherwise, the program cannot compile. D. After calling wait, the method should immediately calls notifyall. E. Calling wait is usually enclosed within a condition that is checked by a while block. 17

4.5 Thread in C++ using Qt Rewrite the following Java code to equivalent C++ code using Qt. Your code will replace /* here */. Please use Qt 4.0 or later. synchronized void deposit( int dep ) balance += dep; notifyall(); Answer: mutex.lock(); balance += dep; cond.wakeall(); mutex.unlock(); #include <QThread> #include <QMutex> #include <QWaitCondition> #include <cstdlib> #include <iostream> #include <ctime> using namespace std; QMutex mutex; QWaitCondition cond; class Account : public QThread public: int balance; Account() balance = 0; void deposit( int dep ) /* here */ void withdraw( int draw ) mutex.lock(); while ( balance < draw ) cond.wait( &mutex ); balance -= draw; mutex.unlock(); void run() ; Account sacct; class Depositor : public QThread 18

public: void run() int i = 0; while ( true ) int x = (int) ( rand() % 10 ); sacct.deposit( x ); if ( i++ % 100 == 0 ) cerr << "balance after deposits: " << sacct.balance << endl; ; class Withdrawer : public QThread public: ; void run() int i = 0; while ( true ) int x = (int) ( rand() % 10 ); sacct.withdraw( x ); if ( i++ % 100 == 0 ) cerr << "balance after withdrawals: " << sacct.balance << endl; 19

4.6 Threads and Object What is the output of this program? Answer: always 100 (the threads do not share objects) class DataObject int dataitem1; int dataitem2; DataObject() dataitem1 = 50; dataitem2 = 50; void itemswap() int x = (int) ( -4.999999 + Math.random() * 10 ); dataitem1 -= x; keepbusy(10); dataitem2 += x; void test() int sum = dataitem1 + dataitem2; System.out.println( sum ); public void keepbusy( int howlong ) long curr = System.currentTimeMillis(); while ( System.currentTimeMillis() < curr + howlong ) ; class RepeatedSwaps extends Thread DataObject dobj; RepeatedSwaps( DataObject d ) dobj = new DataObject(); start(); public void run( ) int i = 0; while ( i < 200 ) dobj.itemswap(); dobj.test(); i++; public class Swap public static void main( String[] args ) DataObject d = new DataObject(); new RepeatedSwaps( d ); 20

new RepeatedSwaps( d ); new RepeatedSwaps( d ); new RepeatedSwaps( d ); 21

Contents 1 Overloading and Overriding 1 1.1 Overloading in C++ and Java.................................... 1 1.2 Overloading in C++......................................... 1 1.3 Overloading in Java......................................... 2 1.4 Overloading in Java......................................... 2 1.5 Overloading and Overriding and Class Hierarchy in Java..................... 3 1.6 Overloading and Overriding..................................... 3 2 Operator Overloading 5 2.1 Operator Overloading and Object-Oriented Programming.................... 5 2.2 Operator Overloading in C++................................... 5 2.3 Operator Overloading as Global Functions............................. 6 2.4 Operator Overloading as Global and Member Functions..................... 7 2.5 Overloading Operator <<...................................... 9 2.6 Overloading Operator <<...................................... 10 3 Exception Handling 11 3.1 Exception in C++.......................................... 11 3.2 Exception in Java........................................... 11 3.3 Exception in Java........................................... 11 3.4 Exception in C++.......................................... 12 3.5 Exception in C++.......................................... 13 3.6 Exception in Java........................................... 14 4 Multiple Thread and Synchronization 15 4.1 Thread States in Java........................................ 15 4.2 Thread in Java............................................ 15 4.3 Thread Execution Order....................................... 16 4.4 Java wait-notify.......................................... 16 4.5 Thread in C++ using Qt...................................... 18 4.6 Threads and Object......................................... 20 22