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

exam 1
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

Exam 1 Exam 1

  • Average: 27.5 / 40
  • Grade Range: 14 - 39

Question 1

  • All true

– A class with an abstract method must be declared as abstract – An abstract class cannot be instantiated. – A subclass of an abstract class may be instantiated if it overrides all abstract methods and provides a body for each

Question 2

  • FileReader input = new FileReader(args[0])

– Reader reads characters – FileReader reads characters from a File – The argument to FileReader is a filename

  • In this case the filename is the first argument found on the

command line java className foo bar moo 7

args

args[0] args[1] args[2] args[3]

foo bar moo 7

Question 3

  • Both false

– A finally block will always execute regardless

  • f whether the exception is caught or not

– A try block can have an unlimited number of catch clauses, not a maximum of 2.

Question 4

  • Answer is (b)

– The catch blocks are tested in turn, the first one that matches the subclass of Exception caught will be executed

  • In the example, ArithmeticException is caught
  • The first catch block is executed, the others are ignored
  • “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
slide-2
SLIDE 2

2

Question 5

  • Answer is (a)

A B C

max(int x, int y) max(int x, int y) max(int x, int y)

Question 5

  • Code will compile

– C.max overrides B.max which overrides A.max

  • This is good subclass behavior and is okay

– Switching the arguments in B.max is okay since both arguments are ints

  • May not run as expected but it will pass the

compiler

Question 5

  • Let’s step through code

– C c = new C(); – c.max (13, 29); – In C.max(13, 29):

  • return (super.max (x + 10, y + 10)
  • Call B.max (23, 39)

Question 5

  • Let’s step through code

– In B.max(23, 39)

  • return super.max (y, x) – 10;
  • call A.max (39, 23)
  • A.max (39, 23) will return 39
  • B.max(23, 39) will return 39 – 10 = 29
  • C.max(13, 29) returns 29
  • “29” gets printed

Question 6

  • Answer is (c)

A B C

int f() { return 0;} int f() { return 1;} int f() { return 2;}

Question 6

  • In main

– A ref1 = new C(); // okay since C is subclass of B // 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

  • Even though it is declared as class B

– This is okay – Polymorphism

  • C.f() is called.
  • “2” is printed.
slide-3
SLIDE 3

3

Question 7

Mother Daughter

identify() identify()

Question 7

  • In main

– Mother m = new Daughter(); – Daughter d = new Daughter(); – m.identify(); – d.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

  • To get data from the keyboard

– You read from System.in – Gave 1 point if you gave correct instantiation of some InputStream.

  • new FileInputStream(…)

Question 9

  • Could be interpreted as what happens

during compile time or runtime:

– Compile time:

  • Will cause compile error
  • Why: exception not caught and not declared
  • 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

  • Could be interpreted as what happens

during compile time or runtime:

– Runtime:

  • Exception will be passed to the one who called the

method

  • Passing up will continue until main is reached at

which point the program will terminate.

Question 10

  • Reader/Writer

– Reads and writes characters

  • InputStream/OutputStream

– Reads and writes bytes

slide-4
SLIDE 4

4

Question 11

  • Truck Class:

class Truck extends Auto { private int towingWeight; public Truck (int year, int doors, int weight){ super (year, doors); // must be first towingWeight = weight; } public String report () { return (“Vehicle is a “ + getYear() + “ Truck “); } }

Question 11

  • Alternately, since Auto data is protected

class Truck extends Auto { private int towingWeight; public Truck (int year, int doors, int weight){ super.year= year; super.doors = doors; towingWeight = weight; } public String report () { return (“Vehicle is a “ + year + “ Truck “); } }

Question 12

  • BufferedWriter output = new BufferedWriter (

new OutputStreamWriter ( new FileOutputStream (“myTest”)))

  • - Program writes character data (BufferedWriter)
  • - This data is buffered for eficiency (BufferedWriter)
  • - Converted to bytes (OutputStreamWriter)
  • - written to file “myTest” (FileOutputStream)

Question 13

  • We subclass Exceptions

– So that we can distinguish a specific exception condition – Method catching exceptions can take different actions based on type of exception.

Question 14

  • The program will not compile

– 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.

Exam

  • Any questions?