Set - 3

Question 1 :

Explain term POLIMORPHISM and give an example using eg. SHAPE object: If I have a base class SHAPE, how would I define DRAW methods for two objects CIRCLE and SQUARE ?

Answer :

POLYMORPHISM : A phenomenon which enables an object to react differently to the same function call.
in C++ it is attained by using a keyword virtual


public class SHAPE{
	public virtual void SHAPE::DRAW()=0;

Note here the function DRAW() is pure virtual which means the sub classes must implement the DRAW() method and SHAPE cannot be instatiated

public class CIRCLE::public SHAPE{
	public void CIRCLE::DRAW()
		// TODO drawing circle

public class SQUARE::public SHAPE{
	public void SQUARE::DRAW(){
		// TODO drawing square

now from the user class the calls would be like 
SHAPE *newShape;

When user action is to draw

public void MENU::OnClickDrawCircle(){
	newShape = new CIRCLE();

public void MENU::OnClickDrawCircle(){
	newShape = new SQUARE();

the when user actually draws

public void CANVAS::OnMouseOperations(){


class SHAPE{
	public virtual Draw() = 0; //abstract class with a pure virtual method

class CIRCLE{
    public int r;
    public virtual Draw() { this->drawCircle(0,0,r); }

class SQURE
    public int a;
    public virtual Draw() { this->drawRectangular(0,0,a,a); }

Each object is driven down from SHAPE implementing Draw() function in its own way.

Question 2 :

Describe PRIVATE, PROTECTED and PUBLIC – the differences and give examples?

Answer :

class Point2D{
	int x; int y;
	public int color;
	protected bool pinned;
	public Point2D() : x(0) , y(0) {} //default (no argument) constructor

Point2D MyPoint;

You cannot directly access private data members when they are declared (implicitly) private:

MyPoint.x = 5; // Compiler will issue a compile ERROR
//Nor yoy can see them:
int x_dim = MyPoint.x; // Compiler will issue a compile ERROR

On the other hand, you can assign and read the public data members:

MyPoint.color = 255; // no problem
int col = MyPoint.color; // no problem

With protected data members you can read them but not write them:

MyPoint.pinned = true; // Compiler will issue a compile ERROR 

bool isPinned = MyPoint.pinned; // no problem


Question 3 :

What is a COPY CONSTRUCTOR and when is it called? 

Answer :

A copy constructor is a method that accepts an object of the same class and copies it's data members to the object on the left part of assignement:

class Point2D{
	int x; int y;

	public int color;
	protected bool pinned;
	public Point2D() : x(0) , y(0) {} //default (no argument) constructor
		public Point2D( const Point2D & ) ;

	Point2D::Point2D( const Point2D & p ){
		this->x = p.x;
		this->y = p.y;
		this->color = p.color;
		this->pinned = p.pinned;

		Point2D MyPoint;
		MyPoint.color = 345;
		Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345 


Question 4 :

What is Boyce Codd Normal form?

Answer :

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds:
* a- > b is a trivial functional dependency (b is a subset of a)
* a is a superkey for schema R

Question 5 :

What is virtual class and friend class? 

Answer :

Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

Question 6 :

What is the word you will use when defining a function in base class to allow this function to be a polimorphic function?

Answer :


Question 7 :

What do you mean by binding of data and functions?

Answer :


Question 8 :

What are 2 ways of exporting a function from a DLL?

Answer :

1.Taking a reference to the function from the DLL instance.
2. Using the DLL 's Type Library

Question 9 :

What is the difference between an object and a class?

Answer :

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.
- A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.
- The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.
- An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

Question 10 :

Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321]?

Answer :

quicksort ((data + 222), 100);


Question 11 :

What is a class?

Answer :

Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class.

Question 12 :

What is friend function?

Answer :

As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.

Question 13 :

Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array? 

Answer :

Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in O(n log n) time.

Question 14 :

What is abstraction?

Answer :

Abstraction is of the process of hiding unwanted details from the user.

Question 15 :

What are virtual functions? 

Answer :

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don't know about the derived class.

Question 16 :

What methods can be overridden in Java?

Answer :

In C++ terminology, all public methods in Java are virtual. Therefore, all Java methods can be overwritten in subclasses except those that are declared final, static, and private.

Question 17 :

What are the defining traits of an object-oriented language? 

Answer :

The defining traits of an object-oriented langauge are:
* encapsulation
* inheritance
* polymorphism

Question 18 :

What is the need for a Virtual Destructor ? 

Answer :

Destructors are declared as virtual because if do not declare it as virtual the base class destructor will be called before the derived class destructor and that will lead to memory leak because derived class’s objects will not get freed.Destructors are declared virtual so as to bind objects to the methods at runtime so that appropriate destructor is called.

Question 19 :

Differences of C and C++
Could you write a small program that will compile in C but not in C++ ? 

Answer :

In C, if you can a const variable e.g. 
const int i = 2; 
you can use this variable in other module as follows 
extern const int i; 
C compiler will not complain. 

But for C++ compiler u must write 
extern const int i = 2; 
else error would be generated.

Question 20 :

Bitwise Operations - Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively), what is output equal to in? 

Answer :

output = 

(X & Y) | (X & Z) | (Y & Z);