CMSC 131 (Summer 2019) Week 08 Study Questions Flashcards

1
Q

What is the output for the following program?

public class Nonsense {

   public static void foo(int x) {
      if (x == 2) {
         throw new NullPointerException();
      } else if (x == 3) {
         throw new NumberFormatException();
      }
      System.out.println("A");
   }
   public static void main(String[] args) {
      for (int i = 1; i <= 3; i++) {
         try {
            foo(i);
            System.out.println("B");
         } catch (NullPointerException e) {
            System.out.println("C");
         } finally {
            System.out.println("D");
         }
         System.out.println("E");
      }
      System.out.println("F");
   }
}
A
A
B
D
E
C
D
E
D
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Where can you use a “continue” statement?

A

Inside of any loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe how a continue statement behaves in a while loop.

A

Goes to top of loop immediately and checks the boolean condition to see if looping should continue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Describe how a continue statement works in a for-loop.

A

It will execute the statement that is usually processed at the end of an iteration through the loop, and then check the boolean condition to see if looping should continue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where can you use a “break” statement?

A

Either in a switch statement or in a loop. (We will learn about “switch” statements later.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe how a break statement behaves.

A

Causes the inner-most loop or switch statement to be exited immediately.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write some faulty code that generates a null-pointer exception, catch the exception immediately and print out “exception caught” in your catch block.

A

String s = null;
try {
int x = s.length(); // throws exception
} catch (NullPointerException e) {
System.out.println(“exception caught”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write some code that prompts the user for a numerical value, and reads their input into an int variable. Run the program, and try entering some text (like “cat”) instead of a number. Notice what kind of exception is thrown. Now modify your program so that it catches this exception, and instead of crashing the program, have it tell the user that he/she must enter a NUMBER, and then prompt them for input again.

A

Scanner s = new Scanner(System.in);

        boolean goodNumberEntered = false;

        while (true) {
           System.out.println("enter a number: ");
            try {
               int n = s.nextInt();
            } catch(java.util.InputMismatchException e) {
                s.nextLine();  //removes previous input from stream
                System.out.println("No, you MUST ENTER A NUMBER!");
                continue;
            }
            break;
        }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a method called smallSum that takes two int parameters, x and y. If the absolute value of the sum of the integers is more than 100, throw an ArithmeticException, passing the String “I don’t like big numbers” to the constructor of the exception. If the sum is less than 100, then return the sum. Write a quick driver to test out your method. After making sure everything works correctly, modify the driver so that it catches the exception and prints out the message that was passed to the exception’s constructor, but doesn’t crash the program.

A
public static int smallSum(int x, int y) {
    if (Math.abs(x + y) > 100) {
       throw new ArithmeticException("I don't like big numbers!");
    }
    return x + y;
}
public void static main(String[] args) {
   try {
      smallSum(50, 60);
   } catch (ArithmeticException e) {
      System.out.println(e.getMessage());
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the relationship between exception handling and the call stack. What happens if an exception is thrown but not caught anywhere in your program?

A

When an exception is thrown, Java looks for an appropriate catch block in the place where the problem occurred. If a catch is not found there, then Java discards the current frame on the call stack, and looks for an appropriate catch block in the previous frame. If none is found, that frame is discarded, etc. If the exception is not caught anywhere, then the program terminates, and the exception propagates to the “outside world”, which for us is the Eclipse IDE. Eclipse displays that red and blue error notification with stack trace in the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Under what circumstances will the finally block run?

A

As long as the “try” block has been entered, the finally block will run (no matter how you leave!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly