Question

Given the following program, which statements are true? public class ExceptionLabExercise {     public static void...

Given the following program, which statements are true?

public class ExceptionLabExercise {

    public static void main(String[] args) {

        try {

            System.out.println(args[0]);

        } finally {

            System.out.println("FINALLY");

        }

    }

}

C. Given the following classes,

public class TestClass {

    public static void main(String[] args) throws A {

        try {

            f();

        } finally {

            System.out.println("Done.");

Test Stem / Question

Choices

4: Run the class above. What happens when you run with no arguments?

A: It printed finally then thrown ArrayIndexOutOfBoundsException

B: It printed ArrayIndexOutOfBoundsException

C: It printed finally

D: Nothing happend

5: If an ArrayIndexOutOfBoundsException occurred, what is the best way to handle this?

A: Check the length of the array before printing the arguments

B: Add a catch clause for the said exception

C: Always use finally clause in order to avoid exceptions

D: Nothing. That Exception needs not be caught.

} catch (Exception1 e) {

            throw e;

        }

    }

}

public class Exception1 extends Throwable{}

Test Stem / Question

Choices

6: What is the problem with the above code?

A: The finally statement must be preceeded by the catch clause

B: Exceptions cannot be extended

C: It is illegal to throw an exception

D: Nothing is wrong

7: What should be done with the code?

A: Move the catch clause before the finally clause.

B: Do not use Throwable as the supertype of Exception 1.

C: Do not throw the exception.

D: Nothing is wrong

8: What happens after you have been able to fix the code?

A: “Done” is printed and an exception is shown in the stack

B: “Done” is printed

C: Exception is shown in the stack

D: Nothing is wrong

public class JavaApplication1 {

    public static void main(String[] args) {

        RuntimeException exception = null;

        throw exception;

    }

}

Test Stem / Question

Choices

9: What is the problem with the above code?

A: null exception cannot be thrown

B: the main method must be declared with throws statement

C: It is illegal to throw an exception

D: Nothing is wrong

10: How should you fix the code? Choose from the following:

  1. Instantiate the exception
  2. Add a throws statement in the method declaration
  3. Do no throw the exception

A: A & B

B: A only

C: B only

D: All is correct

Fix the codes and show the correct answer

Homework Answers

Answer #1

Answer 4:C: It printed finally

as it throws exception and executes in finally block

Answer 5: A: Check the length of the array before printing the arguments

Answer 6:
A: The finally statement must be preceeded by the catch clause

Answer 7:
A: Move the catch clause before the finally clause.

Answer 8:
Done” is printed and an exception is shown in the stack

prints the stack trace because it is re throwing the exception

Answer 9:
A: null exception cannot be thrown

Answer 10:

Instantiate the exception

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
What will be the output of the given code? using System; class MyProgram { static void...
What will be the output of the given code? using System; class MyProgram { static void Main(string[] args) { try { int a, b; b = 0; a = 5 / b; Console.WriteLine("No exception will occur."); } catch (ArithmeticException e) { Console.WriteLine("Exception occurs."); } finally { Console.WriteLine("Program is executed."); } Console.ReadLine(); } } A Program is executed. B No exception will occur. C Exception occurs. Program is executed. D No exception will occur. Program is executed.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
What is the output of the following Java program? public class Food {     static int...
What is the output of the following Java program? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { s = flavor; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         pepper.setFlavor("spicy");         System.out.println(pepper.getFlavor());     } } Select one: a. sweet b. 1 c. The program does not compile. d. 2 e. spicy...
//What is the output? import java.util.HashMap; public class AirportCodes {    public static void main (String[]...
//What is the output? import java.util.HashMap; public class AirportCodes {    public static void main (String[] args) {       HashMap<String, String> airportCode = new HashMap<String, String>();       airportCode.put("GRX", "Granada, Spain");       airportCode.put("ALB", "Albany, USA");       airportCode.put("IWJ", "Iwami, Japan");       System.out.print("ALB: ");       System.out.println(airportCode.get("ALB"));       airportCode.put("IWJ", "Ivalo, Finland");       System.out.print("IWJ: ");       System.out.println(airportCode.get("IWJ"));    } }
Analyze this code and run it, if there are any issues note them and fix them,...
Analyze this code and run it, if there are any issues note them and fix them, if not give the output and Big O notation runtime: public class PrintBits { public static void printBits(int a) { try { String str = Integer.toBinaryString((Integer) a); for(int i = 0; i < str.length(); i++){ System.out.print (str.charAt(i)); } } catch (ClassCastException e) { throw new RuntimeException ("Argument is not an Integer"); } } public static void main (String[] args){ printBits (-17); System.out.println(); printBits (17);...
What is the output of the following Java program? interface Food {     public void printFlavor();...
What is the output of the following Java program? interface Food {     public void printFlavor(); } class Pepper implements Food {     public void printFlavor() { System.out.println("spicy"); } } public class Lunch {     public static void main(String[] args) {        Food pepper = new Pepper();         pepper.printFlavor();     } } Select one: a. spicy b. no output c. the program does not compile d. bland e. bland spicy
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT