Sign In
Computer Science

Python Programming Quiz & Flashcards

Master Python Programming concepts with our interactive study cards featuring 51 practice Quiz questions and 52 flashcards to boost your exam scores and retention in Computer Science.

Create your own study sets

Turn any PDF, lecture notes, or ChatGPT conversation into interactive quizzes in seconds.

Get started

51 Multiple Choice Questions and Answers on Python Programming

Revise and practice with 51 comprehensive MCQ on Python Programming, featuring detailed explanations to deepen your understanding of Computer Science Quiz concepts. Perfect for quick review and exam preparation.

1 What does the 'len()' function do in Python?

A. Returns the number of items in an object
B. Calculates the memory size of an object
C. Converts an object to a list
D. Checks if an object is empty
Explanation

The 'len()' function is used to return the number of items in an object.

2 Which of the following is used to create an empty dictionary?

A. {}
B. []
C. ()
D. set()
Explanation

An empty dictionary is created using curly braces '{}'.

3 What is the output of 'print(type([]))'?

A. <class 'list'>
B. <class 'tuple'>
C. <class 'dict'>
D. <class 'set'>
Explanation

The empty square brackets '[]' denote a list, so the type is '<class 'list'>'.

4 How do you create a comment in Python?

A. Using #
B. Using //
C. Using <!-- -->
D. Using /* */
Explanation

Comments in Python are created using the '#' symbol.

5 What is the result of '5 // 2' in Python?

A. 2
B. 2.5
C. 3
D. 1
Explanation

The '//' operator performs floor division, which results in '2' for '5 // 2'.

6 Which keyword is used to handle exceptions?

A. try
B. catch
C. throw
D. handle
Explanation

The 'try' keyword initiates a block of code that will attempt an operation which may raise an exception.

7 What is the role of 'self' in class methods?

A. Refers to the instance of the class
B. Indicates a private variable
C. Denotes a static method
D. Refers to the superclass
Explanation

'self' refers to the instance of the class and is used to access variables and methods associated with the instance.

8 What does the 'break' statement do?

A. Exits the current loop
B. Skips the current loop iteration
C. Restarts the loop
D. Continues to the next block
Explanation

The 'break' statement exits the current loop prematurely.

9 Which of the following is a mutable type?

A. List
B. Tuple
C. String
D. Integer
Explanation

Lists are mutable, meaning their elements can be changed.

10 What is the purpose of the 'isinstance()' function?

A. To check if an object is an instance of a class
B. To create an instance of a class
C. To delete an instance
D. To convert an instance to another type
Explanation

'isinstance()' checks if an object is an instance or subclass thereof of a class.

11 How do you open a file for writing in Python?

A. open('file.txt', 'w')
B. open('file.txt', 'r')
C. open('file.txt', 'a')
D. open('file.txt', 'x')
Explanation

The 'w' mode in open() is used to open a file for writing.

12 What keyword is used to define a function?

A. def
B. func
C. function
D. lambda
Explanation

Functions are defined in Python using the 'def' keyword.

13 Which function is used to convert a string to lowercase?

A. lower()
B. downcase()
C. toLowerCase()
D. strlower()
Explanation

The 'lower()' method is used to convert a string to lowercase.

14 Which of the following data structures does not allow duplicate elements?

A. Set
B. List
C. Dictionary
D. Tuple
Explanation

Sets do not allow duplicate elements.

15 What does the 'pop()' method do in a list?

A. Removes and returns the last item
B. Adds an item to the list
C. Removes all items
D. Sorts the list
Explanation

The 'pop()' method removes and returns the last item from a list if no index is specified.

16 Which operator is used for exponentiation in Python?

A. **
B. ^
C. exp
D. ^^
Explanation

The '**' operator is used for exponentiation in Python.

17 Which statement about Python is true?

A. It supports multiple programming paradigms
B. It does not support object-oriented programming
C. It can only be used for scripting
D. It requires compilation before running
Explanation

Python supports multiple programming paradigms, including procedural, object-oriented, and functional programming.

18 What is a correct syntax to output 'Hello World' in Python?

A. print('Hello World')
B. echo('Hello World')
C. printf('Hello World')
D. cout << 'Hello World'
Explanation

The correct syntax for outputting text in Python is using the 'print()' function.

19 Which of the following is not a valid Python variable name?

A. 2variable
B. variable2
C. _variable
D. variable_
Explanation

Variable names cannot start with a number, making '2variable' invalid.

20 What will 'print(3 * 'abc')' output?

A. abcabcabc
B. abc
C. 3abc
D. abc3
Explanation

Multiplying a string by a number repeats the string, resulting in 'abcabcabc'.

21 What is the purpose of the 'dir()' function?

A. Lists the properties and methods of an object
B. Deletes an object
C. Creates an object
D. Copies an object
Explanation

'dir()' is used to list the properties and methods of an object.

22 Which method is used to add an element to the end of a list?

A. append()
B. insert()
C. add()
D. extend()
Explanation

The 'append()' method adds an element to the end of a list.

23 What is the output of 'print(type(3.14))'?

A. <class 'float'>
B. <class 'int'>
C. <class 'complex'>
D. <class 'decimal'>
Explanation

The number 3.14 is a floating-point number, so its type is '<class 'float'>'.

24 What does the 'zip()' function do?

A. Combines two iterables element-wise
B. Sorts a list
C. Splits a string
D. Joins two strings
Explanation

The 'zip()' function combines two or more iterables element-wise into tuples.

25 What will 'bool([])' return?

A. False
B. True
C. None
D. Error
Explanation

An empty list is considered False in a boolean context.

26 Which keyword is used to create an alias for a module?

A. as
B. alias
C. rename
D. with
Explanation

The 'as' keyword is used to create an alias for a module during import.

27 What is the output of 'print(5 > 3 and 2 < 4)'?

A. True
B. False
C. Syntax Error
D. None
Explanation

Both conditions are true, so the 'and' operation returns True.

28 Which function converts a list of strings to a single string with elements separated by a comma?

A. ','.join(list)
B. list.split(',')
C. list.add(',')
D. str(list)
Explanation

The 'join()' method concatenates list elements into a single string separated by commas.

29 Which operator checks if two variables point to the same object?

A. is
B. ==
C. >
D. <
Explanation

The 'is' operator checks for identity, i.e., if two variables point to the same object.

30 What is the output of 'print('2' + '3')'?

A. 23
B. 5
C. 2 3
D. Error
Explanation

The '+' operator concatenates two strings, so '2' + '3' becomes '23'.

31 What will be the result of '3 % 2'?

A. 1
B. 0
C. 2
D. 3
Explanation

'3 % 2' gives the remainder of the division, which is 1.

32 How do you find the maximum value in a list?

A. max(list)
B. list.max()
C. maximum(list)
D. max(list, key=None)
Explanation

The 'max()' function returns the maximum value in a list.

33 What does 'setdefault()' do in a dictionary?

A. Returns the value of a key if it exists, otherwise inserts the key with a default value
B. Sets a default value for all dictionary keys
C. Deletes a key
D. Sorts the dictionary
Explanation

The 'setdefault()' method returns the value of a key if it exists, otherwise inserts the key with a default value.

34 What will 'print(type({}))' output?

A. <class 'dict'>
B. <class 'list'>
C. <class 'set'>
D. <class 'tuple'>
Explanation

An empty '{}' is interpreted as a dictionary, so the type is '<class 'dict'>'.

35 What is the result of 'not True and False'?

A. False
B. True
C. Syntax Error
D. None
Explanation

'not True' evaluates to False, and 'False and False' results in False.

36 Which of the following is a built-in function in Python?

A. len()
B. list.add()
C. string.concat()
D. dict.sort()
Explanation

'len()' is a built-in function that returns the number of items in an object.

37 What does 'range(5)' return?

A. A sequence of numbers from 0 to 4
B. A sequence of numbers from 1 to 5
C. A sequence of numbers from 0 to 5
D. An error
Explanation

'range(5)' returns a sequence from 0 to 4.

38 What will be the output of 'print(bool(0))'?

A. False
B. True
C. None
D. Error
Explanation

The integer 0 is considered False in a boolean context.

39 Which method is used to remove all elements from a list?

A. clear()
B. remove()
C. delete()
D. pop()
Explanation

The 'clear()' method removes all elements from a list.

40 What is the output of '5 ** 2'?

A. 25
B. 10
C. 7
D. 20
Explanation

The '**' operator raises the first number to the power of the second, so '5 ** 2' is 25.

41 Which of these is a correct way to create a function that takes no arguments and returns 10?

A. def func(): return 10
B. function func() { return 10; }
C. def func: return 10
D. create function func() { return 10; }
Explanation

In Python, 'def func(): return 10' correctly defines a function with no arguments.

42 What will 'print(3 != 3)' output?

A. False
B. True
C. 3
D. None
Explanation

The '!=' operator checks for inequality, so '3 != 3' is False.

43 What is the correct syntax for a conditional expression?

A. a if condition else b
B. if condition: a else b
C. a then b if condition
D. if a then condition else b
Explanation

The syntax 'a if condition else b' is used for conditional expressions in Python.

44 How do you convert a list to a tuple?

A. tuple(list)
B. list.toTuple()
C. convert(list, tuple)
D. list -> tuple
Explanation

The 'tuple()' function converts a list to a tuple.

45 What is the output of 'print(3 * 'abc')'?

A. abcabcabc
B. abc
C. 3abc
D. abc3
Explanation

Multiplying a string by a number repeats the string, resulting in 'abcabcabc'.

46 What is the output of 'print(10 // 3)'?

A. 3
B. 3.33
C. 4
D. Error
Explanation

'10 // 3' performs floor division, which results in '3'.

47 Which function is used to read all lines of a file as a list?

A. readlines()
B. read()
C. readline()
D. file()
Explanation

The 'readlines()' method reads all lines of a file and returns them as a list.

48 Which statement about Python lists is true?

A. They can contain elements of different types
B. They are immutable
C. They require a fixed size
D. They can only store integers
Explanation

Python lists are mutable and can contain elements of different types.

49 What will 'print(type((1,)))' output?

A. <class 'tuple'>
B. <class 'list'>
C. <class 'dict'>
D. <class 'int'>
Explanation

The syntax '(1,)' creates a tuple with a single element.

50 What is the result of '3 and 0'?

A. 0
B. 3
C. True
D. False
Explanation

The 'and' operator returns the first falsy value, which is '0' in this case.

51 Which of the following is a correct way to declare a multi-line string?

A. '''string'''
B. """string"""
C. 'string'
D. Both A and B
Explanation

Both triple single quotes and triple double quotes can be used to declare multi-line strings.