Data Structures Design - AD3251 - Important Questions with Answer - Unit 1 - Abstract Data Types
Data Structures Design - AD3251 - Important Questions with Answer - Unit 1 - Abstract Data Types
I Year/II Semester
QUESTION BANK
Prepared By,
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
www.BrainKart.com
4931_Grace College of Engineering, Thoothukudi
UNIT I ABSTRACT DATA TYPES
Abstract Data Types (ADTs) – ADTs and classes – introduction to OOP – classes inPython
– inheritance – namespaces – shallow and deep copying. Introduction to analysis ofalgorithms –
asymptotic notations –Divide and Conquer– recursion – analyzing recursive algorithms
Unit I – 2 Mark Questions with Answers
Variables are placeholders for representing data. In computer programming, variablesare used
to hold data.
Ex: x2+2y-2=1
A data type in a programming language is a set of data with predefined values. Examples of
data types are: integer-2 Bytes, floating point-4 Bytes, unit number, character, string, etc.
The number of bits allocated for each primitive data type depends on the programminglanguages,
the compiler and the operating system.
For the same primitive data type, different languages may use different sizes.
Depending on the size of the data types, the total available values (domain) will also change.
For example, “int” may take 2 bytes or 4 bytes. If it takes 2 bytes (16 bits), then the total
possible values are minus 32,768 to plus 32,767 (-215 to 215-1). If it takes 4 bytes (32
bits), then the possible values are between -2,147,483,648 and +2,147,483,647 (-231 to 231-1). The
same is the case with other data types.
If the system-defined data types are not enough, then most programming languages allow the
users to define their own data types, called user – defined data types.
Good examples of user defined data types are: structures in C/C + + and classes in Java. For
example, in the snippet below, we are combining many system-defined data types and calling the user
defined data type by the name “newType”.
This gives more flexibility and comfort in dealing with computer memory.
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
www.BrainKart.com
4931_Grace College of Engineering, Thoothukudi
struct newType
{
int data1;
float data2;
.
.
.
char data-n;
};
A data structure is a special format for organizing and storing data. General data structure types
include arrays, files, linked lists, stacks, queues, trees, graphs and so on.
Depending on the organization of the elements, data structures are classified into two types:
1) Linear data structures: Elements are accessed in a sequential order but it is notcompulsory
to store all elements sequentially. Examples: Linked Lists, Stacks and Queues.
2) Non – linear data structures: Elements of this data structure are stored/accessed ina non-
linear order. Examples: Trees and graphs.
4. Implementation of ADTs can be changed without requiring changes to the programthat uses
the ADTs.
7. Characteristics of Python?
Robustness
Adaptable
Reusable
Modular
For example, if a program is expecting a positive integer and instead is given a negative integer,
then the program should be able to recover gracefully from this error.
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
9. Is Python Adaptable?
Software, needs to be able to evolve over time in response to changing conditions in its
environment. Thus, another important goal of quality software is that it achieves adaptability(also called
evolvability).
Related to this concept is portability, which is the ability of software to run with minimalchange
on different hardware and operating system platforms. An advantage of writing software in Python is
the portability provided by the language itself.
Such reuse should be done with care, however, for one of the major sources of software errors
in the Therac-25 came from inappropriate reuse of Therac-20 software.
Using modularity in a software system can also provide a powerful organizing framework that
brings clarity to an implementation.
Python supports abstract data types using a mechanism known as an abstract base class (ABC).
An abstract base class cannot be instantiated (i.e., you cannot directly create aninstance of that class),
but it defines one or more common methods that all implementations of the abstraction must have.
An ABC is realized by one or more concrete classes that inherit from the abstract baseclass while
providing implementations for those method declared by the ABC.
Wrapping up on Class and Data together into single unit is called Encapsulation. Different
components of a software system should not reveal the internal details of their respective
implementations.
Encapsulation yields robustness and adaptability, for it allows to fix bugs or add new
functionality with relatively local changes to a component.
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
15. Define Class.
A class is a collection of objects. A class contains the blueprints or the prototype from which
the objects are being created. It is a logical entity that contains some attributes and methods.
A class provides a set of behaviors in the form of member functions (also known as methods),
with implementations that are common to all instances of that class.
A class determines the way that state information for each instance, it is represented in the form
of attributes (also known as fields, instance variables, or data members)
Example:
class Person:
init (self,a,b):
Print(“Sum=”,a+b)
Obj=Person(2,3);
Output:
init is a Special method that serves as the constructor of the class. Its primary responsibility
is to establish the state of a newly created credit card object with appropriate instance variables.
Example:
class Person:
init (self,a,b):
Print(“Sum=”,a+b)
Obj=Person(2,3);
This allows a new class to be defined based upon an existing class as the starting point. In object-
oriented terminology, the existing class is typically described as the base class,parent class, or superclass,
while the newly defined class is known as the subclass or child class.
Syntax:
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
Derived class inherits features from the base class where new features can be added toit. This
results in re-usability of code.
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
21. What is Multilevel Inheritance?
When a child class becomes a parent class for another child class.
Example:
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
1. Built-In
2. Global
3. Local
These have differing lifetimes. As Python executes a program, it creates namespaces asnecessary and
deletes them when they’re no longer needed.
This is the namespace that holds all the global objects. This namespace gets createdwhen the
program starts running and exists till the end of the execution.
Example of Global Namespace in Python:
text="Python"
def func():
print(text)
func()
print(text)
Output:
Python
Python
These namespaces exist as long as the functions exist. This is the reason we cannotglobally access a
variable, created inside a function.
Output:
NameError: name 'var2' is not defined
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
28. How to Copy an Object in Python?
In Python, we use = operator to create a copy of an object. It only creates a new variablethat shares
the reference of the original object.
In Python, there are two other ways to create copies:
o Shallow Copy
o Deep Copy
To make these copy work, copy module is used.
For example:
import copy
copy.copy(x)
copy.deepcopy(x)
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
Example: Copying a list using deepcopy()
import copy
Output:
old_list = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Old list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
new_list = copy.deepcopy(old_list)
print("Old list:", old_list) print("New New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
list:", new_list)
The running time of an algorithm can be calculated by executing it on various test inputs and
recording the time spent during each execution.
• Experimental running times of two algorithms are difficult to directly compare unless the
experiments are performed in the same hardware and software environments.
• Experiments can be done only on a limited set of test inputs; hence, they leave out therunning
times of inputs not included in the experiment (and these inputs may be important).
• An algorithm must be fully implemented in order to execute it to study its running time
experimentally.
Worst case
• Defines the input for which the algorithm takes a long time (slowest time to
complete).
• Input is the one for which the algorithm runs the slowest.Best
case
• Defines the input for which the algorithm takes the least time (fastest time to
complete).
• Input is the one for which the algorithm runs the fastest.
Average case
• Provides a prediction about the running time of the algorithm.
• Run the algorithm many times, using many different inputs and divide by thenumber
of trials.
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
• Assumes that the input is random.
Solution: 3nlog n− 2n = nlog n+ 2n(logn− 1) ≥ nlogn for n ≥ 2; hence, we can take c =1and n0 = 2
in this case.
Big-Theta Notation - says that two functions growing at the same rate, up to constant factors.
There are some general rules to help us determine the running time of an algorithm.
1) Loops: The running time of a loop is, at most, the running time of the statements inside the loop
(including tests) multiplied by the number of iterations.
38. How will you analyse the running time complexity of Nested Loops?
Nested loops: Analyze from the inside out. Total running time is the product of the sizesof all the
loops.
Example:
//outer loop
For(i=1;i<=n;i++)
For(j=1;j<=n;j++)
K=k+1 //constant time, c
Total time = c × n × n = c n2 = O(n2 ).
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
39. How will you analyse the running time complexity of Consecutive Statements?
For(j=1;j<=n;j++)
K=k+1 //constant time, c
40. How will you analyse the running time complexity of if-then-else statements?
If-then-else statements - Worst-case running time: the test, plus either the then part orthe else part
(whichever is the larger).
//test : constant
If(length()==0)
Return false;
Else:
For(int n=0;n<length();n++)
If(!list[n].equals(otherList.list[n]))
Return false;
Recursion is a technique by which a function makes one or more calls to itself during execution,
until the condition gets satisfied. Recursion provides a powerful alternative for performing repetitive
tasks.Example – Finding Factorial of a given number:
def factorial(n):
if n == 0:
return 1
else:
return n*factorial(n−1)
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com
42. Give the procedure for drawing an English Ruler using Recursion.
PART-B
3. Inheritance (12)
AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
All 2nd Semester Subjects
Professional English - II - HS3252 Engineering Graphics - GE3251
Statistics and Numerical Methods - Physics for Electronics Engineering -
MA3251 PH3254
Physics for Electrical Engineering - Physics for Civil Engineering - PH3201
PH3202
Materials Science - PH3251 Basic Electrical and Electronics
Engineering - BE3251
Physics for Information Science - Basic Civil and Mechanical Engineering -
PH3256 BE3255
Basic Electrical and Instrumentation Electric Circuit Analysis (Circuit
Engineering - BE3254 Theory) - EE3251
Programming in C - CS3251 Circuit Analysis - EC3251
Data Structures Design - AD3251