CSIT 112: Fundamentals of Programming II - Midterm Review

CSIT 112: Fundamentals of Programming II Midterm Review

Sample Questions and Key Concepts

Question 1

  • Prompt: Which of the following messages passed to the String str could throw a StringIndexOutOfBoundsException?

    • str.replace('a', 'A')

    • str.length()

    • str.equals(str)

    • str.charAt(2)

Question 2

  • Prompt: If int[] x = new int[15]; and the statement x[-1] = 0; is executed, which of the following Exceptions is thrown?

    • ArithmeticException

    • NegativeArraySizeException

    • NullPointerException

    • ArrayIndexOutOfBoundsException

Question 3

  • Prompt: Consider values as an int array filled to capacity. What is the output of System.out.println(values[7]);?

    • Output 18

    • Output 7

    • Cause a syntax error

    • Cause an ArrayOutOfBoundsException to be thrown

Question 4 - 7

Class Definitions
  • Class A1:
    ```java
    public class A1 {
    public int x;
    private int y;
    protected int z;

    }

- **Class A2 extends A1**:  

java
public class A2 extends A1 {
protected int a;
private int b;

}

- **Class A3 extends A2**:  

java
public class A3 extends A2 {
private int q;

}

- **Q4:** Which lists are accessible in A3?  
  - `a, b, q`  
  - `x, a, q`  
  - `a, q`  
  - `x, z, a, q`  
- **Q5:** True statements about A1, A2, A3:  
  - A1 is a subclass of A2 and A2 is a subclass of A3  
  - A2 and A3 are both subclasses of A1  
  - A1 and A2 are both subclasses of A3  
  - A3 is a subclass of A2 and A2 is a subclass of A1  
- **Q6:** Accessibility of instance data `y` of class A1:  
  - Accessible in A1, A2 and A3  
  - Accessible in A1 and A2  
  - Accessible only in A3  
  - Accessible only in A1  
- **Q7:** Lists accessible in class A2:  
  - `x, y, z, a, b`  
  - `a, b`  
  - `z, a, b`  
  - `x, z, a, b`  

### Question 8
- **Array and Selection Sort:** How many passes will it take for Selection Sort to sort a given array?  
  - 4  
  - 7  
  - 5  
  - 6  

### Question 9
- **Array and Insertion Sort:** How many passes will it take for Insertion Sort to sort a given array?  
  - 4  
  - 7  
  - 5  
  - 6  

### Question 10
- **Given Class Implementation:** What is the output of executing the following class?  

java
public class Test {
public static void main(String[] args) {
int x = 5;
System.out.print(inc(x) + " " + x);
}
int inc(int x) {
return x + 1;
}
}

- **Answer:** 65  

### Question 11
- **Star Printing Example:** How many `*` will be printed by this program?  

java
for (j=0; j<5; j++) for (k=10; k>j; k--)
System.out.println("*");

- **Answer:** 40  

### Exception Handling Questions (12 - 14): 
#### Skeletal Code Reference:  

java
public static void main(String[] args) {
try {
ExceptionThrowerCode etc = new ExceptionThrowerCode();
etc.m1();
} catch (ArithmeticException ae) { … }
}
public class ExceptionThrowerCode {
public void m1() { … }
public void m3() {
try { … }
catch (ArithmeticException ae) { … }
}
}
```

  • Q12: If an ArithmeticException arises in m3, where is it caught?

    • caught in m3

    • caught in main

    • caught in m1

    • caught in m2

  • Q13: If a NullPointerException arises in m1, where is it caught?

    • caught in m3

    • caught in main

    • not caught, leading to program termination

    • caught in m1

    • caught in m2

  • Q14: If a NullPointerException arises in m3, where is it caught?

    • caught in m3

    • caught in main

    • not caught, leading to program termination

    • caught in m1

    • caught in m2

Question 15

  • Selection Sort After First Pass: Which of the following lists accurately shows the array after the first pass of the Selection Sort algorithm?

    • 9, 4, 12, 2, 6, 8, 18

    • 2, 4, 6, 8, 9, 12, 18

    • 2, 4, 12, 6, 8, 9, 18

    • 2, 4, 12, 9, 6, 8, 18

  • Insertion Sort Similarity: After the first pass through Insertion Sort algorithm, accurate list:

    • 2, 4, 9, 12, 6, 8, 18

    • 4, 9, 2, 12, 6, 8, 18

    • 9, 4, 12, 2, 6, 8, 18

    • 4, 9, 12, 2, 6, 8, 18

Final Outputs

  • Output of Example Programs:

  • If x = 0, a = 5, b = 5, what will x become after being executed with certain statements:

    • 0

    • 5

    • 2

    • 4

Comparable Implementations

  • Q19: To implement Comparable in a class, which method(s) must be defined?

    • both compares and equals

    • both lessThan and greaterThan

    • compareTo

    • compares

    • equals

Inheritance Concepts

  • Q20: What concepts does inheritance through an extended (derived) class support?

    • correctness

    • code reuse

    • information hiding

    • interfaces

Searching Algorithms

  • Q21: Which searching algorithm has better performance?

    • Linear search

    • Binary search

    • The performance of above two algorithms is equivalent

  • Q22: Which statement is true?

    • Binary search can be applied to all arrays

    • Binary search can only be applied to arrays sorted in increasing order

    • Binary search can only be applied to sorted arrays