All Question paper with solution mean Bachelorexam.com

Python Programming Question Paper -2021-22 |with solution

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.

  1. Python’s programming cycle is dramatically shorter than that of traditional programming Cycle.
  2. In Python, there are no compile or link steps.
  3. Python programs simply import modules at runtime and use the objects they contain. Because of this, Python programs run immediately after changes are made.
  4. 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.
  5. 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.
  6.  Fig. 1.1.1 shows Python’s impact on the programming cycle. Python’s programming cycle
python programming btach 2021-22

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: 

  1. ADT only defines as what operations are to be performed but not how these operations will be implemented.
  2. It does not specify how data will be organized in memory and what algorithms will be used for implementing the operations.
  3. It is called “abstract” because it gives an implementation-independent vIew.
  4. The process of providing only the essentials and hiding the details is known as abstraction.
  5. 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.
  6.  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: 

  1. IDE is a software package that consists of several tools for developing and testing the software.
  2. An IDE helps the developer by automating the process.
  3. IDEs integrate many tools that are designed for SDLC.
  4. IDEs were introduced to diminish the coding and typing errors.
  5. Some of the Python IDEs are:
  1. 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.
    1. Features of PyCharm:
      1. It has smart code navigation, good code editor, a function for quick refactoring.
      2. The integrated activities with PyCharm are profiling, testing, debugging, remote development, and deployments.
      3. PyCharm supports Python web development frameworks, Angular JS, JavaScript, CSS, HTML and live editing functions.
  2. 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.
    1. Features of Spyder:
      1. It has good syntax highlighting and auto code completion features.
      2. Spyder Python explores and edits variables directly from GUI.
      3. It performs very well in multi-language editor.
  3. PyDev: It is an external plug-in for Eclipse and is very popular as a Python interpreter.
    1. Features of PyDev:
      1. PyDev has strong parameters like refactoring, debugging, type hinting, code analysis, and code coverage function.
      2. PyDev supports tokens browser, PyLint integration, interactive console, remote debugger, Unittest integration, etc.
  4. IDLE: IDLE is a basic IDE mainly used by beginner level developers.
    1. IDLE Python is a cross-platform IDE, hence it increases the flexibility for users.
    2. It is developed only in Python in collaboration with Tkinter
    3. GUI toolkit.
    4. The feature of multi-window text editor in IDLE has some great functions like smart indentation, call tips, Python colorizing, and undo option.
    5. It also comes with a strong debugger along with continuous breakpoints, local spaces, and global view.
    6. It supports browsers, editable configurations, and dialog boxes.
  5. Visual studio: It enables development for various platforms and has its own marketplace for extensions.
    1. Features of visual studio:
      1. It supports Python coding in visual studio, debugging, and other activities.
      2. It has both paid and free versions in the market with great features.

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: 

  1. Tuples are the sequence or series values of different types separated by commas (,).
  2. 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:

  1. To create a tuple, all the items or elements are placed inside parentheses separated by commas and assigned to a variable.
  2. 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: 

  1. Unpacking allows to extract the components of the sequence into individual variables.
  2. Several different assignments can be performed simultaneously.
  3. 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.
  4. 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.
  5. 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)

  1. 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: 

  1. Python has a built-in open ()  function to open files from the directory.
  2. Two arguments that are mainly needed by the open () function are:.
    1. File name: It contains a string type value containing the name of the file which we want to access.
    2. Access_mode: The value of access_mode specifies the mode in which we want to open the file, i.e., read, write, append etc.
  3. 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:

  1. ‘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.
  2. 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.
  3. Proper closing of a file frees up the resources held with the file.
  4. The closing of file is done with a built-in function close 0).
  5. Syntax:

fileObject. close ()

For example:

# open a file

>>> f=open (“test. txt”, “wb”)

# perform file operations

>>> f. close)     # close the file

C. Write operation : 

  1. After opening a file, we have to perform some operations on the file. Here we will perform the write operation.
  2. In order to write into a file, we have to open it with w mode or a mode, on any writing-enabling mode.
  3. 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

  1. In order to read from a file, we must open the file in the reading mode (r mode).
  2. We can use the read (size) method to read the data specified by size.
  3. If no size is provided, it will end up reading to the end of the file.
  4. The read() method enables us to read the strings from an opened file.
  5. 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.

  1. 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.
  2. The algorithm maintains two sub-arrays in a given array:
    1. The sub-array which is already sorted.
    2. Remaining sub-array which is unsorted.
  3. In every iteration of selection sort, the smallest element from the unsorted sub-array is picked and moved to the sorted sub-array.
  4. 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:

  1. To create a tuple, all the items or elements are placed inside parentheses separated by commas and assigned to a variable.
  2. 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:

  1. Sieve of Eratosthenes is a simple and ingenious ancient algorithm for finding all prime numbers up to any given limit.
  2. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the first prime number, 2.
  3. 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.
  4. Following is the algorithm to find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:
    1. Create a list of consecutive integers from 2 to n : (2, 3, 4, …, n).
    2. Initially, let p equal 2, the first prime number.
    3. 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.
    4. 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: 

  1. 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.
  2. 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.
  3. 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.
  4. Otherwise, the item is searched for in the sub-array to the right of the middle item.
  5. This process continues on the sub-array as well until the size of the sub-array reduces to zero.
  6. 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 : 

  1. 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.
  2. The merge() function is used for merging two halves.
  3. 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.
  4. 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.


bachelor exam preparation all question paper with solution important questions with solution

Python Programming Important Links:

LabelLink
Subject SyllabusSyllabus
Short QuestionsShort-question
Important Unit-1Unit-1
Important Unit-2Unit-2
Important Unit-3Unit-3
Important Unit-4Unit-4
Important Unit-5Unit-5
Question paper – 2021-222021-22

AKTU Important Links | Btech Syllabus

Link NameLinks
Btech AKTU CircularsLinks
Btech AKTU SyllabusLinks
Btech AKTU Student DashboardStudent Dashboard
AKTU RESULT (One VIew)Student Result

Important Links-Btech (AKTU) | Python Programming syllabus

LabelLinks
Btech InformationInfo Link
Btech CSECSE-LINK
Quantum-PageLink
Python Programming SyllabusPython Programming Syllabus

Leave a Comment