



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Practice Exam
public class A { A() { System.out.println("A.A()"); } public void f(A) { System.out.println("A.f(A)"); } public void f(B) { System.out.println("A.f(B)"); } public void g(A) { System.out.println("A.g(A)"); } };
public class B extends A { B() { System.out.println("B.B()"); } public void f(A) { System.out.println("B.f(A)"); } public void f(B) { System.out.println("B.f(B)"); } public void g(B) { System.out.println("B.g(B)"); } public static void h(A) { System.out.println("B.h(A)"); } }
What are the effects of each of the following statements executed in sequence (e.g., prints xxx, causes a run-time exception to be thrown, gives a compile-time error, etc.)?
A a = new A(); B b = new B(); A ab = new B(); a.f(ab); a.g(b); b.f(ab); b.g(a); b.h(ab); ab.f(a); ab.h(a); ab.f(ab);
class L1Exception extends Exception { public L1Exception() {} public L1Exception(String msg) { super(msg); } }
class L2Exception extends L1Exception { public L2Exception() {} public L2Exception(String msg) { super(msg); } }
public class ExceptionTest{ public static void f(int x) throws L1Exception,L2Exception { if (x > 0) { throw new L1Exception(); } else { throw new L2Exception(); } }
public static void g(int y) throws L2Exception,L1Exception { if (y <= 0) { throw new L1Exception(); } else { throw new L2Exception(); } }
public static void main(String[] args) throws L1Exception, L2Exception { int a = Integer.parseInt(args[0]), b=Integer.parseInt(args[1]); try { f(a); g(b); } catch (L1Exception e) { System.out.println(e); }
try { g(b); } catch(L2Exception e) { System.out.println(e); } finally { if (a <= 0) f(a); System.out.println("quitting"); } }
class CubbyHole {
class Semaphore { boolean filled; Semaphore (boolean b) { filled = b; } }
private int seq[]; private Semaphore available[]; private int numBins;
CubbyHole(int i) { numBins = i; seq = new int [numBins]; available = new Semaphore [numBins]; for (int j = 0; j < i; j++) available[j] = new Semaphore (false); }
public int get(int bin) { // Write the code that goes here }
public void put(int value,int bin) { // Write the code that goes here }
class Producer extends Thread { private CubbyHole cubbyhole;
public Producer(CubbyHole c) { cubbyhole = c; }
public void run() { int i,j; while (true) { i = getValue(); // gets value from user j = chooseBin(); // gets buffer ID from user (0..numBins-1) cubbyhole.put(i,j); System.out.println(i + " " + j); } } }
class Consumer extends Thread { private CubbyHole cubbyhole;
public Consumer(CubbyHole c) { cubbyhole = c; }
public void run() { int value, bin; while (true) { bin = chooseBin(); // gets buffer ID from user (range 0..numBins-1) value = cubbyhole.get(bin); System.out.println(bin + " " + value);
} } }
class ProducerConsumerTest { public static int numBins = 10; public static void main(String args[]) { CubbyHole c = new CubbyHole(numBins); Producer p1 = new Producer(c); Consumer c1 = new Consumer(c); p1.start(); c1.start(); }