Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Select the correct answer. 1) Which of the following will open a file named MyFi

ID: 3807197 • 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

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.

Explanation / Answer

1) Answer c) File file = new File("MyFile.txt");                   

                  FileReader inputFile = new FileReader(file);

File file = new File() open a file

and FileReader is used to read the file

2)Answer: D) 1

the do while loop executes 1 times. do while loop check termination condition at end of the loop.

here, x=11 after first loop of do while loop x=11+20=31 is less than 100 therefore condition is wrong therefore loop terminated and do while loop executes only 1 time.

3) Answer: C) It tells the program to give the student a grade of -1, if he does not have any grades.

while loop check condition at beginning. in above code if student enters -1 loop terminates.

4) Answer: B) The File class's exists method.

   

above exists() method is used check if file exists

    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote