This post is about Python Programming Question Paper with Solution -2021-22.
I hope you find this post useful for your next Python programming exams.
Dudes 🤔.. You want more useful details regarding this subject. Please keep in mind this as well. Important Questions For Python Programming: *Unit-01 *Unit-02 *Unit-03 *Unit-04 *Unit-05 *Short-Q/Ans *Question-Paper with solution 21-22
Section-A (Important Question in Pyhton Programming)
Q Explain the Programming cycle for Python?
Ans.
- Python’s programming cycle is dramatically shorter than that of traditional programming Cycle.
- In Python, there are no compile or link steps.
- Python programs simply import modules at runtime and use the objects they contain. Because of this, Python programs run immediately after changes are made.
- In cases where dynamic module reloading can be used, it is even possible to change and reload parts of a running program without stopping it at all.
- Since Python is interpreted, there is a rapid turnaround after program changes. And because Python’s parser is embedded in Python-based systems, it is easy to modify programs at runtime.
- Fig. 1.1.1 shows Python’s impact on the programming cycle. Python’s programming cycle
Qb What will be the output of the following Python code?
Ans: Syntax error at else.
Correction in question:
Qc. What will be the output of the following Python code?
def cube(x):
return x * x * x
× = cube(3)
print(x)
Ans. Output: 27
Qd. How do we define an Interface for an ADT? OR Explain ADT interface in Python programming?
Ans:
- ADT only defines as what operations are to be performed but not how these operations will be implemented.
- It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations.
- It is called “abstract” because it gives an implementation-independent vIew.
- The process of providing only the essentials and hiding the details is known as abstraction.
- The user of data type does not need to know how that data type is implemented, for example, we have been using primitive values like int, float, char data types only with the knowledge that these data type can operate and be performed on without any idea of how they are implemented.
- So, a user only needs to know what a data type can do, but not how it will be implemented.
Qe. How do you perform a search in Python?
Ans: Searching in Python can be done in the following two ways:
- 1. Linear search
- 2. Binary search
Section-B Long Question Answer | Question Paper-2021-22
Qa.What do you mean by Python IDE? Explain in detail.
Ans:
- IDE is a software package that consists of several tools for developing and testing the software.
- An IDE helps the developer by automating the process.
- IDEs integrate many tools that are designed for SDLC.
- IDEs were introduced to diminish the coding and typing errors.
- Some of the Python IDEs are:
- PyCharm: PyCharm assists the developers to be more productive and provides smart suggestions. It saves time by taking care of routine tasks, hence increases productivity.
- Features of PyCharm:
- It has smart code navigation, good code editor, a function for quick refactoring.
- The integrated activities with PyCharm are profiling, testing, debugging, remote development, and deployments.
- PyCharm supports Python web development frameworks, Angular JS, JavaScript, CSS, HTML and live editing functions.
- Features of PyCharm:
- Spyder: Spyder is widely used for data science works. It is mostly used to create a secure and scientific environment for Python. Spyder Python uses PyQt (Python plug-in) which a developer can add as an extension.
- Features of Spyder:
- It has good syntax highlighting and auto code completion features.
- Spyder Python explores and edits variables directly from GUI.
- It performs very well in multi-language editor.
- Features of Spyder:
- PyDev: It is an external plug-in for Eclipse and is very popular as a Python interpreter.
- Features of PyDev:
- PyDev has strong parameters like refactoring, debugging, type hinting, code analysis, and code coverage function.
- PyDev supports tokens browser, PyLint integration, interactive console, remote debugger, Unittest integration, etc.
- Features of PyDev:
- IDLE: IDLE is a basic IDE mainly used by beginner level developers.
- IDLE Python is a cross-platform IDE, hence it increases the flexibility for users.
- It is developed only in Python in collaboration with Tkinter
- GUI toolkit.
- The feature of multi-window text editor in IDLE has some great functions like smart indentation, call tips, Python colorizing, and undo option.
- It also comes with a strong debugger along with continuous breakpoints, local spaces, and global view.
- It supports browsers, editable configurations, and dialog boxes.
- Visual studio: It enables development for various platforms and has its own marketplace for extensions.
- Features of visual studio:
- It supports Python coding in visual studio, debugging, and other activities.
- It has both paid and free versions in the market with great features.
- Features of visual studio:
Qb. How can you randomize the items of a list in place in Python ?
Ans: The method shuffle() can be used to randomize the items of a list in place However, this function is not accessible directly and therefore we need to import or call this function using a random static object.
Syntax: shuffle (list_name)
Here. ‘list name’ is passed as a parameter which could be a list or tuple. The shuffle() returns a reshuffled list of items.
For example:
import random
list = [20, 16, 10, 5];
random.shuffle(list)
print “Reshuffled list:”, list random.shuffle(list)
print “Reshuffled list:, list
Output :
Reshuffled list: (16, 10, 5, 20]
Reshuffled list : (5, 20, 10, 16]
Qc. Explain Tuples and Unpacking Sequences in Python Data Structure.
Ans:
- Tuples are the sequence or series values of different types separated by commas (,).
- Values in tuples can also be accessed by their index values, which are integers starting from 0.
For example:
The names of the months in a year can be defined in a tuple:
>>> months = (January, february, March, April, May, June, July,
August, September’, ‘October’, November, December)
Creating tuples in Python:
- To create a tuple, all the items or elements are placed inside parentheses separated by commas and assigned to a variable.
- Tuples can have any number of different data items (that is, integer, float, string, list, etc.).
For examples:
1. A tuple with integer data items:
>>> tuple = (4, 2, 9, 1)
>>> print tuple
(4, 2, 9, 1) # Output
2. A tuple with items of different data types:
>>>tuple_mix = (2, 30, “Python”, 5.8, “Program”)
>>>print tuple_mix
(2, 30, ‘Python’, 5.8, ‘Program’) # Output
3. Nested tuple:
>>>nested_tuple = (“Python”, [1, 4, 2], [‘”john”, 3.9])
>>> print nested_tuple
(Python’, [1, 4, 2], [john’, 3.91) # Output
4. Tuple can also be created without parenthesis :
>>>tuple = 4.9, 6, ‘house’
>>>print tuple
(4.9, 6, house’)
# Output
Unpacking sequences:
- Unpacking allows to extract the components of the sequence into individual variables.
- Several different assignments can be performed simultaneously.
- We have multiple assignments in Python where we can have multiple LHS assigned from corresponding values at the RHS. This is an example of unpacking sequences.
- There is one restriction, the LHS and RHS must have equal length. That is, every value that is created at RHS should be assigned to LHS.
- Strings and tuples are example of sequences. Operations applicable on sequences are: Indexing, repetition, concatenation.
For example:
>>> student
(Aditya’, 27, (Python’, ‘Abha’, 303))
>>> name, roll, redcourse = student
>>> name
Output: Aditya
>>> roll
27
>>> redcourse
(Python’, ‘Abha’, 303)
- Since strings are also sequences, we can also get individual characters in the string using unpacking operation.
For example:
>>> x1, ×2, x3, ×4 = ‘Abha’
>>> print (×1, ×2, ×3, ×4)
A b h a
Qd. What are file input and output operations in Python Programming?
Ans. Following are the file I/O operations in Python:
A. Open operation:
- Python has a built-in open () function to open files from the directory.
- Two arguments that are mainly needed by the open () function are:.
- File name: It contains a string type value containing the name of the file which we want to access.
- Access_mode: The value of access_mode specifies the mode in which we want to open the file, i.e., read, write, append etc.
- Syntax:
file_object = open(file_name [, access_mode])
For example:
>>>f= open (“test.txt”)
#Opening file current directory
>>>f= open (“C:/Python27/README.txt”)
#Specifying full path #Output
>>>f
<open file ‘C:/Python27/README. txt’, mode ‘r’ at 0x02BC5128>
#Output
B. Close operation:
- ‘When the operations that are to be performed on an opened file are finished, we have to close the file in order to release the resources.
- Python comes with a garbage collector responsible for cleaning up the unreferenced objects from the memory, we must not rely on it to close a file.
- Proper closing of a file frees up the resources held with the file.
- The closing of file is done with a built-in function close 0).
- Syntax:
fileObject. close ()
For example:
# open a file
>>> f=open (“test. txt”, “wb”)
# perform file operations
>>> f. close) # close the file
C. Write operation :
- After opening a file, we have to perform some operations on the file. Here we will perform the write operation.
- In order to write into a file, we have to open it with w mode or a mode, on any writing-enabling mode.
- We should be careful when using the w mode because in this mode overwriting persists in case the file already exists.
For example:
# open the file with w mode
>>>f= open (*C :/Python27/test.txt”, “w”)
# perform write operation
>>>f. write (writing to the file line 1/n’)
>>>f. write (writing to the file line 2/n’)
>>>f. write (writing to the file line 3/n’)
>>>f. write (writing to the file line 4′)
# close the file after writing
>>> f.close ()
The given example creates a file named test. txt if it does not exist, and overwrites into it if it exists. If we open the file, we will find the following content in it.
Output :
Writing to the file line 1
Writing to the file line 2
Writing to the file line 3
Writing to the file line 4
D. Read operation:
- In order to read from a file, we must open the file in the reading mode (r mode).
- We can use the read (size) method to read the data specified by size.
- If no size is provided, it will end up reading to the end of the file.
- The read() method enables us to read the strings from an opened file.
- Syntax:
file object. read ([size])
For example:
# open the file
>>> f= open (“C:/Python27/test.txt”, “y”)
>>>f. read (7) # read from starting 7 bytes of data
‘writing’ # Output
>>>f. read (6) # read next 6 bytes of data to the
# Output
Qe. Solve the Tower of Hanoi problem for n= 3 disk and show all the steps.
Ans: Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e., a disk can only be moved if it is the uppermost disk on a stack.
- No larger disk may be placed on top of a smaller disk.
Section-c | important Question in Python Programming
3a. Write a program in Python to execute the Selection sort algorithm.
Ans.
- The selection sort algorithm sorts an array by repeatedly finding the smallest element (considering ascending order) from an unsorted list and swapping it with the first element of the list.
- The algorithm maintains two sub-arrays in a given array:
- The sub-array which is already sorted.
- Remaining sub-array which is unsorted.
- In every iteration of selection sort, the smallest element from the unsorted sub-array is picked and moved to the sorted sub-array.
- Code:
Def slectionSort(nlist):
for fillslot in range(len(nlist) - 1, 0, - 1) :
maxpos = 0
for location in range(1, fillslot + 1) :
if nlist [location]>nlist [maxpos] :
maxpos = location
temp = nlist [fillslot]
nlist[fillslot] = nlist[maxpos]
nlist [maxpos] = temp
nlist = [14, 46, 43, 27, 57, 41, 45, 21, 70]
selectionSort(nlist)
print (nlist)
Output :
[14, 21, 27, 41, 43, 45, 46, 57, 70]
Output : [14, 21, 27, 41, 43, 45, 46, 57, 70]
5. Time complexity:
- i.Best case: O(n2)
- ii.Worst case: O(n2)
- iii. Average case: O(n2)
3b. Explain why Python is considered an interpreted language.
Ans. 1. Python is called an interpreted language because it goes through an interpreter, which turns code we write into the language understood by computer’s processor.
2. Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine.
3. This distinction makes for two important points :
a. Python code is fast to develop: As the code is not needed to be compiled and built, Python code can be readily changed and executed .This makes for a fast development cycle.
b. Python code is not as fast in execution: Since the code is not directly compiled and executed and an additional layer of the Python virtual machine is responsible for execution, Python code runs a little slow as compared to conventional languages like C, C++, etc.
4a. Write a Python program to construct the following pattern, using a nested for loop.
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Ans. Program :
n = 5
for i in range(n):
for j in ranged (i):
print(“*”,end=””)
print()
for i in range (n):
for j in range (i,n):
print(“*”,end=””)
print()
4b Write a Program to display fibonacci sequence for n terms.
>>> number = int (input ("Enter the value for × (where x > 2) ?")
# first the terms
>>> x1 = 0
>>> x2 = 1
>>> count = 2
# check if the number of terms is valid
>>> if numbers < = 0 :
print ("Please enter positive integer")
elif numbers == 1:
print ("Fibonacci sequence is :")
print (x1)
else:
print ("Fibonacci sequence is :")
print (x1,"",", ×2)
while count < numbers:
xth = x1 + x2
print (xth)
#update values
xl = x2
×2 = xth
count + = 1
Output :
Enter the value for n (where n > 2) ? 10
Fibonacci sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
5a. Write a Python program to change a given string to a new string where the first and last chars have been exchanged.
Ans:
defchange_sring(str1):
return stril-1:] + stri[1:-1] + strif:1]
print(change_sring(abed’))
Output : dbca
5b. Write a Python program to add an item in a tuple.
Ans: Creating tuples in Python:
- To create a tuple, all the items or elements are placed inside parentheses separated by commas and assigned to a variable.
- Tuples can have any number of different data items (that is, integer, float, string, list, etc.).
For examples:
1. A tuple with integer data items:
>>> tuple = (4, 2, 9, 1)
>>> print tuple
(4, 2, 9, 1)# Output
2. A tuple with items of different data types:
>>>tuple_mix = (2, 30, “Python”, 5.8, “Program”)
>>>print tuple
(2, 30, ‘Python’, 5.8, ‘Program’) # Output
3. Nested tuple:
>>>nested_tuple = (“Python”, [1, 4, 2],[“john”,3.9])
>>>print nested_tuple
(‘Python’, [1, 4, 2], [‘john’,3.9]) # Output
4. Tuple can also be created without parenthesis:
>>>tuple = 4.9, 6, ‘house’
>>>print tuple (4.9, 6, ‘house’) # Output
6a. How to create and import a module in Python?
Ans.
Create a Module:To create a module just save the code we want in a file with the file extension .py.
For example:
Save the given code in a file named mymodule.py def greeting(name);
print(“Hello,” + name)
Importing a module: Now we can use the newly created module or built-in module, by using the import statement:
For example:
Import the module named mymodule, and call the greeting function:
importmymodule
mymodule.greeting(“Aditya Kumar”)
Output: Hello, Aditya Kumar
Qb. Explain the algorithm Sieve of Eratosthenes used in Python Programming.
Ans:
- Sieve of Eratosthenes is a simple and ingenious ancient algorithm for finding all prime numbers up to any given limit.
- It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2.
- The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between them that is equal to that prime.
- Following is the algorithm to find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:
- Create a list of consecutive integers from 2 to n : (2, 3, 4, …, n).
- Initially, let p equal 2, the first prime number.
- Starting from p°, count up in increments of p and mark each of these numbers greater than or equal to p? itself in the list. These numbers will be p(p+1), p(p+2), p(p+3), etc.
- Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step c.
7a. Write a Recursive function in python Binary Search (Arr, I, R, X to search the given element X to be searched from the List Arr having R elements, where 1 represent lower bound and R represents the upper bound.
Ans:
- Binary search follows a divide and conquer approach. It is faster than linear search but requires that the array be sorted before the algorithm is executed.
- Binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of item is returned.
- If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item.
- Otherwise, the item is searched for in the sub-array to the right of the middle item.
- This process continues on the sub-array as well until the size of the sub-array reduces to zero.
- Code:
def binarysearch(arr, 1, r, x) :
while 1 <=r:
mid = 1 + (r - 1)/2;
#Check if x is present at mid
if arr [mid] == x:
return mid
# If x is greater, ignore left half
elif arr [mid] < x :
1= mid + 1
# If x is smaller, ignore right half
else:
r = mid - 1
# If we reach here, then the element was not present
return - 1
# Test array
arr = [2, 3, 4, 10, 40]
X = 10
# Function call
result = binarySearch(arr, 0, len(arr) - 1, x)
if result! = - 1:
print "Element is present at index % d" % result
else:
print "Element is not present in array"
Output:
Element is present at index 3
7b. Explain the terms Merge List and Merge Sort in Python Programming.
Ans.
Merge List:
1. Merging is defined as the process of creating a sorted list/array of data items from two other sorted array/list of data items.
2. Merge list means to merge two sorted list into one list.
Code for merging two lists and sort it:
a=[]
c=[]
n1=int(input("Enter number of elements:")) ;
for i in range(1, n1+1):
b=int (input("Enter element:"))
a.append(b)
n2=int(input("Enter number of elements:”))
for i in range(1, n2+1):
d=int(input("Enter element:"))
c.append(d)
new=a+c
new.sort()
print("Sorted list is:", new)
Merge Sort :
- Merge sort is a divide and conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves.
- The merge() function is used for merging two halves.
- The merge(arr, I, m, r) is key process that assumes that arr[l..m] and arr [m + 1 ..r] are sorted and merges the two sorted sub-arrays into one.
- Code:
5.Time Complexity
Recurrence relation of merge sort is given by
Assume n = 2k for some k.
k = log2n
Then, I(n) = n*7(1) + Cn*log2n
Time complexity of Merge sort is On log n) in all three cases (worst, average and best) as merge sort always divides the array into two halves and take linear time to merge two halves.
All Question Paper | AKTU Quantum | solution of question paper
Python Programming Important Links:
Label | Link |
---|---|
Subject Syllabus | Syllabus |
Short Questions | Short-question |
Important Unit-1 | Unit-1 |
Important Unit-2 | Unit-2 |
Important Unit-3 | Unit-3 |
Important Unit-4 | Unit-4 |
Important Unit-5 | Unit-5 |
Question paper – 2021-22 | 2021-22 |
AKTU Important Links | Btech Syllabus
Link Name | Links |
---|---|
Btech AKTU Circulars | Links |
Btech AKTU Syllabus | Links |
Btech AKTU Student Dashboard | Student Dashboard |
AKTU RESULT (One VIew) | Student Result |
Important Links-Btech (AKTU) | Python Programming syllabus
Label | Links |
---|---|
Btech Information | Info Link |
Btech CSE | CSE-LINK |
Quantum-Page | Link |
Python Programming Syllabus | Python Programming Syllabus |