DOC PREVIEW
Purdue ECE 462 - Final Exam

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 41 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 41 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 41 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 41 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 41 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 41 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 41 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 41 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 41 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ECE 462 Final Exam15:20-17:20, December 15, 20071 Overloading and Overriding1.1 Overloading in JavaWhat is the output of this program? If the program cannot compile, write “cannot compile”.Answer: cannot compileclass Base {public void foo() {System.out.println( "Bf1" );}public void foo( int i ) {System.out.println( "Bf2" );}}class Derived extends Base {public void foo() {System.out.println( "Df1" );}public void foo( int i, int j ) {System.out.println( "Df2" );}}class J3Q1 {public static void main( String[] args ){Base d = new Derived();d.foo();d.foo( 3 );d.foo( 3, 4 );}}11.2 Overloading and Overriding and Class Hierarchy in C++Which line of this program causes compile-time error? If there are multiple answers, you need to answeronly one. If the program has no compile-time error, write the program’s output.Answer: Line 7 In member function ‘void Base::bar(int)’: 7 : error: no matching function for call to‘Base::foo(int&)’1 #include <iostream>2 using namespace std;3 class Base {4 public:5 Base() { }6 virtual void bar() { foo(); }7 void bar(int x) { foo(x); }8 virtual void foo() { cout <<"Bf1"<< endl;}9 };10 class Derived : public Base {11 public:12 Derived() { }13 ~Derived(){}14 virtual void foo() { cout << "Df1" << endl; }15 virtual void foo(int x) { cout <<"Df2"<< endl;}16 };17 int main() {18 Derived* p = new Derived;19 p->bar();20 delete p;21 return 0;22 }21.3 Overloading and OverridingWhat is the output of this program? If the program cannot compile, write “cannot compile”.Answer: Dfclass Base {public Base() {}public void foo(){System.out.println("Bf" );}public void bar() { foo(); }}class Derived extends Base {public void foo() {System.out.println( "Df" );}public Derived() {}}class J3Q3 {public static void main( String[] args ) {Base p = new Derived();p.bar();}}31.4 Overloading in C++ and JavaWhich statement is correct?Answer: A, B, C, or D (or any combination of them)A. In C++, if a function is overloaded, it can still be overridden in a derived class.B. In Java, if a function is overloaded, it can still be overridden in a derived class.C. Overloaded functions in C++ can use primitive types or programmer-defined classes.D. All above.E. In Java, overloaded functions can not use objects as parameters.1.5 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.41.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”.Answer: 7 (= 4 + 3). The value is modified in this sequence: val = 1, val = 5, val += 6, val = 4, val +=3, val = 7class MyBaseClass {protected int val;public MyBaseClass() { val = 1; }public void foo() { val += 2; }public void foo(int i) { val += 3; }public void foo(String str) { val = 4; }public int getVal() { return val; }}class MyDerivedClass extends MyBaseClass {public MyDerivedClass () { val = 5; }public void foo() { val += 6; }}class J3Q6 {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());}}52 Template Classes and the STL Library in C++2.1 Template in C++Which statement is correct?Answer: AA. A stack can be efficiently implemented using a list.B. A list can be efficiently implemented using a vector.C. A vector can be efficiently implemented using a queue.D. A list can be efficiently implemented using a set.E. A set can be efficiently implemented using a stack.2.2 Container and IteratorReplace /* here */ by declaring (and defining) an iterator.Answer: ListIterator iter = animals.listIterator();import java.util.*;class J5Q5 {public static void main( String[] args ){List<String> animals = new ArrayList<String>();animals.add( "cheetah" );animals.add( "lion" );animals.add( "cat" );animals.add( "fox" );animals.add( "cat" );/* here */ /* ---------- */while ( iter.hasNext() ) {System.out.println( iter.next() );/* output: cheetah lion cat fox cat */}}}62.3 C++ Set and Class HierarchyWhat is the output of this program? If the program cannot compile, write “cannot compile”.Answer: B0 D1 B2 (different orders acceptable, no duplicates)#include <iostream>#include <string>#include <set>using namespace std;class BaseC{protected:int b_val;public:BaseC(int val): b_val(val) {}virtual void print() { cout << "B" << b_val << endl; }};class DerivedC: public BaseC{public:DerivedC(int val): BaseC(val) {}virtual void print() { cout << "D" << b_val << endl; }};int main(){set<BaseC*> bset;BaseC * bobj[3];bobj[0] = new BaseC(0);bobj[1] = new DerivedC(1);bobj[2] = new BaseC(2);bset.insert(bobj[0]);bset.insert(bobj[1]);bset.insert(bobj[2]);bset.insert(bobj[2]);bset.insert(bobj[1]);bset.insert(bobj[0]);typedef set<BaseC*>::const_iterator CI;for (CI iter = bset.begin();iter != bset.end();iter++){(* iter) -> print();}return 0;}72.4 C++ TemplateWhat is the output of this program? If the program cannot compile, write “cannot compile”.Answer: 15 h ece462 6.5#include <iostream>#include <string>using namespace std;template <class Txxx, class T2> class Container2{public:Container2(Txxx t1in, T2 t2in): c2_t1(t1in), c2_t2(t2in){ /* nothing */ }Txxx getT1(void) { return c2_t1; }T2 getT2(void) { return c2_t2; }private:Txxx c2_t1;T2 c2_t2;};int main(void){Container2<int, char> obj1(15, ’h’);cout << obj1.getT1() << " " << obj1.getT2() << endl;Container2<string, float> obj2("ece462", 6.5);cout << obj2.getT1() << " " << obj2.getT2() << endl;return 0;}82.5 Template in C++Which statement is correct?Answer: EA. A template can be used in built-in container classes only. A programmer cannot create a class witha template.B. A template can be replaced by a primitive type (int, char, double ...) only. C++ does not allowreplacing a template by a class.C. A template can be replaced by a class only. C++ does not allow replacing a template by a primitivetype.D. A class can have only one


View Full Document

Purdue ECE 462 - Final Exam

Download Final Exam
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 Final Exam 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 Final Exam 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?