Python Question Bank

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Python Programming Question Bank

S.NO. Unit 1 MARKS

Which of the following statements produce an error in Python?


x, y, z = 1,2,3 # s1
1 a, b= 4,5,6 # s2 2
u= 7,8,9 # s3
(List all the statements that have error.)

2 Explain the role of precedence with an example. 2

Consider the program:


x= ['12', 'hello', 456]
3 x[0] *= 3 2
x[1][1] = 'bye'
Explain why this program generates an error.

4 Explain why Python is considered an interpreted language. 10

What is short circuit evaluation? What is printed by the following Python program?
a=0
b=2
5 10
c=3
x = c or a
print(x)

6 Explain the Programming Cycle for Python in detail. 2

7 What do you mean by Python IDE? Explain in detail. 5

8 Explain why python is considered an interpreted language. 5

9 Discuss why Python is called as dynamic and strongly typed language? 2

10 What do you mean by operator precedence and associativity? Explain. 4

11 Write short notes on Type conversion in python. 4

12 Write Python program to swap two numbers without using intermediate/Temporary 4


variables. Prompt the user for
S.NO. Unit 2 MARKS
Write a program factors(N) that returns a list of all positive divisors of N (N>=1).
For example:
1 factors(6) returns [1,2,3,6] 10
factors(1) returns [1]
factors(13) returns [1,13]
Write a function makePairs that takes as input two lists of equal length and returns a
single list of same length where k-th element is the pair of k-th elements from the
input lists.
2 10
For example,
makePairs([1,3,5,7],[2,4,6,8]) returns [(1,2),(3,4),(5,6),(7,8)]
makePairs([],[]) returns []
What will be the output of the following Python code?
i=0
while i< 3:
3 print(i) 2

i += 1
else:
print(0)
4 How can you randomize the items of a list in place in Python? 5
Write a Python program to construct the following pattern, using a nested for loop.
*
**
***
5 5
****
***
**
*

6 How pass statement is different from a comment? 2

7 Write a for loop that prints numbers from 0 to 57, using range function. 2

8 Explain the use of break and continue with a suitable example. 4

9 Write a program to check an input number is prime or not. 4

Write a program that accepts a sentence and calculate the number of digits, uppercase
10 4
and lowercase letters.
11 Write a program to check an input year is leap year or not. 4
S.NO. Unit 3 MARKS
Write a recursive Python function “rprint” to print all elements in a list in reverse.
1 rprint([]) prints nothing 2
rprint([1,2,3]) prints 3 2 1

2 Describe the behavior of “range (s, e)” in Python. 2

Write a Python function average to compute the average of a list of numbers. The
function must use try-except to handle the case where the input list is empty.
3 10
Further, in that case the average for the empty list should be set to 0.0 using the
except block.
Write a Python program, triangle(N), that prints a right triangle having base and
height consisting of N * symbols as shown in these examples:
triangle(3) prints:
*
**
***
4 10
triangle(5) prints:
*
**
***
****
*****
What will be the output of following code?
def cube(x):
5 return x * x * x 2
x = cube(3)
print (x)
6 Explain Tuples and Unpacking Sequences in Python Data Structure. 5
Write a Python program to change a given string to a new string where the first and
7 5
last chars have been exchanged.

8 Write a Python program to add an item in a tuple. 5

9 What is the difference between Python Arrays and Lists? 2

Write a python function named ComputeAverage to find average of a list of


10 4
numbers. It should handle the exception if the list is empty and return 0 in that case.
S.NO. Unit 4 MARKS
Write a function lessthan(lst, k) to return list of numbers less than k from a list lst.
The function must use list comprehension.
1 10
Example:
lessthan([1, -2, 0, 5, -3], 0) returns [-2, -3]
Show an example where both Keyword arguments and Default arguments are used
2 10
for the same function in a call. Show both the definition of the function and its call.
Write a Python program, countSquares(N), that returns the count of perfect squares
less than or equal to N (N>1).

For example: countSquares(1) returns 1

3 # Only 1 is a perfect square <= 1 10


countSquares(5) returns 2
# 1, 4 are perfect squares <= 5
CountSquares(55) returns 7
# 1, 4, 9, 16, 25, 36, 49 <= 55

Write a Python function, alternating(lst), that takes as argument a sequence lst. The
function returns True if the elements in lst are alternately odd and even, starting with
an even number. Otherwise it returns False.

4 For example: 10
alternating([10, 9, 9, 6]) returns False
alternating([10, 15, 8]) returns True
alternating([10]) returns True
alternating([]) returns True
alternating([15, 10, 9]) returns False
Write a Python function, searchMany(s, x, k), that takes as argument a sequence s
and integers x, k (k>0). The function returns True if there are at most k occurrences
of x in s. Otherwise it returns False.

5 For example: 10
searchMany([10, 17, 15, 12], 15, 1) returns True
searchMany([10, 12, 12, 12], 12, 2) returns False
searchMany([10, 12, 15, 11], 17, 1) returns False
6 How do we define an Interface for an ADT? 2

7 What are File input and output operations in Python Programming? 5

8 Write a program to produce Fibonacci series in Python. 5

9 How to create and import a module in Python? 5

10 Explain the algorithm Sieve of Eratosthene used in Python. 5

There is a file named Input.Txt. Enter some positive numbers into the file named
11 Input.Txt. Read the contents of the file and if it is an odd number write it to 4
ODD.TXT and if the number is even, write it to EVEN.TXT
Discuss Exceptions and Assertions in Python. Explain with a suitable example.
12 4
Explain any two built-in exceptions.
What is the significance of module in Python? What are the methods of importing a
13 4
module? Explain with suitable example
What do you mean by recursion? Write a recursive function to compute the factorial
14 4
of an input number N.

15 What are different types of inheritance supported by Python? Explain. 4

S.NO. Unit 5 MARKS

1 What is the use of “raise” statement? Describe with an example. 2

2 Explain the use of “with” construct in Python with an example program. 2

How do you read an input from a user in Python to be used as an integer in the rest of
3 2
the program? Explain with an example.
What is the output of the following program?
4 2
(lambda x,y : y - 2*x) (1, 11)

5 Explain the use of __lt__ function in a class Python? 10

Write a Python function removekth(s, k) that takes as input a string s and an integer
k>=0 and removes the character at index k. If k is beyond the length of s, the whole
of s is returned. For example,

6 10
removekth(“PYTHON”, 1) returns “PTHON”
removekth(“PYTHON”, 3) returns “PYTON”
removekth(“PYTHON”, 0) returns “YTHON”
removekth(“PYTHON”, 20) returns “PYTHON”
7 Describe the differences between a linear search and a binary search? 10

How can you create Python file that can be imported as a library as well as run as a
8 10
standalone script?
Describe the difference between
import library
9 and 10
from library import *
When used in a python program. Here library is some python library.

10 How do you perform a search in Python? 2

11 Solve the Tower of Hanoi problem for n= 3 disk and show all the steps. 5

12 Write a program in Python to execute the Selection sort algorithm. 5

Write a Recursive function in python BinarySearch(Arr,l,R,X) to search the given


13 element X to be searched from the List Arr having R elements, where l represent 5
slower bound and R represents the upper bound.

Explain the terms Merge List and Merge Sort in Python Programming. 5
14

15 Implement the binary search technique. 4

16 Implement selection sort using Python 4

You might also like