0% found this document useful (0 votes)
9 views8 pages

Python Programming Question Set

This document is a Python question paper designed for a 45-minute exam. It contains 20 questions that test various programming concepts and outputs in Python, along with instructions for filling out personal information and answering the questions. Candidates are required to write their answers in the space provided and adhere to specific formatting guidelines.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Python Programming Question Set

This document is a Python question paper designed for a 45-minute exam. It contains 20 questions that test various programming concepts and outputs in Python, along with instructions for filling out personal information and answering the questions. Candidates are required to write their answers in the space provided and adhere to specific formatting guidelines.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Question – Set I (Python)

• Write answers with in question papers in the space provided.


• Total Time: 45 minutes.
• Do all the rough work in the rough sheets provided.
• Fill below details (use CAPITAL LETTERS only)

Date:

Full Name:

E-Mail:

Qualification:

Mobile Number:

Alternative Mobile Number(Optional):

College Name:

Last Degree (B.E / MCA/ M.E) Passed Out :

• For choose the answers you can round the correct option or write down your answer
in the space provided.
• Use always capital letters as much as possible in your answers
• Try not to have too many corrections on the answers.
1. What is the output of following program?
print(1 + 2)
print(1 + '2')

1. 3
ArithmeticError
2. 3
3
3. 3
TypeError
4. 3
12

Answer:

2. What is the output of following program?


class A:
val = 1

class B(A):
pass

class C(A):
pass

print([Link], [Link], [Link])


[Link] = 2
print([Link], [Link], [Link])
[Link] = 3
print([Link], [Link], [Link])

Answer:

3. What is the output of following program?


data = 50
try:
data = data/0
except ZeroDivisionError:
print('Cannot divide by 0 ', end = '')
else:
print('Division successful ', end = '')

try:
data = data/5

1
except:
print('Inside except block ', end = '')
else:
print('GFG', end = '')

Answer:

4. What is the output of following program?


r = lambda q: q * 2
s = lambda q: q * 3
x=2
x = r(x)
x = s(x)
x = r(x)
print (x)

Answer:

5. What is the output of following program?


a = 4.5
b=2
print (a//b)

Answer:

6. What is the output of following program?


a = True
b = False
c = False

if not a or b:
print (1)
elif not a or not b and c:
print (2)
elif not a or b or not b and a:
print (3)
else:
print (4)

Answer:

7. What is the output of following program?


count = 1

def doThis():

global count

2
for i in range(3):
count += 1

doThis()

print (count)

Answer:

8. What is the output of following program?


for i in range(2):
for i in range(4, 6):
print(i, end=' ')

Answer:

9. What is the output of following program?


dictionary = {1:1, 2:'2', 3:'3'}
dictionary[1] += 10
del dictionary[1]
dictionary[1] = '10'
del dictionary[2]
[Link](3)
dictionary[2] = 0
dictionary['2'] = 3
dictionary['2'] += 1
[Link](2)
print (len(dictionary))

Answer:

10. What is the output of following program?


l = [1, 2, 3, 4]

[Link]([5, 6, 7, 8])
[Link]([6, 7, 8])
print(len(l))

Answer:

3
11. What is the output of following program?
def return_two_values():
return True, 'hi', 1

print(return_two_values(), type(return_two_values()), sep='\n')

Answer:

12. What is the output of following program?


class Base1(object):
def __init__(self):
self.str1 = "from_base1"
print("Base1")

class Base2(object):
def __init__(self):
self.str2 = "from_base2"
print("Base2")

class Derived(Base1, Base2):


def __init__(self):
# Calling constructors of Base1
# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")

def print_strs(self):
print(self.str1, self.str2)

ob = Derived()
ob.print_strs()

Answer:

13. What is the output of following program?


data = [x for x in range(5)]
temp = [x for x in range(7) if x in data and x % 2 == 0]
print(temp)

Answer:

4
14. What is the output of following program?
from math import sqrt

L1 = [_ ** 2 for _ in range(5) for _ in range(5)].pop()


L1 += 19
print(int(sqrt(L1)), end=" ")
L1 = [__ ** 2 for __ in range(L1) for __ in reversed(range(L1))].pop()
L1 += 16
print(int(sqrt(L1)))

Answer:

15. What is the output of following program?


from random import randrange
L = list()
for x in range(5):
[Link](randrange(0, 100, 2)-10)

print(L)

1) [-8, 88, 8, 58, 0]


2) [-8, 81, 18, 46, 0]
3) [-7, 88, 8, 58, 0]
4) [-8, 88, 94, 58, 0]

Answer:

16. What is the output of following program?


from math import *
a = 2.13
b = 3.7777
c = -3.12
print(int(a), floor(b), ceil(c), fabs(c))

Answer:

17. Fetch the contact information (name, email, mob, address) from
contacts table, who have enrolled for the ‘maths’ as a subject from students
table using roll number as key.

Answer:

5
18. Update the firstName as 'Captain' and lastName as 'America' from
students table , who having student_id or student_roll as 1.

Answer:

19. Crack the code & Unlock the Key ?

A) 062
B) 602

6
C) 042
D) 204

Answer:

20. Can you spot the missing number?

A) 3
B) 6
C) Both A & B
D) None of the above

Answer:

Common questions

Powered by AI

Initially, the dictionary has three keys. After multiple manipulations, including additions and deletions of keys and values, the final dictionary has only one key with an integer number type and a newly inserted key with a string number type, making the length of the dictionary 1 .

The 'append' method adds the entire list as a single element at the end, increasing length by 1, while 'extend' iterates over the added list, inserting its elements independently, increasing length by 4 for [6, 7, 8]. Thus, the final length of the list becomes 7 .

The try-except block first catches a ZeroDivisionError when dividing by zero, printing 'Cannot divide by 0'. No exception occurs when subsequently dividing by 5, so 'GFG' is printed. This shows how Python executes only the relevant except block and how subsequent operations in the else clause proceed if no exceptions occur in the try block .

Inheritance allows class B and class C to access the class variable 'val' from class A. Initially, B.val and C.val will be the same as A.val, but changing B.val only affects class B. Later, when A.val is updated, it affects A.val and C.val but not B.val as B had an overridden value. This demonstrates the use of inheritance and how class variables behave in an inheritance chain .

Using the initial value of x = 2, the operations proceed as follows: first, applying r(x) results in 4, then applying s(x) on the result leads to 12, and finally, applying r(x) again on this result gives 24. This chain of function applications demonstrates how lambda functions can be used to sequentially transform data .

Python functions can return multiple values as a tuple by default. In this example 'return_two_values' returns True, 'hi', and 1 as a tuple, demonstrating Python's flexibility with returning multiple items using a single return statement .

In Python, the '//' operator performs floor division, which discards the fractional part of the division result. Given a = 4.5 and b = 2, the operation a//b results in 2.0 because the division of 4.5 by 2 equals 2.25, and flooring this yields 2.0 .

The global keyword allows the doThis function to modify the global variable 'count'. Every time the function is called, it increments 'count' by 3 during its loop, running a total of 3 times. The final output is 4, as the global count starts at 1 and is incremented across 3 iterations .

The outer and inner loops in the code use the same variable 'i', causing the outer loop's scope to be overwritten by the inner loop. Therefore, for each iteration of the outer loop, only numbers 4 and 5 are printed, repeated for the outer loop's range of 2. This illustrates variable scope limitations across nested loops .

The conditions in the program are evaluated sequentially. The first condition 'not a or b' evaluates to False, the second 'not a or not b and c' also evaluates to False, but the third 'not a or b or not b and a' evaluates to True because 'not b' is True and 'a' is True. Thus, it prints '3' .

You might also like