0% found this document useful (0 votes)
455 views10 pages

Final Paper Resit Exam For Python Programming Making Guide

1. The document provides instructions for a Python programming exam for level 3 students. It includes sections on general questions worth 50 marks and a case study. 2. The general questions section includes 20 multiple choice questions worth 20 marks and 10 open answer questions worth 30 marks. The questions cover Python concepts like data types, operators, functions, classes and libraries. 3. Common Python libraries that can be used for different purposes are mentioned - pygame for games, pandas/numpy for data analysis, and tkinter for GUIs.

Uploaded by

ckiadj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
455 views10 pages

Final Paper Resit Exam For Python Programming Making Guide

1. The document provides instructions for a Python programming exam for level 3 students. It includes sections on general questions worth 50 marks and a case study. 2. The general questions section includes 20 multiple choice questions worth 20 marks and 10 open answer questions worth 30 marks. The questions cover Python concepts like data types, operators, functions, classes and libraries. 3. Common Python libraries that can be used for different purposes are mentioned - pygame for games, pandas/numpy for data analysis, and tkinter for GUIs.

Uploaded by

ckiadj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

SEMESTER:1: RESIT EXAMINATION

SPECIALTY: BTECH LEVEL: 3


COURSE TITLE: PROGRAMMING USING COURSE
PYTHON A CODE:
SEIT413A
AUTHORISED DOCUMENTS: YES  or NO  Time: 3 Hours Lecturer Name’s: AZOBOU KIADJEU CEDRIC

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.

SECTION A: GENERAL QUESTIONS (50 marks)


PART 1: MCQ 20 Marks
1. What is the maximum possible length of an identifier?
a. 16
b. 32
c. 64
d. None of these above
2. Who developed the Python language?
a. Zim Den
b. Guido van Rossum
c. Niene Stom
d. None of the above
3. Which character is used in Python to make a single line comment?
a. #
b. //
c. <!--
d. >//
4. Which of the following statements is correct regarding the object-oriented programming
concept in Python?
a. Classes are real-world entities while objects are not real
b. Objects are real-world entities while classes are not real
c. Both objects and classes are real-world entities
d. All of the above
5. Which of the following declarations is incorrect?
a. _x = 2
b. __x=2
c. __x__=2
d. None of the mentioned
6. Which of the following is not a keyword in Python language?
a. val
b. raise
1/3
c. try
d. with
7. Which of the following words cannot be a variable in python language?
a. _val
b. val
c. try
d. __try
8. What will be the output of the following Python code?
i = 1
while True:
if i%3 == 0:
break
print(i)
 
i += 1

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?

>>> class Dog:


2... def walk(self):
3... return "*walking*"
4...
5... def speak(self):
6... return "Woof!"
7...
8>>> class JackRussellTerrier(Dog):
9... def speak(self):
10... return "Arff!"
11...

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

PART 2: OPEN ANSWER QUESTIONS 30 Marks


1. What is Polymorphism in oop? 02 marks
Polymorphism means “Many Forms”. A subclass can define its own unique behavior and still
share the same functionalities or behavior of its parent/base class.
2. What is __init__? 02 marks
__init__ is a method or constructor in Python. This method is automatically called to allocate
memory when a new object/ instance of a class is created. All classes have the __init__ method
3. 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.
4. What is self in Python? 02 marks
Self is an instance or an object of a class. In Python, this is explicitly included as the first
parameter. However, this is not the case in Java where it’s optional. It helps to differentiate
between the methods and attributes of a class with local variables. The self-variable in the init
method refers to the newly created object while in other methods, it refers to the object whose
method was called
5. How can you generate random numbers in Python? 02 marks
Random module is the standard module that is used to generate a random number. The
method is defined as:
import random
random.random
6. Which function can you used to randomize the items of a list in place in Python? 02 marks
The function “shuffle(x)” where x represents the list. It’s a function of “random” package.
from random import shuffle
x = ['Keep', 'The', 'Blue', 'Flag', 'Flying', 'High']
shuffle(x)
print(x)
7. What does “*args” mean as argument of a function in python and why would we use it? 02 marks
We use *args when we aren’t sure how many arguments are going to be passed to a function,
or if we want to pass a stored list or tuple of arguments to a function.
8. Let us consider the following python code: 03 marks

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

You might also like