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

JAVA 1. What is output by the following code? (Assume there is a class NegativeN

ID: 3684833 • Letter: J

Question

JAVA

1.

What is output by the following code? (Assume there is a class NegativeNumberException defined in this package.) public static void main(String[] args) { try { int num = 99; sampleMethod(num); } catch(Exception ex) { System.out.print("CaughtMain"); } } public static void sampleMethod(int n) throws Exception { try { if(n>0) throw new Exception(); else if(n<0) throw new NegativeNumberException(); else System.out.print("Good"); System.out.print("InSample"); }catch(NegativeNumberException ex) { System.out.print("CaughtSample"); } finally { System.out.print("InFinal"); } System.out.print("AfterFinal"); } }

2. What is output if the following line in main is replaced?

int num = -99;

3.

What is output if the following line in main is replaced?

int num = 0;

4.

Use the code below to answer the next 5 questions. Which statements will execute if no exceptions are thrown?

            public static void main(String[] args) {

                        try {

                                    statement1;

                                    methodA();

                                    statement2;

                        } catch (ExceptionQ ex) {

                                    statement3;

                        } finally {

                                    statement4;

                        }

statement5;

            }

            public static void methodA() {

                        try {

                                    statemen6;

                                    methodB();

                                    statement7;

                        } catch (ExceptionR ex) {

                                    statement8;

                        } finally {

                                    statement9;     

                        }

                        statement10;     

            }

            public static void methodB() {

                        try {

                                    statement11;

                        } catch (ExceptionS ex) {

                                    statement12;

                        } catch (ExceptionT ex) {

                                    statement13;

                        } finally {

                                    statement14;

                        }

                        statement15;

            }

5.Which statements will execute if an exception of type ExceptionS is thrown before statement11 in methodB?

6.Which statements will execute if an exception of type ExceptionR is thrown before statement11 in methodB?

7.Which statements will execute if an exception of type ExceptionQ is thrown before statement11 in methodB?

8.Which statements will execute if an exception of type ExceptionC is thrown before statement11 in methodB?

9.

Write a method to read in two pieces of information from a user (using a Scanner object).

First, read in the name of a file. Using this name, create a file object to be returned from the method.

Use exception handling so that the code will compile.

Second, read in the number of lines in the file and store this in an int.

Use exception handling to account for the user typing a non-number.

If the user enters in a proper number, print it back out to the user before returning the file.

Note: you are not opening or reading from the file or checking the number- you are just creating the File object and returning it (presumably to be dealt with elsewhere).

Here is the method header- use this exact header with no changes:

public File readFileAndLength()

Explanation / Answer

1. Output: InFinalCaughtMain
2. Output: CaughtSampleInFinalAfterFinal
3. Output: GoodInSampleInFinalAfterFinal
4. Following statements will execute if no exceptions are thrown
   statement1
   statemen6
   statement11
   statement14
   statement15
   statement7
   statement9
   statement10
   statement2
   statement4
   statement5
  
5. statement1
   statemen6
   statement12
   statement14
   statement15
   statement7
   statement9
   statement10
   statement2
   statement4
   statement5
  
6.   statement1
   statemen6
   statement14
   statement15
   statement7
   statement9
   statement10
   statement2
   statement4
   statement5
  
7.   statement1
   statemen6
   statement14
   statement15
   statement7
   statement9
   statement10
   statement2
   statement4
   statement5
  
8.   statement1
   statemen6
   statement14
   statement15
   statement7
   statement9
   statement10
   statement2
   statement4
   statement5
  
9.   

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FileException {

   public File readFileAndLength(){
      
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter file name: ");
       String fileName = sc.next();
      
       File file = null;
       try{
          
           file = new File(fileName);
          
           if(!file.exists())
               throw new FileNotFoundException();
          
           // reading number of lines
           System.out.print("Enter number of lines: ");
           String line = sc.next();
          
           int lineNum = Integer.parseInt(line); // if user enter non-integer value, it will throw NumberFormatException
          
       }catch(NumberFormatException e){
           System.out.println("Please enter integer value");
       }
       catch(FileNotFoundException e){
           System.out.println("File does not exist");
       }
       finally{
           System.out.println("Please create a file ");
       }
      
       return file;
   }
}