Computer Science : Class Design

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

1 3 Next →

Example Question #1 : Class Inheritance

True or False.

The class BetterMan inherits from the class Man.

 

public class BetterMan extends Man {

 

}

Possible Answers:

True

False

Correct answer:

True

Explanation:

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

Example Question #1 : Class Inheritance

Consider the following code:

public class Rectangle {

     private double width, height;

     public Rectangle(double w,double h) {

          width = w;

          height = h;

     }

    

     public double getArea() {

          return width * height;

     }

    

     public double getPerimeter() {

          return 2 * width + 2 * height;

     }

}

 

public class Square {

     private double side;

     public Square(double s) {

          side = s;

     }

     public double getArea() {

          return side * side;

     }

     public double getPerimeter() {

          return 4 * side;

     }

}

Which of the following represents a redefinition of Square that utilizes the benefits of inheritance?

Possible Answers:

public class Square {

     private Rectangle r;

     public Square(double s) {

          r = new Rectangle(s,s);

     }

     public double getArea() {

          return r.getArea();

     }

     public double getPerimeter() {

          return r.getPerimeter();

     }

}

public class Square extends Rectangle {

     private double side;

     public Square(double s) {

          super(s,s);

     }

}

public class Square extends Rectangle {

     public Square(double s) {

         new Rectangle(s,s);

     }

}

public class Square implements Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

public class Square extends Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

Correct answer:

public class Square extends Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

Explanation:

We know that a square really is just a subclass of a rectangle, for it is merely a rectangle having four sides that are all equal. Using inheritance, you can very easily reuse much of your Rectangle code. First, you need to extend the Rectangle class:

public class Square extends Rectangle { . . .

Next, you can be rid of the field side. This allows you to alter the constructor for Square to call the Rectangle constructor. You do this using super (because Rectangle is the superclass).

After this, you can delete the getArea and getPerimeter methods, for they will be handled by the superclass. This gives you a very simple bit of code!

Example Question #1 : Class Inheritance

Which of the following is TRUE about the Object class?

Possible Answers:

None of these answers are true.

Object is the only class that never has any descendants.

Every class has all of Object's methods.

Object has one superclass, Entity.

Object inherits a minimum of four classes.

Correct answer:

Every class has all of Object's methods.

Explanation:

Object is the most basic class which all others, even user made ones, inherit. Therefore, all classes have Object's methods. A way to think of classes is to think of a tree: the Object class is the lowest node on the tree, where all other nodes can connect back to.

Example Question #1 : Class Inheritance

True or False.

The class BetterMan inherits from the class Man.

 

public class BetterMan extends Man {

 

}

Possible Answers:

True

False

Correct answer:

True

Explanation:

The class BetterMan inherits methods from the class man. The keyword "extends" means that BetterMan will get all the methods from Man plus be able to extend the class by adding its own methods. All methods from Man can be used in BetterMan by calling the keyword better.

Example Question #21 : Class Design

What does the code print?

class Parent{
    final public void show() {
       System.out.println("Parent::show() called");
    }
}
  
class Child extends Parent {
    public void show() {
       System.out.println("Child::show() called");
    }
}
  
public class Main {
    public static void main(String[] args) {
        Parent parent = new Child();
        parent.show();
    }
}
Possible Answers:

Parent::show() called

Child::show() called

Compiler Error

Parent::show() called

Runtime Error

Child::show() called

 

Correct answer:

Compiler Error

Explanation:

Final methods can't be overriden. So the code won't compile because of this. Now if the final modifier were to be removed. The code would compile and run and produce:

Child::show()

Example Question #301 : Computer Science

class Pet {
public:
    Pet() {}
    virtual void bar() {cout << "In pet bar(); }
};


class Cat : public Pet {
public:

    virtual void eat() {cout << "Cat eating"; }

    virtual void bar() {cout << "In Cat bar()"; }

};

Given the above classes, what would the result of:

int main(){

    Pet * petPtr = new Cat();

    petPtr -> eat();

}

Possible Answers:

Compilation error

None of these

The program runs and prints: "Pet eating"

The program runs and prints: "Cat eating"

The program compiles but has a runtime error

Correct answer:

Compilation error

Explanation:

When a child class inherits the properties of a parent class, some attributes are inherited and some aren't. In C++, the constructor of the parent class is not inherited. The Cat class does not have a constructor therefore the program will not compile.

1 3 Next →
Learning Tools by Varsity Tutors