DOC PREVIEW
Purdue ECE 462 - Midterm Exam 2

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 462 Midterm Exam 210:30-11:20AM, October 19, 20071 Overloading and Overriding1.1 Overloading in C++ and JavaWhich statement is correct?Answer: EA. 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 inoverloaded 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++Which statement is correct?Answer: AA. 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 cannotdecide 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.11.3 Overloading in JavaWhich statement is correct?Answer: AA. In Java, a program cannot compile if ambiguity occurs in deciding which version of an overloadedfunction to choose.B. In Java, a user can give “hint” during program execution to decide which version of an overloadedfunction 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, hereY 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 argu-ments have different types.1.4 Overloading in JavaWhat is the output of this program?Answer: Eclass 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. ()()()2C. (int)(int)(int)D. (int, int)(int, int)(int, int)E. ()(int)(int, int)1.5 Overloading and Overriding and Class Hierarchy in JavaWhat 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 OverridingWhat is the output of this program? You can write the steps how val is changed without doing thecalculation. For example, you can write 1 + 2 instead of 3. If the program cannot compile, write “cannotcompile”.3Answer: 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());}}42 Operator Overloading2.1 Operator Overloading and Object-Oriented ProgrammingWhich statement is correct?Answer: EA. 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 overloadedonly for user-defined classes.E. Operator overloading is not essential for object-oriented programming.2.2 Operator Overloading in C++Which statement is correct?Answer: CA. If operators < and == are overloaded, it is unnecessary to overload <=. Calling <= will be automat-ically 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.52.3 Operator Overloading as Global FunctionsWhich 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; .62.4 Operator Overloading as Global and Member FunctionsWhich 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


View Full Document

Purdue ECE 462 - Midterm Exam 2

Download Midterm Exam 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm Exam 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm Exam 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?