Docsity

Practice Exam 3 Solutions - Programming Languages | CS 403, Exams of Programming Languages

Toggle sidebar
bg1
CS 403/503 Exam 3 Fall 2007 Solution
CS 403 has built-in extra credit up to 120 points. CS 503 will be prorated to 100-point scale.
1. Rewrite the following C++ program as an equivalent object-oriented Java program.
Use good object-oriented style, including encapsulation, constructors, methods,
inheritance, late method binding, polymorphism, information hiding, code reuse, etc.
[48 points]
#include <iostream>
#include <cstring>
using namespace std;
enum E {Y, Z};
struct X { E tag; int i; char c[8];
union { int j; char d[8]; };
};
int F (X & w) {
if (w.tag != Y) return w.i;
else return w.i * w.j;
}
char * G (X & w) {
if (w.tag != Z) return w.c;
else return strcat (w.c, w.d); // string catenation
}
int main( ) {
X arr[2];
arr[0].tag = Y;
arr[0].i = 2;
strcpy (arr[0].c, "ab"); // copy "ab" into arr[0].c
arr[0].j = 3;
arr[1].tag = Z;
arr[1].i = 7;
strcpy (arr[1].c, "cd");
strcpy (arr[1].d, "ef");
cout << F(arr[0]) << " " << G(arr[0]) << endl;
cout << F(arr[1]) << " " << G(arr[1]) << endl;
}
Output:
6 ab
7 cdef
pf3
pf4
pf5

Partial preview of the text

Download Practice Exam 3 Solutions - Programming Languages | CS 403 and more Exams Programming Languages in PDF only on Docsity!

CS 403/503 Exam 3 Fall 2007 Solution

CS 403 has built-in extra credit up to 120 points. CS 503 will be prorated to 100-point scale.

  1. Rewrite the following C++ program as an equivalent object-oriented Java program. Use good object-oriented style, including encapsulation, constructors, methods, inheritance, late method binding, polymorphism, information hiding, code reuse, etc. [48 points]

#include #include using namespace std;

enum E {Y, Z};

struct X { E tag; int i; char c[8]; union { int j; char d[8]; }; };

int F (X & w) { if (w.tag != Y) return w.i; else return w.i * w.j; }

char * G (X & w) { if (w.tag != Z) return w.c; else return strcat (w.c, w.d); // string catenation }

int main( ) { X arr[2]; arr[0].tag = Y; arr[0].i = 2; strcpy (arr[0].c, "ab"); // copy "ab" into arr[0].c arr[0].j = 3;

arr[1].tag = Z; arr[1].i = 7; strcpy (arr[1].c, "cd"); strcpy (arr[1].d, "ef");

cout << F(arr[0]) << " " << G(arr[0]) << endl; cout << F(arr[1]) << " " << G(arr[1]) << endl; } Output: 6 ab 7 cdef

class X { protected int i; protected String c; public X (int ii, String cc) { i=ii; c=cc; } public int F ( ) { return i; } public String G ( ) { return c; } public void print ( ) { System.out.println (F( ) + " " + G( )); } }

class Y extends X { private int j; public Y (int ii, String cc, int jj) { super (ii, cc); j = jj; } public int F ( ) { return ij; } }*

class Z extends X { private String d; public Z (int ii, String cc, String dd) { super (ii, cc); d=dd; } public String G ( ) { return c+d; } }

class MAIN { public static void main (String[ ] args) { X[ ] arr = new X[2]; arr[0] = new Y (2, "ab", 3); arr[1] = new Z (7, "cd", "ef"); arr[ 0 ].print( ); arr[1].print( ); } }

  1. This problem is about memory management using the heap.

a. Write C++ code that causes a dangling pointer and a memory leak. Show where the dangling pointer and the memory leak occur. [12 points]

**int p = new int(23); p = new int(99); // memory leak: the block containing 23 is now inaccessible delete p; cout << p; // dangling pointer: the block addressed by p containing 99 was deleted

b. Select any one of these three garbage collection algorithms: mark-and-sweep algorithm, copying algorithm, or reference counting algorithm. Explain how this algorithm works. [12 points]

See pages 265-266 in textbook.

  1. Write the output of this Java program in the indicated box. [24 points] class A extends Exception { } class B extends A { } class C extends B { } class D extends A { } class E extends D { }

class MAIN { static void F (boolean x) throws A { try { System.out.println(1); G(x); System.out.println(2); } catch (B b) { System.out.println(3); } catch (D d) { System.out.println(4); } catch (A a) { System.out.println(5); } finally { System.out.println(6); if (x) throw new E( ); System.out.println(7); } } static void G (boolean y) throws A { try { System.out.println(8); H(y); System.out.println(9); } catch (E e) { System.out.println(10); } catch (D d) { System.out.println(11); } catch (A a) { System.out.println(12); } finally { System.out.println(13); if (y) throw new C( ); System.out.println(14); } } static void H (boolean z) throws A { try { System.out.println(15); if (z) throw new B( ); System.out.println(16); } catch (C c) { System.out.println(17); } catch (B b) { System.out.println(18); } catch (A a) { System.out.println(19); } finally { System.out.println(20); if (z) throw new D( ); System.out.println(21); } } public static void main (String[ ] args) { try { System.out.println(22); F(true); System.out.println(23); } catch (D d) { System.out.println(24); } catch (B b) { System.out.println(25); } catch (A a) { System.out.println(26); } finally { System.out.println(27); } } }

Output: