0% found this document useful (0 votes)
17 views

OOPS_Data structure

Uploaded by

creator.kruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

OOPS_Data structure

Uploaded by

creator.kruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

OOP

1. Differentiate between base call and derived class

Base Class: A base class is a class in Object-Oriented Programming language, from which other
classes are derived. The class which inherits the base class has all members of a base class as well as
can also have some additional properties. The Base class members and member functions are
inherited to Object of the derived class. A base class is also called parent class or superclass.

Derived Class: A class that is created from an existing class. The derived class inherits all members
and member functions of a base class. The derived class can have more functionality with respect to
the Base class and can easily access the Base class. A Derived class is also called a child
class or subclass.

2. Define data encapsulation

Encapsulation in C++ is defined as the wrapping up of data and information in a single unit. In Object
Oriented Programming, Encapsulation is defined as binding together the data and the functions that
manipulate them.

Consider a real-life example of encapsulation, in a company, there are different sections like the
accounts section, finance section, sales section, etc. Now,

 The finance section handles all the financial transactions and keeps records of all the data
related to finance.
 Similarly, the sales section handles all the sales-related activities and keeps records of all the
sales.

3. Discuss Classes in OOPs.

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design


applications and computer programs. It utilizes several techniques from previously established
paradigms, including modularity, polymorphism, and encapsulation.

OOP revolves around four main principles:

1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism

 Benefits of OOP- Modularity, Reusability, Pluggable and Debuggable, Enhanced Software


Maintenance

4. What is the fundamental idea of OOPs? Specirfy and four OOPs language

There are many object-oriented programming languages, including JavaScript, C++, Java, and
Python.

5. What is the difference between program module and an object ?

A program module is a self-contained independent program segment only it does not provide
security to data whereas an object is a collection of data members and member functions that
operate on data and data is provided with security.

Modular programming (also called "top-down design" and "stepwise refinement") is a software
design technique that emphasizes separating the functionality of a program into independent,
interchangeable modules, such that each contains everything necessary to execute only one aspect
of the desired functionality.

6. Explain the advantages of OOPS.

OOP stands for Object-Oriented Programming. It mainly works on Class, Object,


Polymorphism, Abstraction, Encapsulation and Inheritance. Its aim is to bind together the
data and functions to operate on them.
 OOP language allows to break the program into the bit-sized problems that can be solved
easily (one object at a time).
 The new technology promises greater programmer productivity, better quality of software
and lesser maintenance cost.
 OOP systems can be easily upgraded from small to large systems.
 It is possible that multiple instances of objects co-exist without any interference,
 It is very easy to partition the work in a project based on objects.

7. Explain the disadvantages and limitations of OOPS.


Disadvantages

 The length of the programmes developed using OOP language is much larger than the
procedural approach. Since the programme becomes larger in size, it requires more time to
be executed that leads to slower execution of the programme.

 We can not apply OOP everywhere as it is not a universal language.

 Programmers need to have brilliant designing skill and programming skill along with proper
planning because using OOP is little bit tricky.

 OOPs take time to get used to it. The thought process involved in object-oriented
programming may not be natural for some people.
 Everything is treated as object in OOP so before applying it we need to have excellent
thinking in terms of objects.

 Limitations

It requires more time and effort to learn and master the OOP concepts and principles.

It is not suitable for all types of problems

It can result in larger and slower programs.

It demands careful planning, designing, and documentation of the classes and objects
involved in the project.

It can increase the complexity and difficulty of understanding and maintaining large-scale
projects.

DATA STRUCTURES

1. What are linear and non-linear data structure ?


Linear Dats Structures - Data structure where data elements are arranged sequentially or
linearly where each and every element is attached to its previous and next adjacent

In linear data structure, single level is involved. Therefore all the elements in single run only.
Linear data structures are easy to implement because computer memory is arranged in a
linear way. Its examples are array, stack, queue, linked list, etc.

Data structures where data elements are not arranged sequentially or linearly are
called non-linear data structures. In a non-linear data structure, single level is not involved.

Therefore, all the elements in single run only. Non-linear data structures are not easy to
implement in comparison to linear data structure. It utilizes computer memory efficiently in
comparison to a linear data structure. Its examples are trees and graphs.

2. What is linkled list ?


A linked list is a linear data structure that consists of a series of nodes connected by pointers
(in C or C++) or references (in Java, Python and JavaScript). Each node contains data and a
pointer/reference to the next node in the list.

3. Name the data structure whose relationship between data elements by means of links

A linked list is a linear data structure that consists of a series of nodes connected by pointers.
Each node contains data and a pointer/reference to the next node in the list.
Types of linked lists-
Singly Linked List
Doubly Linked List
Circular Linked List
Circular Doubly Linked List
Header Linked List

4. Mention any one application of stacks


The Stack is Last In First Out (LIFO) data structure. This data structure has some important
applications in different aspect. These are like below –
o Expression Handling
o Backtracking Procedure
o Function call and return process

5. Define Array
Array is a linear data structure where all elements are arranged sequentially. It is a collection
of elements of same data type stored at contiguous memory locations.

6. What is binary tree.


A binary tree is a hierarchal data structure in which each node has at most two children. The
child nodes are called the left child and the right child.

7. 3 Marks, What is data structure, write its classification.

A data structure is a way of organizing and storing data in a computer so that it can be
accessed and manipulated efficiently. It provides a framework for managing data elements and their
relationships. They are essential for tasks like searching, sorting, and retrieving information.

Classification of Data Structures:

1. Linear Data Structures:

o These structures organize data elements sequentially.

o Examples:

 Arrays: Fixed-size collection of elements of the same data type. Elements are
accessed using an index.

 Linked Lists: Dynamic collection of nodes, where each node contains data
and a reference to the next node.

 Stacks: Follows the Last-In-First-Out (LIFO) principle. Used for managing


function calls, undo operations, etc.

 Queues: Follows the First-In-First-Out (FIFO) principle. Used for scheduling


tasks, printing jobs, etc.

Non-linear Data Structures:

These structures do not organize data sequentially.

Examples:

Trees: Hierarchical structure with a root node and child nodes. Used for representing hierarchical
relationships (e.g., file systems, organization charts).

Graphs: Consists of nodes connected by edges. Used for modeling complex relationships (e.g., social
networks, transportation networks).

8. What is transversal ? Write an algorithm for transversal in linear array.


Traversal in a Linear Array is the process of visiting each element once. Traversal is done by
starting with the first element of the array and reaching to the last. Traversal operation can
be used in counting the array elements, printing the values stored in an array, updating the
existing values or summing up all the element values.

Algorithm- Traversal in a Linear Array


To apply process on this array of ten elements a counter variable is needed to store the
address of current element. After processing each element the counter is incremented and
same process is applied to the next element. This is repeated until the counter reaches the
last index of the array.

9. Explain the memory representation of one dimensional array.

Memory representation in a one-dimensional array: Elements are stored in contiguous memory


locations.Each element is identified by its index or position within the array.Accessing elements is
done sequentially based on the subscript
10. Define the following w.r.t. binary tree
A tree is a non-linear data structure. It has no limitation on the number of children. A binary
tree has a limitation as any node of the tree has at most two children: a left and a right child.
1. Root- Node in which no edge is coming from the parent. Example -node A
2. Sub tree- A subtree of a tree T is a tree S consisting of a node in T and all of its
descendants in T. The subtree corresponding to the root node is the entire tree; the
subtree corresponding to any other node is called a proper subtree.
For example, in the following case, Tree1 is a subtree of Tree2.
3. Depth- Longest upward path from yje leaf to the root node
4. Degree- Number of children of a particular parent. Example- Degree of A is 2 and
Degree of C is 1. Degree of D is 0.

11. Explain the various operation performed on Queue (FIFO)/Stack (LIFO) ?


A Stack is a linear data structure that follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies
that the element that is inserted last, comes out first and FILO implies that the element that
is inserted first, comes out last.
Key Operations on Stack Data Structures

 Push: Adds an element to the top of the stack.


 Pop: Removes the top element from the stack.
 Peek: Returns the top element without removing it.
 IsEmpty: Checks if the stack is empty.
 IsFull: Checks if the stack is full (in case of fixed-size arrays).

12.Explain the memory representation of single linked list.


A singly linked list is a fundamental data structure in computer science and programming, it
consists of nodes where each node contains a data field and a reference to the next node in
the node. The last node points to null, indicating the end of the list. This linear structure
supports efficient insertion and deletion operations, making it widely used in various
applications.
12. 5 marks, Write an algorithm to search element in an array using linear search.

Linear search is a type of sequential searching algorithm. In this method, every element within the
input array is traversed and compared with the key element to be found. If a match is found in the
array the search is said to be successful; if there is no match found the search is said to be
unsuccessful and gives the worst-case time complexity.

Linear Search Algorithm

The algorithm for linear search is relatively simple. The procedure starts at the very first index of the
input array to be searched.

Step 1 − Start from the 0th index of the input array, compare the key value with the value present in
the 0th index.

Step 2 − If the value matches with the key, return the position at which the value was found.

Step 3 − If the value does not match with the key, compare the next element in the array.

Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was
found.

Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit the
program.
13. 5 marks, Write an algorithm to search element in an array using binary search.

Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half. The idea of binary search is to use the information that the array is sorted and
reduce the time complexity to O(log N).
14. Write an algorithm to perform PUSH and POP operation on stack

Stacks are a type of container adaptors that follow LIFO(Last In First Out) property, where a new
element is added at one end and an element(at the top) is removed from that end only. Basically,
the insertion and deletion happen on the top of the stack itself.
tack::push()
push() function is used to insert or ‘push’ an element at the top of the stack. This is an inbuilt
function from C++ Standard Template Library(STL). This function belongs to the <stack> header
file. The element is added to the stack container and the size of the stack is increased by 1.
Syntax: stackname.push(value)
Parameters: The value of the element to be inserted is passed as the parameter.
Result: Adds an element of value the same as that of the parameter passed at the top of the
stack.
Examples: Input : mystack
mystack.push(6);
Output : 6

Input : mystack
mystack.push(0);
mystack.push(1);
Output : 0, 1
Errors and Exceptions:
 Shows an error if the value passed doesn’t match the stack type.
 Shows no exception throw guarantee if the parameter doesn’t throw any exception.

Output - 2 1 0

stack::pop()
The pop() function is used to remove or ‘pop’ an element from the top of the stack(newest or
the topmost element in the stack). This is an inbuilt function from C++ Standard Template
Library(STL). This function belongs to the <stack> header file. The element is removed from the
stack container and the size of the stack is decreased by 1.
Syntax: stackname.pop()
Parameters: No parameters are passed.
Result: Removes the newest element in the stack or basically the top element.
Output - 2 1

15. Write an algorithm for insertion sort


Insertion sort is a simple sorting algorithm that works by building a sorted array one element at
a time. It is considered an ” in-place ” sorting algorithm, meaning it doesn’t require any
additional memory space beyond the original array.

Initial:
 Current element is 23
 The first element in the array is assumed to be sorted.
 The sorted part until 0th index is : [23]
First Pass:
 Compare 1 with 23 (current element with the sorted part).
 Since 1 is smaller, insert 1 before 23 .
 The sorted part until 1st index is: [1, 23]
Second Pass:
 Compare 10 with 1 and 23 (current element with the sorted part).
 Since 10 is greater than 1 and smaller than 23 , insert 10 between 1 and 23 .
 The sorted part until 2nd index is: [1, 10, 23]
Third Pass:
 Compare 5 with 1 , 10 , and 23 (current element with the sorted part).
 Since 5 is greater than 1 and smaller than 10 , insert 5 between 1 and 10
 The sorted part until 3rd index is : [1, 5, 10, 23]
Fourth Pass:
 Compare 2 with 1, 5, 10 , and 23 (current element with the sorted part).
 Since 2 is greater than 1 and smaller than 5 insert 2 between 1 and 5 .
 The sorted part until 4th index is: [1, 2, 5, 10, 23]
Final Array:
 The sorted array is: [1, 2, 5, 10, 23]

16. Apply binary search 10,20,30, 35,40,45,50,55, 60 and search for the item 35.
To understand the working of binary search, consider the following illustration:Consider
an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.

The Binary Search Algorithm can be implemented in the following two ways
 Iterative Binary Search Algorithm
 Recursive Binary Search Algorithm

17. Write an algorithm for insertion/delete elements into a queue.

18. Explain the operation performed on data structure.


Data Structure is the way of storing data in computer’s memory so that it can be used easily and
efficiently. There are different data-structures used for the storage of data. It can also be defined
as a mathematical or logical model of a particular organization of data items. The representation
of particular data structure in the main memory of a computer is called as storage structure.
For Examples: Array, Stack, Queue, Tree, Graph, etc.
Operations on different Data Structure:
There are different types of operations that can be performed for the manipulation of data in
every data structure. Some operations are explained and illustrated below:
 Traversing: Traversing a Data Structure means to visit the element stored in it. It visits data in
a systematic manner. This can be done with any type of DS.
Traversing:
Accessing each data exactly once in the data structure so that each data item in traversed or
visited.
In data structure operation, traversing operation of an array, every element of an array is
accessed exactly for once for processing.
it is also called the visiting of an array.
For Example:
Let CB is a Linear Array (unordered) with N elements.

1 //Write a Program which performs traversing operation.


2 #include <stdio.h>
3 void main()
4 {
5 int CB[] = {3,5,7,9,11};
6 int i, n = 5;
7 printf("The array elements are:\n");
8 for(i = 0; i < n; i++)
9 {
10 printf("CB[%d] = %d \n", i, CB[i]);
11 }
12 }

Output Result

1 The array elements are:


2 CB[0] = 3
3 CB[1] = 5
4 CB[2] = 7
5 CB[3] = 9
6 CB[4] = 11

Searching: it is Finding the location of data within the data structure that satisfies the searching
condition or the criteria.
Inserting: Adding new data in the data structure is referred to as insertion.
Deleting: in this operation Removing data from the data structure is referred to as deletion.
Sorting: Arranging the data in some logical order, for example, is numerical increasing order or
alphabetically.
Merging: Combining the data of two different sorted files into a single sorted file.
Must follow the below-given steps while Designing data structures:

First In a particular Program, Determine the logical picture of the data.

Second, Select the representation of data.

Third, Develop those operations that will be applied to

Different Data Structures are as follows:

You might also like