OOP
Static
Inheritance
More Inheritance
Interfaces
100

When an object no longer has any reference variables referring to it, what happens to it?

The garbage collector makes the memory it occupies available for new objects.

100

Name 2 important features when creating a static class. (Mention 1 more for a bonus)

1. Class is marked static and all members are static.

2. Cannot initialize instances of a static class.

3. Classes cannot inherit from a static class. 

100

Class Student is inheriting class Person and wants change a behavior (a method).
Given that C# is used, the method to be changed must be marked as 

_______ in the base-class and
_______ in the derived-class.


virtual in the base-class 

override in the derived-class

100

Classes can extend or derive from how many class(es) in C# .NET? Which OOP pillar does this represent.

Only 1: a class can inherit from one class only. 

However, you may have multiple layers of Inheritance.

100

What is interface? 

What is the main purpose of using interfaces in OOP design?

A type (construct) that only contains method/property signatures and no implementation. /

To define a contract for a shared behavior / to specify what a class can do without specifying how

200

What access modifier should be used for:

Data member / Method Members

Data member always private

Method Members usually public (private for methods for internal use)

200

What can an instance method member access in a class ?

Instance methods and instance data members.
(All members that are not marked static)

Static methods and static data members.
(All members that are marked static)

200

A protected access modifier for an instance member indicates that the member can be accessed in:

- Inside the same class 

- In any class that inherits from the class containing the protected member.

200

You have learned two types of class relations:

The “Has-A” relationship refers to _____, which means______ and

The “Is-A” relationship refers to _____, which means______


Composition: which means an object of a class is a data member of another class.

Inheritance: a derived-class (child) inherits all the members of a base-class (parent) [Code reuse]

200

What is the main purpose of using interfaces in OOP design?

To define a contract for behavior / to specify what a class can do without specifying how

300

A class default constructor refers to?

Do you need to create one when create a class?

A constructor without parameters

No need to create one, the compiler will create one for you.

300

class Food{
     public static GetCount(){ .... }
}

class Cookie: Food{  ...  }

Which of the following is the correct way to access GetCount() method? Explain.

Food.GetCount();
Cookie.GetCount();

Both are correct. Static method can be accessed via both parent or child class name. 

300

What is the base or parent class of any defined class in C#? (Of all defined classes) 

Object class.


300

You have encountered a class that is marked as abstract. What can you infer about that class?

What does it mean to be an abstract class?

1. You cannot create instances of that class.

2. Class mainly created for Inheritance purposes.

3. Class may include a method without implementation, but a derived-class must implement.

300

Can you implement multiple interfaces on a class? If so how?

class ABD: Iinterface1,Iinterface2, Iinterface3

{ .. }

400

Examine the following section of code:

ChocolateBox section1 = new ChocolateBox(10);
ChocolateBox section2 = section1;
ChocolateBox section3 = section1;
ChocolateBox section5 = new ChocolateBox(20);
section1  = null;

How many objects (total) are created? After the last statement has executed, how many objects are now accessible (Do not count garbage collected objects)?

2 created and 2 accessible

3 pointers to 1st object and 1 point to 2nd object

400

static class Calc
{
     public static Sum(x,y) { .... }
}

Create instance of Calc and use the Sum method to calculate the sum of 10 & 20

cannot ..

Calc.Sum(10,20)

400

class Shape {
     public Shape (string x)
     { .... }
}

class Square: Shape {
     public Square(string y): base(y)
     { .... }
}

Which constructor will be called first when you create a Square object?

The parent (shape) then the child (Square)

400

abstract class Shape {

     public abstract double GetArea();
}

class Square : Shape {

     public double GetSide(){ ...}
}

Can you create an object of type Square? Why?

No, compiler error.

Square must implement abstract method GetArea()

400

public interface IShape { double GetArea(); }

public class Circle : IShape { public double Radius;  }

Can you create an instance of Circle? Why?

No, it does not implement GetArea() 

500

Examine the following section of code:

ChocolateBox Alice = new ChocolateBox(10);
Alice = new ChocolateBox(20);

How many objects (total) are created? After the last statement has executed, how many objects are now accessible (Do not count garbage collected objects)?

2 created and 1 accessible

500

A class has a private static data member: _xyz

A programmer created a public instance getter: GetXYZ() for that static data member. 

Can GetXYZ() access the data member and get the value of this static data member in Main. Explain

Yes, but only by creating an instance of the class.

The getter should be marked as static to access it by class name.

500

class Shape {
     public Shape (string x){ .... }
}

class Square : Shape {
     public Square(string y){ .... }
}

Can you create an object of type Square? Why?

No, you need to call the base constructor by using the base keyword in the Square class.

500

class Animal { .. }
class SeaAnimal : Animal { .. }
class LandAnimal : Animal { .. }
class WildAnimal : LandAnimal { .. }

//What type of casting is used in the following? Explain if there is an error. 
1. WildAnimal wAnimal = new WildAnimal();
2. Animal animal = wAnimal;
3. SeaAnimal sAnimal = new Animal();
4. LandAnimal lAnimal = animal;
5. SeaAnimal sAnimal2 = wAnimal as SeaAnimal;

1. No casting: pointer and instance have the same type
2. Upcasting: child instance assigned to parent
3. Error: Down-casting cannot create parent and assign to child (need as, is or explicit for the compiler not to give an error. But at runtime it will not cast as it is not a SeaAnimal)
4. Error: Down-casting (need as, is or explicit) parent pointer assigned to child pointer (will work if down casted properly)
5. Error: cannot cast (different branch - sibling)

500

What is polymorphism? Give an example where is can be observed. 

A method with the same signature having different implementations in different classes. It can be observed when override methods (Inheritance), implementing (overriding) abstract methods (Inheritance) and when implementing and interface. 

M
e
n
u