midterm exam2 - sample questions (1)

.pdf

School

Miami University *

*We aren’t endorsed by this school

Course

271

Subject

Computer Science

Date

May 13, 2024

Type

pdf

Pages

7

Uploaded by DeaconKoala4263 on coursehero.com

These are meant to give you an example of some of the kinds of questions you might be asked on a midterm. Your exam may or may not have any of these questions on it. Your exam will definitely include other kinds of questions not found on this sample. Don't study for these particular questions. But this should give you an idea of the kinds of code you will be expected to write and read, concepts you should understand, and so on. I will not provide answers or check your work on these if you decide to practice them. 1. To over ride a superclass method in a subclass, the subclass method ____. a) Must use the same method name and the same parameter types. b) Must include a call to the super class method with the same name. c) Must use the same method name and different parameter types. d) Must use a different method name. e) None of the above 2. Consider the following code snippet: public class Motorcycle extends Vehicle { private String model; public Motorcycle(int numberAxles, String modelName) { super(numberAxles); model = modelName; } } What does the above code do? a) It calls a private method of the Vehicle class from within a method of the Motorcycle class. b) It calls a public method of the Vehicle class from within a method of the Motorcycle class. c) It calls the constructor of the Vehicle class from within the constructor of the Motorcycle class. d) It calls the constructor of the Motorcycle class from within the constructor of the Vehicle class. 3. Suppose the Auto class extends the Vehicle class, and both classes have an implementation of the public void moveForward(int) method. Now, in a separate tester class, you write the following: Vehicle myAuto = new Auto(); Which one of the following would allow you to call the moveForward() method in the Vehicle class? a) ((Vehicle)myAuto).moveForward(20); b) ((super)myAuto).moveForward(20); c) myAuto.super.moveForward(20); d) It is not possible to call the Vehicle moveForward method using the myAuto variable. Page 1 of 7
4. Which of the following will return a negative result? a) "aaazbb".compareTo("aaabbb"); b) "wxyz".compareTo("wx"); c) "abc".compareTo("abcde"); d) "efg".compareTo("def"); e) None of the above Suppose you have the following classes and interfaces, and that all classes have a no-parameter constructor defined. public interface Lyftable public class Vehicle public abstract class LandVehicle extends Vehicle Which of the following are valid? Circle Yes or No Statement Valid? (circle yes or no) LandVehicle v3 = new LandVehicle(); YES NO Lyftable c1 = new Cab(); YES NO Suppose that a Media class has several subclasses. One of the subclasses is Book . The Media class has a getTitle() method that returns the title as a String . None of the subclasses has its own getTitle() method defined. One of its subclasses, Book , has a getPages() method that returns the number of pages in the book as an int . The getPages() method only exists in the Book class, and not in the Media class and not in any of the other subclasses. Write a method named process() that has as its only parameter a Media array. The array may contain some null elements. The method should: Print the title of each non- null element in the array. Return the sum of all the pages of all the book objects that are in the array. Contain exactly one loop, and the loop should be a "for each" style loop. public int process(Media[] media) { } Page 2 of 7
Consider the definition for a Car . Complete/modify the definition for the FourWheelDriveCar class. A four wheel drive car is to be a child of the Car class. FourWheelDriveCar cars can be configured with heavy duty tires (or not). FourWheelDriveCar cars are 20% more costly to drive than a normal car if they are using regular tires and 25% more if they are using heavy duty tires. You should make changes to the Car class only if absolutely needed. Reuse existing code as much as possible. public class Car { private String make; private double mpg; public Car(String make, double mpg) { // code hidden from view } // Copy constructor public Car(Car c) { // code hidden from view } // returns the number of gallons needed to travel a specific distance public double gallonsForTrip(double dist) { return dist / mpg; } public double costOfTrip(double dist) { double wearAndTear = dist * 0.25; double gasCost = 2.45 * gallonsForTrip(dist); return gasCost + wearAndTear; } public String toString() { return make + " MPG: " + mpg; } public String getMake() { return make; } public double getMPG() { return mpg; } public boolean canGoToMaunaKea() { return false; } } // This is a subclass of Car public class FourWheelDriveCar { private boolean heavyDutyTires; public FourWheelDriveCar(boolean heavyTires, String make, double mpg){ } // CONTINUED ON NEXT PAGE… Page 3 of 7
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help