





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!






CMSC 433 Exam 1: March 7, 2002
public class A { A() { System.out.println("A.A()"); } public void f(A arg) { System.out.println("A.f(A)"); } public void g(A arg) { System.out.println("A.g(A)"); } };
public class B extends A { B() { System.out.println("B.B()"); } public void f(B arg) { System.out.println("B.f(B)"); } public void g(A arg) { System.out.println("B.g(A)"); } public static void h(A arg) { System.out.println("B.h(A)"); } }
public class C extends B { C() { System.out.println("C.C()"); } public void f(A arg) { System.out.println("C.f(A)"); } public void f(B arg) { System.out.println("C.f(B)"); } public void g(B arg) { System.out.println("C.g(B)"); } public static void h(A arg) { System.out.println("C.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(); prints A.A() A ab = new B(); prints A.A() prints B.B() B bc = new C(); prints A.A() prints B.B() prints C.C() C c = new C(); prints A.A() prints B.B() prints C.C() a.f(bc); prints A.f(A) bc.h(c) prints C.h(a) bc.f(ab); prints C.f(A) c.g(a); prints B.g(A) c.g(bc); prints C.g(B) ab.h(ab); compile time error ab.g(ab); prints B.g(A)
class Front extends ArrayIndexOutOfBoundsException {}
public class ExceptionFunction { public static boolean change( int[] course , int index ) { try { System.out.println("Start change"); if (index < 0) { throw new Front(); } course[index] = 1; } catch (Front e) { course [-index] = 1; System.err.println("OutOfBounds: " + index); return false; } catch (ArrayIndexOutOfBoundsException e) { course[index - 7] = 1; System.err.println("OutOfBounds: " + index); return true; } finally {System.out.println("In Final Block"); } return false; }
public static void main( String args[] ) { try {int students[] = new int[5]; System.out.println("Main"); System.out.println(change( students, Integer.parseInt(args[0]))); System.out.println("After Change"); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Over Here"); } } }
What is the output of this program when run as follows?
java ExceptionFunction 2 Main Start change In Final Block false After Change java ExceptionFunction - Main Start change OutOfBounds: - In Final Block false After Change java ExceptionFunction 10 Main Start change OutOfBounds: 10 In Final Block true After Change java ExceptionFunction - Main Start change In Final Block Over Here
public class Bidder extends Thread {
String bidderName; Auction auction; int maxBid;
Bidder (String name, Auction a, int top) { bidderName = name; auction = a; maxBid = top; }
public void run () { int min; int currentBid = auction.getBid(); String currentBidder;
//YOU MUST WRITE THIS CODE // If this bidder has been outbid, must try to raise bid by 1 // up to maxBid }
Write the code for Auction’s setBid() and for Bidder’s run() methods. You may not change any code that is already written.
synchronized void setBid (int offered, String offeredBy) { //ADD YOUR CODE HERE // will accept bid if higher than currently offered // bidder can’t increase their own bid // if bid accepted, bidder thread should block until // higher bid is accepted or bidder wins auction boolean bidAccepted = false; if (offered > currentBid && offered > minBid && !highestBidder.equals(offeredBy)) { highestBidder = offeredBy; currentBid = offered; bidAccepted = true; System.out.println("Bid of " + currentBid + " accepted"); notifyAll(); try {wait();} catch (InterruptedException e) {}; } }
public void run () { int min; int currentBid = auction.getBid(); String currentBidder;
while (auction.biddingAllowed() && currentBid < maxBid) {
// ADD YOUR CODE HERE // If bidder has been outbid, will try to raise bid by 1 // up to maximum bid synchronized (auction) { min = auction.getMinBid(); currentBid = auction.getBid(); currentBidder = auction.getHighestBidder(); if (currentBid < maxBid) {auction.setBid(currentBid+1,bidderName);} } } }