Quiz J5-3 | CS 2113 Software Engineering - Spring 2021

Quiz J5-3

Submit your answers via this Google Form

Question #1

Say we want to write a sumArray function that takes a user’s input for how
many numbers in an array to sum. Which implementations below have proper error
checking and will NOT cause the program to stop?

    // Option 1
    public static int sumArray(int[] array){
        int sum = 0;
        Scanner scanner = new Scanner(System.in);
        int arrSize = scanner.nextInt();
        for(int i =0; i < arrSize; i++)
            sum += array[i];
        return sum;
    }
    // Option 2
    public static int sumArray(int[] array){
        int sum = 0;
        Scanner scanner = new Scanner(System.in);
        int arrSize = scanner.nextInt();
        try{
            for (int i = 0; i < arrSize; i++)
                sum += array[i];
        } catch (Error e) {
            e.printStackTrace();
        }
        return sum;
    }
    // Option 3 (Assume proper error checking wherever this function is called)
    public static int sumArray(int[] array) throws Exception{
        int sum = 0;
        Scanner scanner = new Scanner(System.in);
        int arrSize = scanner.nextInt();
        for (int i = 0; i < arrSize; i++)
            sum += array[i];
            
        return sum;
    }
    // Option 4
    public static int sumArray(int[] array){
        int sum = 0;
        try{
            Scanner scanner = new Scanner(System.in);
            int arrSize = scanner.nextInt();
            for (int i = 0; i < arrSize; i++)
                sum += array[i];
        } catch (Error e) {
            e.printStackTrace();
        }
        return sum;
    }
    // Option 5
    public static int sumArray(int[] array){
        int sum = 0;
        try{
            Scanner scanner = new Scanner(System.in);
            int arrSize = scanner.nextInt();
            for (int i = 0; i < arrSize; i++)
                sum += array[i];
        } catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        } catch (InputMismatchException e) {
            e.printStackTrace();
        }
        return sum;
    }

Question 2

Consider the program below with multiple errors:

    public static void main(final String args[]) {
        List<Integer> some_numbers = new ArrayList<Integer>();
        some_numbers.add(1);
        some_numbers.add(100);
        try {
            int a = some_numbers.get(2) / 0;
        } catch (ArithmeticException e) {
            System.out.println("First");
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Second");
        } catch(Exception e){
            System.out.println("Third");
        }
    }

Which error is caught first?

Question 3

Consider our naive implementation of a string subsequence function below:

    public static String subsequence(int start, int end, String string) throws Throwable{
        String subsequence = "";
        try{
            for(int i = start; i <= end; i++)
                subsequence += string.charAt(i);
        }catch(ArrayIndexOutOfBoundsException e){
            throw new Throwable();
        }catch(NullPointerException e){
        }
        return subsequence;
    }
    public static void main(final String args[]) {
        String str = "Subsequence";
        try{
            str = subsequence(3, 100, str);
        }catch (Throwable t){
            //Don't do anything
        }
        System.out.println(str);
    }

What’s the output?

Question 4

Assume that subsequence is identical to the function in question #3, what’s
the output of the below program?

    public static void main(final String args[]) {
        String str = null;
        try{
            str = subsequence(3, 100, str);
        }catch (Throwable t){
            //Don't do anything
        }
        System.out.println(str);
    }