Centre For Innovation And Entrepreneurship, Jamia Millia Islamia
University
Python Programming Examination
Time: 60 Minutes Maximum Marks: 70
Instructions:
1. Answer all the questions.
2. Select the most appropriate option.
1. Which of the following is NOT a valid Python keyword?
a) def
b) pass
c) class
d) function
2. In Python, comments begin with which symbol?
a) //
b) #
c) /*
d) --
3. Which operator is used for exponentiation in Python?
a) ^
b) **
c) //
d) %
4. How do you convert a string "123" to an integer in Python?
a) int(123)
b) str(123)
c) int("123")
d) float(123)
5. What is the default mode of the input() function in Python?
a) Reads integer
b) Reads string
c) Reads float
d) Reads boolean
6. What will be the output of the following code?
7. x = 10
8. if x > 5:
9. print("Greater")
10. else:
print("Smaller")
a) Smaller
b) Greater
c) Error
d) None
11. What is the correct syntax for an if-elif-else statement in Python?
a) if (condition) { ... } elif (condition) { ... } else { ... }
b) if condition: ... elif condition: ... else: ...
c) if (condition): ... elif (condition): ... else: ...
d) None of the above
12. Which of the following demonstrates a nested if statement?
a) if x > 10: if y > 5: print("Nested")
b) if (x > 10 && y > 5): print("Nested")
c) if x > 10: if y > 5 { print("Nested") }
d) None of the above
13. What will be the output of this code?
14. x = 10
15. if x > 15:
16. print("Greater")
17. elif x == 10:
18. print("Equal")
19. else:
print("Smaller")
a) Greater
b) Equal
c) Smaller
d) None
20. How is the else part executed in a Python if statement?
a) When the if condition is False
b) Always
c) When the if condition is True
d) Only with an elif
11. What will the following code output?
x=5
while x > 0:
print(x, end=" ")
x -= 1
a) 1 2 3 4 5
b) 5 4 3 2 1
c) 1 2 3 4
d) Infinite Loop
12. Which keyword is used to skip the current iteration of a loop in Python?
a) break
b) pass
c) continue
d) skip
13. How many times will the loop execute?
14. for i in range(3):
print(i)
a) 4
b) 3
c) 2
d) Infinite
15. What is the purpose of the pass keyword in Python?
a) To exit the loop
b) To skip iteration
c) To act as a placeholder
d) To end a function
16. What is the output of the following code?
17. for i in range(2):
18. for j in range(2):
print(i, j)
a) 0 0, 0 1, 1 0, 1 1
b) 0 0, 1 0, 1 1, 0 1
c) 0 1, 1 0
d) Infinite loop
16. What is the output of the following code?
17. x = "Hello"
18. y = True
print(type(y))
a) <class 'str'>
b) <class 'int'>
c) <class 'bool'>
d) <class 'NoneType'>
19. Which of the following data types is immutable in Python?
a) List
b) Tuple
c) Dictionary
d) Set
20. Which of the following represents a complex number?
a) 1+2j
b) 1j
c) 2j
d) All of the above
21. What is the default data type of None in Python?
a) int
b) float
c) NoneType
d) bool
22. How can you define a range of numbers in Python?
a) range(start, stop, step)
b) range[1, 10]
c) range{1 to 10}
d) None of the above
21. What is the output of the following code?
22. x = "hello"
print(x[1])
a) h
b) e
c) l
d) o
23. Which of the following is used for string formatting in Python?
a) %
b) +
c) *
d) &
24. What does the len() function do when applied to a string?
a) Returns the number of words in the string
b) Returns the length of the string
c) Returns the first character of the string
d) None of the above
25. What is the output of the following code?
26. s = "Python"
print(s[:3])
a) Py
b) Pyt
c) thon
d) Python
27. How can you convert a string to uppercase in Python?
a) string.upper()
b) string.toUpper()
c) string.toUpperCase()
d) string.capitalize()
26. What will be the output of the following code?
x = [1, 2, 3]
y = x
x.append(4)
print(y)
a) [1, 2, 3]
b) [1, 2, 3, 4]
c) Error
d) None
27. Which of the following methods can be used to remove whitespace from the beginning
and end of a string?
a) strip()
b) remove()
c) trim()
d) cut()
28. What is the result of the following code?
x = {1, 2, 3}
y = {2, 3, 4}
z = x & y
print(z)
a) {1, 2, 3, 4}
b) {1}
c) {2, 3}
d) None
29. What does the update() method do in a dictionary?
a) Updates values based on a key
b) Adds a key-value pair to the dictionary
c) Merges two dictionaries
d) Clears the dictionary
30. What is the output of this code?
x = [i for i in range(5) if i % 2 == 0]
print(x)
a) [0, 1, 2, 3, 4]
b) [0, 2, 4]
c) [1, 3]
d) None
31. Which of the following can be used to create an immutable set?
a) set()
b) frozenset()
c) dict()
d) immutable()
32. What is the output of the following code?
def func(x, y=2):
return x * y
print(func(3))
a) 3
b) 6
c) Error
d) None
33. Which of these is a valid way to define a dictionary?
a) d = {}
b) d = dict()
c) d = {'key': 'value'}
d) All of the above
34. How can you reverse a list in Python?
a) list.reverse()
b) reversed(list)
c) list[::-1]
d) All of the above
35. What is the difference between is and == in Python?
a) is checks for equality, == checks for identity
b) is checks for identity, == checks for equality
c) Both are identical
d) None of the above
36. What is the output of the following code?
def func():
yield 1
yield 2
x = func()
print(next(x))
a) 1
b) 2
c) Error
d) None
37. Which method is used to create a copy of a list?
a) copy()
b) clone()
c) duplicate()
d) None
38. What does the zip() function do?
a) Compresses files
b) Combines two iterables element-wise
c) Iterates over a dictionary
d) Creates a list
39. Which data structure does not allow duplicates in Python?
a) List
b) Tuple
c) Set
d) Dictionary
40. What will be the output of this code?
def func(a, b):
return a + b
print(func(b=5, a=3))
a) 8
b) 15
c) Error
d) None
41. What will the following code produce?
def func(x):
if x > 0:
return x
else:
return -x
print(func(-3))
a) -3
b) 3
c) Error
d) None
42. Which of the following is used to iterate over keys in a dictionary?
a) for key in dict:
b) for dict in keys:
c) for key in dict.keys()
d) Both a and c
43. What is the output of the following code?
x = [1, 2, 3]
y = x[:]
y.append(4)
print(x)
a) [1, 2, 3, 4]
b) [1, 2, 3]
c) Error
d) None
44. Which of the following correctly opens a file for reading?
a) open('file.txt', 'r')
b) open('file.txt', 'w')
c) open('file.txt', 'a')
d) None of the above
45. What does the split() function do in a string?
a) Splits the string into a list of characters
b) Splits the string into a list of words
c) Joins two strings
d) None of the above
46. Which of the following is not a valid method for lists in Python?
a) append()
b) insert()
c) update()
d) remove()
47. What will the following code output?
x = 5
def func():
global x
x += 1
func()
print(x)
a) 5
b) 6
c) Error
d) None
48. Which of the following returns the memory address of an object?
a) type()
b) id()
c) hash()
d) None
49. What does the enumerate() function do in Python?
a) Returns a list of indices
b) Returns a tuple of index and value pairs
c) Returns only values
d) None of the above
50. What is the result of the following code?
x = [1, 2, 3]
y = [4, 5, 6]
z = [a * b for a, b in zip(x, y)]
print(z)
a) [1, 2, 3, 4, 5, 6]
b) [4, 10, 18]
c) [0, 0, 0]
d) Error
51. Which of the following data types supports indexing?
a) List
b) String
c) Tuple
d) All of the above
52. What will be the output of this code?
x = (1, 2, 3, 4, 5)
print(x[1:4])
a) (2, 3, 4)
b) [2, 3, 4]
c) Error
d) None
53. What is the result of the following code?
def func():
try:
return 1
finally:
return 2
print(func())
a) 1
b) 2
c) Error
d) None
54. How can you access the last element of a list?
a) list[len(list)]
b) list[-1]
c) list[last]
d) None of the above
55. What does the in keyword do in Python?
a) Checks for membership
b) Declares a loop
c) Initializes a variable
d) None of the above
56. What will be the output of this code?
x = [10, 20, 30]
y = [40, 50]
print(x + y)
a) [50, 70]
b) [10, 20, 30, 40, 50]
c) Error
d) None
57. What will the len() function return for the following?
x = 'Hello World'
print(len(x))
a) 10
b) 11
c) Error
d) None
58. What does the all() function return for the following list?
x = [True, True, False]
print(all(x))
a) True
b) False
c) Error
d) None
(END)