Python - MCQ Questions and Answers

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

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

21. What is the output of print(3 * 'ab' + 'c')?

  • a) ababc
  • b) abababc
  • c) abababcc
  • d) abababc
Answer: D - String multiplication repeats the string: 'ab'*3 = 'ababab', then concatenates 'c'.

22. Which method removes all items from a dictionary?

  • a) clear()
  • b) removeall()
  • c) empty()
  • d) delete()
Answer: A - my_dict.clear() empties the dictionary.

23. What does the pass statement do?

  • a) Acts as a null operation placeholder
  • b) Terminates the program
  • c) Skips the current iteration
  • d) Passes arguments to a function
Answer: A - pass is a no-op statement used where syntax requires a statement but no action is needed.

24. Which module would you use for mathematical operations like sqrt?

  • a) math
  • b) numpy
  • c) random
  • d) statistics
Answer: A - The math module provides mathematical functions: math.sqrt(4).

25. What is the output of print('Python'.find('th'))?

  • a) 2
  • b) 3
  • c) -1
  • d) True
Answer: A - find() returns the lowest index where substring starts (0-based index).

26. How do you create a tuple with a single element?

  • a) (1,)
  • b) (1)
  • c) tuple(1)
  • d) [1]
Answer: A - The comma makes it a tuple: (1,) vs (1) which is just parentheses.

27. What does sys.argv contain?

  • a) Command-line arguments
  • b) Python version info
  • c) Installed modules
  • d) Environment variables
Answer: A - sys.argv is a list of command-line arguments passed to the script.

28. Which operator checks if two variables reference the same object?

  • a) is
  • b) ==
  • c) equals
  • d) same
Answer: A - is checks identity (same memory location), while == checks equality.

29. What is the output of print('Hello'.replace('l', 'L', 1))?

  • a) HeLlo
  • b) HeLLo
  • c) Hello
  • d) HLllo
Answer: A - The third argument (1) limits replacements to the first occurrence only.

30. Which method returns a list of dictionary keys?

  • a) keys()
  • b) get_keys()
  • c) items()
  • d) values()
Answer: A - my_dict.keys() returns a view of all keys (use list() to convert to list).

31. What does the else clause in a for loop do?

  • a) Executes if loop completes normally (no break)
  • b) Always executes after the loop
  • c) Handles exceptions in the loop
  • d) Alternative to elif
Answer: A - The else in loops executes only if no break occurred.

32. Which Python data type is immutable and ordered?

  • a) tuple
  • b) list
  • c) set
  • d) dict
Answer: A - Tuples are immutable ordered sequences: (1, 2, 3).

33. What is the output of print(bool([]))?

  • a) False
  • b) True
  • c) []
  • d) None
Answer: A - Empty sequences are falsy in Python.

34. Which function returns the absolute value of a number?

  • a) abs()
  • b) absolute()
  • c) fabs()
  • d) mod()
Answer: A - abs(-5) returns 5.

35. What does zip() function do?

  • a) Combines iterables element-wise
  • b) Compresses files
  • c) Creates a dictionary
  • d) Sorts a list
Answer: A - zip([1,2], ['a','b']) yields (1,'a'), (2,'b').

36. Which module is used for working with dates?

  • a) datetime
  • b) time
  • c) calendar
  • d) dateutil
Answer: A - The datetime module provides date/time manipulation classes.

37. What is the output of print('Python'.islower())?

  • a) False
  • b) True
  • c) None
  • d) Error
Answer: A - Checks if all cased characters are lowercase (fails due to 'P').

38. Which method splits a string at line breaks?

  • a) splitlines()
  • b) split('\n')
  • c) lines()
  • d) Both A and B
Answer: D - Both methods work, but splitlines() handles different line endings (\n, \r, etc.).

39. What is the output of print(round(3.14159, 2))?

  • a) 3.14
  • b) 3.142
  • c) 3.141
  • d) 3.1
Answer: A - Rounds to 2 decimal places (banker's rounding).

40. Which function returns the smallest item in an iterable?

  • a) min()
  • b) smallest()
  • c) minimum()
  • d) least()
Answer: A - min([3,1,4]) returns 1.
« First 1 2 3 4 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.