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

Data Structures Design - AD3251 - Important Questions with Answer - Unit 1 - Abstract Data Types

The document outlines the curriculum for the 2nd semester of a B.Tech program in Artificial Intelligence and Data Science at Anna University, detailing various subjects including Professional English, Engineering Graphics, and Data Structures Design. It includes a question bank for the Data Structures Design course, covering topics such as Abstract Data Types, classes in Python, inheritance, and various programming concepts. Additionally, it explains key programming principles and concepts, such as modularity, encapsulation, and different types of inheritance in Python.

Uploaded by

sramalingam28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Data Structures Design - AD3251 - Important Questions with Answer - Unit 1 - Abstract Data Types

The document outlines the curriculum for the 2nd semester of a B.Tech program in Artificial Intelligence and Data Science at Anna University, detailing various subjects including Professional English, Engineering Graphics, and Data Structures Design. It includes a question bank for the Data Structures Design course, covering topics such as Abstract Data Types, classes in Python, inheritance, and various programming concepts. Additionally, it explains key programming principles and concepts, such as modularity, encapsulation, and different types of inheritance in Python.

Uploaded by

sramalingam28
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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
www.BrainKart.com
4931_Grace College of Engineering,Thoothukudi

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

B.Tech. - Artificial Intelligence and Data Science

Anna University Regulation: 2021

AD3251- DATA STRUCTURES DESIGN

I Year/II Semester

QUESTION BANK

Unit- I ABSTRACT DATA TYPES

Prepared By,

Mr. S. MANICKAM, AP/CSE

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

1. What are Variables?

Variables are placeholders for representing data. In computer programming, variablesare used
to hold data.
Ex: x2+2y-2=1

2. What are Data Types?

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.

At the top level, there are two types of data types:


• System-defined data types (also called Primitive data types)
• User-defined data types

3. What are System-defined data types (Or Primitive data types)?


Data types that are defined by system are called primitive data types. The primitivedata types
provided by many programming languages are: int, float, char, double, bool, 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.

4. What are User defined 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;
};

5. What are Data Structures?


Data structure is a particular way of storing and organizing data in a computer so that it can be
used efficiently.

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.

6. What are Abstract Data Types? Or What is ADT?


An abstract Data type (ADT) is defined as a mathematical model with a collection of operations
defined on that model. Set of integers, together with the operations of union, intersection and set
difference form a example of an ADT. An ADT consists of data together with functions that operate on
that data.
Advantages/Benefits of ADT:
1.Modularity
2. Reuse

3. code is easier to understand

4. Implementation of ADTs can be changed without requiring changes to the programthat uses
the ADTs.

7. Characteristics of Python?
Robustness
Adaptable
Reusable
Modular

8. Why Python is Robustness?


A program produces the right output for all the anticipated inputs in the program’s application.
In addition, we want software to be robust, that is, capable of handling unexpectedinputs that are not
explicitly defined for its application.

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.

10. What is Reusability?


Developing quality software can be an expensive enterprise, and its cost can be offsetsomewhat
if the software is designed in a way that makes it easily reusable in futureapplications.

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.

11. What are Object-Oriented Design Principles?


Chief principles of object-oriented approach is,
• Modularity
• Abstraction
• Encapsulation

12. What is Modularity?


Modularity refers to an organizing principle in which different components of a softwaresystem
are divided into separate functional units.

Using modularity in a software system can also provide a powerful organizing framework that
brings clarity to an implementation.

13. What is Abstract base class?

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.

14. What is Encapsulation?

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:

16. Define Constructor.

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);

17. What is Inheritance?

In object-oriented programming, the mechanism for a modular and hierarchical organization is


a technique known as inheritance.

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.

18. List the types of Inheritance.


Depending upon the number of child and parent classes involved, there are four types of
inheritance in python.

19. Give example for Single Inheritance.


When a child class inherits only a single parent class.
Example:
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()

20. What is Multiple Inheritance?


When a child class inherits from more than one parent class.
Example:
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")
ob = Child()
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
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()

22. What is Hierarchical Inheritance?


Hierarchical inheritance involves multiple inheritance from the same base or parent
class.
Example:
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child1(Parent):
def func3(self):
print(" this is function 3"):
class Child3(Parent , Child1):
def func4(self):
print(" this is function 4")
ob = Child3()
ob.func1()

23. What is Scope?


Whenever an identifier is assigned to a value that definition is made with a specific scope. Top-
level assignments are typically made in what is known as global scope. Assignments made within the
body of a function typically have scope that is local to that function call.

AD3251_DSD_R2021
https://play.google.com/store/apps/details?id=info.therithal.brainkart.annauniversitynotes
4931_Grace College of Engineering, Thoothukudi
www.BrainKart.com

24. What are Namespaces?


Each distinct scope in Python is represented using an abstraction known as a
namespace. A namespace manages all identifiers that are currently defined in a given scope.
The process of determining the value associated with an identifier is known as name resolution.
In a Python program, there are three types of namespaces:

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.

25. What is Python Global Namespace?

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

26. What are Built-in Namespaces in Python?


This namespace gets created when the interpreter starts. It stores all the keywords or the built-in names. This
is the superset of all the Namespaces. This is the reason we can useprint, True, etc. from any part of the code.

27. What is Python Local Namespace


This is the namespace that generally exists for some part of the time during the execution of the program.
This stores the names of those objects in a function.

These namespaces exist as long as the functions exist. This is the reason we cannotglobally access a
variable, created inside a function.

Example of local namespace:var1="Python"


def func():
var2="Python"print(var2)
func() print(var1)
print(var2) //Error

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)

29. Give example for Python Copy methods.


Here, the copy() return a shallow copy of x. Similarly, deepcopy() return a deep copy
of x.

30. How will you create a reference in python?


A shallow copy creates a new object which stores the reference of the originalelements. So, a
shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects.
This means, a copy process does not recurse or create copiesof nested objects itself.
Example: Create a copy using shallow copy
import copy The output will be:

old_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]


ld list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
new_list = copy.copy(old_list)
print("Old list:", old_list)
print("New list:", new_list)
31. Give the procedure to create a new object from original elements.
A deep copy creates a new object and recursively adds the copies of nested objectspresent in the
original elements.

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)

32. What is the use of Algorithm analysis?


Algorithm analysis helps us to determine which algorithm is most efficient in terms of time
and space consumed.

The running time of an algorithm can be calculated by executing it on various test inputs and
recording the time spent during each execution.

from time import time


start time = time( ) # record the starting time
run algorithm
end time = time( ) # record the ending time
elapsed = end time − start time # compute the elapsed time

33. What are the Challenges of Experimental Analysis?

• 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.

34. What is runtime analysis of algorithms?


Runtime Analysis of Algorithms In general cases, we mainly used to measure and compare the
worst-case theoretical running time complexities of algorithms for the performance analysis. The fastest
possible running time for any algorithm is O (1), commonlyreferred to as Constant Running Time.

35. How to analyse the complexity of user input to an algorithm?


Algorithm analysis depends on which inputs the algorithm takes less time (performingwell) and
with which inputs the algorithm takes a long time.

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.

Lower Bound <= Average Time <= Upper Bound

36. What is Asymptotic Notation?


An Asymptotic Notations is the notation which represent the complexity of an algorithm. It is
used to study how the running time of an algorithm grows as the value of the input or the unknown
variable increases. Therefore, it is also known as the "growth rate" of analgorithm.

Big-O Notation – Upper Bound, represented as f(n) = O(g(n)).

Example-1 Find upper bound for f(n) = 3n + 8


Solution: 3n + 8 ≤ 4n, for all n ≥ 8
∴ 3n + 8 = O(n) with c = 4 and n0 = 8

Big Omega Notation - Lower Bound, represented as f(n) = Ω(g(n)).

Example : 3nlog n−2n is Ω(nlog n).

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.

Example: 3nlog n+4n+5logn is Θ(nlog n).

Solution: 3nlogn ≤ 3nlog n+4n+5logn ≤ (3+4+5) nlogn for n≥2.


37. Give example for Asymptotic Analysis.

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.

Example: // Executes n times


For(i=1;i<=n;i++)
M=m+2 //constant time, c

Total time = a constant c × n = c n = O(n).

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?

Consecutive statements: Add the time complexities of each statementExample:


X=x+1
For(i=1;i<=n;i++)
M=m+2 //constant time, c
//outer loop
For(i=1;i<=n;i++)

For(j=1;j<=n;j++)
K=k+1 //constant time, c

Total time = c0 + c1n + c2n2 = O(n2 ).

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;

Total time = c0 + c1 + (c2 + c3 ) * n = O(n).

41. What is Recursion?

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:

A Recursive Implementation of the Factorial Function

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.

• This is to draw the markings of a typical English ruler.


• For each inch, we place a tick with a numeric label.
• We denote the length of the tick designating a whole inch as the major ticklength.
• Between the marks for whole inches, the ruler contains a series of minor ticks,placed
at intervals of 1/2 inch, 1/4 inch, and so on.

Trace of the English Ruler Code:

43. Give procedure to find disk space using Recursion.


Modern operating systems define file-system directories (which are also sometimes called “folders”)
in a recursive way. Namely, a file system consists of a top-level directory, and the contents of this
directory consists of files and other directories which in turn can contain files and other directories,
and so on.

PART-B

1. Asymptotic Notation. (16)

2. Recursive implementations (8+8+8+8)

3. Inheritance (12)

4. Operator Overloading. (8)

5. Class programs. (8)

6.Functions used to analyse the algorithm. (8)

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

You might also like