Final Paper Exam For Python Programming Making Guide
Final Paper Exam For Python Programming Making Guide
Instructions
1. The answers must be clear as possible to avoid any misunderstanding.
2. Any types of documents rather than the ones given by the invigilators are prohibited
3. In the MCQ part, each question has one (01) and only one good answer. Draw a table with 02 entries one for
question number and the other one for the letter corresponding to your good answer.
4. In the open answer questions, give a clear answer for each question with the maximum details according to the
total number of marks given
5. In the case study, your answers need to be oriented on practice. Give answers that can permit a technician to
achieve the goal. not just for the comprehension.
a. 1 2 3
b. Error
c. 1 2
d. None of the mentioned
9. What will be the output of the following Python expression if x=56.236??
a. 56.236
b. 56.23
c. 56.0000
d. 56.24
10. What will be the output of the following Python function?
len(["hello",2, 4, 6])
a. Error.
b. 6
c. 4
d. 3
11. Which function is called when the following Python program is executed?
f = foo()
print(f)
a. str()
b. format()
c. __str__()
d. __init__()
12. Which function is called when the following Python program is executed?
f = foo()
a. str()
b. __str__()
c. init()
d. __init__()
13. To add a new element to a list we use which Python command?
a. List1.addEnd(5).
b. List1.addLast(5)
c. List1.append(5)
d. List1.add(5)
2/3
14. What will be the output of the following Python program?
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
a. Error.
b. 0 1 2 0
c. 0 1 2
d. None of the above
15. What will be the output of the following Python code?
x = 'abcd'
for i in range(len(x)):
print(i)
a. 0 1 2 3
b. 1 2 3 4
c. a b c d
d. Error
16. In Python, a class is ___________ for a concrete object
a. A distraction.
b. A blueprint
c. A nuisance
d. An instance
17. The correct way to instantiate the following Dog class is:
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
a. Dog("Rufus", 3)
b. Dog.__init__("Rufus", 3)
c. Dog.create("Rufus", 3)
d. Dog()
18. With the object “buddy” we access to a class attribute, with
a. buddy.name;
b. buddyname;
c. buddy (name);
d. buddy__name
19. What’s the output of the following code snippet?
4. What are the common built-in data types in Python? Give four (04) of them with a description
of data it contains 02 marks
Numbers– They include integers, floating point numbers, and complex numbers. E.g. 1,
7.9,3+4i
List– An ordered sequence of items is called a list. The elements of a list may belong to
different data types. E.g. [5,’market’,2.4]
Tuple– It is also an ordered sequence of elements. Unlike lists, tuples are immutable,
which means they can’t be changed. E.g. (3,’tool’,1)
String– A sequence of characters is called a string. They are declared within single or
double quotes. E.g. “Sana”, ‘She is going to the market’, etc.
Set– Sets are a collection of unique items that are not in order. E.g. {7,6,8}
4/3
Dictionary– A dictionary stores values in key and value pairs where each value can be
accessed through its key. The order of items is not important. E.g. {1:’apple’,2:’mango}
Boolean– There are 2 Boolean values- True and False.
5. In a python code, give a rule to identify: 02 marks
a. A local variable: If a variable is defined or assigned a new value anywhere within the
function’s body, it’s assumed to be local.
b. A global variable: Those variables that are only referenced inside a function are
implicitly global. Or defined out of any function.
c. A private class property: Those property declared with the prefix __ (double
underscore)
d. A protected class property: Those property declared with the prefix _ (simple
underscore)
6. What is “django” in python? 02 marks
Is a framework written in python that allow to build web application and REST full API.
7. Give the command used to create a new Django project 02 marks
django-admin startproject projectName
8. Give in few words what the following libraries can be used for? 02 marks
a. Pygame: int ()
b. Tkinter: For Graphical User Interface
c. Seaborn: Statistical plotting library
d. Numpy: For scientific computing
9. How to install Python on Windows and set path variable? 02 marks
To install Python on Windows, follow the below steps:
Install python from this link: https://www.python.org/downloads/
After this, install it on your PC. Look for the location where PYTHON has been installed
on your PC using the following command on your command prompt: cmd python.
Then go to advanced system settings and add a new variable and name it as
PYTHON_NAME and paste the copied path.
Look for the path variable, select its value and select ‘edit’.
Add a semicolon towards the end of the value if it’s not present and then type
%PYTHON_HOME%
10. Give in 02 lines a python code that can be used to generate random numbers 02 marks
import random
random.random()
#This returns a random floating-point number in the range [0,1)
11. What is a lambda function? 02 marks
An anonymous function is known as a lambda function. This function can have any number of
parameters but, can have just one statement
12. Explain the keyword “pass” in Python 02 marks
Pass means no-operation Python statement, or in other words, it is a place holder in a
compound statement, where there should be a blank left, and nothing has to be written there.
13. Which package is used by “pip” to setup and install another package? 02 marks
setuptools.
14. Give one sentence to explain the following “pip” command? 02 marks
a. Pip install: To install an external package or a library in python language
b. Pip uninstall: To remove a package
c. Pip show: To display all the details about an installed package
d. Pip list: To display all the external package already installed
5/3
15. Python is a language suitable for “web scraping”. what do you understand by web scraping? 02
marks
Is collecting data from websites using an automated process.
6/3
Section B: PROBLEMS (20 marks)
A fraction in arithmetic is a number numerator divided by a denominator. In a simple fraction,
both are integers. A fraction is proper when the numerator is less than denominator and improper if
numerator is greater than the denominator. Any fraction can be written in decimal form by carrying
out the division of the numerator by the denominator. But in original form it is written “a/b” where a
and b are respectively numerator and denominator. In that original form notation, a fraction should
be in irreducible form mean the GCD (Greatest Common Divisor) of a and b should be 1. If not, the
fraction needs to be simplified. This simplification is done by dividing both numerator and
denominator by the GCD of the 02 numbers.
In this problem, our ambition is to build a calculator of fractions to help first cycle secondary
school students. To achieve the goal, we decide to first of all write a class “Fraction” to model a
fraction with the operation that we can applied on it.
Note: Comment your code to avoid any misunderstanding.
1. Write the python code for the Fraction class with a constructor that allow a user to define a
fraction with string value (such that 2/3) or with 02 integers values . 05 marks
class Fraction:
def __init__(self, *args):
if(len(args)==1):
#Then it must be an integer or a string that represents the
fraction
if(isinstance(args[0],int)):
self.__numerator = args[0]
self.__denominator = 1
self.__Rdenominator = 1
self.__Rnumerator = args[0]
elif(isinstance(args[0],str)):
listData = args[0].split('/')
if(len(listData)> 2):
raise TypeError("The precised string doesn't represent a
fraction")
elif(len(listData)== 1):
num=int(listData[0])
denom=1
else:
num=int(listData[0])
denom=int(listData[1])
7/3
if(denom == 0):
raise("Denominator of a fraction can't be null value")
try:
self.__numerator = num
self.__denominator = denom
self.__Rdenominator = denom
self.__Rnumerator = num
if(denom < 0):
self.__Rdenominator = -1 * denom
self.__Rnumerator = -1 * num
except:
raise TypeError("The fraction managed in this program are
formed by integer")
elif(len(args)==2):
if(isinstance(args[0],int) and isinstance(args[1],int)):
self.__numerator = args[0]
self.__denominator = args[1]
self.__Rdenominator = args[1]
self.__Rnumerator = args[0]
if(args[1] < 0):
self.__Rdenominator = -1 * denominator
self.__Rnumerator = -1 * numerator
else:
raise TypeError("The precised fraction parameters must be
integer")
2. Write a public method without argument named “isProper” that return 1 if the “self” fraction is
proper and 0 if not 02 marks
def isProper(self):
if(self.__numerator < self.__denominator)
return 1
else
return 0
8/3
3. Write a private method named “ComputeGCD” without argument that compute the GCD of the
numerator and denominator of a fraction 04 marks
def __GcdComputation(self):
n1= self.__numerator
n2= self.__denominator
while (n1 != n2):
if(n1>n2):
n1=n1-n2
else:
if(n2>n1):
n2=n2-n1
return n1
4. Write a public method without argument named “Simplify” which return the simplify version
of the “self” fraction. 02 marks
def Simplify(self):
gcd = self.__ComputeGCD (self.__Rnumerator,self.__Rdenominator)
self.__Rnumerator = int(self.__Rnumerator/gcd)
self.__Rdenominator = int(self.__Rdenominator/gcd)
return Fraction(self.__Rnumerator, self.__Rdenominator)
5. Write a public method without argument named “decimal” that return the decimal value of the
“self” fraction 02 marks
def decimal(self):
return self.__numerator / self.__denominator
6. Write a public method that take on fraction in argument named “addFraction” and return the
product of this fraction with the “self” fraction. Ensure that the result must be irreducible. 04
marks
def addFraction(self,fraction):
fractionToAdd = Fraction("0/1")
if(isinstance(fraction,str)):
fractionToAdd = Fraction(fraction)
elif(isinstance(fraction,int)):
fractionToAdd = Fraction(str(fraction))
9/3
elif(isinstance(fraction,Fraction)):
fractionToAdd = fraction
else:
raise("The precised value is not a fraction")
n1 = self.__Rnumerator * fractionToAdd.__Rdenominator
n2 = self.__Rdenominator * fractionToAdd.__Rnumerator
numResult = n1 + n2
denomResult = self.__Rdenominator * fractionToAdd.__Rdenominator
fractionResult = Fraction(numResult, denomResult)
return fractionResult.__simplify()
7. Write a small code that can be executed to display a window (GUI) with the title “my first
window in python”. 03 marks
import tkinter
mainWindow = tkinter.Tk()
mainWindow.title("le titre de ma première fenêtre")
mainWindow.mainloop()
10/3