Computer Science : Object-Oriented Program Design

Study concepts, example questions & explanations for Computer Science

varsity tutors app store varsity tutors android store

Example Questions

Example Question #4 : Class Inheritance

True or False.

The class BetterMan inherits from the class Man.

 

public class BetterMan extends Man {

 

}

Possible Answers:

False

True

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 #3 : 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 implements Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

public class Square extends Rectangle {

     public Square(double s) {

         new Rectangle(s,s);

     }

}

public class Square extends Rectangle {

     public Square(double s) {

          super(s,s);

     }

}

public class Square extends Rectangle {

     private double side;

     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 #11 : Class Design

Which of the following is TRUE about the Object class?

Possible Answers:

Object is the only class that never has any descendants.

None of these answers are true.

Object inherits a minimum of four classes.

Object has one superclass, Entity.

Every class has all of Object's methods.

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 #4 : Class Inheritance

True or False.

The class BetterMan inherits from the class Man.

 

public class BetterMan extends Man {

 

}

Possible Answers:

False

True

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 #5 : Class Inheritance

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:

Child::show() called

 

Parent::show() called

Compiler Error

Runtime Error

Parent::show() called

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 #1 : Class Inheritance

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:

The program compiles but has a runtime error

Compilation error

The program runs and prints: "Pet eating"

The program runs and prints: "Cat eating"

None of these

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.

Example Question #1 : Program Design

Consider the code below:

private static class Philosopher {

        private String name;

        private String favoriteSubject;

        public Philosopher(String n, String f) {

                name = n;

                favoriteSubject = f;

        }      

        public String getName() {

                return name;

        }     

        public String getFavoriteSubject() {

                return favoriteSubject;

        }       

        public void speak() {

                System.out.println("Hello, World!  My name is "+name + ".  My favorite subject is "+favoriteSubject);

        }

}

private static class Nominalist extends Philosopher {

        boolean franciscan; 

        public Nominalist(String n,boolean frank) {

                super(n,"logic");

                franciscan = frank;

        }       

        public void speak() {

                super.speak();

                if(franciscan) {

                        System.out.println("I am a Franciscan");

                } else {

                        System.out.println("I am not a Franciscan");

                }

        }       

        public String whoMightHaveTaughtMe() {

                if(franciscan) {

                        return "Perhaps William of Ockham?....";

                } else {

                        return "Perhaps it was Durandus of St. Pourçain — scandalous, a Dominican nominalist!";

                }

        }

}

If you wished to make a toString() method for both Philosopher and Nominalist, using the same output as in each class' current speak() methods, what would be the best way to organize your code?

I. Have toString() invoke both classes' speak() methods.

II.  Have the  speak() method invoke the new toString() method.

III.  Create a new subclass and write the toString() method in that.

Possible Answers:

II

III

I

I and III

Correct answer:

II

Explanation:

The toString() method must return a string value.  Therefore, it makes the most sense that you would have this method generate the output that you currently generate in speak().  Then, you could have speak() invoke the toString() method to output the same string data.

Example Question #1 : Using Functional Decomposition

Given a class Thing and the code:

    Thing thingOne, thingTwo;

What function call is the following equivalent to?

    thingTwo = thingOne;

Possible Answers:

None of the above

operator=(thingTwo, thingOne);

thingOne uses the copy constructor of thingTwo

thingTwo.operator = (thingOne);

ostream& Thing::operator=(const Thing& rhs);

Correct answer:

thingTwo.operator = (thingOne);

Explanation:

What's given to us is that thingOne and thingTwo have already been created, this is a vital piece of information. Let's go through each of the choices

operator=(thingTwo, thingOne);

This line of code makes no sense and is syntactically wrong.

 

ostream& Thing::operator=(const Thing& rhs);

This line of code says to access the "operator=" method from the "Thing" class and put it onto the ostream, this has nothing to do with assigning thingOne to thingTwo.

When we come across the choice of the copy constructor, we have to keep in mind that the copy constructor is only used when an object use to to create another one, for example we have a given class called Foo:

Foo foo;

Foo foobar = foo;

In this case, we are using foo to create foobar.

 

The only choice that works is:

thingTwo.operator=(thingOne);

This line of code means that the thingOne object is being assigned to thingTwo.

 

Example Question #1 : Program Design

Consider the history of the following popular programming languages:

PHP

Java

Objective-C

Python

Which of the following is the closest ancestor shared by ALL of these languages?

Possible Answers:

Lisp

Smalltalk

C

Ada

Ruby

Correct answer:

C

Explanation:

All of these languages are C-based languages.

  • Ruby was invented in 1995, the same year as PHP, so it could not have influenced earlier languages like Objective-C and Python.
  • Lisp did influence at least one language, Python, but it did not influence any others.
  • Ada directly influenced Java, and because it influenced C, it can be argued that it is an ancestor of these other languages; however, because of this, Ada is not the CLOSEST ancestor.
  • Smalltalk influenced Objective-C, but no other languages on this list.

A clue was the answer "Objective-C," which is a strict superset of C that adds Object Orientation.

Example Question #2 : Program Design

What is the value of the string kitchen after the following code is run?

  1. class home
  2. {
  3. public:
  4. home(string);
  5. void searchhome();
  6. int buyhome();
  7. private:
  8. string kitchen();
  9. };
  10. home::home(string c)
  11. {
  12. kitchen=c;
  13. }
  14. int main()
  15. {
  16. str=’big’;
  17. home(str);
  18. }
Possible Answers:

str

'small'

'big'

c

void

Correct answer:

'big'

Explanation:

The constructor here in line 4 of the class definition is where it gets tricky. In the initialization of the constructor, we note that the input is a string.

Going down to line 10, to where the constructor function is defined, we see that a constructor with an input of c, which is defined as a string, will set the value of kitchen to c.

Finally, going down to our main code, we see that the value of the constructor in main is 'big', defined in str.

So kitchen='big'.

Learning Tools by Varsity Tutors