0% found this document useful (0 votes)
16 views

Python Quiz 30 Questions Moderate Level

The document contains a moderate-level Python quiz consisting of 30 questions, each with multiple-choice answers. Questions cover various topics including list operations, functions, file handling, classes, and lambda functions. Each question is followed by the correct answer.

Uploaded by

engninja13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Quiz 30 Questions Moderate Level

The document contains a moderate-level Python quiz consisting of 30 questions, each with multiple-choice answers. Questions cover various topics including list operations, functions, file handling, classes, and lambda functions. Each question is followed by the correct answer.

Uploaded by

engninja13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Python Quiz - Moderate Level (30

Questions)
1. 1. What is the output of this code?
a = [1, 2, 3]
print(a[::-1])

A. [1, 2, 3]

B. [3, 2, 1]

C. [1, 3, 2]

D. Error

Answer: [3, 2, 1]

2. 2. Which of the following statements about Python lists is true?

A. Lists are immutable

B. Lists are stored as key-value pairs

C. Lists can contain elements of different data types

D. Lists must contain only integers

Answer: Lists can contain elements of different data types

3. 3. What will this return?


bool([]) and bool({})

A. True

B. False

C. None

D. Error

Answer: False
4. 4. How many times will print() be executed?
for i in range(2, 10, 2):
print(i)

A. 4

B. 5

C. 6

D. 3

Answer: 4

5. 5. What will be the output?


x = 10
print("Even" if x % 2 == 0 else "Odd")

A. Even

B. Odd

C. 10

D. True

Answer: Even

6. 6. What is the output of this function call?


def add(x, y=5): return x + y
print(add(3))

A. 3

B. 5

C. 8

D. Error

Answer: 8
7. 7. Which of the following will NOT raise an error?

A. return outside a function

B. Using global inside a function

C. A function without def

D. Defining two functions with the same name

Answer: Using global inside a function

8. 8. What will be printed?


def f():
x = 10
def g():
print(x)
g()
f()

A. 10

B. Error

C. None

D. Undefined

Answer: 10

9. 9. What will this code return?


def func(a, b, c=3):
return a + b + c
print(func(1, 2))

A. 6

B. 3

C. Error

D. 1

Answer: 6
10. 10. Which of the following is true about default parameters?

A. They must come before required parameters

B. You can skip required parameters if you define default ones

C. They must come after required parameters

D. Python doesn’t allow default parameters

Answer: They must come after required parameters

11. 11. What will print("Python"[::-1]) output?

A. nohtyP

B. Python

C. Error

D. nothyP

Answer: nohtyP

12. 12. What does set([1,2,2,3]) return?

A. {1, 2, 2, 3}

B. {1, 2, 3}

C. [1, 2, 3]

D. Error

Answer: {1, 2, 3}

13. 13. What is the output of: sorted(['python', 'Java', 'C'])?

A. ['C', 'Java', 'python']

B. ['python', 'Java', 'C']


C. ['Java', 'C', 'python']

D. ['C', 'python', 'Java']

Answer: ['C', 'Java', 'python']

14. 14. Which operation modifies a list in place?

A. sorted()

B. sort()

C. reversed()

D. max()

Answer: sort()

15. 15. What will be the output?


a = (1, 2, 3)
a[0] = 10

A. a becomes (10, 2, 3)

B. Error

C. None

D. Tuple is updated

Answer: Error

16. 16. What does the following do?


open("file.txt", "r")

A. Opens file in write mode

B. Opens file in append mode

C. Opens file in read mode

D. Creates a new file


Answer: Opens file in read mode

17. 17. Which of these will handle both IndexError and ValueError?

A. try:
# code
except (IndexError, ValueError): pass

B. try:
# code
except IndexError and ValueError: pass

C. try:
# code
except: IndexError, ValueError

D. try:
# code
catch IndexError, ValueError:

Answer: try:
# code
except (IndexError, ValueError): pass

18. 18. What is the output of: type(open("file.txt", "r"))?

A. <class 'file'>

B. <class 'str'>

C. <class '_io.TextIOWrapper'>

D. <class 'TextFile'>

Answer: <class '_io.TextIOWrapper'>

19. 19. How do you write to a file in Python?

A. open("f.txt", "r")

B. open("f.txt", "w")
C. file.open("f.txt")

D. write("f.txt")

Answer: open("f.txt", "w")

20. 20. What does finally do in a try-except-finally block?

A. Runs only if there is an exception

B. Runs only if no exception

C. Always runs

D. Only runs with return

Answer: Always runs

21. 21. What does __init__ do?

A. Initializes an object

B. Deletes an object

C. Checks if object exists

D. None of the above

Answer: Initializes an object

22. 22. What is the output?


class A:
def __init__(self): self.name = "A"
a = A()
print(a.name)

A. A

B. name

C. self.name

D. Error
Answer: A

23. 23. Which is true about inheritance?

A. Python doesn’t support inheritance

B. A class can only inherit from one parent

C. Child classes cannot override parent methods

D. A class can inherit from multiple classes

Answer: A class can inherit from multiple classes

24. 24. What is self in Python class methods?

A. Refers to class name

B. Refers to module

C. Refers to the instance

D. A placeholder

Answer: Refers to the instance

25. 25. What is method overriding?

A. Changing method names

B. Having two methods with same name in different scopes

C. Using multiple functions

D. None

Answer: Having two methods with same name in different scopes

26. 26. What will this code print?


x = [i*i for i in range(3)]
print(x)
A. [1, 2, 3]

B. [0, 1, 4]

C. [0, 1, 2]

D. Error

Answer: [0, 1, 4]

27. 27. What does zip([1,2], [3,4]) return?

A. [(1,3), (2,4)]

B. [1,2,3,4]

C. [[1,3], [2,4]]

D. None

Answer: [(1,3), (2,4)]

28. 28. What does map(int, ['1','2','3']) return?

A. [1,2,3]

B. <map object>

C. 1,2,3

D. Error

Answer: <map object>

29. 29. What will be printed?


x = None
if not x:
print("No value")

A. No value

B. None
C. Error

D. 0

Answer: No value

30. 30. Which of the following is the correct syntax for a lambda function?

A. lambda x: x+1

B. def lambda x: x+1

C. lambda = x: x+1

D. def x: return x+1

Answer: lambda x: x+1

You might also like