-->

Programming with Python MCQs || diplomachakhazana

diplomachakhazana





 JOIN US

Programming with Python 


* MCQ Questions 

1. Is Python case sensitive when dealing with identifiers? a) yes 

b) no 

c) machine dependent 

d) none of the mentioned 

Answer: a 

Explanation: Case is always significant. 

2. What is the maximum possible length of an identifier? a) 31 characters 

b) 63 characters 

c) 79 characters 

d) none of the mentioned 

Answer: d 

Explanation: Identifiers can be of any length. 

3. Which of the following is invalid? 

a) _a = 1 

b) __a = 1 

c) __str__ = 1 

d) none of the mentioned 

Answer: d 

Explanation: All the statements will execute successfully but at the cost of reducedreadability. 

4. Which of the following is an invalid variable? 

a) my_string_1 

b) 1st_string 

c) foo 

d) _ 

Answer: b 

Explanation: Variable names should not start with a number. 

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

Explanation: As Python has no concept of private variables, leading underscores areused to indicate variables that must not be accessed from outside the class. 

6. Which of the following is not a keyword? 

a) eval

b) assert 

c) nonlocal 

d) pass 

Answer: a 

Explanation: eval can be used as a variable. 

7. All keywords in Python are in _________ 

a) lower case 

b) UPPER CASE 

c) Capitalized 

d) None of the mentioned 

Answer: d 

Explanation: True, False and None are capitalized while the others are in lower case. 

8. Which of the following is true for variable names in Python? a) unlimited length 

b) all private members must have leading and trailing underscores c) underscore and ampersand are the only two special characters allowed d) none of the mentioned 

Answer: a 

Explanation: Variable names can be of any length. 

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

Answer: b 

Explanation: Spaces are not allowed in variable names. 

10. Which of the following cannot be a variable? 

a) __init__ 

b) in 

c) it 

d) on 

Answer: b 

Explanation: in is a keyword. 

11. What is the output of print 0.1 + 0.2 == 0.3? 

a) True 

b) False 

c) Machine dependent 

d) Error 

View Answer 

Answer: b 

Explanation: Neither of 0.1, 0.2 and 0.3 can be represented accurately in binary. The

round off errors from 0.1 and 0.2 accumulate and hence there is a difference of 5.5511e-17 between (0.1 + 0.2) and 0.3. 

12. Which of the following is not a complex number? 

a) k = 2 + 3j 

b) k = complex(2, 3) 

c) k = 2 + 3l 

d) k = 2 + 3J 

Answer: c 

Explanation: l (or L) stands for long. 

13. What is the type of inf? 

a) Boolean 

b) Integer 

c) Float 

d) Complex 

Answer: c 

Explanation: Infinity is a special case of floating point numbers. It can be obtainedbyfloat(inf). 

14. What does ~4 evaluate to? 

a) -5 

b) -4 

c) -3 

d) +3 

Answer: a 

Explanation: ~x is equivalent to -(x+1). 

15.What does ~~~~~~5 evaluate to? 

a) +5 

b) -11 

c) +11 

d) -5 

Answer: a 

Explanation: ~x is equivalent to -(x+1). 

16.Which of the following is incorrect? 

a) x = 0b101 

b) x = 0x4f5 

c) x = 19023 

d) x = 03964 

Answer: d 

Explanation: Numbers starting with a 0 are octal numbers but 9 isnt allowed in octal numbers.

17. What is the result of cmp(3, 1)? 

a) 1 

b) 0 

c) True 

d) False 

Answer: a 

Explanation: cmp(x, y) returns 1 if x > y, 0 if x == y and -1 if x < y. 

18. Which of the following is incorrect? 

a) float(inf

b) float(nan

c) float(56+78

d) float(12+34

Answer: d 

Explanation: +cannot be converted to a float. 

19. What is the result of round(0.5) round(-0.5)? 

a) 1.0 

b) 2.0 

c) 0.0 

d) None of the mentioned 

Answer: b 

Explanation: Python rounds off numbers away from 0 when the number to be rounded off is exactly halfway through. round(0.5) is 1 and round(-0.5) is -1. 

20. What does 3 ^ 4 evaluate to? 

a) 81 

b) 12 

c) 0.75 

d) 7 

Answer: d 

Explanation: ^ is the Binary XOR operator. 

21. Which of the following statements create a dictionary? a) d = {} 

b) d = {john:40, peter:45} 

c) d = {40:john, 45:peter

d) All of the mentioned 

Answer: d 

Explanation: Dictionaries are created by specifying keys and values.

22. What will be the output of the following Python code snippet? d = {"john":40, "peter":45} 

a) john, 40, 45, and peter” 

b) johnand peter” 

c) 40 and 45 

d) d = (40:john, 45:peter

Answer: b 

Explanation: Dictionaries appear in the form of keys and values. 

23. What will be the output of the following Python code snippet? d = {"john":40, "peter":45} 

"john" in d 

a) True 

b) False 

c) None 

d) Error 

Answer: a 

Explanation: In can be used to check if the key is int dictionary. 

24. What will be the output of the following Python code snippet? d1 = {"john":40, "peter":45} 

d2 = {"john":466, "peter":45} 

d1 == d2 

a) True 

b) False 

c) None 

d) Error 

Answer: b 

Explanation: If d2 was initialized as d2 = d1 the answer would be true. 

25. What will be the output of the following Python code snippet? d1 = {"john":40, "peter":45} 

d2 = {"john":466, "peter":45} 

d1 > d2 

a) True 

b) False 

c) Error 

d) None 

Answer: c 

Explanation: Arithmetic > operator cannot be used with dictionaries.

26. Which of the following is a Python tuple? 

a) [1, 2, 3] 

b) (1, 2, 3) 

c) {1, 2, 3} 

d) {} 

Answer: b 

Explanation: Tuples are represented with round brackets. 

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

Explanation: Values cannot be modified in the case of tuple, that is, tuple is immutable. 

28. What will be the output of the following Python code? >>>t=(1,2,4,3) 

>>>t[1:3] 

a) (1, 2) 

b) (1, 2, 4) 

c) (2, 4) 

d) (2, 4, 3) 

Answer: c 

Explanation: Slicing in tuples takes place just as it does in strings. 

29. What will be the output of the following Python code? >>>t=(1,2,4,3) 

>>>t[1:-1] 

a) (1, 2) 

b) (1, 2, 4) 

c) (2, 4) 

d) (2, 4, 3) 

Answer: c 

Explanation: Slicing in tuples takes place just as it does in strings. 

30. What will be the output of the following Python code? >>>t = (1, 2, 4, 3, 8, 9) 

>>>[t[i] for i in range(0, len(t), 2)] 

a) [2, 3, 9] 

b) [1, 2, 4, 3, 8, 9] 

c) [1, 4, 8] 

d) (1, 4, 8)

Answer: c 

Explanation: Execute in the shell to verify. 

31. What will be the output of the following Python code? d = {"john":40, "peter":45} 

d["john"] 

a) 40 

b) 45 

c) john” 

d) peter” 

Answer: a 

Explanation: Execute in the shell to verify. 

32. What will be the output of the following Python code? >>>t = (1, 2) 

>>>2 * t 

a) (1, 2, 1, 2) 

b) [1, 2, 1, 2] 

c) (1, 1, 2, 2) 

d) [1, 1, 2, 2] 

Answer: a 

Explanation: * operator concatenates tuple. 

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

Explanation: A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations. 

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

Explanation: The argument given for the set must be an iterable. 

35. What will be the output of the following Python code? nums = set([1,1,2,3,3,3,4,4])print(len(nums)) 

a) 7 

b) Error, invalid syntax for formation of set

c) 4 

d) 8 

Answer: c 

Explanation: A set doesnt have duplicate items. 

36. What will be the output of the following Python code? a = [5,5,6,7,7,7] 

b = set(a)def test(lst): 

if lst in b: 

return 1 

else: 

return 0for i in filter(test, a): 

print(i,end=" ") 

a) 5 5 6 

b) 5 6 7 

c) 5 5 6 7 7 7 

d) 5 6 7 7 7 

37. Which of the following statements is used to create an empty set? a) { } 

b) set() 

c) [ ] 

d) ( ) 

Answer: b 

Explanation: { } creates a dictionary not a set. Only set() creates an empty set. 

38. What will be the output of the following Python code? >>> a={5,4}>>> b={1,2,4,5}>>> a<b 

a) {1,2} 

b) True 

c) False 

d) Invalid operation 

Answer: b 

Explanation: a<b returns True if a is a proper subset of b. 

39. If a={5,6,7,8}, which of the following statements is false? a) print(len(a)) 

b) print(min(a)) 

c) a.remove(5) 

d) a[2]=45 

Answer: d 

Explanation: The members of a set can be accessed by their index values since theelements of the set are unordered. 

40. If a={5,6,7}, what happens when a.add(5) is executed?

a) a={5,5,6,7} 

b) a={5,6,7}

c) Error as there is no add function for set data type 

d) Error as 5 already exists in the set 

Answer: b 

Explanation: There exists add method for set data type. However 5 isnt added againas set consists of only non-duplicate elements and 5 already exists in the set. Executein python shell to verify. 

41. What will be the output of the following Python code? >>> a={4,5,6}>>> b={2,8,6}>>> a+b 

a) {4,5,6,2,8} 

b) {4,5,6,2,8,6} 

c) Error as unsupported operand type for sets 

d) Error as the duplicate item 6 is present in both sets 

Answer: c 

Explanation: Execute in python shell to verify. 




diplomachakhazana


null

0 Comments