Select the correct answer. (1) Which of the following will open a file named MyF
ID: 3821108 • Letter: S
Question
Select the correct answer.
(1) Which of the following will open a file named MyFile.txt and allow you to read data from it?
(a) File file = new File("MyFile.txt");
(b) FileWriter inputFile = new FileWriter();
(c) File file = new File("MyFile.txt");
FileReader inputFile = new FileReader(file);
(d) FileWriter inputFile = new FileWriter("MyFile.txt");
(2) How many times will the following do - while loop be executed?
int x = 11;
do {
x += 20;
} while (x > 100);
(a) 0 (b) 4 (c) 5 (d) 1 (e) 2
(3) Examine the following code:
Scanner keyboard = new Scanner(System.in);
int studentGrade = 0;
int totalOfStudentGrades = 0;
while(studentGrade != -1) {
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
totalOfStudentGrades += studentGrade;
}
In the loop header: while(studentGrade != -1) , what is the purpose
of " -1 " ?
(a) It initializes the count of the number of grades.
(b) It is a sentinel.
(c) It tells the program to give the student a grade of -1 , if he does not have any grades.
(d) If the grade is not -1 , the while statement will not be executed.
(4) You can use this method to determine whether a file exists.
(a) the File class's canOpen method
(b) the File class's exists method
(c) the Scanner class's exists method
(d) the FileWriter class's fileExists method
(5) Which line(s) below opens MyFile.txt allows to append data to its existing contents?
(a) File fwriter = new File("MyFile.txt");
FileWriter outFile = new FileWriter(fwriter, true);
(b) FileWriter outfile = new FileWriter(true, "MyFile.txt");
(c) File fwriter = new File("MyFile.txt");
FileWriter outFile = new FileWriter(fwriter);
(d) File outfile = new File("MyFile.txt", true);
(6) A(n) ________ is an object that is generated in memory as the result of an error or an unexpected event.
(a) default exception handler (b) error message
(c) exception handler (d) exception
(7) In a try / catch construct, after the catch statement is executed ________ .
(a) the program returns to the statement following the statement in which the exception occurred
(b) the program resumes at the statement that immediately follows the try / catch construct
(c) the program terminates
(d) the program resumes at the first statement of the try statement
(8) An exception’s default error message can be retrieved using this method.
(a) getErrorMessage() (b) getDefaultErrorMessage()
(c) getMessage() (d) getDefaultMessage()
(9) In the following code, assume that inputFile references a Scanner object that has been successfully used to open a file:
double totalIncome = 0.0;
while (inputFile.hasNext())
{
try
{
totalIncome += inputFile.nextDouble();
}
catch(InputMismatchException e)
{
System.out.println("Non-numeric data encountered in the file.");
inputFile.nextLine();
}
finally
{
totalIncome = 35.5;
}
}
What will be the value of totalIncome after the following values are read from the file?
2.5
8.5
3.0
5.5
abc
1.0
(a) 35.5 (b) 75.0 (c) 0.0 (d) 19.5
(10) Why does the following code cause a compiler error?
try
{
number = Integer.parseInt(str);
}
catch (IllegalArgumentException e)
{
System.out.println("Bad number format.");
}
catch (NumberFormatException e)
{
System.out.println(str + " is not a number.");
}
(a) Because you can have only one catch clause in a try statement.
(b) Because the Integer.parseInt method does not throw a NumberFormatException.
(c) Because the Integer.parseInt method does not throw an IllegalArgumentException.
(d) Because NumberFormatException inherits from
IllegalArgumentException. The code should handle
NumberFormatException before IllegalArgumentException.
Explanation / Answer
Question 1
Answer:
(c) File file = new File("MyFile.txt");
FileReader inputFile = new FileReader(file);
Question 2
Answer:
(d) 1
Question 3
Answer: (b) It is a sentinel.
Question 4
Answer: (b) the File class's exists method
Question 5
Answer:
(a) File fwriter = new File("MyFile.txt");
FileWriter outFile = new FileWriter(fwriter, true);
Question 6
Answer: (d) exception
Question 7
Answer:
(b) the program resumes at the statement that immediately follows the try / catch construct
Question 8
Answer:
(c) getMessage()
Question 9
Answer:
(a) 35.5
Question 10
Answer:
(d) Because NumberFormatException inherits from
IllegalArgumentException. The code should handle
NumberFormatException before IllegalArgumentException.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.