chap 9

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given the definitions of class A and class B class A { public int i; public int j; public A() { i = 1; j = 2; } } class B extends A { int a; public B() { super(); } } what is the output of this code B obj = new B(); System.out.println(obj.i + " " + obj.j);

1 2

Which of the following is the best example of a Superclass / Subclass relationship?

Fruit / Banana

Given the class definitions of Vector2D and Vector3D below: public class Vector2D { private int x; private int y; public Vector2D() {} public Vector2D(int x,int y) { this.x = x; this.y = y; } // other code } public class Vector3D extends Vector2D { public int z; // other code } Which of the constructors that follow would be valid to put in the Vector3D class? Possible constructors for Vector3D: I. public Vector3D() {} II. public Vector3D(int x, int y, int z) { super(x,y); this.z = z; } III. public Vector3D(int x, int y) { super.x = x; super.y = y; this.z = 0; }

I and II

Given the following class declarations public class Company { /* Implementation not shown */ } public class OnlineCompany extends Company { /* Implementation not shown */ } public class StartUp extends OnlineCompany { /* Implementation not shown */ } In another class, the following static method appears: public static void printName(OnlineCompany company) { /* Implementation not shown */ } Which of the following objects can be passed as a parameter so that the code compiles and runs? I. OnlineCompany abc = new StartUp(); II. StartUp abc = new StartUp(); III. Company abc = new OnlineCompany();

I and II only

Consider the following class declarations public class Student { public void printSchool() { System.out.println("City Schools"); } } public class HSStudent extends Student { public void schoolName() { System.out.println("City High"); } } public class MSStudent extends Student { public void printSchool() { System.out.println("City Middle"); } } Which of the following will print City Schools? I. Student jackson = new Student(); jackson.printSchool(); II. HSStudent jackson = new HSStudent(); jackson.printSchool(); III. MSStudent jackson = new MSStudent(); jackson.printSchool();

I, II only

Consider the following class declarations public class Car { /* Implementation Not Shown */ } public class ElectricCar extends Car { /* Implementation Not Shown */ } The following line of code appears in a test class: ArrayList<Car> cars = new ArrayList<Car>(); Which of the following objects can be added to the ArrayList? I. Car leaf = new ElectricCar(); II. Car mustang = new Car(); III. ElectricCar model3 = new ElectricCar();

I, II, III

public class BaseballTeam { private String name; private String location; public BaseballTeam(String name, String location) { this.name = name; this.location = location; } public boolean equals(Object other) { if (other == null) { return false; } BaseballTeam team = (BaseballTeam) other; if (name.equals(team.name) && location.equals(team.location)) { return true; } return false; } } If 4 BaseballTeam objects are created as such: BaseballTeam one = new BaseballTeam("Athletics", "Oakland"); BaseballTeam two = new BaseballTeam("Athletics", "Kansas City"); BaseballTeam three = new BaseballTeam("Athletics", "Oakland"); BaseballTeam four = two; Which of the following will print true? I. System.out.println(one.equals(three)); II. System.out.println(two.equals(four)); III. System.out.println(one == three); IV. System.out.println(two == four);

I, II, and IV Only

Consider the following class declarations public class Park { private String name; public Park(String name) { this.name = name; } public String toString() { return name + " park"; } } public class Field extends Park { private String type; public Field(String name, String type) { super(name); this.type = type; } public String toString() { return super.toString() + " with a " + type + " field"; } }

II and III Only

Given the following code: public class Animal { private String type; public Animal (String type) { this.type = type; } } public class Dog extends Animal { private String color; /** Constructor Goes Here **/ } Which of the following could be the constructor for the Dog class? I. public Dog(String color) { super.type = "Dog"; this.color = color; } II. public Dog(String color) { super("Dog"); this.color = color; } III. public Dog(String type, String color) { super(type); this.color = color; }

II and III only

Given the following code: public int addNumbers(int one, int two) Which method will correctly override this method when placed in a subclass? I. public int addNumbers(int one, int two, int three) II. public int addNumbers(int one, int two) III. public int addNumbers(int a, int b) IV. public int addNumbers()

II and III only

Given the following class structure, which class is on the top of the hierarchy? public class Grandparent { /* Implementation not shown */ } public class Parent extends Grandparent { /* Implementation not shown */ } public class Child extends Parent { /* Implementation not shown */ } public class GrandChild extends Child { /* Implementation not shown */ }

Object

The Java Classes Skeleton, Spider, and Zombie all extend the Java Class Monster. The Monster Class is defined below. public class Monster { private String name; private String type; private int x; private int y; public Monster(String name, String type) { this.name = name; this.type = type; this.x = 0; this.y = 0; } public void move(int x, int y) { this.x = x; this.y = y; } } What variables and methods do the Skeleton, Spider, and Zombie Classes inherit?

Only the method move

Consider the following class declarations public class Parent { public void name() { System.out.println("Parent"); } public void age() { System.out.println("Old"); } } public class Child extends Parent { public void name() { System.out.println("Child"); } public void age() { System.out.println("Young"); } public void grade() { System.out.println("10"); } } Which of the following statements will cause a compile-time error?

Parent person = new Child(); person.grade();

Based on this code snippet public class Shape { public String getShapeName() { return "Shape"; } } public class Rectangle extends Shape { public String getShapeName() { return "Rectangle"; } } public class Square extends Rectangle {} public class Oval extends Shape { public String getShapeName() { return "Oval"; } } public class Circle extends Oval { public String getShapeName() { return "Circle"; } } what does this program output? Shape shape1 = new Shape(); Shape shape2 = new Rectangle(); Shape shape3 = new Square(); Shape shape4 = new Circle(); System.out.println(shape1.getShapeName()); System.out.println(shape2.getShapeName()); System.out.println(shape3.getShapeName()); System.out.println(shape4.getShapeName());

Shape Rectangle Rectangle Circle

Given the following class declarations public class Team { private String name; private String location; public Team(String name, String location) { this.name = name; this.location = location; } public String getName() { return name; } public String getLocation() { return location; } } public class BaseballTeam extends Team { private String name; private String location; public BaseballTeam(String name, String location) { super(name, location); } public String toString() { return super.getLocation() + " " + super.getName(); } }

Team dodgers = new BaseballTeam("Dodgers", "Los Angeles"); System.out.println(dodgers);

Given the following code: public class Car { private String make; public Car (String make) { this.make = make; } public Car() { make = "Unspecified"; } } public class Tesla extends Car { private String color; public Tesla (String color) { this.color = color; } } public static void main(String[] args) { Tesla newCar = new Tesla("blue"); } Which of the following best describes what will happen when the code is executed?

This code will create a superclass object by calling the no-argument Car constructor.

Given the following two classes public class Parent { public void printName(String name){ System.out.println(name + " is a parent"); } } public class Child extends Parent { public void printName(String childName){ System.out.println(childName + " is a child"); } } Does the Child printName method successfully override the Parent method?

Yes, the method is overridden correctly.

Given the following code: public class Calculator { /* Constructor and methods omitted */ public double multiply(double num1, double num2){ return num1 * num2; } } public class ScientificCalculator extends Calculator { /* Constructor and methods omitted */ public double square(double number) { /* Missing Code */ } } What goes in the missing code if the desired return is a square of the number?

return super.multiply(number, number);

Given the following code: public class Tree { private boolean evergreen; /* Constructor and methods omitted */ public String toString() { String line = "This tree is "; if (!evergreen) line += "not "; return line + "an evergreen"; } } public class FruitTree extends Tree { private String fruit; /* Constructor and methods omitted */ public String toString() { /* Missing Code */ } } What goes in the missing code if the desired return is the following (as an example): This Tree is not an evergreen fruit tree

return super.toString() + " fruit tree";

Given the following code: public class Tree { private boolean evergreen; public Tree(boolean evergreen) { this.evergreen = evergreen; } } public class FruitTree extends Tree { private String fruit; public FruitTree (String fruit, boolean evergreen) { /* Missing Code */ } } What is the correct implementation for the subclass constructor?

super(evergreen); this.fruit = fruit;


Ensembles d'études connexes

PSYC 260 Chapter 12: Stereotypes and Discrimination

View Set

Java - I/O (Input/Output), Binary, RAF, Object Serialization - Code / Coding Examples

View Set

Exam 3 Test Bank: Mgmt. of pts. w/ musculoskeletal disorders

View Set

Lecture 6 - secuirty Operations and Aministration

View Set

Pediatric Growth and Development, Nursing Care of Children Health Promotion and Maintenance

View Set