Which code fragment inserted at line ***, enables the code to compile?

Given the code fragment:

public class Base {

BufferedReader br;

String record;

public void process() throws FileNotFoundException {

br = new BufferedReader(new FileReader(“manual.txt”));

}

}

public class Derived extends Base {

// insert code here. Line ***

public static void main(String[] args) {

try {

new Derived().process();

} catch (Exception e) { }

}

}

Which code fragment inserted at line ***, enables the code to compile?

Given the code fragment:

public class Base {

BufferedReader br;

String record;

public void process() throws FileNotFoundException {

br = new BufferedReader(new FileReader(“manual.txt”));

}

}

public class Derived extends Base {

// insert code here. Line ***

public static void main(String[] args) {

try {

new Derived().process();

} catch (Exception e) { }

}

}

Which code fragment inserted at line ***, enables the code to compile?

A.
public void process () throws FileNotFoundException, IOException { super.process ();
while ((record = br.readLine()) !=null) {
System.out.println(record);
}
}

B.
public void process () throws IOException {
super.process ();
while ((record = br.readLine()) != null) {
System.out.println(record);
}
}

C.
public void process () throws Exception {
super.process ();
while ((record = br.readLine()) !=null) {
System.out.println(record);
}
}

D.
public void process (){
try {
super.process ();
while ((record = br.readLine()) !=null) {
System.out.println(record);
}
} catch (IOException | FileNotFoundException e) { }
}

E.
public void process (){
try {
super.process ();
while ((record = br.readLine()) !=null) {
System.out.println(record);
}
} catch (IOException e) {}
}

Explanation:
Incorrect answer:
D: exception java.io.FileNotFoundException has already been caught

Alternatives in a multi-catch statement cannot be related to subclassing Alternative java.io.FileNotFoundExceptionis a subclass of alternative java.io.IOException



Leave a Reply 7

Your email address will not be published. Required fields are marked *


ses

ses

A, B, E are correct. Tested with a file.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class Base {

BufferedReader br;
String record;

public void process() throws IOException {
br = new BufferedReader(new FileReader(“c:/manual.txt”));
}
}

public class Derived extends Base {

// insert code here. Line ***
public void process() {
try {
super.process();
while ((record = br.readLine()) != null) {
System.out.println(record);
}
} catch (IOException e) {
}
}

public static void main(String[] args) {
try {
new Derived().process();
} catch (Exception e) {
}
}
}

Elaine

Elaine

Only E is correct, in A, B subclass’s process()’s throws exceeds the scope of FileNotFoundException.

Charlie

Charlie

Agreed with Elaine.