Final Paper Resit Exam For Python Programming Making Guide
Final Paper Resit 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
b. Error
c. 1 2 3
d. None of the mentioned
9. Which statement is correct?
a. List is mutable & tuple is immutable
b. List is immutable & tuple is mutable
c. Both are mutable
d. Both are immutable
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 one of the following is not a python’s predefined data type?
a. list
b. tuple
c. dictionary
d. class
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)
14. What will be the output of the following Python program?
2/3
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. What’s the output of the following code snippet?
3/3
12>>> bobo = JackRussellTerrier()
13>>> bobo.walk()
a. AttributeError:
b. object has no attribute 'walk';
c. *walking*
d. Arff!
e. Woof!
19. What’s the output of the following code snippet?
>>> class Dog:
2... def walk(self):
3... return "*walking*"
4...
5... def speak(self):
6... return "Woof!"
7...
8>>> class JackRussellTerrier(Dog):
9... def talk(self):
10... return super().speak()
11...
12>>> bobo = JackRussellTerrier()
13>>> bobo.talk()
a. AttributeError:
b. object has no attribute 'walk';
c. *walking*;
d. Arff!;
e. Woof!
20. Given the following code snippet, which of the following REPL output is correct?
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
class JackRussellTerrier(Dog):
pass
class Dachshund(Dog):
pass
class Bulldog(Dog):
pass
miles = JackRussellTerrier("Miles", 4)
buddy = Dachshund("Buddy", 9)
jack = Bulldog("Jack", 3)
4/3
jim = Bulldog("Jim", 5)
a. >>> isinstance(miles, Dog)
False
b. >>> isinstance(jack, Dachshund)
False
c. >>> isinstance(buddy, Bulldog)
True
d. >>> isinstance(miles, Bulldog)
False
e. >>> isinstance(miles, Dog)
True
5/3
stg='AB/CD'
t = len(stg)
x = stg.split('/')
a. Give the value of t and x: t = 5 and x = [AB, CD]
b. What is the type of x variable?
list
9. Explain //, % and * *operators in python? 03 marks
// (Floor Division)- It is a division operator that returns the integer part of the division.
Ex: 5//2=2
% (Modulus)- It returns the remainder of the division.
Ex: 5%2=1
** (Power)- It performs an exponential calculation on the operator. a**b means a raised
to the power of b.
Ex: 5**2=25, 5**3 = 125
10. About list in python, explain the meaning of the following function 03 marks
a. append: It is used to add new elements at the end of the list.
b. insert: It is used to add an element at a particular position in the list.
c. extend: It is used to extend the list by adding a new list.
11. What is a python library? Give an example of python library used to build game software, data
analysis software and GUI (Graphical User Interface) 03 marks
A Python library is a reusable chunk of code that you may want to include in your programs/
projects to achieve some tasks.
Game software: pygame
Data Analysis software: pandas
GUI: tkinter, easyGUI, Gtk
12. Define in python what is a module: 01 mark
Module: in general, are simply Python files with a .py extension and can have a set of functions,
classes, or variables defined and implemented. They can be imported and initialized once using
the import statement. If partial functionality is needed, import the requisite classes or
functions using from foo import bar.
13. What are global, protected and private attributes in Python? Precise how to define it 03 marks
Global variables are public variables that are defined in the global scope. To use the variable
in the global scope inside a function, we use the global keyword.
Protected attributes are attributes defined with an underscore prefixed to their identifier
eg. _sara. They can still be accessed and modified from outside the class they are defined in
but a responsible developer should refrain from doing so.
Private attributes are attributes with double underscore prefixed to their identifier eg.
__ansh. They cannot be accessed or modified from the outside directly and will result in an
AttributeError if such an attempt is made
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”. In this window, add a text input and a button. 03 marks
import tkinter
mainWindow = tkinter.Tk()
mainWindow.title("le titre de ma première fenêtre")
mainWindow.mainloop()
10/3