Home | | Object Oriented Programming and Data Structures | Important Questions and Answers: Data Abstraction & Overloading

Chapter: Object Oriented Programming and Data Structure : Data Abstraction & Overloading

Important Questions and Answers: Data Abstraction & Overloading

Object Oriented Programming and Data Structure - Data Abstraction & Overloading - Important Questions and Answers: Data Abstraction & Overloading

 

PART - A

 

1. What is the output of the following program, if it is correct? Otherwise indicate the mistake:

 

int l=10;

Void main [] {

int l=20; {

int l=30; cout<<l<<::l;

}}

 

The program is in correct due to syntax error. Within the main function, there is no need of another opening braces in the int l=20; and also closing braces.

 

2. Difference between Class and structure?

 

·        Class is the ADT where as structure is udt.

·        Class needs access specifier such as private, public & private where as structure

·        members can be accessed by public by default & don’t need any accessfiers.

 

·        Class is oops where structure is borrowed from traditional structured [pop] concept.

 

3. What is abstract Class?

 

Ø An abstract class is a class that is designed to be specifically used as a base class.

Ø An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier [= 0] in the declaration of a virtual member function in the class declaration

 

4. List out the advantages of new operator over malloc[]

 

·        It automatically computes the size of the data object.

·        It automatically returns the correct pointer type

·        It is possible to initialize the objects while creating_ the memory space.

·        It can be overloaded.

 

5. What are the basic concepts of OOS?

 

1.     Objects.

2.     Classes.

3.     Data abstraction and Encapsulation.

4.     Inheritance.

5.     Polymorphism.

6.     Dynamic binding.

7.     Message passing

 

 

6. What is the difference between local variable and data member?

 

·        A data member belongs to an object of a class whereas local variable belongs to its current scope.

 

·        A local variable is declared within the body of a function and can be used only from the point at which it is declared to the immediately following closing brace.

 

·        A data member is declared in a class definition, but not in the body of any of the class member functions.

 

·        Data members are accessible to all member function of the class.

 

7.  What is the function parameter? Difference between parameter and Argument.

 

·        A function parameter is a variable declared in the prototype or declaration of a function: o void foo[int x]; // prototype -- x is a parameter

 

o void foo[int x] // declaration -- x is a parameter

o {

 

}

·        An argument is the value that is passed to the function in place of a parameter

 

8. What is data hiding?

·        The insulation of data from direct access by the program is called as data hiding or information binding.

 

·        The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it.

 

9. What are the advantages of Default Arguments?

 

The function assigns a default value to the parameter which does not have a matching argument in the function call. They are useful in situations where some arguments always have the same value.

 

e.g., float amt [float P, float n, float r = 0.15];

 

10.            List out the basic concepts of Object Oriented Programming.

 

·        Objects

 

·        Classes

 

·        Data Abstraction and Encapsulation

·        Inheritance

·        Polymorphism

·        Dynamic Binding

·        Message Passing

 

11.            What are abstract classes?

 

Classes containing at least one pure virtual function become abstract classes.

 

o Classes inheriting abstract classes must redefine the pure virtual functions; otherwise the derived classes also will become abstract. Abstract classes cannot be instantiated.

 

12.            Define abstraction and Encapsulation

 

Data Abstraction

 

Abstraction refers to the act of representing the essential features without including the background details or explanations.

 

Data Encapsulation

The wrapping up of data and functions into a single unit is known as data encapsulation.

 

13.            What is the Need for Static Members

 

·        Class members can be declared using the storage class specifier static in the class member list. Only one copy of the static member is shared by all objects of a class in a program.

 

·        When you declare an object of a class having a static member, the static member is not part of the class object.

 

14. Define Polymorphism.

·        Polymorphism is another important oops concept.

·          Polymorphism means the ability to take more than one form.

 

·        For example, an operation may exhibit different behavior in different instances, behavior depends upon the types of data used in the operation.

 

15.            What do you mean by pure virtual functions?

 

1.     A pure virtual member function is a member function that the base class forces derived classes to provide.

 

2.     Any class containing any pure virtual function cannot be used to create object of its own type.

 

16.            Write a C++ program to check the given integer is prime or composite number.

 

#include<conio.h>

 

#include<stdio.h>

int main[]

{

 

int num,d,ctr; clrscr[];

printf["\n Enter a number="];

scanf["%d",&num];

d=1;

ctr=0;

 

while[d<=num]

{

if[num%d==0]

ctr++;

d=d+1;

}

if[ctr==2]

 

printf["\n %d is a prime number",num]; else

 

printf["\n %d is a composite number",num];

getch[];

return[0];

}

 

17. What is function Prototype?

 

A function prototype or function interface in C, Perl, PHP or C++ is a declaration of a function that omits the function body but does specify the function's return type, name and argument types.

 

While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.

 

18. List out four Storage Classes in C++

 

Storage classes are used to specify the lifetime and scope of variables. How storage is allocated for variables and how variable is treated by complier depends on these storage classes. These are basically divided into 5 different types :

 

1. Global variablES

2. Local variables

3. Register variables

4. Static variables

5. Extern variables

 

19.            What is an identifier?

 

·        Identifiers are names for various programming elements in c++ program. such as variables, arrays, function, structures, union, labels ect.,

 

·        An identifier can be Composed only of uppercase, lower case letter, underscore and digits, but should start only with an alphabet or an underscore.

 

 

20.            What is a keyword?

 

Keywords are word whose meanings have been already defined in the c compiler. They are also called as reserved words.

(ex) main(), if, else, else, if, scanf, printf, switch, for, goto, while ect.,

 

21.            List out the benefits of oops.

 

Can create new programs faster because we can reuse code

Easier to create new data types

Easier memory management

Programs should be less bug-prone, as it uses a stricter syntax and type checking.

  `Data hiding', the usage of data by one program part while other program parts cannot

 

access the data Will whiten your teeth

 

22.            List out the application of oops.

 

Client server computing

Simulation such as flight simulations.

 

Object-oriented database applications.

Artificial intelligence and expert system

Computer aided design and manufacturing systems.

 

23. Define data hiding.

The purpose of the exception handling mechanism is to provide a means to detect and report an “exceptional circumstance” so that appropriate action can be taken.

 

 

 

 

24. What is the use of scope resolution operator?

 

In C, the global version of the variable cannot be accessed from within the inner block. C++ resolves this problem by introducing a new operator :: called the scope resolution operator. It is used to uncover a hidden variable.

 

Syntax:

::  variable name

 

25.            When will you make a function inline?

 

When the function definition is small, we can make that function an inline function and we can mainly go for inline function to eliminate the cost of calls to small functions.

 

26. What is overloading?

 

Overloading refers to the use of the same thing for different purposes.

There are 2 types of overloading:

  Function overloading

  Operator overloading

 

27.            What is the difference between normal function and a recursive function?

 

·        A recursive function is a function, which call it whereas a normal function does not.

·        Recursive function can’t be directly invoked by main function

 

28.            What are objects? How are they created?

 

Objects are basic run-time entities in an object-oriented programming system. The class

variables are known as objects. Objects are created by using the syntax: classname obj1,obj2,…,objn;

 

(or) during definition of the class:

class classname

 

{

-------

 

}obj1,obj2,…,objn;

 

29.            List some of the special properties of constructor function.

 

They should be declared in the public section.

They are invoked automatically when the objects are created.

They do not have return types, not even void and cannot return values.

Constructors cannot be virtual.

 

Like other C++ functions, they can have default arguments

 

30. Describe the importance of destructor.

 

A destructor destroys the objects that have been created by a constructor upon exit from the program or block to release memory space for future use. It is a member function whose name is the same as the class name but is preceded by a tilde.

 

Syntax:

~classname()

{ }

 

31.            What do you mean by friend functions?

 

C++ allows some common functions to be made friendly with any number of classes, thereby allowing the function to have access to the private data of thse classes. Such a function need not be a member of any of these classes. Such common functions are called friend functions.

 

32. What are member functions?

 

Functions that are declared within the class definition are referred as member function.

 

33. Define dynamic binding.

 

Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.

 

34.            Write any four properties of constructor.

 

·        Constructors should be declared in the public section.

·        They are invoked automatically when the objects are created.

·        They do not have return types

·        They cannot be inherited

 

 

35.            List any four Operators that cannot be overloaded.

 

Class member access operator (. , .*)

Scope resolution operator (::)

Size operator ( sizeof )

Conditional operator (?:)

 

36. What is a Destructor?

 

A destructor is used to destroy the objects that have been created by a constructor. It is a special member function whose name is same as the class and is preceded by a tilde ‘~’ symbol.

When an object goes out from object creation, automatically destructor will be executed.

 

Example:

 

class File { public:

 

~File(); //destructor declaration };

File::~File()

{ close(); // destructor definition

}

 

37. What is the Need for initialization of object using Constructor?

 

If we fails to create a constructor for a class, then the compiler will create a constructor by default in the name of class name without having any arguments at the time of compilation and provides the initial values to its data members. So we have to initialize the objects using constructor

 

 

 

 

 

38. What is a Copy Constructor

 

A copy constructor is used to declare and initialize an object from another object. It takes a reference to an object of the same class as an argument

 

Eg:

integer i2(i1);

 

would define the object i2 at the same time initialize it to the values of i1. Another form of this statement is

 

Eg: integer i2=i1;

The process of initializing through a copy constructor is known as copy initialization.

 

39. Give an example for a Copy Constructor

 

#include<iostream>

 

#include<conio.h>

using namespace std;

class Example {

 

// Variable Declaration int a,b;

public:

 

//Constructor with Argument

Example(int x,int y) {

//  Assign Values In Constructor

a=x;

b=y;

cout<<"\nIm Constructor";

}

void Display() {

cout<<"\nValues :"<<a<<"\t"<<b;

}

};

int main() {

 

Example Object(10,20); //Copy Constructor

Example Object2=Object

//Constructor invoked.

Object.Display();

Object2.Display();

//Wait For Output Screen

getch();

return 0;

}

 

 

40.            What is the Need for Destructors?

 

·        Destructor is used to destroy an object.

·        By destroying the object memory occupied by the object is released.

 

41.            Explain the functions of Default Constructor

 

The main function of the constructor is, if the programmer fails to create a constructor for a class, then the compiler will create a constructor by default in the name of class name without having any arguments at the time of compilation and provides the initial values to its data members.

 

Since it is created by the compiler by default, the no argument constructor is called as default constructor.

 

42.            What is the need for Overloading an operator

 

·        To define a new relation task to an operator, we must specify what it means in relation to the class to which the operator is applied.

 

·        This is done with the help of a special function called operator function.

 

·        It allows the developer to program using notation closer to the target domain and allow user types to look like types built into the language.

·        The ability to tell the compiler how to perform a certain operation when its corresponding operator is used on one or more variables.

 

43.            What is the function of get and put function

 

Cin.get(ch) reads a character from cin and stores what is read in ch.

Cout.put(ch) reads a character and writes to cout

 

PART – B

 

1.  State the merits and demerits of object oriented methodology.

 

Merits:

Can create new programs faster because we can reuse code

Easier to create new data types

Easier memory management

Programs should be less bug-prone, as it uses a stricter syntax and type checking.

  `Data hiding', the usage of data by one program part while other program parts cannot

 

access the data Will whiten your teeth

 

Demerits:

disadvantages of C++ over Java:

Java protects you from making mistakes that C/C++ don’t, as you’ve

C++ has many concepts and possibilities so it has a steep learning curve

 

extensive use of operator overloading, function overloading and virtual

functions can very quickly make C++ programs very complicated

shortcuts offered in C++ can often make it completely unreadable, just like in C

 

2. Explain the basic concepts of object oriented programming in detail with example.

BASIC CONCEPTS OF OBJECT ORIENTED PROGRAMMING

 

These include:

 

·        Objects

·        Classes

·        Data abstraction and encapsulation

·        Inheritance

·        Polymorphism

·        Dynamic binding

 

·        Message passing

 

Objects:

Objects are the basic run-time entities in an object oriented programming. They may representa person, a place, a bank account or any item that the program has to handle. They may represent user-defined data such as vectors, time and lists. Programming problem is analyzed in terms of objects and the nature of communication b/n them.

 

When a program is executed, the objects interact by sending messages to another. Each object contains data and code to manipulate the data. Objects can interact without having to know the etails of each others data or code. It is sufficient to know the type of message accepted, and the type of message accepted and the type of response returned by the objects.

 

Classes:

 

The entire set of data and code of an object can made a user defined data type with the help of a class. In fact the objects are variable of the type class. Once class has been defined we can create any number of objects belonging to that class. In short, a class serves as a blueprint or

 

a plan or a template. It specifies what data and what functions will be included in objects of that class. Defining the class doesn’t create any objects, just as the mere existence of a type int doesn’t create any variables.

 

Data Abstraction and Encapsulation:

 

The wrapping up of data and functions into a single unit is known as encapsulation. It is the most striking feature of the class. The data is not accessible to the outside world and only

 

those functions which are wrapped in the class can access it. These functions provide interface b/n the object’s data and the program. This insulation of the data from direct access by the

program is called data hiding or information hiding.

 

Abstraction represents the act of representing the essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost and functions to operate on these attributes. The attributes are called data members and functions are called member functions or methods. Since the classes use the concept of data abstraction, they are known as Abstract Data Types (ADT).

 

Inheritance:

 

It is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification. For example, the bird ‘robin’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’. This concept provides the idea

 

of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by a deriving a new class from an existing one. The new class will have the combined features of both the classes.

 

Polymorphism:

 

It means the ability to take more than one form. An operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. For example the operation addition will generate sum if the operands are numbers whereas if the operands are strings then the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

 

A single function name can be used to handle different types of tasks based on the number and types of arguments. This is known as function overloading. It allows objects to have different internal structures to share the same external interface.

 

Dynamic Binding:

 

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic Binding (late binding) means the code associated with the given procedure call is not known until the time of the call at run-time. It is associated with the polymorphism and inheritance.

 

Message Passing:

 

The process of programming in OOP involves the following basic steps:

 

·        Creating classes that define objects and their behavior

·        Creating objects from class definitions

·        Establishing communication among objects

 

A message for an object is request for execution of a procedure and therefore will invoke a function (procedure) in the receiving object that generates the desired result.

 

Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent.

 

E.g.: employee.salary(name); Object: employee

Message: salary Information: name

 

 

3. State the rules to be followed while overloading an operator.write a program to illustrate overloading.

 

OPERATOR OVERLOADING:

 

Operator overloading means giving additional meaning to existing operators

By operator overloading an existing operator can be made to perform different operations

than the stipulated one.

It doesn’t change the meaning and precedence of the original operator.

Almost all the operators in c++ can be overloaded except the following

 

1)    Sizeof ()

2)    Conditional operator (?:)

3)    Scope resolution operator (::)

4)    Class member access operator (.,.*)

 

example the operation addition will generate sum if the operands are numbers whereas if the operands are strings then the operation would produce a third string by concatenation. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

 

A single function name can be used to handle different types of tasks based on the number and types of arguments. This is known as function overloading.

It allows objects to have different internal structures to share the same external interface.

 

Dynamic Binding:

 

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic Binding (late binding) means the code associated with the given procedure call is not known until the time of the call at run-time. It is associated with the polymorphism and inheritance.

 

 

Message Passing:

 

The process of programming in OOP involves the following basic steps:

 

·        Creating classes that define objects and their behavior

·        Creating objects from class definitions

·        Establishing communication among objects

 

A message for an object is request for execution of a procedure and therefore will invoke a function (procedure) in the receiving object that generates the desired result. Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent.

 

E.g.: employee.salary(name); Object: employee

Message: salary Information: name

 

 

3. State the rules to be followed while overloading an operator.write a program to illustrate overloading.

 

OPERATOR OVERLOADING:

 

Operator overloading means giving additional meaning to existing operators

  By operator overloading an existing operator can be made to perform different operations than

the stipulated one.

It doesn’t change the meaning and precedence of the original operator.

 

Almost all the operators in c++ can be overloaded except the following

 

o Sizeof ()

o Conditional operator (?:)

o Scope resolution operator (::)

o Class member access operator (.,.*)

 

SYNTAX:

 

Return-type operator op-symbol(argument list)

{

body of the function;

}

 

Generally there are three general classifications of operator namely Operator

Unary binary ternary

 

Among the above unary and binary operators can be overloaded and ternary operator cannot

be overloaded.

OVERLOADING UNARY OPERATOR:

 

Unary operators like unary + , Unary - ….. can be overloaded as follows

 

EXAMPLE:

#include

 

#include class unary

{

 

int a; public: void get()

{

a=10;

}

void show()

{

cout <<"\n The value of the object is\n"<

}

void operator - ()

{

a=-a;

}

};

void main()

{

 

clrscr();

unary u;

u.get();

u.show();

-u; //unary operator - called

u.show();

getch();

}

 

O/P:

 

The value of the object is 10

 

The value of the object is -10

 

  In the above program, the unary operator minus is overloaded.

  In this an object ‘u’ is created and given the value 10

  When the unary operator – is called the operator function is invoked and the value of the member data of the object ‘u’ is negated.

 

  The negated value is then displayed.

 

 

4. (i) Describe the application of oop technology

 

Applications of OOP:

·        Real time systems

·        Simulation and modeling

·        AI and expert systems

·        Neural networks and parallel programming.

·        Decision support and office automation systems

 

(ii) What is an inline function?

 

INLINE FUNCTIONS :

 

An inline function is a function that is expanded in line when it is invoked. That is , the complier replaces the function call with the corresponding function code.The inline function are defined as follows:

 

inline function header

 

{

 

function body

}

 

Example :

 

inline int cube(int a)

{

return (a*a*a);

}

 

 

Program :

#include

 

inline float mul(float x, float y)

{

return (x*y);

}

inline float div(double p, double q)

{

return( p / q )

}

int main()

{

 

float a=12.35;

float b=9.32;

cout<< mul(a,b);

cout<

 

return 0;

}

 

5. (i). Explain copy constructor with suitable c++ coding.

 

Constructor:

 

A constructor is a special member function whose task is to initialize the objects of its class. It is

 

special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class it’s created.

A constructor is declared and defined as follows:

 

class sample

{

 

int m,n;

public: sample()

 

{

m = n = 0;

}

//  some code

};

void main()

{

sample s; // object is created

//  some code

}

 

During the object creation, it also initializes the data member’s m and n to 0.

 

Default Constructor:

A constructor that accepts no parameters is called the default constructor.

 

Characteristics of a constructor:

 

·        They should be declared in the public section

·        They are invoked automatically when the objects are created

·        They do not have return types

·        They cannot be inherited

 

·        They can have default arguments

·        It cannot be virtual

·        We cannot refer to their addresses

·        An object with a constructor or destructor can not be used as a member of a union

·        They make ‘implicit calls’ to the operators new and delete when memory allocation is required

 

Copy constructor:

 

A copy constructor takes a reference to an object of the same class as itself as an argument.

Example:

 

It sets the value of every data element of s3 to the value of the corresponding data element of s2

 

Constructors with default arguments:

 

It is possible to define constructors with default arguments. Example:

 

Inside the class definition: sample(int a, int b = 10) Inside the main():

sample s2(20);

 

assigns 20 to x and 10 to y. where as

sample s5(25,35) ; assigns 25 to x and 35 to y

 

(ii).List out the rules for overloading operator.

 

OPERATOR OVERLOADING:

 

Operator overloading means giving additional meaning to existing operators

By operator overloading an existing operator can be made to perform different operations

than the stipulated one.

It doesn’t change the meaning and precedence of the original operator.

Almost all the operators in c++ can be overloaded except the following

 

o Sizeof ()

o Conditional operator (?:)

o Scope resolution operator (::)

o Class member access operator (.,.*)

 

SYNTAX:

 

Return-type operator op-symbol(argument list)

{

body of the function;

}

 

Generally there are three general classifications of operator namely Operator

Unary binary ternary

 

Among the above unary and binary operators can be overloaded and ternary operator cannot be overloaded.

 

6. Compare and contrast the following control structure with example:

(i). break statement & continue statement.

 

Break continue

 

a) Used to terminate the loops or to Used to transfer the control to the Exit loop from a switch. Start of loop

 

b)    The break statement when executed The continue statement when causes Immediate termination of loop executed caused immediate containing it. termination of the current iteration of the loop.

 

(ii). Do – while and the while statement.

 

While loop do-while loop

 

c)  The while loop tests the condition The do-while loop tests the condition before each iteration after the first iteration

d) If the condition fails initially the loop Even if the condition fails initially Is skipped entirely even in the first the loop is executed once in a Iteration.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming and Data Structure : Data Abstraction & Overloading : Important Questions and Answers: Data Abstraction & Overloading |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.