WorksheetsMidtermExam-ComProg1-MakScie
Total questions: 100
Worksheet time: 2hrs 40mins
What will be the type of variable x? x = 3.14
int
float
str
complex
Which of the following creates a complex number in Python?
x = 5+2j
x = complex(5,2)
Both a and b
None
What will be printed?
a = True
b = False
print(type(a+b))
int
bool
float
str
Which of these is mutable?
tuple
list
str
int
What will be the result? x = "Python" print(len(x))
6
5
7
8
Which is valid variable name in Python?
2num
_count
num@var
for
What will this print? x = "123" print(type(int(x)))
str
int
float
bool
Which function gives memory address of variable?
address()
id()
loc()
pos()
Output? x = None print(type(x))
NoneType
Null
Object
Undefined
Which is immutable?
List
Set
Dictionary
String
Q11. a = 10 b = a a = 20 print(b)
10
20
None
Error
Which is used for dynamic typing?
C
Java
Python
C++
print(type([1,2,3]) is list)
True
False
print(isinstance((1,2,3), tuple))
True
False
None
1
dict stores elements as:
key-value pairs
only values
only keys
ordered sequence only
What will input() return by default?
int
float
str
None
x = input("Enter: ")
print(type(x))
User enters 25.
Output type?
str
int
float
bool
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output?
My name is Alice and I am 25 years old.
Error
Which statement is correct for formatted output?
print("Age: %d" % 25)
print("Age: {}.format(25)")
Both a and b
None
Output? print("Hello", "World", sep="-")
Hello World
Hello-World
Hello,World
Error
print("Hello", end="*") print("World")
Hello World
Hello*World
Hello World
Error
x = 10 y = 20 print("Sum is", x+y)
Sum is 1020
Sum is 30
print("{0} {1}".format("Python", "Rocks"))
Rocks Python
Python Rocks
Error
None
Which reads input as integer?
x = int(input())
x = input(int)
x = read(int)
None
What will happen? print("5" + "5")
10
55
Error
None
Output? print(5 // 2)
2.5
2
3
Error
Output? print(7 % 3)
1
2
3
Error
What will be printed? x = 2 ** 3 print(x)
6
8
9
Error
Which operator has highest precedence?
+
-
**
//
x = 10 x += 5 print(x)
10
15
5
Error
print(10 == 10.0)
True
False
print(True + True)
1
2
TrueTrue
Error
Which is a logical operator in Python?
&&
||
and
bitand
print(4 & 5) (BINARY AND)
4
5
1
0
print(4 | 5) (BINARY OR)
4
5
1
5
Which is the identity operator?
is
==
=
!=
Output?
x = [1,2,3]
y = [1,2,3]
print(x is y)
True
False
Output? x = [1,2,3] y = x print(x is y)
True
False
Output? print(3 not in [1,2,4])
True
False
a = 10
b = 20
print(a if a > b else b)
10
20
Error
None
x = 5 if x % 2 == 0: print("Even") else: print("Odd")
Even
Odd
Error
None
Output?
x = 10
if x > 5:
print("A")
elif x > 8:
print("B")
else:
print("C")
A
B
C
Error
Which keyword is used in Python conditional branching?
switch
case
if
when
x = 0
if x:
print("True")
else:
print("False")
True
False
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C")
A
B
C
Output?
x = 3
if x > 5:
print("A")
else:
if x > 2:
print("B")
else:
print("C")
A
B
C
if True and False: print("Yes") else: print("No")
Yes
No
if not 0: print("Pass") else: print("Fail")
Pass
Fail
x = 7
if x % 2 == 0:
print("Divisible by 2")
elif x % 3 == 0:
print("Divisible by 3")
else: print("Other")
Divisible by 2
Divisible by 3
Other
Which is correct ternary operator syntax?
if condition ? a : b
a if condition else b
a when condition else b
None
x = "yes" if x: print("True branch")
True branch
Nothing
Error
if "apple" in "pineapple": print("Found")
Found
Nothing
Which values are treated as False?
0
None
""
All above
x = -1 if x: print("Valid")
Valid
Nothing
x = 15
if x % 5 == 0 and x % 3 == 0:
print("FizzBuzz")
FizzBuzz
Nothing
x = 1 while x <= 3: print(x) x += 1
1 2 3
0 1 2
Infinite
Error
What happens if condition is always True?
Executes once
Infinite loop
Error
None
x = 0 while x < 5: x += 2 print(x)
4
5
6
Error
Which keyword exits loop?
continue
exit
break
stop
x = 0
while x < 3:
print(x)
x += 1
else:
print("Done")
What is the output of the above code?
0 1 2 Done
1 2 3 Done
0 1 2 3 Done
0 1 Done
What is the output of the following code? for i in range(3): print(i, end=" ") else: print("Done")
0 1 2
0 1 2 Done
Done only
Error
count = 0 while count < 3: count += 1 if count == 2: continue print(count)
1 2 3
1 3
2 3
None
Which loop is entry-controlled?
while
for
Both
None
x = 10 while x > 0: print(x, end=" ") x //= 2
10 9 8 ...
10 5 2 1
n = 123 sum = 0 while n > 0: sum += n % 10 n //= 10 print(sum)
6
7
5
Error
while False: print("Hello")
Hello
Nothing
Error
i = 1 while i < 5: print(i, end=" ") i += 2
1 2 3 4
1 3
x = 5 while x: print(x) x -= 1
5 4 3 2 1
5 4 3 2 1 0
Infinite
None
Which is valid infinite loop?
while True: pass
for i in range(): pass
loop forever
None
i = 0 while i < 5: print("Hi") break
Hi (once)
Hi (five times)
Nothing
Error
i = 0 while i < 3: print(i) i += 1
0 1 2
1 2 3
0 1 2 3
Error
for i in range(3): print(i)
1 2 3
0 1 2
0 1 2 3
Error
What does range(5) generate?
0,1,2,3,4,5
1,2,3,4,5
0,1,2,3,4
None
for i in range(2, 6): print(i, end=" ")
2 3 4 5
2 3 4 5 6
1 2 3 4
Error
for i in range(2, 10, 3): print(i, end=" ") Which of the above is the correct output?
a) 2 3 4 5 6 7 8 9
b) 2 5 8
c) 2 4 6 8
d) Error
Which is valid loop over a list?
for i in [1,2,3]:
for (i=0; i<3; i++):
loop i in [1,2,3]
None
for ch in "Hi": print(ch)
Hi
H i
Error
for i in range(3): print("Yes") else: print("Done") What is the output of the above code?
Yes Yes Yes
Yes Yes Yes Done
Done only
Error
for i in range(1,5):
if i == 3:
break
print(i)
What is the ouput?
(a)
for i in range(5):
if i % 2 == 0:
continue
print(i)
What is the output?
(a)
for i in range(3):
pass
print(i)
What is the output?
(a)
for i in range(5, 0, -2):
print(i, end=" ")
What is the output?
(a)
nums = [10,20,30]
for n in nums:
print(n//10)
What is the output?
(a)
for i in range(len("Python")):
print(i, end=" ")
What is the Output?
(a)
for i in [1,2,3]:
print(i*i)
What is the output?
(a)
for i in "123":
print(int(i) + 1)
What is the output?
(a)
for i in range(1,4):
for j in range(1,3):
print(i,j)
How many pairs are printed?
(a)
for i in range(2):
for j in range(2):
print(i+j)
What is the output?
(a)
What is printed?
for i in []:
print("Loop")
print("End")
(a)
for i in range(3):
for j in range(2):
if j == 1:
break
print(i, j)
What is the output?
(a)
Which is faster for iterating over lists in Python?
(a)
x = [1,2,3]
for i in x:
if i % 2 == 0:
print("Even")
else:
print("Odd")
What is the Output?
(a)
s = "Python"
count = 0
for ch in s:
if ch in "aeiou":
count += 1
print(count)
What is the output?
(a)
total = 0
for i in range(1,6):
if i % 2 == 0:
continue
total += i
print(total)
What is the output?
(a)
i = 0
while i < 4:
i += 1
if i == 3:
break
print(i)
What is the output?
(a)
n = 1234
rev = 0
while n > 0:
rev = rev*10 + n%10
n //= 10
print(rev)
What is the output?
(a)
x = [1,2,3,4]
sum = 0
for n in x:
sum += n
print(sum)
What is the output?
(a)
for i in range(1,4):
for j in range(1,3):
if i == 2 and j == 1:
continue
print(i,j)
How many pairs printed?
(a)
for i in range(1,6):
if i % 2 == 0:
print(i*i)
What is the output?
(a)
n = 5
fact = 1
for i in range(1, n+1):
fact *= i
print(fact)
What is the output?
(a)
