O Level Python Paper January 2021
निम्नलिखित पायथन प्रोग्राम का आउटपुट खोजें।
Find the output of following Python Programs.
a= “Meetmeatafterparty”
b= 13
print a+b -
The variable
ais assigned a string value “Meetmeatafterparty”, but it is not enclosed in quotes. This will result in a syntax error. -
The variable
bis assigned an integer value 13.
print statement print a + b attempts to concatenate the string value of a with the integer value of b, which is not allowed in Python. This will result in a TypeError.-
-
पायथन में एक स्ट्रिंग (String) और एक पूर्णांक (Integer) को
+ऑपरेटर का उपयोग करके सीधे नहीं जोड़ा जा सकता। यहTypeErrorदेगा।
-
Correct Answer: C) error in code
निम्नलिखित पायथन प्रोग्राम का आउटपुट खोजें।
Find the output of the following Python programs.
class Acc:
def __init__(self, id):
self.id = id
id = 555
acc = Acc(111)
print (acc.id)
- The
__init__method takes a parameterid, and assigns that value to the instance attributeself.id. - The line
id = 555modifies the localidvariable in the constructor, but does not affect the instance variableself.id. - When you print
acc.id, it will print the value that was passed into the constructor, which is111.
Output:
Explanation:
- The
idinside the__init__method is a local variable. Changing it doesn't affect the instance variableself.id. - So, the value of
self.idremains as111even thoughid = 555is set within the constructor.self.id = idइंस्टेंस वेरिएबल को असाइन करता है। अगली लाइनid = 555केवल एक लोकल वेरिएबल को बदलती है, क्लास के self.idको नहीं। इसलिएacc.idका मान 111 ही रहेगा।
Correct Answer: A) 111
निम्नलिखित में से कौन सा एक अमान्य चर है?
Which of the following is an invalid variable?
The invalid variable in the list is:
(D) 1st_string
In most programming languages, variable names cannot start with a number. So, 1st_string is an invalid variable name because it starts with a digit (1).
The other options are valid variable names:
- (A) my_string_1: This is valid.
- (B) foo: This is valid.
- (C) __: This is valid, though it's often used for special purposes in some languages (like Python).
-
-
पायथन में वेरिएबल का नाम कभी भी अंक (digit) से शुरू नहीं हो सकता।
-
Correct Answer: D) 1st_string
निम्नलिखित पायथन प्रोग्राम का आउटपुट खोजें।
Find the output of the following Python programs.
x=['ab','cd']
for i in x:
i.upper()
print(x) -
The
upper()method: Theupper()method returns a new string with all characters in uppercase. However, strings in Python are immutable, meaning that callingupper()on a string does not modify the original string in place. Instead, it returns a new string. In the loop, the result ofi.upper()is not assigned to any variable or used, so it has no effect on the original listx. -
The loop: The loop iterates through each string in the list
x. For each string,i.upper()is called, but since the result is not used, the strings in the listxremain unchanged. -
Printing the list
x: After the loop finishes, the listxremains as['ab', 'cd']because no modifications have been made to it. -
स्ट्रिंग के मेथड जैसे
upper()मूल स्ट्रिंग को नहीं बदलते (क्योंकि स्ट्रिंग Immutable होती है)। जब तक आपi = i.upper()नहीं करते और उसे वापस लिस्ट में नहीं डालते, लिस्ट वैसी ही रहेगी।
Correct Answer: C) ['ab','cd']
निम्नलिखित प्रोग्राम का आउटपुट क्या है?
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)
The given program formats the string using the format() method:
{0:4f}: Formatsa(which is 2) as a float with 4 decimal places →2.0000{0:3d}: Formatsa(which is 2) as an integer with a width of 3 →2{2}: Printsc(which is -8){1}: Printsb(which is'3.77')
So, the output is:
(A) 2.0000 2 -8 3.77
{0:4f} संख्या को फ्लोट में बदलता है (डिफ़ॉल्ट 6 डेसिमल), {0:3d} उसे इंटीजर की तरह स्पेस के साथ दिखाता है। (नोट: प्रश्न के विकल्पों में डेसिमल की संख्या में अंतर हो सकता है, लेकिन तर्क यही है)।
Correct Answer: A) 2.0000 2 -8 3.77
निम्नलिखित पायथन प्रोग्रामों का आउटपुट पता करें:
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)
Let's analyze the given Python code step by step:
def gfg(x, l=[]):
for i in range(x):
l.append(i * i)
print(l)
gfg(2)
-
Function
gfg:- It takes two parameters:
xandl.lhas a default value of an empty list[]. - Inside the function, a loop runs from
0tox-1(inclusive). In each iteration, it appendsi*i(the square ofi) to the listl.
- It takes two parameters:
-
Calling
gfg(2):x = 2andluses the default value[](an empty list).- The loop will run for
i = 0andi = 1:- When
i = 0:0*0 = 0, so0is appended to the list. - When
i = 1:1*1 = 1, so1is appended to the list.
- When
-
Final result: After the loop, the list
lwill contain[0, 1]and that will be printed.
Output:
(B) [0, 1]
Explanation:
- Since the list
lstarts empty and the loop runs twice (fori = 0andi = 1), the final list becomes[0, 1]. -
-
range(2)का मतलब है 0 और 1।i*iकरने पर0*0=0और1*1=1लिस्ट में जुड़ेंगे।
-
Correct Answer: B) [0, 1]
मान लीजिए t=(1, 2, 4, 3), तो निम्नलिखित में से कौन सा गलत है?
Suppose t = (1, 2, 4, 3), which of the following is incorrect?
Let's break down the options for the given tuple t = (1, 2, 4, 3):
-
(A) print(t[3]): This is correct. Indexing starts from 0, so
t[3]refers to the fourth element, which is3. This will print3. -
(B) t[3] = 45: This is incorrect. Tuples are immutable in Python, meaning their elements cannot be changed after creation. Attempting to modify
t[3]will result in a TypeError. -
(C) print(max(t)): This is correct. The
max()function returns the largest element in the tuple. Here, the largest element is4, so it will print4. -
(D) print(len(t)): This is correct. The
len()function returns the number of elements in the tuple. Here, the length oftis4, so it will print4.
Answer:
The incorrect statement is:
(B) t[3] = 45
टपल (Tuple) Immutable होते हैं, यानी एक बार बनने के बाद उनके तत्वों को बदला नहीं जा सकता।
Correct Answer: B) t[3]=45
निम्नलिखित पायथन कोड का आउटपुट क्या होगा?
What will be the output of the following Python code?
t1 = (1, 2, 4, 3)
t2 = (1, 2, 3, 4)
print(t1<t2) The given Python code is:
t1 = (1, 2, 4, 3)
t2 = (1, 2, 3, 4)
print(t1 < t2)
Step-by-Step Explanation:
- Tuples in Python are ordered, immutable collections of elements.
- When comparing two tuples, Python compares them element by element from left to right.
- If the first elements are equal, it moves to the next element and so on.
- If one tuple is smaller than the other at any point, the comparison is decided based on that element.
Now, let's compare t1 and t2:
t1 = (1, 2, 4, 3)t2 = (1, 2, 3, 4)
Comparing element by element:
- The first elements (
1in botht1andt2) are equal, so we move to the next element. - The second elements (
2in botht1andt2) are also equal, so we move to the next element. - The third elements are different:
t1has4, whilet2has3.- Since
4 > 3,t1is not less thant2.
- Since
Therefore, the result of the comparison t1 < t2 is False.
Answer:
(B) False
पायथन टपल्स की तुलना पहले तत्व से शुरू करता है। यहाँ $1=1$ और $2=2$ है, लेकिन तीसरे स्थान पर $4 < 3$ गलत है, इसलिए परिणाम False आएगा।
Correct Answer: B) False
निम्नलिखित में से कौन सा कथन recursion के बारे में गलत है?
Which of the following statements is false about recursion ?
यह गलत है। एक रिकर्सिव फ़ंक्शन बिना कुछ रिटर्न किए भी प्रिंट जैसा कार्य कर सकता है (जैसे ट्री ट्रैवर्सल)। हालाँकि, बेस केस होना अनिवार्य है।
Correct Answer: D) Every recursive function must have a return value (प्रत्येक पुनरावर्ती फ़ंक्शन का एक रिटर्न मान होना चाहिए)
निम्नलिखित पायथन कोड का आउटपुट क्या होगा?
What will be the output of the following Python code ?
import functools
l=[1, 2, 3, 4, 5]
m=functools.reduce(lambda x, y:x if x>y else y, l)
print(m) The code uses functools.reduce() to find the largest number in the list l = [1, 2, 3, 4, 5].
- The
lambda x, y: x if x > y else yfunction compares two numbers at a time and keeps the larger one. - After comparing all elements, the largest number
5is stored inm.
Output:
(D) 5reduce फ़ंक्शन यहाँ दी गई लिस्ट में सबसे बड़ी संख्या (Max) ढूँढ रहा है।
Correct Answer: D) 5
निम्नलिखित पायथन प्रोग्राम का आउटपुट ____ है
The output of following Python program is _____.
r = lambda q: q*2
s = lambda q: q * 3
x = 2
x = r(x)
x = s(x)
x = r(x)
print x
Let's break down the given Python code step by step:
r = lambda q: q * 2
s = lambda q: q * 3
x = 2
x = r(x)
x = s(x)
x = r(x)
print(x)
Step-by-Step Explanation:
-
Define the lambdas:
r = lambda q: q * 2: This lambda function doubles the input.s = lambda q: q * 3: This lambda function triples the input.
-
Initial value of
x:x = 2
-
Apply
r(x):x = r(x)meansx = 2 * 2 = 4. Now,xis4.
-
Apply
s(x):x = s(x)meansx = 4 * 3 = 12. Now,xis12.
-
Apply
r(x)again:x = r(x)meansx = 12 * 2 = 24. Now,xis24.
-
Final output:
print(x)prints the final value ofx, which is24.
Answer:
(B) 24
$x=2 \rightarrow r(2)=4 \rightarrow s(4)=12 \rightarrow r(12)=24$.
Correct Answer: B) 24
आइए निम्नलिखित पायथन कोड पर विचार करें, इस कोड का आउटपुट है
Let consider the following Python code. The output of this code is ____
a = True
b = False
c = False
if a or b and c:
print("TRUE")
else:
print("FALSE") Let's analyze the given Python code:
a = True
b = False
c = False
if a or b and c:
print("TRUE")
else:
print("FALSE")
Step-by-Step Explanation:
-
Variables:
a = Trueb = Falsec = False
-
Condition in the
ifstatement:- The expression
a or b and cinvolves both the logicalorandandoperators. - Operator Precedence: In Python,
andhas higher precedence thanor. So,b and cis evaluated first.
- The expression
-
Evaluate
b and c:b and cevaluates asFalse and False, which isFalse.
-
Evaluate
a or (b and c):- Now the condition becomes
a or False, which isTrue or False, which isTrue.
- Now the condition becomes
-
Result:
- Since the condition evaluates to
True, the code inside theifblock is executed, so"TRUE"is printed.
- Since the condition evaluates to
Answer:
(A) TRUE
if a or b and c में or की वजह से यदि a (True) है, तो पूरा एक्सप्रेशन True हो जाता है।
Correct Answer: A) TRUE
पुनरावर्ती फ़ंक्शन आमतौर पर गैर-पुनरावर्ती फ़ंक्शन की तुलना में _______ मेमोरी स्थान लेते हैं।
Recursive functions usually take ________ memory space than non-recursive function.
-
-
रिकर्शन 'Stack' का उपयोग करता है, इसलिए यह अधिक मेमोरी लेता है।
-
Correct Answer: A) MORE
ट्यूपल्स और सूचियों के बीच अंतर यह है कि ट्यूपल्स को सूचियों के विपरीत बदला नहीं जा सकता है और ट्यूपल्स _______ का उपयोग करते हैं, जबकि सूचियाँ वर्ग कोष्ठक का उपयोग करती हैं।
The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use _______ ,whereas lists use square brackets.
टपल () का उपयोग करते हैं।
Correct Answer: B) parentheses