Python Question Bank
Python Question Bank
Unit 1.
1. Explain the different string formats available in Python with examples. 5 Marks
2. Discuss the int(), float(), str(), chr() and complex() type conversion functions with examples. 5
Marks
3. Discuss the ord(), hex(), oct(), complex() and float() type conversion functions with examples. 5
Marks
4. Describe the is and is not operators and type() function. Also, discuss why Python is called as
dynamic and strongly typed language. 5 Marks
5. Illustrate the different types of control flow statements available in Python with flowcharts. 5
Marks
6. Write a Program to Prompt for a Score between 0.0 and 1.0. If the Score is out of range, print an
error. If the score is between 0.0 and 1.0, print a grade using the following table 6 Marks
7. Write a program to display the fibonacci sequences up to nth term where n is provided by the
user. 7 Marks
8. Write a program to repeatedly check for the largest number until the user enters “done”. 5 Marks
9. Write a program to find the sum of all Odd and Even numbers up to a number specified by the
user. 6 Marks
10. Explain the need for continue and break statements. Write a program to check whether a number
is prime or not. Prompt the user for input. 8 Marks
11. Describe the need for catching exceptions using try and except statements. 10 Marks
12. Describe the syntax for the following functions and explain with an example.
a) abs() b) max() c) divmod() d) pow() e) len() 5 Marks
13. Write Pythonic code to solve the quadratic equation ax**2 + bx + c = 0 by getting input for
coefficients from the user. 8 Marks
14. Find the area and perimeter of a circle using functions. Prompt the user for input. 6 Marks
15. Write a Python program using functions to find the value of nPr and nCr without using inbuilt
factorial() function. 8 Marks
16. Write a program to print the sum of the following series 1 + 1/2 + 1/3 +. …. + 1/n 8 Marks
Unit 2.
1. Write Python program to replace comma-separated words with hyphens and print hyphen-separated
words in ascending order. 5 Marks
2. Explain the need for Unicodes. 5 Marks
3. Discuss the following list functions
a) len() b) sum() c) any() d) all() e) sorted() 5 Marks
4. Explain the following list methods with an example. a) append() b) extend() c) insert() d) index() e)
sort() 5 Marks
5. Check if the items in the list are sorted in ascending or descending order and print suitable
messages accordingly. Otherwise, print “Items in list are not sorted” 7 Marks
6. Write Pythonic code to multiply two matrices using nested loops and also perform transpose of the
resultant matrix. 8 Marks
Unit 3.
1. Write Python program to sort words in a sentence in decreasing order of their length. Display the
sorted words along with their length. 7 Marks
2. Write a function which receives a variable number of strings as arguments. Find unique characters
in each string. 6 Marks
3. Write Pythonic code to create a function called most_frequent that takes a string and prints the
letters in decreasing order of frequency. Use dictionaries. 8 Marks
4. Consider the string ‘brontosaurus’. Write Pythonic code that implements and returns the
functionality of histogram using dictionaries for the given string. Also, write the
function print_hist() to print the keys and their values in alphabetical order from the values returned
by the histogram function. 8 Marks
5. Write Pythonic code that implements and returns the functionality of histogram using dictionaries.
Also, write the function print_hist to print the keys and their values in alphabetical order from the
values returned by the histogram function. 8 Marks
Unit 4.
1. Describe the different access modes of the files with an example. 6 Marks
2. Discuss the following methods associated with the file object
a) read() b) readline() c) readlines() d) tell() e) seek() f) write() 6 Marks
3. Write Python Program to Reverse Each Word in “secret_societies.txt” file. 6 Marks
4. Write Python Program to Count the Occurrences of Each Word and Also Count the Number of
Words in a “quotes.txt” File. 7 Marks
5. Write Python Program to Find the Longest Word in a File. Get the File Name from User. 7 Marks
6. Consider a File Called “workfile”. Write Python Program to Read and Print Each Byte in the
Binary File. 5 Marks
7. Write Python program to demonstrate the writing of data to a CSV file using DictWriter class. 6
Marks
8. Discuss the following methods supported by compiled regular expression objects.
a) search() b) match() c) findall() 6 Marks
9. Why do you need regular expressions in Python? Consider a file “ait.txt”. Write a Python program
to read the file and look for lines of the form X-DSPAM-Confidence: 0.8475 X-DSPAM-Probability:
0.458 Extract the number from each of the lines using a regular expression. Compute the average of
the numbers and print out the average. 8 Marks
10. Consider a line “From stephen.marquard\@uct.ac.za Sat Jan 5 09:14:16 2008” in the file email.txt.
Write Pythonic code to read the file and extract email address from the lines starting from the word
“From”. Use regular expressions to match email address. 8 Marks
Unit 5.
1. Write Python program to calculate the Arc Length of an Angle by assigning values to the radius
and angle data attributes of the class ArcLength. 7 Marks
2. Write Python Program to simulate a Bank Account with support for depositMoney,
withdrawMoney and showBalance Operations. 8 Marks
3. Given three Points (x1, y1), (x2, y2) and (x3, y3), write a Python program to check if they are
Collinear. 7 Marks
4. Discuss inheritance in Python programming language. Write a Python program to demonstrate the
use of super() function. 10 Marks
5. Program to demonstrate the Overriding of the Base Class method in the Derived Class. 8 Marks
6. Write Python program to demonstrate Multiple Inheritance. 8 Marks
7. Given the Coordinates (x, y) of a center of a Circle and its radius, write Python program to
determine whether the Point lies inside the Circle, on the Circle or outside the Circle. 8 Marks
8. Write Python Program to Demonstrate Multiple Inheritance with Method Overriding. 8 Marks
9. Write Pythonic code to overload “+”, “-” and “*” operators by providing the methods __add__,
__sub__ and __mul__. 8 Marks
10. Write Pythonic code to create a function named move_rectangle() that takes an object
of Rectangle class and two numbers named dx and dy. It should change the location of the Rectangle
by adding dx to the x coordinate of corner and adding dy to the y coordinate of corner. 10 Marks