CMPS 258 Final, Spring 2005

American University of Beirut · Programming Languages · 15 pages

All CMPS 258 files
CMPS 258 Final, Spring 2005, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 258 Final, Spring 2005, page 2 CMPS 258 Final, Spring 2005, page 3 CMPS 258 Final, Spring 2005, page 4 CMPS 258 Final, Spring 2005, page 5 CMPS 258 Final, Spring 2005, page 6 CMPS 258 Final, Spring 2005, page 7 CMPS 258 Final, Spring 2005, page 8 CMPS 258 Final, Spring 2005, page 9 CMPS 258 Final, Spring 2005, page 10

Showing the first 10 of 15 pages. Download the PDF to see all of it.

More CMPS 258 files

Show the text of this final exam
Final exam CMPS 258 \ . Spring 2005 OF aeiRyt Duration: 90 minutes etl The following questions are regarding the C and C++ languages unless specified otherwise: 01. Which of the function declarations below cannot appear in the same source file along with the following declaration? int foo(int x); (l1pt) {a) int foo(int x, int y); (b) double foo(int y); (c) double foo(double z); (d) all of the above (e) none of the above 02. In what way C++ differs from Java when it comes to binding of messages to methods? (2pts) 03. (a) (b) (c) (d) (e) 04. {a) (b) (c) (da) os. (a) (b) (¢) (d) 06. (a) (b) (c} (d) {e) What makes an operator binary? (1 pt) it produces a result that can be stored in binary it requires two operands it is composed of two characters it has two different meanings depending on where it is used it requires a boolean operand Which of the following the keyword const cannot be used to indicate? (1 pt) that a member function cannot change any of the member variables that a variable cannot be changed that a formal parameter cannot be changed none of the above In C++, given y = 3, the expression (4 > y > 1) (1 pt) evaluates to true evaluates to false will not compile throws an exception Which of the function declarations below would not compile? (2 pts) int fool(int x = 'A', int y = 2); int foo2(int x, int y = 2); int foo3(int x = 'A', int y = 'B'); int food(int x = 1, int y); none of the above 07. What is the output displayed by this code: (2 pts) bool b = falsa; if (b == true); { } if (b «= false); { } Goodbye Hello Hello Goodbye nothing is displayed cout << “ Hello *; cout << “ Goodbye “; . What is the output of the following C++ code? (1 pt) x= 1; int x 2; { aoneeme int x 3; oe a { fo YA gEN. int x = 4; fe ! v cout << x << endl; :~ LIBRARY a\ 4 t { H \ } ‘, vA } “SPP ne f L (a) acne (b) (c) (d) (@) compiler error 09. What is the output of the following Java code? (1pt) int x = 1; { int x = 2; { int x = 3; { int x = 4; System.out.println ("x") ; (c) 1 2 3 4 {e) compiler error 10. What is the difference, if any, between a subclass and a subtype: a) In Java? (2 pts) b) In C++? (2 pts) 11. If you need to compare the type names of 2 objects in Java you would call getClass () .getName () on the objects. How would you do the same in C++? (1pt) 12. Provide the missing code in MyClass so that the following code compiles and prints out the word “Hello”. Explain this behavior. ( 4pts) class MyClass { public: char *data; }; void foo(MyClass obj) { printf ("%s", obj.data); } void main() { char strf{] = “Hello”; foo(str) ; spe eee AY 13. The function foo does not compile. Explain why, clearly. (3pts) class MyClass { public: int x; const int foo() const { K++; return x; } }i 14, Given the following code: (3pts) Pati pee MyClass awn ee ey ” \ public: {3 ip rye us} MyClass() { x = 1; } LIB tA R ! ! int x; . Jf int & bar() OF agignt { ene return x; } }; void main{) { MyClass obj; obj.bar() = 2; a) At the end of main, the value of x is 1 b) At the end of main, the value of x is 2 c) At the end of main, the value of x is ‘junk’ d) The code does not compile 15. What is the output of this code? (6 pts) an class Bl OR { i ; public: [yp Ay, B1l(){ data = 1; } . a Bl(int data){ this->data = data; } No / f yf int data; NL Chimt he eens class B2 { public: B2(){ data = 2; } B2(B2 &b2) { } int data; }e class A { public: A(int datal, int data2) { bl.data = datal; b2.data = data2; } Bl bl; B2 b2; hi void main() { A al(100, 200); A a2(al); A a3 = al; cout << a2.bl.data << " " «<< a2.b2.data << " " «< a3.bl.data << " " << a3.b2.data << endl; } a) 100 200 100 200 b) 100 200 1 2 c) 1 2 100 200 d) 100 junk 100 junk e) compiler error 16. The following code does not compile. Describe and explain any error reported by the compiler.(2 pts) class Vehicle {}; class Plane: public Vehicle {}; void pilot (Vehicle av) {} void fly(Plane sp) {} void main() { Vehicle v; Plane p; pilot(v); // line pilot(p); // line fly(v); // line fly(p); // line PWN 17. The following code does not compile. Describe and explain any error reported by the compiler. (2 pts) class Vehicle {}; class Plane: private Vehicle {}; void pilot (Vehicle &v) {} void fly(Plane &p) {} void main() { Vehicle v; Plane p; pilot(v); // line pilot(p); // line fly(v); // line fly(p); // line BwWNe 18. When an object gets deleted in C++, its destructor gets called. Is there an equivalent concept in Java? Expand. (2 pts) 19. Do you see any potential problem in this code? If yes, provide a fix. (4 pts) class Array { public: Array(int *arr, int len) { // assume len > 0 and arr points to data = new int[len]; // allocated memory >= len*sizeof (int) for (int i = 0; i < len; i++) datafi] = arrfil; } Array(int i) { data = new int; *data = i; } ~Array() { delete [ ] data; } private: int *data; }; 20. Given the C++ code (5pts) class A { public: A() { a = new int [3]; for {(imt i = 0; i < 3; i++) a[i] } ~A() { delete[] a; } " He private int *a; void init(A &x) { Ay: x = yi } int main() { AD; init(p); you might notice that as the program runs following the call init (p), the values stored in p.a start changing unpredictably. a) What might be happening? b) Explain how to fix this problem and provide a modified code. 21. Complete this C++ program so that it will produce the output shown below. (15 pts) Output: Eagle: can fly, cannot swim Robin: can fly, cannot swim Owl: can fly, cannot swim Crow: can fly, cannot swim Woodpecker: can fly, cannot swim Ostrich: cannot fly, cannot swim Kiwi: cannot fly, cannot swim Swan: can fly, can swim Duck: can fly, can swim Penguin: cannot fly, can swim class Bird { string name; public: Bird( ) { } void printInfo( ) { cout << name << ": "; if (canFly( )) cout << "can fly, "; nie else ot ves DN cout << "cannot fly, "; a . 7 \ if (canSwim( )) f oe ‘ cout << “can swim"; : i else / cout << "cannot swim"; 4 cout << endl; } }; class Flightiess_Bird: { public: Flightless Bird( ) { } }i class Swimming Bird: { public: Swimming _Bird( ) { } hi class Flightless Swimming_Bird: { public: int main( ) { Bird *bird[10] = { new Bird("Eagle"), new Bird("Robin"), new Bird("Owl"), new Bird("Crow"), new Bird("Woodpecker") new Flightless_Bird("Ostrich"), new Flightless _Bird("Kiwi"), new Swimming Bird("Swan"), new Swimming _Bird("Duck"), new Flightless Swimming Bird("Penguin") }e for (int k=0; k<10; k++) bird[(k]->printInfo( }; 22. Consider the following C++ program: class Node { public: virtual int trace( ) { return 0; } }; class Leaf: public Node { int value; public: Leaf{int i): value(i) { } virtual int trace( ) { cout << "Leaf " << value << endl; return value; } }; class NonLeaf: public Node { Node *left, *right; public: NonLeaf (Node *a, Node *b): left(a), right{(b) { } virtual int trace( ) { int isleft->trace( ); int j=right->trace( ); int k=fun(i,j); cout << k << endl; return k; } virtual int fun(int, int) { return 0; } MM es Add: public NonLeaf { public: Add(Node *a, Node *b): NonLeaf(a,b) { } virtual int fun(int a, int b) { cout << "Add "; return a+b; } }; class Subtract: public NonLeaf { public: Subtract (Node *a, Node *b): NonLeaf(a,b) { } virtual int fun(int a, int b) { cout << "Subtract "; return a-b; } }i class Multiply: public NonLeaf { public: Multiply (Node *a, Node *b): NonLeaf(a,b) { } virtual int fun(int a, int b) { cout << "Multiply "; return a*b; } }i