(KEY) Practice Quiz 0
(KEY) Practice Quiz 0
Grading:
● There are three problems. Each problem will receive a single E/S/N grade.
● Minor syntax errors will be ignored as long as it is unambiguous what was intended (e.g. forgetting a semicolon,
misspelling a variable name where there is only one close option). Major syntax errors, or errors where it is
unclear what was intended, may have an impact on your grade.
Advice:
● Read all questions carefully. Be sure you understand the question before you begin your answer.
● The questions are not necessarily in order of difficulty. Be sure you at least attempt every question.
● Write clearly and legibly. We cannot award credit for answers we cannot read.
● If you have questions, raise your hand to ask. The worst that can happen is we will say "I can’t answer that."
● Ask questions as soon as you have them. Do not wait until you have several questions.
Initial here to indicate you have read and agreed to these rules:
1. Comparable
(Note that since there are more than four requirements, some of your four tests will need to achieve more than
one requirement.)
For example, suppose the variable list contains the following values:
[1, 2, 3, 4, 5]
Then, after the call list.addAll(2, nums), the variable list would contain:
Notice how the values from nums have been inserted into list beginning at index 2, with existing values
being shifted to the right.
Below is a buggy implementation of addAll. Annotate (write on) the code to correct the method. You may add,
delete, or move code. You may not call any other methods from the ArrayIntList class in your
implementation, and you may not create any additional data structures.
size += values.length;
}
3. Inheritance
Part A: Tracing
Consider the following class implementations.
public class Vehicle { public class HondaCivic extends Car {
public void honk() { public void honk() {
System.out.println("honk"); super.honk();
} System.out.println("hoooonkkkk");
} }
}
public class Car extends Vehicle {
public class Harley extends Motorcycle {
} public void wheelie() {
wheelie(5);
public class Motorcycle extends Vehicle { }
public void wheelie(int n) { }
System.out.print("W");
for (int i = 0; i < n; i++) {
System.out.print("e");
}
System.out.println("!");
}
}
Now consider the following code. For each line, place an X next to the correct outcome. If the code runs
without error, also indicate what output is printed by filling in the blank. If it produces no output, write "none". If
the output includes multiple lines, you may indicate line breaks with a slash character "/" in the output.
Each box should be considered independently.
Run-time error
public class Dog extends Animal { public class Cat extends Animal {
public Dog(String name) { public Cat(String name) {
super(name); super(name);
} }
public void speak() { public void speak() {
super.sayName(); System.out.print("Meow, says ");
System.out.println(" says woof!"); super.sayName();
System.out.println("!");
} }
} }