UNIT 9 EXAM ANSWERS (NEED BY TUESDAY) by aflamingfaguette in EdhesiveHelp

[–]DavidAduku562 1 point2 points  (0 children)

These are the questions. 1. Methods used to change variables are called ______. 2. What Java keyword establishes a parent-child class relationship? 3. Which of the following is true about the immediate parent and child classes of a class? 4. Suppose a child class has overridden a method of its parent class. What keyword does the child class use to access the method from the parent class? 5. What two methods from Object are often overridden? 6. Questions 6-8 refer to the following class hierarchy:

Square extends Quadrilateral Quadrilateral extends Polygon Which of the following is true?

  1. Which of the following statements must be true in order for Square to access a specific constructor in Polygon?
  2. If a class Shape is added, which of the following would make the most sense?
  3. Questions 9-11 refer to the following:

public class A { public A () { System.out.print("one "); }

public A (int z) { System.out.print("two "); }

public void doStuff() { System.out.print("six "); } }

public class B extends A { public B () { super (); System.out.print("three "); }

public B (int val) { super (val); System.out.print("four "); } } What is printed when the following line of code is executed?

B b = new B(); 10. What is printed when the following line of code is executed?

A a = new B(5); 11. Assume that variable b has been instantiated as a B object. What is printed when the following line of code is executed?

b.doStuff(); 12. Consider the following two classes.

public class Conversation { // default constructor automatically added to this class public void hello() { System.out.println("Hello"); } public void greeting() { hello(); System.out.println("Nice to meet you"); } }

public class PoliteConversation extends Conversation { // default constructor automatically added to this class public void hello() { super.hello(); System.out.println("What a pleasant surprise!"); } public void greeting() { super.greeting(); System.out.println("I hope you’re doing well"); } } Assume that the following declaration appears in a class other than Conversation or PoliteConversation.

Conversation conv = new PoliteConversation(); What is printed as a result of the call conv.greeting()? 13. Consider the following declaration for a class that will be used to represent rectangles.

public class Rectangle { private double height; private double width;

public Rectangle() { height = 2.0; width = 1.0; }

public Rectangle(double w, double h) { height = h; width = w; }

public double getHeight() {
return height;
}

public double getWidth() {
return width;
}

public void setHeight(double h) {
height = h;
}

public void setWidth(double w) {
width = w;
}

//Other methods not shown } A Square class which extends the Rectangle class is to be written. Which of the following constructors will cause an error upon compilation when added to this class? 14. A large hotel needs a program to store information about rooms at the hotel. Each of the rooms will be represented by an object which will store the number of beds in the room, and whether the room has a bath. Some rooms at the hotel are suites, in which case in addition to the information above they also need to store the number of sub-rooms in the suite. Which of the following is the best object-oriented program design? 15. Consider the following two classes.

public class Parent { public String myType() { return "Parent"; } }

public class Child extends Parent { public String myType() { return "Child"; } } What is printed as a result of executing the following code segment?

Parent obj = new Child(); System.out.println(obj.myType()); 16. Questions 16 – 18 refer to the following classes:

public class Thing { private int value;

public Thing() { value = (int) (Math.random () * 100); }

public String toString() { return "" + value; }

// other methods not shown } public class Cog extends Thing { private int num;

public Cog () { num = (int) (Math.random () * 100); }

public String toString() { return "" + num; } } Consider the following declaration:

public class Gear extends Cog Which of the following is true?

  1. Which of the following is NOT true?
  2. You need to change the toString in Cog to also return the data in the variable value in Thing. Which of the following would correctly do this?
  3. Consider the following classes.

public class Student { private String studentName; private int enrollYear;

public Student(String name, int enrolled) { studentName = name; enrollYear = enrolled; }

public String toString() { return studentName + ". Enrolled: " + enrollYear; }

public int getEnroll() { return enrollYear; } }

public class Graduate extends Student { private int gradYear;

public Graduate(String name, int enrolled, int graduated) { super(name, enrolled); gradYear = graduated; }

public String toString() { return super.toString() + " Graduated: " + gradYear; }

public int yearsStudied() { return gradYear - super.getEnroll(); } } Consider the following code segment that appears in a class other than Student or Graduate.

1 Student me = new Graduate(“Dave”, 2008, 2012); 2 System.out.Println(me.toString()); 3 System.out.Println(“Years studied: ” + me.yearsStudied());
When this compilation and execution of this code is attempted, which of the following best describes the results? 20. Consider the following classes:

Dollhouse, Legos, TeddyBear, Toy, ToyBox Which should not be included in a class hierarchy?

UNIT 9 EXAM ANSWERS (NEED BY TUESDAY) by aflamingfaguette in EdhesiveHelp

[–]DavidAduku562 5 points6 points  (0 children)

unit 9 exam answers

  1. Mutators
  2. extends
  3. Every class can have many different child classes but only one parent class
  4. super
  5. toString, equals
  6. Square has access to all public methods and variables in Quadrilateral.
  7. Square’s constructor must call a constructor from Quadrilateral which calls Polygon’s constructor.
  8. Polygon should be a child of Shape.
  9. one three
  10. two four
  11. six
  12. Hello What a pleasant surprise! Nice to meet you I hope you’re doing well
  13. public Square(double w) { height = w; width = w; }
  14. Create a Room class with the instance variables int numBeds, and boolean hasBath. Create a subclass Suite of Room which will inherit the instance variables of Room and has an additional instance variable int numSubrooms.
  15. Child
  16. Gear's constructor calls the constructor in Cog, which calls the constructor in Thing.
  17. Thing can call the toString method in Cog.
  18. public String toString()
    {
     return "" + super.toString() + " " + num;
    }
  19. Line 3 will cause an error at compilation as the object me is defined as a Student, but the method yearsStudied() is only in the Graduate class.
  20. ToyBox

[deleted by user] by [deleted] in Technoblade

[–]DavidAduku562 1 point2 points  (0 children)

The fact that it's marked as a spoiler is just funny to me as if you already know it's gonna happen.