Fall 2025 Midterm 2 Practice Problems

CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development

Author

Adriana Picoral

1 Questions

1.1 Access Modifiers

  1. What access modifier allows an attribute or method to be accessed from any class? ______________
  2. What access modifier restricts access to only within the same class? ______________
  3. What access modifier allows an attribute or method to be access within the same class and a child class only (no access from any other class)? ______________
  4. Which of the following access modifiers provides the most restrictive access?

         ▢ a) public

         ▢ b) protected

         ▢ c) private

  1. If a method is declared as protected in a parent class, can it be accessed by a subclass?

         ▢ a) No, never

         ▢ b) Yes, through inheritance

         ▢ c) Only if explicitly imported

  1. What happens if you try to access a private method from a subclass?

         ▢ a) It works fine

         ▢ b) Compilation error

         ▢ c) Runtime error

         ▢ d) The method becomes protected automatically

  1. Which statement about access modifiers is TRUE?

         ▢ a) protected is more restrictive than private

         ▢ b) private allows access from any class

         ▢ c) private methods cannot be access even within the same class

         ▢ d) public is the least restrictive access modifier

1.2 Java keywords

  1. What is the purpose of the extends keyword in Java?

         ▢ a) To create an interface

         ▢ b) To import packages

         ▢ c) To establish inheritance between classes

         ▢ d) To declare a method

  1. Which statement about the this keyword is correct?

         ▢ a) It refers to the current object instance

         ▢ b) It refers to the parent class

         ▢ c) It refers to a static variable

         ▢ d) It refers to the main method

  1. What does super() do when called in a constructor?

         ▢ a) Calls a method in the current class

         ▢ b) Creates a new object

         ▢ c) Deletes the parent class

         ▢ d) Calls the constructor of the parent class

  1. How many classes can a Java class extend using the extends keyword?

         ▢ a) None

         ▢ b) One

         ▢ c) Two

         ▢ d) Unlimited

  1. When must super() be called in a constructor?

         ▢ a) At the end of the constructor

         ▢ b) Anywhere in the constructor

         ▢ c) As the first statement in the constructor

         ▢ d) It’s optional and never required

  1. Which keyword is used to differentiate between instance variables and parameters with the same name?

         ▢ a) extends

         ▢ b) super

         ▢ c) this

         ▢ d) static

  1. What does super.method() do?

         ▢ a) Calls an overridden method from the parent class

         ▢ b) Calls a method from the current class

         ▢ c) Creates a new method

         ▢ d) Deletes a method

1.3 Output

  1. What is the output of this code?
public class Parent {
    protected int x = 10;
}
public class Child extends Parent {
    int x = 20;
    
    public void display() {
        System.out.println(super.x);
    }
}
public class RunStuff {
  
  public static void main(String[] args) {
    Child myChild = new Child();
    myChild.display();
  }
  
}
  1. What will this code output?
class Animal {
    Animal() {
        System.out.println("Animal created");
    }
}
class Dog extends Animal {
    Dog() {
        System.out.println("Dog created");
    }
}
public class Test {
    public static void main(String[] args) {
        Dog d = new Dog();
    }
}
  1. Consider the following code:
public class Midterm {
    public static int foo(int[] numbers) {
        int n = numbers.length;
        int a = 0;
        int b = 0;
        for (int i = 0; i < n; i++) {
            if ((numbers[i] % 10) == 0) {
                // tens
                a += numbers[i];
            } else {
                // not tens
                b += numbers[i];
            }
        }
        return b - a;
    }

    public static void main(String[] args) {
        System.out.println(foo(new int[]{1, 40, 2, 93, 70, 4, 1}));
    }
}

What would the code print? _____________________

1.4 Errors

  1. Identify the error:
class Parent {
    Parent(int x) {
        System.out.println(x);
    }
}
class Child extends Parent {
    Child() {
        System.out.println("Child");
    }
}

         ▢ a) No error

         ▢ b) extends keyword is wrong

         ▢ c) Missing super() call with argument in Child constructor

         ▢ d) Parent class cannot have a parameterized constructor

1.5 Complete the code

public class Fruit {
    /**
     * Create a new fruit. The boolean parameter
     * indicates if this fruit would go well in a
     * fruit salad (true) or would best be left out (false).
     */
    public Fruit(boolean goesInFruitSalad) {
      // unimportant code here.
    }
}

public class Tomato extends Fruit {
    /**
     * Create a new Tomato. As a general rule -- no tomato (ripe
     * or not) should ever go in a fruit salad. The boolean
     * parameter indicates if the tomato is ripe or not.
     */
    public Tomato(boolean ripe) {
        // SOMETHING is required here
    }
}

A line of code is needed to replace the // SOMETHING is required here and make the code compile. Pick the CORRECT line from below. Note – more than one line below will make the code compile, but only one matches the intent of the author as indicated by the comments on Tomato and Fruit classes.

         ▢ a) Fruit(true);

         ▢ b) new Fruit(false);

         ▢ c) new Fruit(true);

         ▢ d) super(false);

         ▢ e) super(true);

2 Key

2.1 Access Modifiers

  1. public
  2. private
  3. protected
    1. private
    1. Yes, through inheritance
    1. Compilation error
    1. public is the least restrictive access modifier

2.2 Java Keywords

    1. To establish inheritance between classes
    1. It refers to the current object instance
    1. Calls the constructor of the parent class
    1. One
    1. As the first statement in the constructor
    1. this
    1. Calls an overridden method from the parent class

2.3 Output

        10
        Animal Created
        Dog created
        -9

2.4 Errors

    1. Missing super() call with argument in Child constructor

2.5 Complete the code

    1. super(false);