1 Which of the following will open a file named MyFiletxt an
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
3) Look at 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.
Solution
1)Ans)C
Code to open the file:
File file = new File(\"MyFile.txt\");
FileReader inputFile = new FileReader(file);
2)Ans)D
Reason:
the value of x is 11
As 11 is less than 100 the condition(while (x>100) will fail)
but as we are using do...while() loop .the condition checks will
be performed after the loop has been executed for atleast one time.
3)Ans)B
Reason:
until the user entered the value -1 this will loop will continue to
execute.That means we are using -1 as sentinel
4)Ans)B
Reason:
to check whether the file exist or not we have to use
file class exists method
5)Ans)A
Reason:To append the data at the end of the file.We have to use
File fwriter = new File(\"MyFile.txt\");
FileWriter outFile = new FileWriter(fwriter, true);
6)ans)D
Reason:
When ever the execption is raised in the try block
an exception object will be created by jvm and the information regarding the
exception will be stored inside the exception class object
and throws it out of the try block.Then it will be handled by the
catch block if we provided.If catch block is not provided
then it will be handled by the default excetional handler
present in jvm.
7)ans)B
Reason:
When the exception is raised.JVM stops executing the code inside the try block.JVM creates the exception object and throws it out
of the try block.which will be caught by the catch block.
8)Ans)C
Reason:we can use getMessage() method to retrive the default error message
9)Ans)A
Reason:when the execption is raised.Jvm stops executing code inside the
try block.Then that exception will be handled by the catch block.
Even if the exception is there or not the finally block will get executed.
10)Ans)D
Reason:We have to provide the more specific type of exceptions first followed by the
generic type of exception.



