exam 1
play

Exam 1 Average: 27.5 / 40 Exam 1 Grade Range: 14 - 39 Question - PDF document

Exam 1 Average: 27.5 / 40 Exam 1 Grade Range: 14 - 39 Question 1 Question 2 FileReader input = new FileReader(args[0]) All true A class with an abstract method must be Reader reads characters declared as abstract


  1. Exam 1 • Average: 27.5 / 40 Exam 1 • Grade Range: 14 - 39 Question 1 Question 2 • FileReader input = new FileReader(args[0]) • All true – A class with an abstract method must be – Reader reads characters declared as abstract – FileReader reads characters from a File – An abstract class cannot be instantiated. – The argument to FileReader is a filename • In this case the filename is the first argument found on the – A subclass of an abstract class may be command line instantiated if it overrides all abstract methods and provides a body for each java className foo bar moo 7 args foo bar moo 7 args[0] args[1] args[2] args[3] Question 3 Question 4 • Answer is (b) • Both false – The catch blocks are tested in turn, the first one that – A finally block will always execute regardless matches the subclass of Exception caught will be of whether the exception is caught or not executed • In the example, ArithmeticException is caught – A try block can have an unlimited number of • The first catch block is executed, the others are ignored catch clauses, not a maximum of 2. • “5” is printed – The finally block is always executed • “2” is printed – After the try/catch/finally is done, execution continues with next statement. • “1” is printed 1

  2. Question 5 Question 5 • Answer is (a) • Code will compile – C.max overrides B.max which overrides A.max A max(int x, int y) • This is good subclass behavior and is okay – Switching the arguments in B.max is okay since both arguments are ints B max(int x, int y) • May not run as expected but it will pass the compiler C max(int x, int y) Question 5 Question 5 • Let’s step through code • Let’s step through code – C c = new C(); – In B.max(23, 39) • return super.max (y, x) – 10; – c.max (13, 29); • call A.max (39, 23) • A.max (39, 23) will return 39 – In C.max(13, 29): • B.max(23, 39) will return 39 – 10 = 29 • return (super.max (x + 10, y + 10) • C.max(13, 29) returns 29 • “29” gets printed • Call B.max (23, 39) Question 6 Question 6 • In main • Answer is (c) – A ref1 = new C(); // okay since C is subclass of B A int f() { return 0;} // which is a subclass of A – B ref2 = (B)ref1; // Cast ok since C is subclass of B – print ref2.f(); – Ref2 is referencing an object of type C B int f() { return 1;} • Even though it is declared as class B – This is okay – Polymorphism • C.f() is called. C int f() { return 2;} • “2” is printed. 2

  3. Question 7 Question 7 • In main Mother identify() – Mother m = new Daughter(); – Daughter d = new Daughter(); – m.identify(); – d.identify(); Daughter identify() – Both m and d reference objects of type Daughter • This is okay since Daughter is a subclass of Mother • Polymorphism • Daughter.identify is called in both cases – Output • This is a Daughter speaking • This is a Daughter speaking Question 8 Question 9 • Could be interpreted as what happens • To get data from the keyboard during compile time or runtime: – You read from System.in – Compile time: – Gave 1 point if you gave correct instantiation of • Will cause compile error some InputStream. • Why: exception not caught and not declared • new FileInputStream(…) • Recall: – If method A calls method B and method B throws an exception, A can either: » Catch it » Declare that it can throw the same kind of Exception. Question 9 Question 10 • Could be interpreted as what happens • Reader/Writer during compile time or runtime: – Reads and writes characters – Runtime: • InputStream/OutputStream • Exception will be passed to the one who called the – Reads and writes bytes method • Passing up will continue until main is reached at which point the program will terminate. 3

  4. Question 11 Question 11 • Alternately, since Auto data is protected • Truck Class: class Truck extends Auto { class Truck extends Auto { private int towingWeight; private int towingWeight; public Truck (int year, int doors, int weight){ public Truck (int year, int doors, int weight){ super.year= year; super (year, doors); // must be first super.doors = doors; towingWeight = weight; towingWeight = weight; } } public String report () { public String report () { return (“Vehicle is a “ + getYear() + “ Truck return (“Vehicle is a “ + year + “ Truck “); “); } } } } Question 12 Question 13 • BufferedWriter output = new BufferedWriter ( • We subclass Exceptions new OutputStreamWriter ( – So that we can distinguish a specific exception new FileOutputStream (“myTest”))) condition -- Program writes character data (BufferedWriter) – Method catching exceptions can take different -- This data is buffered for eficiency (BufferedWriter) actions based on type of exception. -- Converted to bytes (OutputStreamWriter) -- written to file “myTest” (FileOutputStream) Question 14 Exam • The program will not compile • Any questions? – try block without a corresponding catch – The fact that f is private is okay • Since main is a method of X7, it can access its private members. 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend