Sie sind auf Seite 1von 19

EC6301 Object Oriented Programming and Data Structures

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY


TWO MARKS
EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES
UNIT – I
2 MARKS:
1. Define object oriented programming?
OOP is an approach that provides a way of modularizing programs by creating
partitioned memory areas for both data and functions that can be used as templates for
creating copies of such modules on demand.

2. List some features of OOP?


 Emphasis is on data rather than procedures.
 Programs that are divided into what are known as objects.
 Follows bottom – up approach in program design.
 Functions that operate on the data of an object are tried together in the data structure.

3. What do you mean by object?


Objects are basic run-time entities in an object-oriented system. They may represent a
person, a place, a bank account, a table of data or any item that the program has to handle.
Each object has the data and code to manipulate the data and theses objects interact with
each other.

4. What do you mean by reusability?


The process of adding additional features to an existing class without modifying it is
known as ‘Reusability’. The reusability is achieved through inheritance. This is possible by
deriving a new class from an existing class. The new class will have the combined features of
both the classes.

5. Define Compile time polymorphism / Early Binding / static Binding


Compile time polymorphism is implemented by using the overloaded functions and
overloaded operators. The overloaded operators or functions are selected for invokation by
matching arguments both by type and number. This information is known to the compiler at
the compile time therefore the compiler is able to select the appropriate function for a particular
function call at the compile time itself.
This is called as ‘Early Binding’ or ‘Static Binding’ or ‘Static Linking’ or ‘Compile time
polymorphism’. Early binding simply means an object is bound to its function call at the
compile time.

6. Define Runtime Polymorphism?


At runtime, when it is known what class objects are under consideration, the
appropriate version of the function is invoked. Since the function is linked with a particular
class much later after its compilation, this process is termed as ‘late binding’. It is also

1
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

known as dynamic binding because the selection of the appropriate function is done
dynamically at run time. This runtime polymorphism can be achieved by the use of
pointers to objects and virtual functions.

7. List out the benefits of OOPS.


 Through inheritance, we can eliminate redundant code and extend the use of existing
classes.
 The principle of data hiding helps the programmer to build secure programs.
 It is possible to have multiple instances of an object to co-exist without any
interference.
 Object oriented systems can be easily upgraded from small to large systems.
 Software complexity can be easily managed.

8. List out the applications of OOPS.


 Real time systems
 Simulation and modeling
 Object oriented data bases
 AI and expert systems
 Neural networks and Parallel programming
 Decision support and Office automation systems
 CAD/CAM systems

9. What is an expression?
An expression is a combination of operators, constants and variables arranged as per
rules of the language. It may include function calls which return values.

10. Define keywords?


Keywords are explicitly reserved identifiers and cannot be used as names for the
program variables or other user defined program elements.

11. Why do we need the preprocessor directive #include <iostream.h>?


This directive causes the preprocessor to add the contents of the iostream.h file to the
program. It contains the declarations for the identifier cout and the operator <<. It contains
function prototypes for the standard input output functions.

12. What is formal parameter?


If a function is to use arguments, it must declare variables that will accept the values
of the arguments. These variables are called the formal parameters of the function.
Example:
int max(int a , int b) // Variables a and b are formal parameters
{
if(a>b) return a;
return b; }
2
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

13. What is global variable?


Global variables are known throughout the program and may be used by any piece
of code. Also, they will hold their value throughout the program’s execution.

14. What is the use of exit() function?


The exit() function causes immediate termination of the entire program, forcing a
return to the operating system.

The general form :


Void exit(int return code);
The value of return code is returned to the calling process, which is usually the
operating system. Zero is generally used as a return code to indicate normal program
termination.

15. Define structure.


A Structure is a group of items in which each item is identified by its own
identifier, each of which is known as a member of the structure.

16. What is the general form of a class definition in C ++

Class class_name
{
Private:
Variable declaration;
Function declaration;
Public:
Variable declarations;
Function declarations;};

17. Differentiate Structure and Classes


Structures Classes
1. By default the members of the 1. By default the members of Class are private.
structure are public.
2. Data hiding is not possible 2. Data hiding is possible
3. Sturcture data type cannot be treated 3. Objects can be treated like built-in-types by
like built-in-type means of operator overloading.
4. Structure are used only for holding 4. Classes are used to hold data and functions
data
5. Keyword ‘struct’ 5. Keyword ‘class’

3
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

18. How objects are created?


Once a class has been declared, we can create variables of that type by using the
class name .The class variables are known as objects .The necessary memory space is
allocated to an object at the time of declaration.
Ex : class student // class declaration
{
char name[10];
int rollno;
public :
void getdata( );
void putdata ();
};
student s1, s2; // memory for s1,s2 are created.s1,s2 are objects.

19. How the members of a class can be accessed?


The private data of a class can be accessed only through the member functions of
that class.
The format for calling a member function:
Objectname.function_name(actual _arguments);

20. What is nesting of classes?


A class can contain objects of other classes as its members. It is called nesting of classes.
Class A{
….};
Class B
{ A a;
};

21. Define local classes.


Classes can be defined and used inside a function or a block. Such classes are called
local classes.
void test( int a) //function
{ …..
class student //local class
{ …. …
};

student s1(a); //create local class object
…..
}

4
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

22. What is the use of reference variables?


A reference variable provides an alias (alternate name) for a previously defined
variable. A reference variable is created as follows:
Datatype & reference-name =variablename;
Example:
int i=10;
int &sum=i;
cout<<sum; // output:10
sum=100;
cout<<i; // output:100

23. Give the various types of constructors.


There are four types of constructors. They are
 Default constructors – A constructor that accepts no parameters
 Parameterized constructors – The constructors that can take arguments
 Copy constructor – It takes a reference to an object of the same class as itself as an
argument
 Dynamic constructors – Used to allocate memory while creating objects

24. What do you mean by automatic initialization of objects?


C+ + provides a special member function called the constructor which enables an
object to initialize itself when it is created. This is known as automatic initialization.
25. Define default constructor?
A constructor that accepts no parameters is called default constructor.
Ex : The default constructor for Class A is
A:: A( ).

26. What is parameterized constructor?


The constructor that take arguments are called parameterized constructor. When a
constructor has been parameterized the object declaration statement such as integer will not
work.

27. What is the advantage of using dynamic initialization?


The advantage of using dynamic initialization is that various initialization formats can
be provided using overloaded constructors.

28. What are copy constructors? Explain with example?


The constructor that creates a new class object from an existing object of the same
class. Eg:- integer i2(i1) or integer i2 = i1 would define the object i2 at the same time initialize
the values of i1.
5
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

29. What do you mean by constructor overloading?


When more than one constructor function is defined in a class, then it is called
constructor overloading.

30. What do you mean by Dynamic constructors?


The constructors can be used to allocate memory while creating objects. This will
enable the system to allocate the right amount of memory for each object, thus resulting in
saving of memory. Allocation of memory to objects at the time of their construction is known
as dynamic construction of objects. The memory is allocated with the help of ‘new’ operator.

31. What do you mean by destructor?


 Destructor is used to destroy the object that have been created by a constructor.
 It is a member function, whose name is same as the class name preceded by a tilde.
 The destructor never takes any arguments nor does it return any value.
 It will be invoked implicitly by the compiler upon exit from the program to clean up
storage.
e.g., ~integer ( ) { }

32. Which constructor gets called when we create an array of objects?


Default constructor

33. How do you allocate / unallocate an array of things?


Use “p = new T[n]” for allocating memory and “delete[] p” is for releasing of
allocated memory. Here p is the array of type T and of size n.
Example:
Fred*p=new Fred[100]; // allocating 100 Fred objects to p
... delete[]p; //release memory
Any time we allocate an array of objects via new , we must use [] in the delete
statement. This syntax is necessary because there is no syntactic difference between a
pointer to a thing and a pointer to an array of things.

34. Can you overload the destructor for class?


 No. You can have only one destructor for a class. It's always called Fred::~Fred(). It
never takes any parameters, and it never returns anything.
 You can't pass parameters to the destructor anyway, since you never explicitly call a
destructor.

35. What is the advantage of using dynamic initialization?


The advantage of using dynamic initialization is that various initialization formats
can be provided using overloaded constructor.

6
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

36. What do you mean by nesting of member functions?


A member function can be called by using its name inside another member function
of the same class. This is known as nesting of member functions.

37. What are the two ways of defining member functions?


Member functions can be defined in two places
 Outside the class definition
 Inside the class definition

38. What is a const member function?


If a member function does not alter any data in the class, then we may declare it as a
const member function.
e.g. : void getbalance ( ) const;
void mul(int,int) const;

39. What are the special characteristics of a friend function?


 It is not in the scope of the class to which it has been declared as friend.

 Since it is not in the scope of the class, it cannot be called using the object of the class.
 It can be invoked like a normal function without the help of any object
 Unlike member functions it cannot access the member names directly and has to use
an object name and dot membership operator with each member name
 Usually it has the object as arguments.

40. Explain about ‘static’ variables.


The special characteristics of static member variable are
 It is initialized to zero when the first object of its class is created.
 Only one copy of that member is created for the entire class, no matter how
many objects are created.
 It is visible only within the class, but its lifetime is the entire program

41. Explain about static member functions.


 A member function can have access to only other static members declared in
the same class
 A static member function can be called using the class name as follows
Classname :: function name;
42. What are the properties of a static data member?
The properties of a static data member are,
 It is initialized to zero when the first object is created and no other initialization is
permitted.

7
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

 Only one copy of that member is created and shared by all the objects of that class.
 It is visible only within the class, but the life time is the entire program.
43. Give the operator in C++ which cannot be overloaded?
 Sizeof ->size of operator
 :: ->scope resolution opertor
 ?: -> conditional operator
 . ->Membership operator
 .* ->pointer to member operator

44. How can we overload a function?


With the help of a special operator called operator function. The general form of an
operator function is:
Return type class name :: operator op(arg list)

45. What are the steps that involves in the process of overloading?
 Creates a class that defines the data type that is to be used in the overloading
operation.
 Declare the operator function operator op() in the public part of a class.
 Define the operator function to implement the required operation.

46. How do you overload a dereferencing operator ->?


The dereferencing operator ->, also called the class member operator, can be used as
a unary postfix operator. The general usage is shown here: Object->element;
Here, object is the object that activates the call. The operator ->( ) function must
return a pointer to an object of the class that operator ->( ) operates upon. The element must
be some member accessible within the object. Note: An operator ->( ) function must be a
member of the class upon which it works.

47. How do you overload a comma operator?


The comma is a binary operator. We can make an overloaded comma perform any
operation we want. If we want the overloaded comma to perform in a fashion similar to its
normal operation, then our version must discard the values of all operands except the right
most. The right most value becomes the result of the comma operation.

48. State the difference between a constructor and destructor.

Constructor Destructor
1.A constructor is used to initialize the A destructor is used for releasing dynamically
object allocated memory
2.No symbol precedes the class name A tilde symbol precedes the class name
3.Constructors can be overloaded Destructors cannot be overloaded
8
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

16 MARKS:
1. a) Compare object oriented methodology with structure programming methodology with an
example.
b) What is meant by return by reference? Explain.
2 . Explain the following with example.
a. Parameterized constructor
b. Copy constructor
c. Default argument constructor
d. Dynamic constructor
3. Write an operator overloading program for manipulating matrices.
4. Explain friend class and friend function with example.
5. What are access specifiers? How are they used to protect data in c++?
6. What is function polymorphism (overloading)? Explain with suitable example.
7. Explain the method of invoking function dynamically.

UNIT II

1.What is meant by inheritance and give its types?


The mechanism of deriving a new class from an old one is called inheritance. The old
class is referred to as the base class and the new one is called the derived class or the subclass.
 Single inheritance
 Multiple inheritance
 Multilevel inheritance
 Hierarchical inheritance
 Hybrid inheritance

2. Give the syntax for inheritance.


The syntax of deriving a new class from an already existing class is given by,
class derived-class : visibility-mode base-class
{
body of derived class
}

3. What's the difference between public, private, and protected?


 A member (either data member or member function) declared in a private section of
a class can only be accessed by member functions and friends of that class
 A member (either data member or member function) declared in a protected section
of a class can only be accessed by member functions and friends of that class, and by
member functions and friends of derived classes
 A member (either data member or member function) declared in a public section of a
class can be accessed by anyone.

4. Why can’t derived class access private things from base class?
To protect from future changes to the base class. Derived classes do not get access to
private members of a base class. This effectively "seals off" the derived class from any changes
made to the private members of the base class.

9
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

5. What is an ABC?
An abstract base class. An ABC is a class that has one or more pure virtual member
functions. You cannot make an object (instance) of an ABC.

6. What's the difference between how virtual and non-virtual member functions are called?
Non-virtual member functions are resolved statically. That is, the member function is
selected statically (at compile-time) based on the type of the pointer (or reference) to the object.
In contrast, virtual member functions are resolved dynamically (at run-time). That is, the
member function is selected dynamically (at run-time) based on the type of the object, not the
type of the pointer/reference to that object. This is called "dynamic binding."

7. What are the rules for virtual function?


 They cannot be static members
 They are access by using object pointers
 A virtual function can be a friend of another class.

8. What is pure virtual function?


A pure virtual function has no definition relative to the base class. Only the function's
prototype is included. To make a pure virtual function, use this general form: virtual type
func-name(parameter-list)=0; The key part of this declaration is the setting of the function equal
to Zero .When a virtual function is made pure, it forces any derived class to override it.

9. How ambiguity is resolved in inheritance?


In the inheritance when more than one class has a function with the same name and
same type signature, then there exists ambiguity. It can be solved by using the class resolution
operation with the function.

Eg: A
B
class A
{ public:
void display()
{ cout<<"A"; }
};
class B : public A
{ public:
void display()
{ cout<<"B"; }
};
void main()
{ B b;
b.A :: display();

b.B :: display();
}
output:

10
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

10. What is a virtual base class?


Multipath inheritance may lead to duplication of inherited members from a
'grandparent' base class. This may be avoided by making the common base class as
virtual base class.
When a class is made a virtual base class, only one copy of that class is
inherited, regardless of how many inheritance paths exist between the virtual base
class and a derived class.

11. What is an abstract class and concrete 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.An abstract class is one which defines an
interface, but does not necessarily provide implementations for all its member
functions. An abstract class is meant to be used as the base class from which other
classes are derived. The derived class is expected to provide implementations for the
member functions that are not implemented in the base class. A derived class that
implements all the missing functionality is called a concrete class.

12. What does 'this' pointer refer to and its application?


A unique keyword called this to represent an object has invokes a member
function. 'this' is a pointer that points to the object for which this function was called.
Application:It is used to return the object it points to.
Eg: The statement
return *this;
inside a member function will return the object that invoked the function.

13. How can we differentiate composition over inheritance?


Composition over inheritance (or Composite Reuse Principle) in object-oriented
programming is a technique by which classes may achieve polymorphic behavior and code
reuse by containing other classes that implement the desired functionality instead of through
inheritance.

14. Define virtual destructors.


Virtual destructor is a destructor that is declared with keyword ‘virtual’ in the base
class. This makes all derived class destructors virtual even though they do not have the same
name as the base class destructor.

15. Give the visibility of base class members in derived class?

Base Class Derived Class Visibility


Visibility Protected
Public derivation Private derivation
derivation
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected

16 MARKS:
1. Write a c++ program to illustrate the concept of base class and derived class.

11
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
2. Explain object composition with example.
3. Explain the types of inheritance with example .
4. Write a c++ program to illustrate the following concepts.
a. Abstract base class.
b. This pointer
5. Explain the visibility and accessibility of private, protected and public inheritance.
6. Write a program to illustrate the significance of virtual function and virtual
destructor.
7. Illustrate casting class pointers with an example in c++.

UNIT III
2 MARKS:

1. Define an Abstract Data Type (ADT)


An abstract data type is a set of operations. ADTs are mathematical
abstractions;nowhere in an ADT’s definition is there any mention of how the set of
operations isimplemented. Objects such as lists, sets and graphs, along with their
operations can beviewed as abstract data types.

2. What are the advantages of modularity?


• It is much easier to debug small routines than large routines
• It is easier for several people to work on a modular program simultaneously
• A well-written modular program places certain dependencies in only one
routine, making changes easier.

3. Define Linked Lists


Linked list consists of a series of structures, which are not necessarily adjacent
inmemory. Each structure contains the element and a pointer to a structure containing
itssuccessor. We call this theNext Pointer. The last cell’sNext pointer points to NULL.

4. List the basic operations carried out in a linked list


The basic operations carried out in a linked list include:
• Creation of a list
• Insertion of a node
• Deletion of a node
• Modification of a node
• Traversal of the list

5. List out the advantages and disadvantages of using a linked list


Advantages:
• It is not necessary to specify the number of elements in a linked list during its
declaration
• Linked list can grow and shrink in size depending upon the insertion and deletion
that occurs in the list
• Insertions and deletions at any place in a list can be handled easily and efficiently

12
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
• A linked list does not waste any memory space
Disadvantages:
• Searching a particular element in a list is difficult and time consuming
• A linked list will use more storage space than an array to store the same number
of elements

6. List out the applications of a linked list


Some of the important applications of linked lists are manipulation of
polynomials, sparse matrices, stacks and queues.

7. State the difference between arrays and linked lists


Arrays Linked Lists
Size of an array is fixed Size of a list is variable
It is necessary to specify the number of It is not necessary to specify thenumber of
elements duringdeclaration. elements duringdeclaration
Insertions and deletions are Insertions and deletions are carried
somewhat difficult out easily
It occupies less memory than alinked list for It occupies more memory
the same number ofelements

8. Define a stack
Stack is an ordered collection of elements in which insertions and deletions are
restricted to one end. The end from which elements are added and/or removed is referred
to as top of the stack. Stacks are also referred as piles, push-down lists and last-in-first-
out (LIFO) lists.

9. List out the basic operations that can be performed on a stack


The basic operations that can be performed on a stack are
• Push operation
• Pop operation
• Peek operation
• Empty check
• Fully occupied check

10. Mention the advantages of representing stacks using linked lists than arrays
• It is not necessary to specify the number of elements to be stored in a stackduring its
declaration, since memory is allocated dynamically at run timewhen an element is
added to the stack
• Insertions and deletions can be handled easily and efficiently
• Linked list representation of stacks can grow and shrink in size withoutwasting
memory space, depending upon the insertion and deletion that occursin the list
• Multiple stacks can be represented efficiently using a chain for each stack.

11. Define a queue and list its operations.


Queue is an ordered collection of elements in which insertions are restricted toone end
called the rear end and deletions are restricted to other end called the front end.Queues are
also referred as First-In-First-Out (FIFO) Lists.
Operations: Enqueue and Dequeue

12. Define a priority queue

13
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
Priority queue is a collection of elements, each containing a key referred as thepriority
for that element. Elements can be inserted in any order (i.e., of alternatingpriority), but are
arranged in order of their priority value in the queue. The elements aredeleted from the queue
in the order of their priority (i.e., the elements with the highestpriority is deleted first). The
elements with the same priority are given equal importanceand processed accordingly.

13. State the difference between queues and linked lists


The difference between queues and linked lists is that insertions and deletionsmay
occur anywhere in the linked list, but in queues insertions can be made only in therear end
and deletions can be made only in the front end.

14. List the applications of stacks


• Towers of Hanoi
• Reversing a string
• Balanced parenthesis
• Recursion using stack
• Evaluation of arithmetic expressions

15. List the applications of queues


• Jobs submitted to printer
• Real life line
• Calls to large companies
• Access to limited resources in Universities
• Accessing files from file server

16.Convert the infix expression to postfix expression.(a+b^c^d)*(e+f/d)


(a+(b^(c^d)))*(e+f/d)
(abcd^^+)*(efd/+)
abcd^^+efd/+*

16 MARKS:
1. Explain in detail about the linked list implementation of list ADT using an
example.
2. Explain in detail about the array implementation of list ADT using an example.
3. Explain any three applications of linked list in detail with suitable example.
4. Explain any three polynomial manipulations with suitable algorithms.
5. Explain the operations of stack with suitable pseudo code.
6. Explain the applications of stack with suitable example.
7. Explain the operations performed on queue in detail. Write a suitable pseudo code
to implement these queue operations.
8. Explain the applications of queue with suitable example.

UNIT IV
2MARKS:

1. Define a tree
A tree is a collection of nodes. The collection can be empty; otherwise, a treeconsists of
a distinguished node r, called the root, and zero or more nonempty (sub) treesT1, T2,…,Tk,
each of whose roots are connected by a directed edge from r.

14
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
2. Define degree of the node
The total number of sub-trees attached to that node is called the degree of the
node.

For node A, the degree is 2 and for B and C, the degree is 0.

3. Define depth and height of a node and tree


For any node ni, the depth of ni is the length of the unique path from the root to ni.
The height of ni is the length of the longest path from ni to a leaf.
The depth of the tree is the depth of the deepest leaf. The height of the tree is equal to
the height of the root. Always depth of the tree is equal to height of the tree.

4.What do you mean by level of the tree?


The root node is always considered at level zero, then its adjacent children are
supposed to be at level 1 and so on.
Here, node A is at level 0, nodes B and C are at level 1 and nodes D and E are at level 2.

5.Define a path in a tree


A path in a tree is a sequence of distinct nodes in which successive nodes are
connected by edges in the tree.

6.Define a full binary tree and complete binary tree


A full binary/perfect binary tree is a tree in which all the leaves are on the same level
and every non-leaf node has exactly two children. The number of nodes n in a perfect binary
tree can be found using this formula: n = 2h+1-1 where h is the depth of the tree.
A complete binary tree is a tree in which every non-leaf node has exactly two children
not necessarily to be on the same level.

7.State the properties of a binary tree


• The maximum number of nodes on level n of a binary tree is 2n-1, where n≥1.
• The maximum number of nodes in a binary tree of height n is 2n-1, where n≥1.
• For any non-empty tree, nl=nd+1 where nl is the number of leaf nodes and nd is the
number of nodes of degree 2.

8. What is meant by binary tree traversal and different binary tree traversal
techniques?
Traversing a binary tree means moving through all the nodes in the binary tree,
visiting each node in the tree only once.
• Preorder traversal
• Inorder traversal

15
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
• Postorder traversal
• Level order traversal

9. State the merits and demerits of linear representation of binary trees.


Merits :
• Storage method is easy and can be easily implemented in arrays
• When the location of a parent/child node is known, other one can be determined
easily
• It requires static memory allocation so it is easily implemented in all programming
language
Demerits:
Insertions and deletions in a node take an excessive amount of processing time due to
data movement up and down the array.

10. State the merit and demerits of linked representation of binary trees.
Merits:
Insertions and deletions in a node involve no data movement except the
rearrangement of pointers, hence less processing time.
Demerits:
• Given a node structure, it is difficult to determine its parent node
• Memory spaces are wasted for storing null pointers for the nodes, which have one or
no sub-trees • It requires dynamic memory allocation, which is not possible in some
Programming language.

11. Define a set and its applications.


A set S is an unordered collection of elements from a universe. An element
cannotappear more than once in S. The cardinality of S is the number of elements in S.
Anempty set is a set whose cardinality is zero. A singleton set is a set whose cardinalityis
one.
• Maintaining a set of connected components of a graph
• Maintain list of duplicate copies of web pages
• Constructing a minimum spanning tree for a graph

12.What do you mean by union-by-weight?


Keep track of the weight ie)size of each tree and always append the smaller tree to
the larger one when performing UNION.

13. Define Graph.


A graph G consist of a nonempty set V which is a set of nodes of the graph, a setE
which is the set of edges of the graph, and a mapping from the set for edge E to a set ofpairs
of elements of V. It can also be represented as G=(V, E).

14.DifferentiateBFSandDFS.

No. DFS BFS


1. Backtracking is possible from a Backtracking is not possible
dead end
2. Vertices from which exploration is The vertices to be explored are
incomplete are processed in a organized as a
3. LIFO order
Search is done in one particular FIFOvertices
The queue in the same level are
direction maintained
parallely
16
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures

15. What is meant by an adjacency matrix?


1 𝑖𝑓 𝑉𝑖𝑗 ∈ 𝐺 𝑓𝑜𝑟 𝑖 ≤ 𝑗 ≤ 𝑛
aij = {
0 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

16. What is the degree of a graph?


The degree (or valency) of a vertex of a graph is the number of edgesincident to the
vertex, with loops counted twice. Vertex which has the maximum degree is called the degree
of a graph.

16 MARKS:
1. What are the types of binary trees? Explain in detail about the binary tree
representation.
2. Explain the tree traversal techniques in detail with suitable example.
3. Illustrate the application of trees using necessary examples.
4. How can a graph be represented in different ways? Explain each representation in
detail.
5. What are graph traversals? Explain each of them with suitable examples.
6. Explain breadth first search algorithm for the traversal of any graph with suitable
example.
7. Explain depth first search algorithm for the traversal of any graph with suitable
example.

UNIT V

2 MARKS:

1. Difference between searching and sorting.


Searching is to search the element in the given list, whereas Sorting is to arrange the
given list in a particular order (ascending or descending order ).

2. What are the two main classifications of sorting based on the source of data?
a. Internal sorting
b. External sorting
External sorting is a process of sorting in which large blocks of data stored in
storage Devices are moved to the main memory and then sorted.
Internal sorting is a process of sorting the data in the main memory.

3. What are the various factors to be considered in deciding a sorting algorithm?


a. Programming time
b. Execution time of the program
c. Memory needed for program environment

4. What is the objective of sorting algorithms?


The problem of sorting is arranging the elements of a set into an order. Sorting
algorithms are very useful in our real world applications, some of the applications are:
1. Entries in telephone books.
2. Dictionaries in alphabetical order.

17
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
3. Working with large set of data in computers are facilitated when the data are
stored.

5. What is the advantage of quick sort?


Quick sort reduces unnecessary swaps and moves an item to a greater
distance, in one move.

6. What is the purpose of quick sort?


The purpose of the quick sort is to move a data item in the correct direction,
just enough for to reach its final place in the array.

7. What are the three basic operations that are involved in quick sort?
1. Divide
2. Conquer
3. Combine

8. What is pivot element?


The pivot is the element which is used to partition the array of elements into number
of segments. It can be used in quick sort.

9. Give the analysis of insertion sort.


1.Because of nested loops, each of which can take N iterations. Therefore it is O(N2).
2. The innermost is executed at most P+1 times for each value of P.
3. Execution time = ∑𝑁 𝑖=2 𝑖 = 2 + 3 + 4 + ⋯ + 𝑁 = 𝜃(𝑁 )
2
2
4. Average case is also 𝜃(𝑁 )

10. Sort 45, 15, 20, 5, 10 using insertion sort.


Step 1: 45 15 20 5 10
A[0] A[1] A[2] A[3] A[4]

Step 2: 15, 45, 20, 5 ,10


Step 3: 15, 45, 20, 5,10
Sorted key unsorted

15, 20, 45, 5, 10


Step 4: 5, 15, 20, 45, 10
Step 5: sorted list is 5,10,15,20,45.

11. Give the analysis of merge sort.


1. For N=1, the time to do merge sort is constant, denoted by 1.
T(1)=1.
2. To perform a merge sort for N numbers the time is equal to do two recursive merge
sorts of size N/2, plus the time to merge, which is linear.
T(N) = 2T(N/2)+N
4. Running time of merge sort is O(N log N).

12. Give the worst case and best case analysis of quick sort.
Worst case: T(N) = O(N2)
Best case : O(N log N)

13. Difference between linear/sequential search and binary search.

18
Prepared by Mr. S.Jagadeesan AP/CSE
EC6301 Object Oriented Programming and Data Structures
In the sequential search, we compare against the first item, there are at most \(n-1\)
more items to look through if the first item is not what we are looking for.
Instead of searching the list in sequence, a binary search will start by examining the
middle item. If that item is the one we are searching for, we are done. If it is not the correct
item, we can use the ordered nature of the list to eliminate half of the remaining items. If the
item we are searching for is greater than the middle item, the upper half of the array will be
considered for further searching. Otherwise, lower half of the array will be considered.

14. What is the worst case and best case no of comparison in linear search?
Best case : 1 step (i.e.,) O(1)
Worst case : n steps (i.e.,) O(n)

15. Give the worst case and best case analysis of binary search algorithm.
Worst case performance: O(log n)
Best case performance: O(1)

16. What are connected components?


A connected component (or just component) of an undirected graph is a subgraph in
which any two vertices are connected to each other by paths, and which is connected to no
additional vertices in the supergraph.

16 MARKS:

1. Explain the insertion sort with its time complexities.


2. Explain quick sort with its time complexities.
3. Write the algorithms of merge sort and explain in detail.
4. How do you perform linear search? Illustrate with example.
5. How do you perform binary search? Illustrate with example.

19
Prepared by Mr. S.Jagadeesan AP/CSE

Das könnte Ihnen auch gefallen