You are on page 1of 16

COMPUTER SCIENCE - FAQ

1. Differentiate between OOPS and POPS.


A)
POPS

Emphasis on doing things


(functions)
Follows top-down
approach in program
design
Presence of global
variables increase
chances of accidental
change in data
Features like
encapsulation,
polymorphism, etc., are
NOT available in this type
of programming

OOPS

Emphasis on objects

Follows bottom-up
approach in program
design
Its data hiding features
prevent accidental
change in data

Features like
encapsulation,
polymorphism, etc., are
present

2. Define:
i) Data hiding
The data members declared under the private section which
are NOT available outside the class is known as data hiding.
ii) Data encapsulation
The method of binding the data members and member
functions into a single unit is called data encapsulation.
iii) Data abstraction
The method of representing the essential details and
thereby hiding the background details is known as data
abstraction.
iv) Modularity
A property of the system that has been decomposed into a
set of cohesive and loosely coupled modules is known as
modularity.
v) Class

A collection of data members and associated member


functions put into a single unit is called a class.
vi) Object
The instance of the class is called an object i.e., the class is
implemented by creating an object.
3. What are the three access specifiers?
A) The three access specifiers are Public, Private and Protected.
Public:
Public members are the members (data members and member
functions) that can be directly accessed by any function.
Private:
Private members are the class members that are hidden from the
outside world. The private members implement the OOP concept of data
hiding. The private members of a class can be used only by member
functions of the class in which it is declared.
Protected:
Protected members are the members that can be used only by member
functions of the class in which it is declared.
The difference between protected and private members is that
protected members are inheritable whereas private members are not.
4. What are the 2 types of classes?
A) There are 2 types of classes Abstract and concrete classes.
Abstract (Base class): The class from which another class inherits its
properties is known as the abstract or base class.
Concrete (Derived class): A new class that inherits from the base class is
known as the concrete or derived class.
5. What are the three types of member functions?
A) Accessor:
These are those number functions that allow us to access the data
number of the object. However, accessor functions cannot (do not)
change the value of data members.
Mutator:
These are the member functions that allow us to change the data
members of an object.
Manager:

These are the member functions with specific functions (example:


constructors and destructors) that deal with initializing and destroying
class instances.
6. What is an inline function? Give examples.
A) The inline functions are used in C++ to execute the program faster. The
coding of normal functions and inline functions are similar except the
inline functions start with the keyword inline.
Working:
After writing any program it is first compiled to get an executable code.
The OS then loads this code into the memory and each statement is
executed in a particular memory address.
When a function call is encountered, the function stores the memory
address, loads the function into the stack address, and returns the value
of the function. It then jumps back to the address of the statement that
was saved.
With inline code, the compiler replaces the function call statement with
the function code itself and then compiles the entire code.
With inline functions, the compiler does not have to jump to another
location and jump back to the calling program.
Example:
inline void max(int a, int b)
{
cout<<(a>b ? a:b);
}
void main()
{
int x, y;
cin>>x>>y;
cout<<max(x,y);
}
The function max in the above code would not be called during
execution rather its code would be inserted into the main program and
then compiled.
7. What are static data members? Give example.
A) A static data member of a class is just like a global variable for its class.
That is, the data member is globally available for all the objects of that

class type. The static data members are usually maintained to store
values common to the entire class.
Example:
class abc
{
int a;
static int b;
};
int abc::b=3;
8. What are static member functions?
A) Member functions that access only the static members of the class. They
can be declared using the keyword static before the function definition in
the class program.
Example:
class abc
{
int a;
static int b;
public:
void indata()
{
cin>>a;
}
static void show()
{
cout<<b;
}
};
int abc::b=3;
void main()
{
abc ob;
ob.indata();
abc::show();
}
9. What is a nested class? Give example.
A) A class declared within another class is called a nested class.
Example:
class outer

{
int a;
class inner
{
int b;
public:
int c;
void prn()
{
cout<<Inner<<endl;
cin>>b>>c;
}
};
inner ob1;
public:
inner ob2;
void second()
{
cout<<Outer<<endl;
cin>>a;
}
};
10. Differentiate between structure and class.
A)
STRUCTURE

CLASS

It is a collection of data
types.

The data types are saved


in public mode by
default.

It is a collection of data
types and member
functions.
They are saved as
private by default.

11. What are the different methods of defining member functions?


A) Member functions can be defined in 2 places :
Outside the class definition:
A member function defined outside the class definition is written as
Class-name :: function-name
Syntax:
Return_type
class_name :: function_name (parameter list)
{
function body
}
Inside the class definition:
When a member is defined inside a class the function definition is just
similar to the normal function definitions.
12. What is scope resolution operator? Give example.
A) The scope resolution operator :: is usually used to differentiate between
class member names and other names. It is also used to differentiate
between a normal variable and a global variable:
int x;
class A
{
public:
int x;
void f(int i)
{
x = i;
//assigned to A::i
::x = i;
//assigned to global variable x
}
};
13. Differentiate between member functions and ordinary function.
A) i) Member functions have full access to privilege to both the public and
private members of the class, while, in general, non-member functions
have access only to the public members of the class.
ii) Member functions are defined within the class scope that is they are
not visible outside the scope of class. Non-member functions are also
visible outside the scope of class.

14. How is memory allocated?


A) When a class is defined, memory is allocated for its member functions
and they are stored in the memory. When an object is created, separate
memory space is allocated for its data members. All objects work with
the one copy of member function shared by all.
15. What are the merits and demerits of inline functions?
A)
Merits:
They save on the overheads of a function call as the function is not
invoked; rather its code is replaced in the program.
Demerits:
With more function calls, more memory is wasted as for every function
call, the same function code is inserted in the program. Repeated
occurrences of same function code waste memory space. Also, inlining
large functions result in heavy memory penalty. Inline is not allowed for
functions that return values and are having any loop or switch or goto.
16. What is a constructor? Give an example.
A) It is a special member function used to initialize the data members with
legal values.
Characteristics:
It has the same name as that of the class name.
It does not have a return type (including void)
It is automatically invoked by the compiler, whenever the object is
created.
Generally, it is declared under public so that it is accessible by the
object of the class.
Syntax:
classname()
{

Example:
class example
{
int x;
float y;
char z;
public:
example()
{
x = 17;
y = 99.797;
z = f;
} };
17. What are the types of constructors? Give an example.
A) There are three types of constructors:
Default constructor:
The constructor that will not take any arguments is called default
constructor.
Parameterized constructor:
The constructor which takes arguments is called parameterized
constructor.
Copy constructor:
It is used to copy one object into another object of the same class.
Example:
class demo
{
int x;
float y;

char z;
public:
demo()

//DEFAULT CONSTRUCTOR

{
x = 5;
y = 20.7;
z = u;
}
demo(int a, float b, char c)
//PARAMETERIZED
{
x = a;
y = b;
z = c;
}
demo(demo &t)
//COPY CONSTRUCTOR
{
x = t.x;
y = t.y;
z = t.z;
}
void disp()
//MEMBER FUNCTION
{
cout<<x<< : <<y<< : <<z<<endl;
} };
void main()
{
demo ob;

//DEFAULT CONSTRUCTOR

ob.disp();
demo ob1(5, 20.7, u);

//PARA - IMPLICIT

ob1.disp();
demo ob2 = demo(5, 20.7, u);//PARA. -EXPLICIT
ob2.disp;
demo ob3(ob2);

//COPY METHOD 1

ob3.disp;
demo ob4 = ob3;

//COPY METHOD 2

ob4.disp(); }
18. Differentiate between implicit and explicit constructors.
A)
Implicit:
In this method, the constructor is invoked even when its name has not
been mentioned in the statement.
Explicit:
By explicit call, the name of the constructor is explicitly provided to
invoke it so that the object can be initialized.
19. What are temporary instances? Give examples.
A) Temporary instances live in the memory for as long as they are being
used or referenced in an expression and after that they die. Temporary
instances are anonymous i.e., they do not bear a name.
Example:
class sample
{
int i, j;
public:
sample(int a, int b);
{
i = a;
j = b;
}
void print()
{
cout<<i<< : <<j<<endl;
}
};

void main()
{
sample s(8, 10);
s.print();
sample(6,4).print();
}

//TEMPORARY INSTANCE

20. What is the importance of constructors?


A) A constructor of a class is needed so that the compiler automatically
initializes an object as soon as it is created. A class constructor, if
defined, is called whenever a program creates an object of that class.
The constructor takes over this very important duty of initialization of an
object being created and relieves us from this task.
21. What are the characteristics of a destructor?
Destructors have the same name as the class name and are preceded
by the tilde (~) symbol.
It is invoked by the compiler automatically when the object goes out
of scope(when the program execution gets over)
22. Explain constructor overloading with an example.
A) If a class contains multiple constructors, then it is called constructor
overloading or polymorphism.
Example:
class demo
{
int x;
float y;
char z;
public:
demo()
{
x = 5;

y = 20.7;
z = u;
}
demo(int a, float b, char c)
{
x = a;
y = b;
z = c;
}
void disp()
{
cout<<x<< : <<y<< : <<z<<endl;
} };
void main()
{

demo ob;
ob.disp();
demo ob1(5, 20.7, u);
ob1.disp();

}
23. Explain inheritance.
A) Inheritance is the method or extracting some or all the properties of one
class(base class) into another class(derived class)
24. What is the need for inheritance?
Capability to express the inheritance relationship which ensures
the closeness with the real world models.
Reusability of code
It is faster to develop.
Easy maintenance.
Easy to extend.
Transitive nature of inheritance.

25. Explain the types of inheritance.


A) There are 5 types of inheritance:
Single inheritance:
When a derived class inherits only from one base class, then it is called
single inheritance.

Multilevel inheritance:

When a derived class inherits from a class that itself inherits from
another class, it is known as multilevel inheritance.

Y
Z

Multiple inheritance:
When a derived class inherits from multiple base classes, it is called
multiple inheritance.

X
Z

Hierarchial inheritance:
When many derived classes inherit from a single base class, it is known
as hierarchial inheritance.

X
W

Hybrid inheritance:
When a derived class inherits from multiple base classes and all of its
base classes inherit from a single base classes inherit from a single base
class then this form of inheritance is known as hybrid inheritance.
26. Explain function overloading with example.
A) A function name having several definitions that are differentiable by the
number or type of arguments is known as function overloading.
Example:
void area(int r)
{
cout<<"Area of Circle: "<<pi*r*r;
}
void area(int a, int b)
{
cout<<"Area of rectangle: "<<a*b;
}
void area(int a,int b, int c)
{
float ar, s;
s = (a+b+c)/2;
ar = sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle: "<<ar;
}
void main()
{

int ch;
int a,b,r,c;
cout<<"1.Area of Circle<<endl;
cout<<2.Area of Rectangle<<endl;
cout<<3.Area of Triangle<<endl;
cout<<4.Exit<<endl;
cout<<Enter your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radius of the Circle:";
cin>>r;
area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
area(a,b);
break;
case 3:
cout<<"Enter sides of the Triangle:";
cin>>a>>b>>c;
area(a,b,c);
break;
case 4:
break;
}
getch();
}
27. What is polymorphism?
A) It is a property by which the same message can be sent to objects of
several different classes and each object can respond to it in a different
way depending upon its class. In C++, polymorphism is implemented
through overloading.
28. Explain function signature with an example.

A) A functions argument list (number and type of arguments) is known as


function signature.
Example:
void rectangle(int x, float y);
29. Explain ambiguous error.
A) An ambiguous error occurs when an ambiguous situation is created as to
which function should be called.
Example:
void func(long);
void func(double);
func(10);
Here, 10 can be assigned to long as well as double. Therefore an
ambiguous error occurs.

You might also like