(O-LEVEL PAPER)
M3-R5: PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON With Solution
(JAN 2021)
PART ONE
DURATION: 3 Hours M.M.100
ANSWER ALL THE QUESTIONS:
1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in the “OMR” answer sheet supplied with the question paper, following instructions therein. (1×10)
1.1 Find the output of following Python program
a=”Meetmeafterparty”
b=13
print a+b
A) 29 B) 14
C) error in code D) 15
Solution: There is an error in the code.
In Python 3.x, print statements require parentheses, so the correct syntax would be print(a+b).
In Python 2.x, the above code would raise a TypeError because you are trying to concatenate a string with an integer. If you want to concatenate the string and integer, you would need to convert the integer to a string first using the str() function: print(a+str(b)).
So the correct answer depends on the version of Python being used, but none of the options given are correct.
1.2 Find the output of the following Python program
class Acc:
def_init_(self,id):
self.id=id
id=555
acc=Acc(111)
print acc.id
A) 111 B) 555
C) 666 D) error in code
Solution: There is an error in the code. The __init__() method is not defined correctly. The underscore characters before and after “init” should each be two underscores instead of one.
Also, there should be an indentation before the id=555 line to include it in the class definition.
The correct code should look like this:
class Acc:
def __init__(self, id):
self.id = id
id = 555
acc = Acc(111)
print(acc.id)
The output of this program will be:
111
So the correct answer is option A) 111.
1.3 Which of the following is an invalid variable
A) my_string_1
B) foo
C) _
D) 1st_string
Solution: D) 1st_string
Variable names in Python must follow certain rules. They can only contain letters, numbers, and underscores, but they cannot start with a number. Therefore, 1st_string is an invalid variable name.
The other options (my_string_1, foo, and _) are all valid variable names in Python.
So the correct answer is D) 1st_string.
1.4 x=[‘ab’,’cd’]
for i in x:
i.upper()
print(x)
A) ab
B) cd
C) [‘ab’,’cd’]
D) [‘cd’,’ab’]
find the output of the following Python programs
Solution:
The output of the program will be:
[‘ab’, ‘cd’]
This is because the upper() method does not modify the string in place, but instead returns a new string with all uppercase characters. In the given program, the upper() method is called on each string in the list, but the return values are not stored or used in any way. Therefore, the original list x is printed without any changes.
So the correct answer is C) [‘ab’,’cd’].
1.5 What is the output of the following program?
a=2
b=’3.77′
c=-8
str1='{0:4f}{0:3d}{2}{1}’. format(a,b,c) print(str1)
A) 2.0000 2 -8 3.77
B) 2 3.77 -8 3.77
C) 2.000 3 -8 3.77
D) 2.000 2 8 3.77
Solution: The output of the program will be:
2.0000 2 -8 3.77
Let’s break down the format string ‘{0:.4f} {0:3d} {2} {1}’:
{0:.4f}: Format the first argument a as a floating point number with 4 decimal places.
{0:3d}: Format the first argument a as an integer with 3 digits. Since a is not large enough to have 3 digits, it is padded with spaces.
{2}: Insert the third argument c as is.
{1}: Insert the second argument b as is.
When we call format() with arguments a=2, b=’3.77′, and c=-8, the resulting string is ‘2.0000 2 -8 3.77’.
So the correct answer is A) 2.0000 2 -8 3.77.
1.6 Find out the output of the following Python programs
def gfg(x,l=[]):
for i in range(x):
l.append(i*i)
print(l)
gfg(2)
A) [3,2,1,0,1,4]
B) [0,1]
C) [0,1,0,1,4]
D) error in code
Solution:
The output of the program will be:
[0, 1]
When we call the gfg() function with the argument x=2, it will iterate over the range [0, 1] and append i*i to the list l on each iteration. Since the l parameter has a default value of [], it will create a new empty list the first time the function is called. However, on subsequent calls to the function, the same list object will be used, and the previous values that were appended to it will still be there. So the output will be [0, 1] because those are the values that were appended during the first call to gfg().
1.7 Suppose t=(1,2,3,4), which of the following is incorrect
A) print(t[3])
B) t[3]=45
C) print(max(t))
D) print(Len(t))
Solution:
The incorrect statement is:
B) t[3]=45
Since tuples are immutable in Python, we cannot modify their elements once they are created. Therefore, trying to assign a new value to an element of the tuple t will raise a TypeError.
The other statements are correct:
print(t[3]) will print 4, which is the element at index 3 in the tuple t.
print(max(t)) will print 4, which is the largest element in the tuple t.
print(len(t)) will print 4, which is the number of elements in the tuple t. Note that len() is a built-in function in Python, and it returns the number of items in an object.
1.8 What will be the output of the following Python code?
>>>t1=(1,2,4,3)
>>>t2=(1,2,3,4)
>>>t1<t2
A) true
B) flase
C) Error
D) (1,2,4,3)
Solution: The output of the code will be B) false.
In Python, the comparison operators can be used to compare tuples, where the comparison is made lexicographically. That means, the first elements of the two tuples are compared, and if they are equal, then the second elements are compared, and so on.
In the given code, t1 and t2 are two tuples. The first element of both tuples is 1, which is equal. The second element of both tuples is 2, which is also equal. The third element of t1 is 4, which is greater than the third element of t2, which is 3. Therefore, t1 is greater than t2 lexicographically. Hence, t1 < t2 will return false.
1.9 Which of the following statment is false about recursion?
A) Every recuresive function must have a base case
B) Infinite recursion can occur if the base case isn’t properly mentioned
C) A recursion function makes the code easier to understand
D) Every recursive function must have return value
Solution:
D) Every recursive function must have a return value is false in Python.
In Python, a recursive function can have a return value, but it is not mandatory. It depends on the problem being solved and the design of the function. Some recursive functions are designed to simply perform a task without returning a value, such as printing a sequence or drawing a fractal.
Therefore, option D is false in the context of Python.
1.10 What will be the output of the following Pythone code?
import functools
l=[1,2,3,4,5]
m=functools.reduce(lambda x, y:x if x>y else y,l)
print(m)
A) Error
B) Address of m
C) 1
D) 5
Solution: The output of the given code will be:
5
Explanation:
The reduce() function from the functools module is used to apply a particular function (in this case a lambda function) cumulatively to the items of a sequence, from left to right. The lambda function takes two arguments x and y and returns the maximum of the two. So the reduce() function will return the maximum value from the list l.
In this case, the maximum value in the list l is 5. So, the value of m will be 5 and it will be printed.