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

Unit 4 MCQ Python

The document contains a series of questions and answers related to looping, counting, and list operations in programming. It covers topics such as the output of loops, list methods, and syntax for various operations. Each question is followed by multiple-choice answers, with the correct answer indicated.
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)
5 views

Unit 4 MCQ Python

The document contains a series of questions and answers related to looping, counting, and list operations in programming. It covers topics such as the output of loops, list methods, and syntax for various operations. Each question is followed by multiple-choice answers, with the correct answer indicated.
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
You are on page 1/ 32

Topic: Looping and Counting

1. What is the output of the following code?

count = 0
while count < 3:
print(count)
count += 1

A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Infinite loop
Answer: A

2. What does the range(5) produce?

A) [0, 1, 2, 3, 4, 5]
B) [1, 2, 3, 4, 5]
C) [0, 1, 2, 3, 4]
D) [1, 2, 3, 4]
Answer: C

3. Which loop is guaranteed to execute at least once?

A) for loop
B) while loop
C) do-while loop
D) None of the above
Answer: C (Note: Python doesn't have native do-while, but conceptually correct)

4. What keyword is used to skip the current iteration in a loop?

A) skip
B) stop
C) continue
D) pass
Answer: C

5. What is the output?

for i in range(3):
print(i, end=" ")

A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Error
Answer: A

6. Which loop is used when the number of iterations is not known in advance?

A) for
B) while
C) do-while
D) All of the above
Answer: B

7. What is the output?

i = 1
while i < 5:
i += 1
print(i)

A) 1
B) 4
C) 5
D) Error
Answer: C

8. How many times will the loop run?

for i in range(2, 10, 2):


print(i)

A) 4
B) 5
C) 6
D) 8
Answer: B

9. What is the default start value of range(n)?

A) 0
B) 1
C) n
D) None
Answer: A

10. What is the output?

for i in range(1, 5):


print(i * "*")
A) * * * *
B) *
**

C) ****
D) Error
Answer: B

11. What does break do in a loop?

A) Ends the loop


B) Skips one iteration
C) Restarts the loop
D) Causes an error
Answer: A

12. What will this print?

for i in range(3):
if i == 1:
break
print(i)

A) 0
B) 0 1
C) 1
D) None
Answer: A

13. Which is the correct syntax of a for loop?

A) for(i=0; i<n; i++)


B) for i in range(n):
C) loop i to n:
D) repeat i until n:
Answer: B

14. What happens when the condition in a while loop is false?

A) Loop repeats
B) Loop ends
C) Error
D) Skips one iteration
Answer: B

15. What is the output?


x = 10
while x > 0:
x -= 3
print(x)

A) 0
B) -2
C) -1
D) 1
Answer: B

16. Which of the following is a counting variable?

A) for
B) i
C) if
D) def
Answer: B

17. Which built-in function is often used for counting iterations in a loop?

A) count()
B) len()
C) range()
D) enumerate()
Answer: C

18. What is the output?

for i in range(3):
for j in range(2):
print(i, j)

A) (0,0) (1,1)
B) All combinations of i and j
C) Error
D) (0,1) (1,2)
Answer: B

19. Which of the following is an infinite loop?

A) while True:
B) for i in range(1, 10):
C) while i == 5:
D) for i in range(0):
Answer: A
20. How to iterate through a string character by character?

A) for ch in string:
B) for string in ch:
C) loop ch string:
D) foreach ch in string:
Answer: A

21. What is the output?

for i in range(5):
if i == 3:
continue
print(i)

A) 0 1 2 3 4
B) 0 1 2 4
C) 1 2 3 4
D) Error
Answer: B

22. Which function can be used with a loop to get both index and value?

A) zip()
B) enumerate()
C) list()
D) map()
Answer: B

23. What is the output?

for i in range(3, 0, -1):


print(i)

A) 1 2 3
B) 3 2 1
C) 3 2
D) Error
Answer: B

24. What happens if you forget to update the loop counter in a while loop?

A) Syntax error
B) Infinite loop
C) Loop executes once
D) Nothing
Answer: B
25. What does pass do in a loop?

A) Skips current iteration


B) Exits loop
C) Does nothing
D) Causes error
Answer: C

26. What is the result?

count = 0
for i in range(5):
count += i
print(count)

A) 5
B) 10
C) 15
D) 20
Answer: B

27. Which loop can iterate over lists directly?

A) for
B) while
C) do-while
D) None
Answer: A

28. What is the output?

for i in range(1, 6):


if i % 2 == 0:
print(i)

A) 1 3 5
B) 2 4
C) 1 2 3 4 5
D) Error
Answer: B

29. How to make a loop run in reverse?

A) for i in reverse(5)
B) for i in range(5, 0)
C) for i in range(5, 0, -1)
D) for i = 5 to 1
Answer: C

30. What is printed?

i = 0
while i < 3:
print("Loop", i)
i += 1

A) Loop 0 Loop 1 Loop 2


B) Loop 1 Loop 2 Loop 3
C) Error
D) Nothing
Answer: A
Topics: List values, accessing elements, list length, list
membership, lists and for loops, list operations, list
deletion. Cloning lists, nested lists
1. Which of the following is a valid list?

A) [1, 2, 3]
B) (1, 2, 3)
C) {1, 2, 3}
D) list(1, 2, 3)
Answer: A

2. What is the output of len([1, 2, 3, 4])?

A) 3
B) 4
C) 5
D) Error
Answer: B

3. How do you access the last element of a list lst?

A) lst[last]
B) lst[-1]
C) lst[len(lst)]
D) lst(1)
Answer: B

4. What is the output of:

x = [1, 2, 3]
print(2 in x)

A) True
B) False
C) 2
D) Error
Answer: A

5. What will list(range(3)) return?

A) [0, 1, 2]
B) [1, 2, 3]
C) [0, 1, 2, 3]
D) (0, 1, 2)
Answer: A

6. Which operator is used to concatenate two lists?

A) +
B) *
C) %
D) &
Answer: A

7. What does lst.append(4) do?

A) Adds 4 at beginning
B) Adds 4 at end
C) Adds 4 at index 1
D) Nothing
Answer: B

8. What is the output?

lst = [1, 2, 3]
lst[1] = 10
print(lst)

A) [1, 2, 3]
B) [10, 2, 3]
C) [1, 10, 3]
D) Error
Answer: C

9. What does del lst[2] do?

A) Removes the last item


B) Deletes the list
C) Removes the third element
D) Clears the list
Answer: C

10. Which method removes the first occurrence of a value?

A) remove()
B) pop()
C) delete()
D) discard()
Answer: A

11. What is the output?

lst = [1, 2, 3]
print(lst * 2)

A) [1, 2, 3, 1, 2, 3]
B) [2, 4, 6]
C) [1, 4, 9]
D) Error
Answer: A

12. Which method returns the index of an item?

A) find()
B) locate()
C) index()
D) search()
Answer: C

13. How do you clone a list lst?

A) lst.copy()
B) list(lst)
C) lst[:]
D) All of the above
Answer: D

14. What is the output?

a = [1, 2, 3]
b = a
b.append(4)
print(a)

A) [1, 2, 3]
B) [1, 2, 3, 4]
C) Error
D) None
Answer: B

15. How do you create a nested list?

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


B) [(1, 2), (3, 4)]
C) {{1, 2}, {3, 4}}
D) All of the above
Answer: A

16. What is the output?

nested = [[1, 2], [3, 4]]


print(nested[1][0])

A) 1
B) 2
C) 3
D) 4
Answer: C

17. What is the result of lst.pop(0)?

A) Removes last item


B) Removes first item
C) Removes all
D) Adds element
Answer: B

18. What is the output?

lst = [1, 2, 3]
for i in lst:
print(i, end=", ")

A) 123
B) 1, 2, 3,
C) 1 2 3
D) Error
Answer: B

19. What will lst.clear() do?

A) Deletes one element


B) Removes last item
C) Removes all elements
D) Gives error
Answer: C

20. What is the output?

lst = [1, [2, 3], 4]


print(lst[1][1])
A) 2
B) 3
C) [2, 3]
D) Error
Answer: B

21. lst[::-1] does what?

A) Sorts the list


B) Clones the list
C) Reverses the list
D) None
Answer: C

22. Which of the following creates an empty list?

A) []
B) list()
C) Both
D) Neither
Answer: C

23. What is the result of:

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

A) Removes 4
B) Error
C) Removes last item
D) Returns None
Answer: B

24. What is the output of:

x = [1, 2, 3]
print(x[3])

A) 3
B) Error
C) 4
D) None
Answer: B

25. What is len([])?

A) 0
B) 1
C) Error
D) None
Answer: A

26. What is the output?

x = [1, 2, 3]
y = x.copy()
y.append(4)
print(x)

A) [1, 2, 3, 4]
B) [1, 2, 3]
C) Error
D) None
Answer: B

27. What is the output?

x = [10, 20, 30]


print(20 in x)

A) True
B) False
C) 20
D) Error
Answer: A

28. What does lst.insert(1, 100) do?

A) Replaces index 1 with 100


B) Inserts 100 at index 1
C) Adds 100 at end
D) Gives error
Answer: B

29. Which method adds all elements from another list?

A) extend()
B) append()
C) add()
D) concat()
Answer: A

30. How to check if a list is empty?

if not lst:
A) Correct
B) Error
C) Not efficient
D) None
Answer: A

31. What is the output?

lst = ['a', 'b', 'c']


print(lst[0:2])

A) ['a', 'b']
B) ['a', 'b', 'c']
C) ['b', 'c']
D) Error
Answer: A

32. What will this code do?

lst = [1, 2, 3]
lst += [4]
print(lst)

A) [1, 2, 3, 4]
B) [5]
C) Error
D) [1, 2, 3, [4]]
Answer: A

33. What is the result?

list1 = [1, 2]
list2 = list1
list1[0] = 100
print(list2)

A) [1, 2]
B) [100, 2]
C) [1, 100]
D) Error
Answer: B

34. What is the output?

lst = [10, 20, 30, 40]


print(lst[-2])
A) 20
B) 30
C) 40
D) Error
Answer: B

35. What does this return?

lst = [1, 2, 3]
print(lst.index(3))

A) 2
B) 3
C) 1
D) Error
Answer: A

36. What is the output?

lst = [[1, 2], [3, 4]]


print(len(lst))

A) 2
B) 4
C) 1
D) Error
Answer: A

37. Which will raise an IndexError?

A) lst[0] where lst = []


B) lst = [1]; lst[0]
C) lst = [1]; lst[-1]
D) lst = [1,2,3]; lst[2]
Answer: A

38. How do you copy only part of a list?

A) lst.copy(1:3)
B) lst.slice(1,3)
C) lst[1:3]
D) lst.sub(1,3)
Answer: C

39. What is the result of this loop?

for i in [10, 20, 30]:


print(i, end=' ')

A) 10 20 30
B) [10, 20, 30]
C) i i i
D) Error
Answer: A

40. What is printed?

lst = [1, 2, 3]
lst.remove(2)
print(lst)

A) [1, 2, 3]
B) [1, 3]
C) [2, 3]
D) Error
Answer: B

41. Which one is not a valid list operation?

A) append()
B) extend()
C) add()
D) insert()
Answer: C

42. How do you flatten [[1,2],[3,4]] to [1,2,3,4]?

A) Use sum(nested, [])


B) Use a loop
C) Use list comprehension
D) All of the above
Answer: D

43. What is printed?

lst = [4, 5, 6]
print(6 in lst)

A) True
B) False
C) 6
D) Error
Answer: A
44. How to remove all elements from list a?

A) a.delete()
B) a.remove()
C) a.clear()
D) a.empty()
Answer: C

45. What happens if list.pop() is used on an empty list?

A) Returns None
B) Returns 0
C) Raises IndexError
D) Does nothing
Answer: C

46. What does this do?

a = [1, 2]
b = a[:]
a[0] = 99
print(b)

A) [99, 2]
B) [1, 2]
C) [99]
D) Error
Answer: B

47. Which of the following is not allowed in a list?

A) Numbers
B) Strings
C) Dictionaries
D) All allowed
Answer: D

48. Which syntax creates a list of 5 zeros?

A) [0] * 5
B) [0,0,0,0,0]
C) list(range(5))
D) A and B
Answer: D

49. What is the output?


lst = ['a', 'b', 'c']
for i in range(len(lst)):
print(lst[i])

A) a b c
B) abc
C) ['a', 'b', 'c']
D) Error
Answer: A

50. What is the output of:

list1 = [1, 2]
list2 = list1[:]
print(list1 is list2)

A) True
B) False
C) Error
D) None
Answer: B
Topics: Object oriented programming: introduction to classes, objects and methods

1. What is the basic building block of Object-Oriented Programming?

A) Function
B) Variable
C) Class
D) Module
Answer: C

2. What keyword is used to define a class in Python?

A) object
B) class
C) def
D) method
Answer: B

3. What is an object in Python?

A) A collection of functions
B) An instance of a class
C) A Python file
D) A module
Answer: B

4. What is the correct way to create an object from a class?

class Student:
pass

A) Student.create()
B) student = new Student()
C) student = Student()
D) create Student()
Answer: C

5. Which method is automatically called when an object is created?

A) __create__()
B) __object__()
C) __init__()
D) __new__()
Answer: C

6. What is the output?


class A:
def __init__(self):
print("A created")
obj = A()

A) Nothing
B) Error
C) A created
D) Class A
Answer: C

7. Which of these defines an instance method?

A) def method:
B) def method(self):
C) def method():
D) def method(obj):
Answer: B

8. What does self refer to in a method?

A) The class
B) A function
C) The module
D) The object calling the method
Answer: D

9. Which is NOT a feature of OOP?

A) Inheritance
B) Encapsulation
C) Compilation
D) Polymorphism
Answer: C

10. How do you define a constructor in Python?

A) def constructor(self):
B) def init(self):
C) def __init__(self):
D) constructor()
Answer: C

11. What is method overloading in Python?

A) Using more than one method name


B) Defining methods with the same name but different parameters
C) Calling too many methods
D) It's not supported
Answer: D (Python does not support method overloading directly)

12. What is encapsulation?

A) Hiding the main logic


B) Protecting object data
C) Using many classes
D) Reusing code
Answer: B

13. Which keyword is used to inherit a class?

A) extends
B) inherits
C) super
D) None (inheritance is defined via parentheses)
Answer: D

14. Which is the parent class in:

class B(A):
pass

A) B
B) A
C) self
D) None
Answer: B

15. What is a class variable?

A) Defined inside __init__


B) Specific to each object
C) Shared by all instances
D) Not allowed
Answer: C

16. How do you access a class variable?

A) self.var
B) ClassName.var
C) obj.var
D) All of the above
Answer: D
17. How can you define a class variable?

class A:
x = 5

A) self.x = 5
B) x = 5
C) A.x = 5
D) var x = 5
Answer: B

18. What is the result?

class Test:
def greet(self):
return "Hello"
obj = Test()
print(obj.greet())

A) Hello
B) Test
C) Error
D) None
Answer: A

19. What is the output?

class A:
def __init__(self, x):
self.x = x
obj = A(10)
print(obj.x)

A) x
B) 10
C) Error
D) None
Answer: B

20. Which statement is true about class methods?

A) They use self


B) They use cls
C) They use self and cls
D) No arguments needed
Answer: B

21. How do you define a class method?


A) @staticmethod
B) @classmethod
C) @init
D) @class
Answer: B

22. What is the purpose of @staticmethod?

A) Binds method to class


B) Binds method to instance
C) Method that doesn’t access class or instance
D) Forces static typing
Answer: C

23. Which of the following is correct?

class A:
pass
a = A()
print(isinstance(a, A))

A) True
B) False
C) A
D) Error
Answer: A

24. What does super() do?

A) Calls constructor of parent class


B) Inherits class
C) Accesses local variables
D) Creates a new object
Answer: A

25. Which concept allows reusing code?

A) Polymorphism
B) Inheritance
C) Overloading
D) Constructor
Answer: B

26. Which is an example of polymorphism?

A) len("abc") and len([1,2,3])


B) x = 5 + 3
C) print("Hello")
D) x = 10
Answer: A

27. Which of the following can be used to restrict access to data?

A) public
B) private (with _ or __)
C) class
D) static
Answer: B

28. What is the output?

class A:
def __init__(self):
self.__x = 5
a = A()
print(a.__x)

A) 5
B) Error
C) None
D) __x
Answer: B (It's name mangled)

29. How to access a private variable?

A) Use self.__var
B) Use object._ClassName__var
C) Directly
D) Can't access
Answer: B

30. Which of these is not a magic method?

A) __init__()
B) __str__()
C) __main__()
D) __len__()
Answer: C
Topic: Standard Libraries

1. Which module provides access to mathematical functions?

A) maths
B) math
C) cmath
D) numbers
Answer: B

2. What is the output of math.sqrt(16)?

A) 4
B) 16
C) 8
D) Error
Answer: A

3. Which module is used for random number generation?

A) randomize
B) math
C) random
D) numbers
Answer: C

4. What does random.randint(1, 5) return?

A) Always 1
B) Any integer from 1 to 4
C) Any integer from 1 to 5 (inclusive)
D) Only float numbers
Answer: C

5. Which module can be used to get the current date and time?

A) calendar
B) os
C) datetime
D) time
Answer: C

6. What does datetime.datetime.now() return?

A) Only date
B) Only time
C) Date and time
D) Error
Answer: C

7. Which module is used to interact with the file system?

A) sys
B) os
C) io
D) platform
Answer: B

8. What does os.getcwd() return?

A) Python version
B) Directory path where Python is installed
C) Current working directory
D) List of all directories
Answer: C

9. What module helps to handle command-line arguments?

A) cli
B) os
C) sys
D) argparse
Answer: D

10. What does sys.exit() do?

A) Restarts the program


B) Exits from Python script
C) Clears memory
D) Displays OS name
Answer: B

11. Which module is used to compress files in Python?

A) compress
B) shutil
C) zipfile
D) gzipfile
Answer: C

12. What does math.ceil(4.2) return?


A) 4
B) 5
C) 4.0
D) 5.0
Answer: B

13. What module provides support for regular expressions?

A) string
B) regex
C) re
D) pattern
Answer: C

14. Which of these modules helps in serializing Python objects?

A) pickle
B) zipfile
C) json
D) Both A and C
Answer: D

15. What is the use of time.sleep(2)?

A) Prints time
B) Delays program for 2 seconds
C) Pauses output
D) Stops time module
Answer: B

16. Which module provides tools for working with iterators?

A) looptools
B) functools
C) itertools
D) re
Answer: C

17. Which function from statistics module gives the average of a list?

A) statistics.avg()
B) statistics.mean()
C) math.mean()
D) numpy.mean()
Answer: B
18. What module is used to interact with operating system environment variables?

A) env
B) os
C) sys
D) platform
Answer: B

19. What does platform.system() return?

A) Python version
B) System architecture
C) Operating system name (e.g., Windows, Linux)
D) RAM details
Answer: C

20. Which of the following is not a standard library?

A) math
B) random
C) os
D) numpy
Answer: D

21. Which module allows you to generate combinations and permutations?

A) random
B) functools
C) itertools
D) math
Answer: C

22. Which method is used to generate a random float between 0 and 1?

A) random.integer()
B) random.float()
C) random.random()
D) math.random()
Answer: C

23. What does os.listdir() return?

A) A string of files
B) A dictionary of files
C) A list of file and directory names
D) A list of only directories
Answer: C

24. What is the use of shutil module?

A) File compression
B) File and directory operations
C) Regular expressions
D) Random number generation
Answer: B

25. Which module provides accurate decimal floating point arithmetic?

A) math
B) decimal
C) float
D) fraction
Answer: B

26. What does math.floor(3.9) return?

A) 3
B) 4
C) 3.9
D) Error
Answer: A

27. Which module is used for parsing command-line arguments?

A) sys
B) optparse
C) argparse
D) os
Answer: C

28. Which module provides functions to manipulate dates and times?

A) time
B) datetime
C) Both A and B
D) calendar
Answer: C

29. Which of the following can be used to write JSON data to a file?
A) json.write()
B) json.dump()
C) json.store()
D) json.output()
Answer: B

30. What does calendar.isleap(2024) return?

A) False
B) None
C) True
D) 2024
Answer: C

31. Which module is used to open and work with CSV files?

A) csvfile
B) textio
C) csv
D) spreadsheet
Answer: C

32. Which module provides tools for functional programming?

A) functools
B) functional
C) toolz
D) utility
Answer: A

33. What function from the re module matches patterns at the beginning of a string?

A) re.search()
B) re.findall()
C) re.match()
D) re.compile()
Answer: C

34. What will math.pow(2, 3) return?

A) 6
B) 8
C) 9
D) 2^3 as a string
Answer: B
35. Which module supports multithreading in Python?

A) process
B) multiprocessing
C) concurrent
D) threading
Answer: D

36. Which function is used to read environment variables?

A) os.env()
B) os.environ.get()
C) sys.getenv()
D) platform.env()
Answer: B

37. What does sys.argv provide?

A) OS version
B) Python version
C) Command-line arguments
D) System name
Answer: C

38. What is the output of math.pi?

A) 3.14
B) 3.141592653589793
C) pi
D) Error
Answer: B

39. Which module is used to display formatted time?

A) calendar
B) datetime
C) time
D) os
Answer: C

40. Which module can you use to serialize Python objects into byte streams?

A) json
B) pickle
C) marshal
D) base64
Answer: B

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy