Fall 2025 Midterm 2 Practice Problems
CSCI 1913 – Introduction to Algorithms, Data Structures, and Program Development
1 Questions
1.1 Access Modifiers
- What access modifier allows an attribute or method to be accessed from any class? ______________
- What access modifier restricts access to only within the same class? ______________
- 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)? ______________
- Which of the following access modifiers provides the most restrictive access?
▢ a) public
▢ b) protected
▢ c) private
- 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
- 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
- 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
- What is the purpose of the
extendskeyword in Java?
▢ a) To create an interface
▢ b) To import packages
▢ c) To establish inheritance between classes
▢ d) To declare a method
- Which statement about the
thiskeyword 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
- 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
- How many classes can a Java class extend using the
extendskeyword?
▢ a) None
▢ b) One
▢ c) Two
▢ d) Unlimited
- 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
- Which keyword is used to differentiate between instance variables and parameters with the same name?
▢ a) extends
▢ b) super
▢ c) this
▢ d) static
- 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
- 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();
}
}
- 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();
}
}
- 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
- 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
- public
- private
- protected
- private
- Yes, through inheritance
- Compilation error
- public is the least restrictive access modifier
2.2 Java Keywords
- To establish inheritance between classes
- It refers to the current object instance
- Calls the constructor of the parent class
- One
- As the first statement in the constructor
- this
- Calls an overridden method from the parent class
2.3 Output
10
Animal Created
Dog created
-9
2.4 Errors
- Missing
super()call with argument in Child constructor
- Missing
2.5 Complete the code
- super(false);