Professional Documents
Culture Documents
1 Mark Questions
1. What is the fundamental idea of object oriented programming?
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”,
which may contain data, in the form of attributes and code in the form of procedures or methods.
The major four principles those make a language object oriented are encapsulation, data
abstraction, polymorphism and inheritance.
2. What is an Object?
Object is an instance of a class.
An object is a collection data members and associated member functions.
5. What is Encapsulation?
Data Abstraction refers to the process of representing essential features without including
background details or explanations.
Data encapsulation enables data hiding and information hiding.
7. Define Polymorphism.
The ability of an operator and function to take multiple forms is known as Polymorphism.
8. What is inheritance?
The process of forming a new class from an existing class is known as Inheritance.
The objects of one class acquire the properties of another class through inheritance.
9. What is a base class?
It is the class whose properties are inherited by another class. It is also called Super Class.
2 Mark Questions
1. What is the significance of classes in OOP?
The objects can contain data and code to manipulate the data. The objects can be made user
defined data types with the help of a class. Therefore objects are variables of the type class.
A class is a way of grouping objects having similar characteristics. Once a class is defined, any
number of objects of that class can be created.
4. Mention any two advantages of object oriented programming over earlier programming
methods.
The programs are modularized based on the principle of classes and objects.
Linking code & object allows related objects to share common code. This reduces code duplication
and code reusability.
Data is encapsulated along with functions. Therefore external non- member function cannot access
or modify data, thus providing data security.
Easier to develop complex software, because complexity can be minimized through inheritance.
The concept of data abstraction separates object specification and object implementation.
Creation and implementation of OOP code is easy and reduces software development time.
OOP can communicate through message passing which makes interface description with outside
system very simple.
3 Mark Questions
1. Briefly discuss the classes and objects.
Object
Object is an instance of a class.
An object is a collection data members and associated member functions.
Objects interact by sending messages to one another. The objects can interact with one another
without having to know the details of data or functions within an object.
Class
A class is a blueprint or outline for an object.
A class is a way of grouping objects having similar characteristics.
The objects can be made user defined data types with the help of a class.
2. Explain Inheritance.
Inheritance is the capability of one class to inherit properties from another class. Inheritance provides
the idea of reusability
Base Class is the class whose properties are inherited by another class. It is also called Super Class.
Derived Class is the class that inherits properties from base class (es).It is also called Sub Class.
The different types of inheritance are single inheritance, multilevel inheritance, multiple inheritance,
hierarchical inheritance and hybrid inheritance.
4. Mention any 4 high level languages that follow object oriented programming approach.
The four high level languages that follow object oriented programming approach are:
C++
C#
Java
Python
Visual Basic
5 Mark Questions
1. Write the difference between procedural programming and object oriented programming.
1 Mark Questions
1. What are the two types of members referenced in a class?
The two types of members referenced in a class are data members and member functions.
13. Write an example to show how objects can be used as function arguments?
Example:
class class_name
{
…………………..
return_type function_name(class_name para1, class_name para2)
{
…………………………………………….;
}
…………………………..;
}
main()
{
class_name obj1, obj2, obj3;
obj1.function_name(obj2, obj3);
………………
}
int main()
{
clrscr();
Test a1;
a1.getnum();
a1.show_data();
getch();
return 0;
}
public:
//fields, constructors
Class declaration
//public method declarations
private:
//private fields
//private method declarations
};
main()
{ Object declaration
myClass obj1, obj2;
}
Example:
(Same example as given in the previous question)
4. What is the significance of using access specifiers? Mention different access specifiers.
The data members and member functions can be accessed using access specifiers. They define the scope of
members. The different access specifiers are private, protected and public.
Example:
class box
{
int length;
public : int width;
private : int height;
int get_height ( )
{
return height ;
}
};
int main()
{
box object;
object.length = 10;
object.width = 20;
object.set _height ( 30); // private variable can be accessed only through its public method
return 0;
}
9. What is meant by referencing member functions inside class definition and outside class
definition?
Member functions can be defined in two places:
Inside class definition
Outside class definition
To define member function inside a class the function declaration within the class is replaced by
actual function definition inside the class.
A function defined in a class is treated as inline function.
Only small functions are defined inside class definition.
Syntax:
return_type classname(member function)
Member functions declared within a class must be defined separately outside the class.
The definition of member function is similar to normal function. But a member function has an
„identity label‟ in the header. This label tells the compiler which class the function belongs to.
The scope of the member function is limited to the class mentioned in the header.
Scope resolution operator :: is used to define the member function.
Syntax:
return_type classname :: memberfunction(arg1, arg2, …, argn)
{
function body;
}
Outside Program to use classes using member functions inside and outside class
definition
# include<iostream.h>
class item // class declaration
{
int number; // private by default
float cost;
public :
void getdata(int a, float b);
// main program
int main()
{
item x; // create object x
x.getdata(250, 10.5);
x.putdata();
return 0;
}
public:
void setval(void);
void display(void);
};
The array variable m is a private member of class marks. This can be used
by member function setval( ) and display( ) as follows.
void marks::display(void)
{
cout<< "The marks are : ";
for(i =0;i<5;i++)
cout<<setw(4)<<m[i]<<endl;
}
(Example to be given)
5 Mark Questions
1. Explain class definition and class declaration with syntax and example.
(Combine answer numbers 1, 2 & 3 under 2/3 mark questions).
public:
void getdata();
void dispdata(void);
};
employee supervisor[3] ;
employee sales_executive[5] ;
employee team_leader[10] ;
8. Let product list be a linear array of size N where each element of the array contains following
fields: Itemcode, price and quantity. Declare a class product list with the three data members
and member functions to perform the following:
a) Add values to the product list.
b) Printing the total stock value.
9. A class clock has the following members : a) Hour b) Minute. Create Member Functions:
a) To initialize the data members.
b) Display the time.
c) To convert hours and minutes to minutes.
10. Write a program that receives arrival time, departure time and speed of an automobile in
kilometres/hour as input to a class. Compute the distance travelled in metres/second and
display the result using member functions.
Chapter 9
1 Mark Questions
1. What is a constructor?
A Constructor is a special member function that is called automatically when an object is
created. It is called constructor, because it constructs or decides the initial values for data members of a
class.
2. Write one reason which defines the need to use a constructor. Simplify.
The purpose of a constructor is to mainly initialize the member variables of a class.
or
It is used for initialization of objects.
2 Mark Questions
public:
counter()
{
a = 0;
}
void inc_count()
{
a++;
}
int get_count()
{
return a;
}
};
int main()
{
counter c1, c2;
cout<<“c1 = “<<c1.get_count();
cout<<“c2 = “<<c2.get_count();
c1.inc_count();
c2.inc_count();
cout<<“c1 = “<<c1.get_count()<<endl;
cout<<“c2 = “<<c2.get_count()<<endl;
}
5. Which are the different methods through which constructors are invoked?
Invoking Parameterized Constructors can be done in three different ways
1) Implicit Call
2) Explicit Call
3) Initialization at the time of declaration with “=” operator
6. Write an example to show the use of parameterized constructor through explicit call.
Program to use parameterized constructor thorough explicit call:
#include<iostream.h>
class num
{
private:
int a,b;
public:
num(int p, int q)
{
a = p;
b = q;
}
void display()
{
cout<<"a = "<<a<<" and b = "<<b<<endl;
}
};
void main()
{
num obj1=num(10,20);
num obj2=num(40,50);
cout<<"First construction: ";obj1.display();
cout<<"Second construction: ";obj2.display();
}
Output:
First construction: a = 10 and b = 20
Second construction: a = 40 and b = 50
5 Mark Questions
Syntax
num::num()
{
cout<<“ In constructor: \n”;
x=100;
}
num::~num()
{
cout<<“in destructor”<<endl;
}
void num::display()
{
cout<<“Value of x = ”<<x<<endl;
}
int main()
{
clrscr();
num a;
a.display();
getch();
return 0;
}
Output:
In constructor
Value of x = 100
In destructor
Chapter 10
Inheritance
1 Mark Questions
1. What is inheritance?
Inheritance is the capability of one class to inherit properties from another class.
8. Is inheritance possible in C?
No, because it doesn‟t support object oriented concept.
2 Mark Questions
void main()
{
derive ob;
clrscr();
ob.read();
ob.read1();
ob.display();
ob.display1();
getch();
}
Output:
class AB : public A
{
public:
void displayAB( )
{
cout << “ Intermediate Base class AB”<<endl;
cout << “ Derived from A” << endl;
}
};
#include <iostream>
class A
public:
int x;
};
class B : public A
public:
x = 10;
}
};
class C
public:
int y;
{ y = 4; }
};
public:
void sum()
};
int main()
obj1.sum();
return 0;
}
9. What is multiple inheritance? Give an example.
If a class is derived from more than one base class, it is known as multiple inheritance.
Example:
#include<iostream>
using namespace std;
class Person {
// Data members of person
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
};
int main() {
TA ta1(30);
}
10. What is virtual base class? Give example.
When two or more objects are derived from a common base class, we can prevent multiple copies
of the base class being present in an object derived from those objects by declaring the base class
as virtual when it is being inherited. Such a base class is known as virtual base class.
Example:
#include<iostream>
using namespace std;
class Person {
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
Person() { cout << "Person::Person() called" << endl; }
};
int main() {
TA ta1(30);
}
11. What is an abstract class?
An abstract class is one that is not used to create objects.
An abstract class is designed only to act as a base class (to be inherited by other classes).
3 Mark Questions
1. What is the difference between public and private access specifier with respect to inheritance?
(write definition for both : refer Q10 & Q11 under 1 mark questions).
5 Mark Questions
1. What is the difference between public, private and protected access specifier?
(Refer Q9,Q10 & Q11 under 1 mark question for definition and Q1 under 3 mark question for example).
Pointers
1 Mark Questions
In the above example, variable „a‟ is allocated a memory location of 5000 and it stores the value 10.
Similarly, pointer „ptr‟ is allocated memory location 6000 and it stores the address of variable „a‟ i.e. 5000
as its value.
10. Illustrate the use of “self referential structures” with the help of example.
A self-referential structure is used to create data structures like linked lists, stacks, etc.
Following is an example of this kind of structure where pointer variable *next is of datatype „node‟
which is the structure type itself:
struct node
{
int data;
node *next;
};
15. Show the general form of new and delete operator in C++?
(Refer Q7 & Q8)
16. What is the relationship between array and pointers? Give example.
17. What is the relationship between string and pointers? Give example.
18. What is the relationship between structures and pointers? Give example.
19. What is the relationship between object and pointers? Give example.
20. Explain with example by passing the reference.
21. Explain with example by passing the pointers.