ITSC 1213 FINAL EXAM QUESTIONS AND CORRECT
ANSWERS
If the code inside a try block throws a FileNotFoundException and an IOException,
which of the following are correct ways of handling this. Select all that apply:
a) Having one catch block that catches FileNotFoundException and declaring the
method to throw an IOException.
b) Having one catch block that cataches Exception
c) Having multiple catch blocks starting with IOException followed by an
FileNotFoundException
d) Having one catch block that catches IOException
e) Having multiple catch blocks starting with FileNotFoundException followed by an
IOException
f) Having one catch block that catches FileNotFoundException - Answers - a, b, c, d
Given a valid file and path which statement can be added inside the while loop to print
out the file content to the console
File file = new File("validFile.txt");
Scanner fileReader = new Scanner(file);
while(fileReader.hasNext()) {
//code here
}
a) System.out.printIn(fileReader.next());
b) System.out.printIn(fileReader.nextIn());
c) System.out.printIn(fileReader.nextLine());
d) System.out.printIn(fileReader.printIn()); - Answers - c
Given a document "students.txt" that contains the following line:
Name,Age,ID
martin,20,801
claire,26,802
dora,23,800
faye,18,803
and the following code that reads this file:
ArrayList<String> studentData = new ArrayList();
fileScanner = new Scanner(new File("students.txt"));
fileScanner.nextLine();
, while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] lineArray = line.split(",");
if (Integer.parseInt(lineArray[1]) >= 21 && lineArray[2] > 801) ) {
studentData.add(lineArray[0]);
}
}
What size will the ArrayList be after this code has executed?
a) 2
b) 3
c) 0
d) 1 - Answers - d
Given the following definitions:
public abstract class Manager extends Employee
public class Executive extends Manager
What kinds of classes are these, respectively?
a) Public, final
b) abstract, final
c) abstract, concrete
d) abstract, overridden - Answers - c
Abstract classes only become useful when they are:
a) polymorphic
b) a subclass of java.lang.Object
c) extended
d) instantiated - Answers - c
Abstract methods must be
a) extended
b) nonsense, Java doesn't allow abstract methods
c) overloaded
d) overridden - Answers - d
What is the minimum content that must be present in the body of an abstract method?
a) At least one executable statement
b) a valid implementation of the inherited method's functionality with a matching return
type
c) the body can be completely empty. Just the fact of the method's existence is
sufficient
ANSWERS
If the code inside a try block throws a FileNotFoundException and an IOException,
which of the following are correct ways of handling this. Select all that apply:
a) Having one catch block that catches FileNotFoundException and declaring the
method to throw an IOException.
b) Having one catch block that cataches Exception
c) Having multiple catch blocks starting with IOException followed by an
FileNotFoundException
d) Having one catch block that catches IOException
e) Having multiple catch blocks starting with FileNotFoundException followed by an
IOException
f) Having one catch block that catches FileNotFoundException - Answers - a, b, c, d
Given a valid file and path which statement can be added inside the while loop to print
out the file content to the console
File file = new File("validFile.txt");
Scanner fileReader = new Scanner(file);
while(fileReader.hasNext()) {
//code here
}
a) System.out.printIn(fileReader.next());
b) System.out.printIn(fileReader.nextIn());
c) System.out.printIn(fileReader.nextLine());
d) System.out.printIn(fileReader.printIn()); - Answers - c
Given a document "students.txt" that contains the following line:
Name,Age,ID
martin,20,801
claire,26,802
dora,23,800
faye,18,803
and the following code that reads this file:
ArrayList<String> studentData = new ArrayList();
fileScanner = new Scanner(new File("students.txt"));
fileScanner.nextLine();
, while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
String[] lineArray = line.split(",");
if (Integer.parseInt(lineArray[1]) >= 21 && lineArray[2] > 801) ) {
studentData.add(lineArray[0]);
}
}
What size will the ArrayList be after this code has executed?
a) 2
b) 3
c) 0
d) 1 - Answers - d
Given the following definitions:
public abstract class Manager extends Employee
public class Executive extends Manager
What kinds of classes are these, respectively?
a) Public, final
b) abstract, final
c) abstract, concrete
d) abstract, overridden - Answers - c
Abstract classes only become useful when they are:
a) polymorphic
b) a subclass of java.lang.Object
c) extended
d) instantiated - Answers - c
Abstract methods must be
a) extended
b) nonsense, Java doesn't allow abstract methods
c) overloaded
d) overridden - Answers - d
What is the minimum content that must be present in the body of an abstract method?
a) At least one executable statement
b) a valid implementation of the inherited method's functionality with a matching return
type
c) the body can be completely empty. Just the fact of the method's existence is
sufficient