Python MCQs

Python MCQs

These Python multiple-choice questions and their answers will help you strengthen your grip on the subject of Python. You can prepare for an upcoming exam or job interview with these 800+ Python MCQs.
So scroll down and start answering.

1: What is a correct term for the following piece of Python syntax: 3 + 4

A.   A statement.

B.   A lambda.

C.   A closure.

D.   A suite.

E.   An expression.

2: What would be printed? x = [[1, 2], [3, 4], [5, 6]] x = [x for x in x for x in x] print x

A.   [[1, 2], [1, 2], [1, 2]]

B.   Compilation error

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

D.   [6, 6, 6, 6, 6, 6]

E.   [[1, 2], [3, 4], [5, 6]]

3: From which module can a user import classes and methods to work with regular expressions?

A.   sys

B.   re

C.   regularexpressions

D.   regexp

4: What is the exponentiation (power) operator?

A.   **

B.   ^

C.   Both are correct.

D.   None are correct.

5: What are wxPython, pyQT, and pyGTK?

A.   System administrators' toolkits for Python

B.   GUI programming toolkits for Python

C.   Test suites for Python

D.   Names of popular Python repositories

6: What is the difference between using single quotes and double quotes in Python strings?

A.   Single quotes are for short strings. Double quotes are for long strings.

B.   There is no difference.

C.   Double quotes are for constants. Single quotes are for variables.

D.   In Python you can use only single quotes.

7: What will typing the following at the Python interpreter produce? [3]*3

A.   '9'

B.   [9]

C.   '333'

D.   [3, 3, 3]

8: Which of these values equates to True?

A.   ()

B.   None of these

C.   ""

D.   {}

9: Functions are first-class objects in Python.

A.   Always

B.   Never

10: It is possible to create a while loop that will never stop?

A.   Yes

B.   No

11: Python variables are references to objects.

A.   Always

B.   Never

12: Python files typically have which extension?

A.   py

B.   pl

C.   pt

D.   pie

E.   pil

13: What is the index of the first item in a tuple or list?

A.   0

B.   1

C.   -2,147,483,648

D.   -1

14: What is the purpose of an 'if' statement?

A.   Repeatedly execute a block of code

B.   Conditionally execute a block of code

C.   Execute code from more than one place in a program

15: If you want a function you define to return "a", what should be put on the last line of the function (correctly indented)?

A.   "a"

B.   ret("a")

C.   return "a"

D.   a

16: What will be printed as the result of: animals = ['bear', 'tiger', 'penguin', 'zebra'] print animals[2] ?

A.   bear

B.   tiger

C.   penguin

D.   zebra

17: What character is used to denote the beginning of a comment?

A.   //

B.   {

C.   /*

D.   #

18: How does one print the string "fred" on a line by itself in Python 2.x?

A.   print 'fred'

B.   println('fred')

C.   cout << "fred" << endl;

D.   write('fred')

19: Which Python implementation is best for testing Java code?

A.   CPython

B.   IronPython

C.   Jython

D.   PyPy

20: True or False: Python holds the current working directory in memory.

A.   False

B.   True

21: True or False. in python you can run linux command

A.   False

B.   True

22: What keyword is needed to use an external module?

A.   use

B.   package

C.   external

D.   import

23: What does the sys module include?

A.   All are correct.

B.   The name of the operating system where your Python code is run.

C.   The largest integer the machine's architecture supports.

D.   The Python interpreter's version number.

24: What is the maximum recursion limit in Python?

A.   It's configurable.

B.   100000

C.   100

D.   10000

E.   1000000

25: What will be the value of a variable x after executing the expression x = y = 1?

A.   This expression will result in a syntax error.

B.   Undefined

C.   0

D.   1

26: Which of the following data structures supports the 'in' operator: list, dictionary, set ?

A.   None.

B.   set

C.   list

D.   dictionary

E.   All three.

27: When opening the Python shell, the prompt you see is

A.   >

B.   #

C.   $

D.   >>>

28: What kind of type system does Python have?

A.   Dynamic.

B.   Inverse.

C.   Loading.

D.   Proportional.

E.   Static.

29: Is it possible to call a function from within another function?

A.   Yes, but only in the interactive shell.

B.   Yes, but only when the program runs on a remote server.

C.   Yes.

D.   No.

30: What data type is the following: [1, 2, 3] ?

A.   Counter.

B.   List.

C.   Dictionary.

D.   Tuple.

E.   Set.

31: What does the print statement do?

A.   Returns the status of all printers installed on the network.

B.   Evaluates its parameters and writes the resulting objects to standard output.

C.   Allows users to input data.

D.   Prints the source code on the default printer.

32: What function do you use to determine the size of a tuple, a list, or a string?

A.   len

B.   length

C.   size

D.   Tuplen for tuples, listlen for lists, strlen for strings

33: How are comments denoted in Python?

A.   <!-- text -->

B.   # text

C.   // text

D.   /* text */

E.   ^^ text ^^

34: What is a string delimited by?

A.   "

B.   """

C.   '''

D.   All of these are correct,

E.   '

35: What does the expression string1 + string2 do?

A.   Repeats string1 string2 times (string2 must be in numeric format).

B.   Concatenates string1 and string2.

C.   It's a syntax error.

D.   Adds string1 to string2 (both must be in numeric format).

36: Which of these will result in an error?

A.   "hello" * 2

B.   "hello" ** 2

C.   len ( "hello" )

D.   "hello" + "hello"

37: What is a variable?

A.   A statement which allows you to choose one variant from a given set

B.   A collection of elements, each identified by an index

C.   An object which may contain some data

D.   An object containing data whose value cannot be altered by the program

38: What operator checks the inequality of two values?

A.   !=

B.   isn't

C.   =/=

D.   ><

E.   >=

39: How are non-anonymous Python functions declared?

A.   With the "function" keyword.

B.   With the "def" keyword.

C.   With function prototypes declared in header files.

D.   With "do function ... done".

40: Which one of following Internet protocols is supported by Python libraries: HTTP, FTP, SMTP, POP3, IMAP4, NNTP, Telnet?

A.   Only HTTP

B.   Only HTTP, FTP, POP3, IMAP4

C.   None of them

D.   All of them

41: When defining a class, what controls which parameters that may be passed when the class is instantiated?

A.   There is no way to pass parameters to a class instance.

B.   Specifying those parameters after the class name.

C.   Specifying those parameters in the definition of an __init__ method of the class.

D.   Writing the desired values after the class name when the class instance is created.

42: What is the del statement used for?

A.   To remove a function from a list given its name.

B.   To remove an item from a list given its index.

C.   To delete a file from the file system given its path.

D.   To delete a user if you are a privileged user.

43: The statement 'a,b = b,a' is a valid way to successfuly switch the values of the variables a and b in Python.

A.   False

B.   True

44: How would you declare a list x in Python with predefined values of 1, 2 and 3 ?

A.   x = list{'1', '2', '3'}

B.   x = list(1, 2, 3)

C.   x = {'1', '2', '3'}

D.   x = [1, 2, 3]

E.   x = new list (1, 2, 3)

45: Which of the following is NOT a valid part of an "if" block?

A.   elseif:

B.   elif:

C.   if:

D.   else:

46: How does one pause the execution of a program for 5 seconds?

A.   import date date.pause(5)

B.   import time time.sleep(5)

C.   import date date.pause(5.0)

47: What is the value of the variable a after the following statement: a=(1,2,3); a=a[2];

A.   2

B.   None and the system generates an error (exception)

C.   None

D.   1

E.   3

48: Which of these lines properly checks the length of a variable named "l"?

A.   l.len()

B.   len(l)

C.   length(l)

D.   l.length()

49: If i and j are numbers, which statement evaluates to the remainder of i divided by j?

A.   i mod j

B.   i % j

C.   i // j

D.   i / j

50: How do you print from 0 to 10 in python?

A.   for i in range(11): print i

B.   for( x = 0; x < 11; x++) printf("%s",x);

C.   while x < 10: print x

D.   for i in range(10): print i