OOP Final Exam Answers
OOP Final Exam Answers
I. Basic Notions
Objectives: The aim of the question is to evaluate your knowledge and skills concerning
with the basic concepts of OOP.
B- Fill in the blanks with the correct answer: [4 Marks, 1 Mark each]
1. A variable known only within the method in which it is declared is called a(n) local
variable.
2. Classes from which objects can be instantiated are called concrete classes.
3. It is possible to have several methods with the same name that each operate on different
types or numbers of arguments. This feature is called method overloading.
4. Methods in a class that do not provide implementations must be declared using keyword
abstract.
C- State whether each of the following is true or false. If a statement is false, explain why. [4
Marks, 1 Mark each]
a. Base class constructors are not inherited by derived classes. False; they are inherited
b. A has-a relationship is implemented via inheritance. False; Is-A not Has-A
c. A Car class has is-a relationships with the Steering_Wheel and Brakes classes. False;
Has-A not Is-A
d. When a derived class redefines a base class method by using the same signature and
return type, the derived class method is said to overload that base class method. False; it's
said to override.
Study the following class diagram, then wrote its corresponding c# code.
using System;
using System.Collections.Generic;
using System.Text;
return string.Format("{0},{1},{2}",base.ToString(),
side1,side2);
}
public override double area()
{
return side1 * side2;
}
class TestShape
{
static void Main(string[] args)
{
rectangle r = new rectangle();
circle c = new circle();
Console.WriteLine( c.ToString());
}
}
Question 3: [8 Marks]
Study the following class, and then answer the questions below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Animal
{
protected double weight;
protected double height;
protected int x_coordiantion;
protected int y_coordination;
}
}
class cat:Animal
{
private string name;
a.talk();
a.walk();
a.drink_milk();
}
The three classes above have some errors . find these erorrs and correct them. [8 marks]
(at least four errors)
1- public abstract void talk() // should be virtual
{
Console.WriteLine("print from the super class");
}
2- public cat(double h, double w) : base()// should be :base(h,w)
{
name="Putchi";
}
3- public void talk()// should be public override void talk
{
return stirng.format("{0}", "meaw"); // the type is void so replace it
with console.writeline
}
4- class test // Main method is missing static void Main( ) { …. }
{
Animal a = new cat();
cat c = new cat();
c.talk();
c.walk();
a.talk();
a.walk();
a.drink_milk();
}
Question 4: [7 Marks]
Extend the class TestShape in Question 2, by defining a List of type Shape.
- Create a Method within the same class (TestShape) that adds a Shape into the List.
- Create a Method within the same class (TestShape) that deletes a Shape in a given
location ( index)
- Use these newly defend methods in the Main method
class TestShape
{
}
Good Luck