Python - MCQ Questions and Answers

Test your knowledge of Python with these interactive multiple-choice questions.

« First 2 3 4 5 Last »
« Previous Page Next Page »

41. What is the output of print([1, 2, 3][::-1])?

  • a) [3, 2, 1]
  • b) [1, 2, 3]
  • c) [3]
  • d) Error
Answer: A - [::-1] reverses the list (step value of -1).

42. Which method returns the number of occurrences of an element in a list?

  • a) count()
  • b) occurrences()
  • c) len()
  • d) find()
Answer: A - my_list.count(item) returns the count.

43. What does the any() function do?

  • a) Returns True if any element is truthy
  • b) Checks if all elements are True
  • c) Counts truthy elements
  • d) Applies OR operation
Answer: A - any([False, True, False]) returns True.

44. Which module is used for working with JSON data?

  • a) json
  • b) simplejson
  • c) pickle
  • d) yaml
Answer: A - Python's built-in json module handles JSON serialization/deserialization.

45. What is the output of print('Python'.center(10, '*'))?

  • a) **Python**
  • b) ***Python**
  • c) **Python***
  • d) Python****
Answer: A - Centers the string in 10 chars, padding with asterisks.

46. Which method removes and returns the last element of a list?

  • a) pop()
  • b) remove()
  • c) delete()
  • d) cut()
Answer: A - my_list.pop() removes and returns the last item.

47. What is the output of print(0.1 + 0.2 == 0.3)?

  • a) False
  • b) True
  • c) Error
  • d) None
Answer: A - Due to floating-point precision, 0.1 + 0.2 equals 0.30000000000000004.

48. Which function returns a list of tuples containing index and value?

  • a) enumerate()
  • b) items()
  • c) iter()
  • d) listify()
Answer: A - enumerate(['a','b']) yields (0,'a'), (1,'b').

49. What does __str__ method do?

  • a) Defines informal string representation
  • b) Initializes object
  • c) Handles string conversion
  • d) Both A and C
Answer: A - __str__ is called by str() and print().

50. Which module is used for command-line argument parsing?

  • a) argparse
  • b) sys
  • c) getopt
  • d) All of the above
Answer: D - All can parse arguments, but argparse is the modern recommended approach.

51. What is the output of print('Python'.isidentifier())?

  • a) True
  • b) False
  • c) None
  • d) Error
Answer: A - Checks if string is a valid Python identifier (variable name).

52. Which method returns a shallow copy of a dictionary?

  • a) copy()
  • b) clone()
  • c) dict()
  • d) All of the above
Answer: A - my_dict.copy() creates a new dictionary with the same key-value pairs.

53. What is the output of print('Hello'.partition('l'))?

  • a) ('He', 'l', 'lo')
  • b) ('Hel', 'l', 'o')
  • c) ('H', 'e', 'llo')
  • d) ('He', 'll', 'o')
Answer: A - Splits at first occurrence into 3 parts: (before, separator, after).

54. Which function returns the largest item in an iterable?

  • a) max()
  • b) largest()
  • c) maximum()
  • d) greatest()
Answer: A - max([3,1,4]) returns 4.

55. What is the output of print('Python'.endswith('on'))?

  • a) True
  • b) False
  • c) None
  • d) Error
Answer: A - Checks if string ends with the specified suffix.

56. Which module is used for working with operating system interfaces?

  • a) os
  • b) sys
  • c) io
  • d) path
Answer: A - The os module provides OS-dependent functionality.

57. What is the output of print('Python'.swapcase())?

  • a) pYTHON
  • b) PYTHON
  • c) python
  • d) Python
Answer: A - Swaps uppercase to lowercase and vice versa.

58. Which method removes whitespace from both ends of a string?

  • a) strip()
  • b) trim()
  • c) clean()
  • d) remove()
Answer: A - ' hello '.strip() returns 'hello'.

59. What is the output of print('Python'.index('th'))?

  • a) 2
  • b) 3
  • c) -1
  • d) Error
Answer: A - Similar to find() but raises ValueError if not found.

60. Which function returns a sequence of numbers starting from 0?

  • a) range()
  • b) sequence()
  • c) numbers()
  • d) list()
Answer: A - range(5) generates numbers from 0 to 4.
« First 2 3 4 5 Last »
« Previous Page Next Page »

Learn Computer Skills with PCBooks

Master computer skills with PCBooks! Explore interactive tutorials, MCQ tests, and online exams for programming, web development, and IT fundamentals. Perfect for beginners and advanced learners.

Online MCQ Tests for Programming

Practice programming MCQ questions and improve your coding skills with our online quizzes. Whether you're learning Python, Java, or C programming, PCBooks has you covered.

Free Web Development Tutorials

Learn HTML, CSS, and JavaScript with our free web development tutorials. Test your knowledge with interactive quizzes and online exams.