0% found this document useful (0 votes)
3K views11 pages

Python MCQs for Developers

This document contains 40 multiple choice questions about Python programming concepts. The questions cover topics like data types (lists, tuples, sets, dictionaries), operators, functions, loops, conditional statements, and more. Sample questions include identifying valid Python syntax, determining output of code snippets, and selecting true statements about built-in data types.

Uploaded by

rammit2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Topics covered

  • File handling,
  • String manipulation,
  • Formatting,
  • Best practices,
  • Comprehensions,
  • Security,
  • Tuples,
  • Decorators,
  • Scope,
  • Imports
0% found this document useful (0 votes)
3K views11 pages

Python MCQs for Developers

This document contains 40 multiple choice questions about Python programming concepts. The questions cover topics like data types (lists, tuples, sets, dictionaries), operators, functions, loops, conditional statements, and more. Sample questions include identifying valid Python syntax, determining output of code snippets, and selecting true statements about built-in data types.

Uploaded by

rammit2007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Topics covered

  • File handling,
  • String manipulation,
  • Formatting,
  • Best practices,
  • Comprehensions,
  • Security,
  • Tuples,
  • Decorators,
  • Scope,
  • Imports
  • Python Developer MCQ-Questions

PYTHON DEVELOPER MCQ-Questions

1. Which of the following is an invalid statement?


a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000, 000

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

3. Which is the correct operator for power(x y)?


a) X^y
b) X**y
c) X^^y
d) None of the mentioned

4. What is the order of precedence in python?


i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v
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

6. Which of the following is incorrect?


a) x = 0b101
b) x = 0x4f5
c) x = 19023
d) x = 03964

7. In python we do not specify types, it is directly interpreted by the compiler, so consider the
following operation to be performed.
1. >>>x = 13 ? 2

objective is to make sure x has a integer value, select all that apply (python [Link])
a) x = 13 // 2
b) x = int(13 / 2)
c) x = 13 % 2
d) All of the mentioned

8. Which of the following is the truncation division operator?


a) /
b) %
c) //
d) |
9. Which of the following expressions results in an error?
a) int(1011)
b) int(‘1011’,23)
c) int(1011,2)
d) int(‘1011’)
10. The output of which of the codes shown below will be: “There are 4 blue birds.”?
a) ‘There are %g %d birds.’ %4 %blue
b) ‘There are %d %s birds.’ %(4, blue)
c) ‘There are %s %d birds.’ %[4, blue]
d) ‘There are %d %s birds.’ 4, blue

11. The formatting method {1:<10} represents the ___________ positional argument, _________
justified in a 10 character wide field.
a) first, right
b) second, left
c) first, left
d) second, right

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


def c(f):
def inner(*args, **kargs):
[Link] += 1
return f(*args, **kargs)
[Link] = 0
return inner
@c
def fnc():
pass
if __name__ == '__main__':
fnc()
fnc()
fnc()
print([Link])
a) 4
b) 3
c) 0
d) 1
13.  What will be the output of the following Python code?
x = ['ab', 'cd']
for i in x:
[Link]()
print(x)
a) [‘ab’, ‘cd’]
b) [‘AB’, ‘CD’]
c) [None, None]
d) none of the mentioned

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


x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
a) no output
b) i i i i i i …
c) a b c d e f
d) abcdef

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

1. >>>"a"+"bc"

a) a
b) bc
c) bca
d) abc

16. Given a string example=”hello” what is the output of [Link](‘l’)?


a) 2
b) 1
c) None
d) 0
17. Which of the following statement prints hello\example\[Link]?
a) print(“hello\example\[Link]”)
b) print(“hello\\example\\[Link]”)
c) print(“hello\”example\”[Link]”)
d) print(“hello”\example”\[Link]”)

18. If a class defines the __str__(self) method, for an object obj for the class, you can use
which command to invoke the __str__ method.
a) obj.__str__()
b) str(obj)
c) print obj
d) all of the mentioned

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


print("abcdef".center(10, '12'))
a) 12abcdef12
b) abcdef1212
c) 1212abcdef
d) error

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


print("xyyzxyzxzxyy".count('xyy', -10, -1))
a) 2
b) 0
c) 1
d) error
21. What will be the output of the following Python code snippet?
print('{:$}'.format(1112223334))
a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error
22. What will be the output of the following Python code snippet?
print('ab\ncd\nef'.splitlines())
a) [‘ab’, ‘cd’, ‘ef’]
b) [‘ab\n’, ‘cd\n’, ‘ef\n’]
c) [‘ab\n’, ‘cd\n’, ‘ef’]
d) [‘ab’, ‘cd’, ‘ef\n’]

23. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0], Which of the following is correct syntax for slicing
operation?
a) print(list1[0])
b) print(list1[:2])
c) print(list1[:-2])
d) all of the mentioned

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


names1 = ['Amir', 'Bear', 'Charlton', 'Daman']
names2 = names1
names3 = names1[:]
names2[0] = 'Alice'
names3[1] = 'Bob'

sum = 0
for ls in (names1, names2, names3):
if ls[0] == 'Alice':
sum += 1
if ls[1] == 'Bob':
sum += 10

print sum
a) 11
b) 12
c) 21
d) 22
25. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after [Link]()?
a) [3, 4, 5, 20, 5, 25, 1]
b) [1, 3, 3, 4, 5, 5, 20, 25]
c) [3, 5, 20, 5, 25, 1, 3]
d) [1, 3, 4, 5, 20, 5, 25]
26. To which of the following the “in” operator can be used to check if an item is in it?
a) Lists
b) Dictionary
c) Set
d) All of the mentioned
27. What will be the output of the following Python code?
1. >>>m = [[x, x + 1, x + 2] for x in range(0, 3)]
a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
c) [1, 2, 3, 4, 5, 6, 7, 8, 9]
d) [0, 1, 2, 1, 2, 3, 2, 3, 4]
28. What will be the output of the following Python code?
a=[[]]*3
a[1].append(7)
print(a)
a) Syntax error
b) [[7], [7], [7]]
c) [[7], [], []]
d) [[],7, [], []]
29. What will be the output of the following Python code?
a=165
b=sum(list(map(int,str(a))))
print(b)
a) 561
b) 5
c) 12
d) Syntax error
30. What will be the output of the following Python code snippet?
my_string = "hello world"
k = [([Link](), len(i)) for i in my_string]
print(k)
a) [(‘HELLO’, 5), (‘WORLD’, 5)]
b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)]
c) [(‘HELLO WORLD’, 11)]
d) none of the mentioned

31. Write the list comprehension to pick out only negative integers from a given list ‘l’.
a) [x<0 in l]
b) [x for x<0 in l]
c) [x in l for x<0]
d) [x for x in l if x<0]

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


import math
[str(round([Link])) for i in range (1, 6)]
a) [‘3’, ‘3’, ‘3’, ‘3’, ‘3’, ‘3’]
b) [‘3.1’, ‘3.14’, ‘3.142’, ‘3.1416’, ‘3.14159’, ‘3.141582’]
c) [‘3’, ‘3’, ‘3’, ‘3’, ‘3’]
d) [‘3.1’, ‘3.14’, ‘3.142’, ‘3.1416’, ‘3.14159’]

33. Write a list comprehension for producing a list of numbers between 1 and 1000 that are
divisible by 3.
a) [x in range(1, 1000) if x%3==0]
b) [x for x in range(1000) if x%3==0]
c) [x%3 for x in range(1, 1000)]
d) [x%3=0 for x in range(1, 1000)]
34. The service in which the cloud consumer does not manage the underlying cloud
infrastructure nor the application(s) contained in the instances as the service provides user
with all of it is:
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[3, 3, 3],
[4, 4, 4],
[5, 5, 5]]
[B[row][col]*A[row][col] for row in range(3) for col in range(3)]
a) [3, 6, 9, 16, 20, 24, 35, 40, 45]
b) Error
c) [0, 30, 60, 120, 160, 200, 300, 350, 400]
d) 0

35.  Which of the following is a Python tuple?


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

36. 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))

37. Is the following Python code valid?


>>> a,b=1,2,3
a) Yes, this is an example of tuple unpacking. a=1 and b=2
b) Yes, this is an example of tuple unpacking. a=(1,2) and b=3
c) No, too many values to unpack
d) Yes, this is an example of tuple unpacking. a=1 and b=(2,3)
38. Tuples can’t be made keys of a dictionary.
a) True
b) False

39. 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

40.  If a={5,6,7,8}, which of the following statements is false?


a) print(len(a))
b) print(min(a))
c) [Link](5)
d) a[2]=45

Common questions

Powered by AI

The result '[[7], [7], [7]]' occurs because the list 'a' is initialized as three references to the same empty list object. When 'a[1].append(7)' is executed, it modifies the single list object that all three elements of 'a' reference, illustrating Python's reference semantics and shared mutable objects .

The expression demonstrates Python's capability to handle newline characters by splitting the string at each newline, resulting in the list ['ab', 'cd', 'ef']. The splitlines() method effectively breaks the string at line boundaries, returning a list of lines sans the newline character .

The statement 'a b c = 1000 2000 3000' is considered invalid in Python because Python does not allow spaces in variable names. The syntax requires variable names to be single words, and each variable must be separated by a comma when assigning multiple variables simultaneously .

The output of the code will be ['ab', 'cd']. The expression 'i.upper()' does not modify the elements of the list in-place; it returns a new string that is not captured or stored. Therefore, the original list remains unchanged .

The output will result in an error because the string's fill character must be exactly one character in length, but '12' is two characters. String centering using fill characters requires a single character .

Local variable names beginning with an underscore are discouraged because they are used to indicate private variables of a class. This can lead to confusion when maintaining or reading code, as it suggests a variable is intended for internal use only. This practice can potentially mislead developers about the variable's scope and visibility, thus affecting code clarity and maintainability .

The pattern 'sum(list(map(int,str(a))))' efficiently sums the digits of an integer 'a' by converting it to a string, mapping each character to an integer, and summing the results. For 'a = 165', it yields the output 12, corresponding to the sum of the digits: 1 + 6 + 5. This approach considers each step's nature—string conversion, mapping to integers, and summation .

The 'in' operator enhances expression evaluations by allowing concise membership checks across various data structures such as lists, dictionaries, and sets. This flexibility makes data handling and lookups more streamlined and efficient, enabling developers to write cleaner and more readable code .

List comprehensions in Python allow for efficient and concise code by enabling the creation of lists in a single line of readable and clear syntax. They are particularly useful for filtering data, as they can perform operations and condition checks inline. For example, a list comprehension can be used to filter out only negative numbers from a list: [x for x in l if x < 0].

The statement 't[3] = 45' is incorrect because tuples are immutable, meaning their elements cannot be changed after initialization. Attempting to assign a new value to an element in a tuple implies a misunderstanding of tuple properties and leads to a TypeError in Python .

You might also like