0% found this document useful (0 votes)
3 views17 pages

Python Programming MCQs

The document contains a series of multiple-choice questions (MCQs) related to Python programming, covering topics such as variable naming, data types, functions, recursion, and libraries. Each question is followed by the correct answer, providing a comprehensive overview of key Python concepts. The content is structured to assist learners in testing their knowledge of Python programming principles.

Uploaded by

tunnel guru
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)
3 views17 pages

Python Programming MCQs

The document contains a series of multiple-choice questions (MCQs) related to Python programming, covering topics such as variable naming, data types, functions, recursion, and libraries. Each question is followed by the correct answer, providing a comprehensive overview of key Python concepts. The content is structured to assist learners in testing their knowledge of Python programming principles.

Uploaded by

tunnel guru
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/ 17

Gujarat Power Engineering and Research Institute

Computer Engineering Department


Python Programming - 2180711 (MCQs)

1. Which of the following is an invalid variable?


a) my_string_1
b) 1st_string
c) foo
d) _

Answer : b

2. Why are local variable names beginning with an underscore discouraged?


a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution

Answer: a

3. Which of the following is not a keyword?


a) eval
b) assert
c) nonlocal
d) pass

Answer: a

4. Which of these in not a core data type?


a) Lists
b) Dictionary
c) Tuples
d) Class

Answer: d

5. Given a function that does not return any value, What value is thrown by default
when executed in shell.
a) int
b) bool
c) void
d) None

Answer: d

6. What will be the output of the following Python code?


>>>str="hello"
>>>str[:2]
a) he
b) lo

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


c) olleh
d) hello

Answer: a

1. Is Python case sensitive when dealing with identifiers?


a) yes
b) no
c) machine dependent
d) not mentioned

Answer: a

8. What is the return type of function id?


a) int
b) float
c) bool
d) dict

Answer: a

9. What error occurs when you execute the following Python code snippet?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError

Answer: b

10. What data type is the object below?


L = [1, 23, 'hello', 1]
a) list
b) dictionary
c) array
d) tuple

Answer: a

11. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary

Answer: d

12.Which of the following results in a SyntaxError?


a) ‘”Once upon a time…”, she said.’
b) “He said, ‘Yes!'”
c) ‘3\’
d) ”’That’s okay”’

Answer: c

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


13. What is the return value of trunc()?
a) int
b) bool
c) float
d) None

Answer: a

14. What will be the value of the following Python expression?


4+3%5
a) 4
b) 7
c) 2
d) 0

Answer: b

15. What will be the output of the following Python statement?


1. >>>"abcd"[2:]

a)a
b)ab
c)cd
d) dc

Answer: c
16. What will be the output of the following Python code?
1. >>>str1="helloworld"
2. >>>str1[::-1]

a) dlrowolleh
b) hello
c) world
d) helloworld

Answer: a

17. What is the output when we execute list(“hello”)?


a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) [‘hello’]
c) [‘llo’]
d) [‘olleh’]

Answer: a

18. To shuffle the list(say list1) what function do we use?


a) list1.shuffle()
b) shuffle(list1)
c) random.shuffle(list1)

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


d) random.shuffleList(list1)

Answer: c
19. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
a) Error
b) None
c) 25
d) 2

Answer: c
20. What will be the output of the following Python code?
1. >>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
2. >>>print(names[-1][-1])

a) A
b) Daman
c) Error
d) n

Answer: d

21. What will be the output of the following Python code?


1. >>>"Welcome to Python".split()

a) [“Welcome”, “to”, “Python”]


b) (“Welcome”, “to”, “Python”)
c) {“Welcome”, “to”, “Python”}
d) “Welcome”, “to”, “Python”

Answer: a

22. Which of the following is a Python tuple?


a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) {}

Answer: b

23. Suppose t = (1, 2, 4, 3), which of the following is incorrect?


a) print(t[3])
b) t[3] = 45
c) print(max(t))
d) print(len(t))

Answer: b

24. What will be the output of the following Python code?

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


1. >>>t=(1,2,4,3)
2. >>>t[1:3]

a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)

Answer: c

25. Which of these about a set is not true?


a) Mutable data type
b) Allows duplicate values
c) Data type with unordered values
d) Immutable data type

Answer: d

26. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
c) set((1,2,3,4))
d) {1,2,3,4}

Answer: a
27. Which of the following functions is a built-in function in python?
a) seed()
b) sqrt()
c) factorial()
d) print()

Answer: d

28. What will be the output of the following Python expression?

round(4.576)

a) 4.5
b) 5
c) 4
d) 4.6

Answer: b

29. he function pow(x,y,z) is evaluated as:


a) (x**y)**z
b) (x**y) / z
c) (x**y) % z
d) (x**y)*z

Answer: c

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


30. What is the output of the function complex()?
a) 0j
b) 0+0j
c) 0
d) Error

Answer: a
31. Which of the following functions does not necessarily accept only iterables as
arguments?
a) enumerate()
b) all()
c) chr()
d) max()

Answer: c

32. Which of the following is the use of function in python?


a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned

Answer: a

33. Which keyword is used for function?


a) Fun
b) Define
c) Def
d) Function

Answer: c

34. Which of the following is the use of id() function in python?


a) Id returns the identity of the object
b) Every object doesn’t have a unique id
c) All of the mentioned
d) None of the mentioned

Answer: a

35. Which of the following refers to mathematical function?


a) sqrt
b) rhombus
c) add
d) rhombus

Answer: a

36. Python supports the creation of anonymous functions at runtime, using a


construct called __________
a) lambda
b) pi

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


c) anonymous
d) none of the mentioned

Answer: a

37. What will be the output of the following Python code?


1. def f(x, y, z): return x + y + z
2. f(2, 30, 400)

a) error
b) 24000
c) 430
d) 432

Answer : d

38. What is a variable defined inside a function referred to as?


a) A global variable
b) A volatile variable
c) A local variable
d) An automatic variable

Answer: c
39. If a function doesn’t have a return statement, which of the following does the
function return?
a) int
b) null
c) None
d) An exception is thrown without the return statement

Answer: c
40. What is the type of each element in sys.argv?
a) set
b) list
c) tuple
d) string

Answer: d
41. How are keyword arguments specified in the function heading?
a) one-star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier

Answer: c
42. How many keyword arguments can be passed to a function in a single function
call?
a) zero

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


b) one
c) zero or more
d) one or more

Answer: c
43. How are variable length arguments specified in the function heading?
a) one star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier

Answer: a
44. Which module in the python standard library parses options received from the
command line?
a) getopt
b) os
c) getarg
d) main

Answer: a
45. How are default arguments specified in the function heading?
a) identifier followed by an equal to sign and the default value
b) identifier followed by the default value within backticks (“)
c) identifier followed by the default value within square brackets ([])
d) identifier

Answer: a
46. Which of the following data structures is returned by the functions globals()
and locals()?
a) list
b) set
c) dictionary
d) tuple

Answer: c
47.What happens if a local variable exists with the same name as the global
variable you want to access?
a) Error
b) The local variable is shadowed
c) Undefined behavior
d) The global variable is shadowed

Answer: d
48.Which is the most appropriate definition for recursion?
a) A function that calls itself
b) A function execution instance that calls another execution instance of the same
function
c) A class method that calls another class method

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


d) An in-built method that is automatically called

Answer: b
49.Which of these is false about recursion?
a) Recursive function can be replaced by a non-recursive function
b) Recursive functions usually take more memory space than non-recursive function
c) Recursive functions run faster than non-recursive function
d) Recursion makes programs easier to understand

Answer: c
50.What is tail recursion?
a) A recursive function that has two base cases
b) A function where the recursive functions leads to an infinite loop
c) A recursive function where the function doesn’t return anything and just prints the
values
d) A function where the recursive call is the last thing executed by the function

Answer: d
51.What happens if the base condition isn’t defined in recursive programs?
a) Program gets into an infinite loop
b) Program runs once
c) Program runs n number of times where n is the argument given to the function
d) An exception is thrown

Answer: a
52.Which of the following functions can help us to find the version of python that
we are currently working on?
a) sys.version
b) sys.version()
c) sys.version(0)
d) sys.version(1)

Answer: a
53.Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity

Answer: a
54.______ is a string literal denoted by triple quotes for providing the
specifications of certain program elements.
a) Interface
b) Modularity
c) Client
d) Docstring

Answer: d

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


55. Which of the following is not a valid namespace?
a) Global namespace
b) Public namespace
c) Built-in namespace
d) Local namespace

Answer: b
56.What will be the output of the following Python code?

from math import factorial


print(math.factorial(5))

a) 120
b) Nothing is printed
c) Error, method factorial doesn’t exist in math module
d) Error, the statement should be: print(factorial(5))

Answer: d
57.What will be the output of the following Python code if the system date is 18th
August, 2016?

tday=datetime.date.today()
print(tday.month())

a) August
b) Aug
c) 08
d) 8

Answer: d
58.Which of the following functions can be used to find the coordinated universal
time, assuming that the datetime module has already been imported?
a) datetime.utc()
b) datetime.datetime.utc()
c) datetime.utcnow()
d) datetime.datetime.utcnow()

Answer: d
59.The sleep function (under the time module) is used to ___________
a) Pause the code for the specified number of seconds
b) Return the specified number of seconds, in terms of milliseconds
c) Stop the execution of the code
d) Return the output of the code had it been executed earlier by the specified number of
seconds

Answer: a

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


60. To include the use of functions which are present in the random library, we
must use the option:
a) import random
b) random.h
c) import.random
d) random.random

Answer: a
61. What will be the output of the following Python code?

import random
random.choice(2,3,4)

a) An integer other than 2, 3 and 4


b) Either 2, 3 or 4
c) Error
d) 3 only

Answer: c

62. Which of the following returns a string that represents the present working
directory?
a) os.getcwd()
b) os.cwd()
c) os.getpwd()
d) os.pwd()

Answer: a
63. What does os.link() do?
a) create a symbolic link
b) create a hard link
c) create a soft link
d) none of the mentioned

Answer: b
64. The command which helps us to reset the pen (turtle):
a) turtle.reset
b) turtle.penreset
c) turtle.penreset()
d) turtle.reset()

Answer: d
65. Which of the following functions does not accept any arguments?
a) position
b) fillcolor
c) goto
d) setheading()

Answer: a

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


66. What will be the output of the following Python code?

import turtle
t=turtle.Pen()
t.goto(300,9)
t.position()

a) 300.00, 9.00
b) 9, 300
c) 300, 9
d) 9.00, 300.00

Answer: a
67. What will be the output of the following Python code?

import turtle
t=turtle.Pen()
t.color(0,0,1)
t.begin_fill()
t.circle(15)
t.end_fill()

a) Error
b) A circle filled in with the colour red
c) A circle filled in with the colour blue
d) A circle filled in with the colour green

Answer: c
68. Which of the following functions can be used to make the arrow black?
a) turtle.color(0,1,0)
b) turtle.color(1,0,0)
c) turtle.color(0,0,1)
d) turtle.color(0,0,0)

Answer: d
69. In which direction is the turtle pointed by default?
a) North
b) South
c) East
d) West

Answer: c
70. The command used to set only the x coordinate of the turtle at 45 units is:
a) reset(45)
b) setx(45)
c) xset(45)
d) xreset(45)

Answer: b

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


71. Which of the following functions returns a value in degrees, counterclockwise
from the horizontal right?
a) heading()
b) degrees()
c) position()
d) window_height()

Answer: a
72. Which of the following functions results in an error?
a) turtle.shape(“turtle”)
b) turtle.shape(“square”)
c) turtle.shape(“triangle”)
d) turtle.shape(“rectangle”)

Answer: d
73. What will be the output of the following Python code?

import turtle
t=turtle.Pen()
t.backward(100)
t.penup()
t.right(45)
t.isdown()

a) True
b) False
c) Yes
d) No

Answer: b
74. The function used to alter the thickness of the pen to ‘x’ units:
a) turtle.width(x)
b) turtle.span(x)
c) turtle.girth(x)
d) turtle.thickness(x)

Answer: a
75.Which module in Python supports regular expressions?
a) re
b) regex
c) pyregex
d) none of the mentioned

Answer: a

76. Which of the following creates a pattern object?


a) re.create(str)
b) re.regex(str)
c) re.compile(str)

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


d) re.assemble(str)

Answer: c
77. What does the function re.match do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned

Answer: a
78. What does the function re.search do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned

Answer: b
79. Which function overloads the + operator?
a) __add__()
b) __plus__()
c) __sum__()
d) none of the mentioned

Answer: a
80. Which operator is overloaded by __invert__()?
a) !
b) ~
c) ^
d) –

Answer: b
81. _____ represents an entity in the real world with its identity and behaviour.
a) A method
b) An object
c) A class
d) An operator

Answer: b
82. . _____ is used to create an object.
a) class
b) constructor
c) User-defined functions
d) In-built functions

Answer: b
83. The assignment of more than one function to a particular operator is _______
a) Operator over-assignment
b) Operator overriding

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


c) Operator overloading
d) Operator instance

Answer: c
84. Which of the following is not a class method?
a) Non-static
b) Static
c) Bounded
d) Unbounded

Answer: a
85. What are the methods which begin and end with two underscore characters
called?
a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods

Answer: a
86.What does print(Test.__name__) display (assuming Test is the name of the
class)?
a) ()
b) Exception is thrown
c) Test
d) __main__

Answer: c
87. Which of the following statements is wrong about inheritance?
a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP

Answer: c

88. Suppose B is a subclass of A, to invoke the __init__ method in A from B, what


is the line of code you should write?
a) A.__init__(self)
b) B.__init__(self)
c) A.__init__(B)
d) B.__init__(A)

Answer: a
89. What type of inheritance is illustrated in the following Python code?

class A():
pass
class B():
pass
class C(A,B):

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


pass

a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance

Answer: b
90. What is the biggest reason for the use of polymorphism?
a) It allows the programmer to think at a more abstract level
b) There is less program code to write
c) The program will have a more elegant design and will be easier to maintain and
update
d) Program code takes up less space

Answer: c
91. Which of the following statements is true?
a) A non-private method in a superclass can be overridden
b) A subclass method can be overridden by the superclass
c) A private method in a superclass can be overridden
d) Overriding isn’t possible in Python

Answer: a
92. How many except statements can a try-except block have?
a) zero
b) one
c) more than one
d) more than zero

Answer: d
93. When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block

Answer: c
94. Is the following Python code valid?

try:
# Do something
except:
# Do something
finally:
# Do something

a) no, there is no such thing as finally


b) no, finally cannot be used with except
c) no, finally must come before except
d) yes

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute


Answer: b
95. Can one block of except statements handle multiple exception?
a) yes, like except TypeError, SyntaxError [,…]
b) yes, like except [TypeError, SyntaxError]
c) no
d) none of the mentioned

Answer: a
96. When is the finally block executed?
a) when there is no exception
b) when there is an exception
c) only if some condition that has been specified is satisfied
d) always

Answer: d
97. What happens when ‘1’ == 1 is executed?
a) we get a True
b) we get a False
c) an TypeError occurs
d) a ValueError occurs

Answer: b
98. To open a file c:\scores.txt for reading, we use _____________
a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)

Answer: b
99. To read two characters from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()

Answer: a
100. What will be the output of the following Python code snippet?

print('Ab!2'.swapcase())

a) AB!@
b) ab12
c) aB!2
d) aB1@

Answer: c

Python Programming (MCQs) || Gujarat Power Engineering and Research Institute

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