Quiznetik
Problem Solving and Python Programming | Set 3
1. Which module in the python standard library parses options received from the command line?
A. getopt
B. os
C. getarg
D. main
Correct : A. getopt
2. What is the type of sys.argv?
A. set
B. list
C. tuple
D. string
Correct : B. list
3. f1(). .
A. error
B. 12
C. 15
D. 1512
Correct : C. 15
4. ,2,3,4 f(5,10,15)
A. 1 2 3 4
B. 5 10 15 4
C. 10 20 30 40
D. 5 10 15 40
Correct : C. 10 20 30 40
5. (1,2) print(total)
A. error
B. 7
C. 8
D. 15
Correct : B. 7
6. Which of the following data structures is returned by the functions globals() and locals()?
A. list
B. set
C. dictionary
D. tuple
Correct : C. dictionary
7. On assigning a value to a variable inside a function, it automatically becomes a global variable.
A. true
B. false
Correct : B. false
8. 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
Correct : D. the global variable is shadowed
9. print(a)
A. 10
B. 25
C. junk value
D. error
Correct : B. 25
10. f() x
A. error
B. 4
C. junk value
D. 1
Correct : D. 1
11. 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
D. an in-built method that is automatically called
Correct : B. a function execution instance that calls another execution instance of the same function
12. Only problems that are recursively defined can be solved using recursion.
A. true
B. false
Correct : B. false
13. 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
Correct : C. recursive functions run faster than non- recursive function
14. ,i+j) print(test(4,7))
A. 13
B. 7
C. infinite loop
D. 17
Correct : D. 17
15. 5 RECURSION
A. 011
B. 110
C. 3
D. infinite loop
Correct : B. 110
16. 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
Correct : D. a function where the recursive call is the last thing executed by the function
17. , tot-2)
A. both a() and b() aren’t tail recursive
B. both a() and b() are tail recursive
C. b() is tail recursive but a() isn’t
D. a() is tail recursive but b() isn’t
Correct : C. b() is tail recursive but a() isn’t
18. Which of the following statements is false about recursion?
A. every recursive function must have a base case
B. infinite recursion can occur if the base case isn’t properly mentioned
C. a recursive function makes the code easier to understand
D. every recursive function must have a return value
Correct : D. every recursive function must have a return value
19. Recursion and iteration are the same programming approach.
A. true
B. false
Correct : B. false
20. 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
Correct : A. program gets into an infinite loop
21. Which of these is not true about recursion?
A. making the code look clean
B. a complex task can be broken into sub- problems
C. recursive calls take up less memory
D. sequence generation is easier than a nested iteration
Correct : C. recursive calls take up less memory
22. Which of these is not true about recursion?
A. it’s easier to code some real-world problems using recursion than non-recursive equivalent
B. recursive functions are easy to debug
C. recursive calls take up a lot of memory
D. programs using recursion take longer time than their non-recursive equivalent
Correct : B. recursive functions are easy to debug
23. Which of the following commands will create a list?
A. list1 = list()
B. list1 = []
C. list1 = list([1, 2, 3])
D. all of the mentioned
Correct : D. all of the mentioned
24. What is the output when we execute list(“hello”)?
A. [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
B. [‘hello’]
C. [‘llo’]
D. [‘olleh’]
Correct : A. [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
25. Suppose list1 is [2445,133,12454,123], what is max(list1)?
A. 2445
B. 133
C. 12454
D. 123
Correct : C. 12454
26. Suppose list1 is [3, 5, 25, 1, 3], what is min(list1)?
A. 3
B. 5
C. 25
D. 1
Correct : D. 1
27. Suppose list1 is [1, 5, 9], what is sum(list1)?
A. 1
B. 9
C. 15
D. error
Correct : C. 15
28. To shuffle the list(say list1) what function do we use?
A. list1.shuffle()
B. shuffle(list1)
C. random.shuffle(list1)
D. random.shufflelist(list1)
Correct : C. random.shuffle(list1)
29. 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
Correct : D. all of the mentioned
30. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
A. error
B. none
C. 25
D. 2
Correct : C. 25
31. Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
A. [2, 33, 222, 14]
B. error
C. 25
D. [25, 14, 222, 33, 2]
Correct : A. [2, 33, 222, 14]
32. >>>print(names[-1][-1])
A. a
B. daman
C. error
D. n
Correct : D. n
33. print sum
A. 11
B. 12
C. 21
D. 22
Correct : B. 12
34. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:
A. [0, 1, 2, 3]
B. [0, 1, 2, 3, 4]
C. [0.0, 0.5, 1.0, 1.5]
D. [0.0, 0.5, 1.0, 1.5, 2.0]
Correct : C. [0.0, 0.5, 1.0, 1.5]
35. >>>list1 < list2 is
A. true
B. false
C. error
D. none
Correct : B. false
36. To add a new element to a list we use which command?
A. list1.add(5)
B. list1.append(5)
C. list1.addlast(5)
D. list1.addend(5)
Correct : B. list1.append(5)
37. To insert 5 to the third position in list1, we use which command?
A. list1.insert(3, 5)
B. list1.insert(2, 5)
C. list1.add(3, 5)
D. list1.append(3, 5)
Correct : B. list1.insert(2, 5)
38. To remove string “hello” from list1, we use which command?
A. list1.remove(“hello”)
B. list1.remove(hello)
C. list1.removeall(“hello”)
D. list1.removeone(“hello”)
Correct : A. list1.remove(“hello”)
39. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5)?
A. 0
B. 1
C. 4
D. 2
Correct : D. 2
40. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5)?
A. 0
B. 4
C. 1
D. 2
Correct : D. 2
41. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse()?
A. [3, 4, 5, 20, 5, 25, 1, 3]
B. [1, 3, 3, 4, 5, 5, 20, 25]
C. [25, 20, 5, 5, 4, 3, 3, 1]
D. [3, 1, 25, 5, 20, 5, 4, 3]
Correct : D. [3, 1, 25, 5, 20, 5, 4, 3]
42. >>>"Welcome to Python".split()
A. [“welcome”, “to”, “python”]
B. (“welcome”, “to”, “python”)
C. {“welcome”, “to”, “python”}
D. “welcome”, “to”, “python”
Correct : A. [“welcome”, “to”, “python”]
43. >>>list("a#b#c#d".split('#'))
A. [‘a’, ‘b’, ‘c’, ‘d’]
B. [‘a b c d’]
C. [‘a#b#c#d’]
D. [‘abcd’]
Correct : A. [‘a’, ‘b’, ‘c’, ‘d’]
44. >>>print(indexOfMax)
A. 1
B. 2
C. 3
D. 4
Correct : A. 1
45. print(myList[i], end = " ")
A. 2 3 4 5 6 1
B. 6 1 2 3 4 5
C. 2 3 4 5 6 6
D. 1 1 2 3 4 5
Correct : C. 2 3 4 5 6 6
46. >>>print(list2)
A. [1, 3]
B. [4, 3]
C. [1, 4]
D. [1, 3, 4]
Correct : B. [4, 3]
47. print(v)
A. [1, 44]
B. [1, 2, 3, 44]
C. [44, 2, 3]
D. [1, 2, 3]
Correct : C. [44, 2, 3]
48. print(v)
A. [1] [2] [3]
B. [1] [1, 2] [1, 2, 3]
C. [1, 2, 3]
D. 1 2 3
Correct : C. [1, 2, 3]
49. print(2)
A. none
B. 1
C. 2
D. error
Correct : C. 2
50. print(names2[2][0])
A. none
B. a
C. b
D. c
Correct : D. c
51. numbers = [1, 2, 3, 4] numbers.append([5,6,7,8]) print(len(numbers))
A. 4
B. 5
C. 8
D. 12
Correct : B. 5
52. 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
Correct : D. all of the mentioned
53. print(len(list1 + list2))
A. 2
B. 4
C. 5
D. 8
Correct : D. 8
54. print(len(mylist))
A. 1
B. 4
C. 5
D. 8
Correct : C. 5
55. return result
A. return a list containing every third item from l starting at index 0
B. return an empty list
C. return a list containing every third index from l starting at index 0
D. return a list containing the items from l starting from index 0, omitting every third item
Correct : A. return a list containing every third item from l starting at index 0
56. print(veggies)
A. [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’] correct 1.00
B. [‘carrot’, ‘celery’, ‘potato’, ‘asparagus’]
C. [‘carrot’, ‘broccoli’, ‘celery’, ‘potato’, ‘asparagus’]
D. [‘celery’, ‘carrot’, ‘broccoli’, ‘potato’, ‘asparagus’]
Correct : A. [‘carrot’, ‘celery’, ‘broccoli’, ‘potato’, ‘asparagus’] correct 1.00
57. >>>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]
Correct : B. [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
58. m = [[x, y] for x in range(0, 4) fo r y in range(0, 4)]
A. 8
B. 12
C. 16
D. 32
Correct : C. 16
59. print(v)
A. 3
B. 5
C. 6
D. 33
Correct : D. 33
60. print(v)
A. 1
B. 3
C. 5
D. 6
Correct : A. 1
61. print(matrix[i][1], end = " ")
A. 1 2 3 4
B. 4 5 6 7
C. 1 3 8 12
D. 2 5 9 13
Correct : D. 2 5 9 13
62. print(m(row), end = " ")
A. 3 33
B. 1 1
C. 5 6
D. 5 33
Correct : D. 5 33
63. print(data[1][0][0])
A. 1
B. 2
C. 4
D. 5
Correct : D. 5
64. print(ttt(data[0]))
A. 1
B. 2
C. 4
D. 5
Correct : C. 4
65. print(points)
A. [[1, 2], [3, 1.5], [0.5, 0.5]]
B. [[3, 1.5], [1, 2], [0.5, 0.5]]
C. [[0.5, 0.5], [1, 2], [3, 1.5]]
D. [[0.5, 0.5], [3, 1.5], [1, 2]]
Correct : C. [[0.5, 0.5], [1, 2], [3, 1.5]]
66. ,b)) print(a)
A. [2,4]
B. [ ]
C. [3,5]
D. invalid arguments for filter function
Correct : C. [3,5]
67. for i in range(3)]; print(x);
A. [0, 1, 2]
B. [1, 2, 5]
C. error, **+ is not a valid operator
D. error, ‘;’ is not allowed
Correct : A. [0, 1, 2]
68. ==0: i; else: i+1; for i in range(4)])
A. [0, 2, 2, 4]
B. [1, 1, 3, 3]
C. error
D. none of the mentioned
Correct : C. error
69. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
A. [x**-1 for x in [(1, 2, 3)]]
B. [1/x for x in [(1, 2, 3)]]
C. [1/x for x in (1, 2, 3)]
D. error
Correct : C. [1/x for x in (1, 2, 3)]
70. for x in l]
A. [1, 1, 1, 1, 1]
B. [1, 0, 1, 0, 1]
C. [1, 0, 0, 0, 0]
D. [0, 1, 0, 1, 0]
Correct : B. [1, 0, 1, 0, 1]
71. for y in l2]
A. [4, 8, 12, 5, 10, 15, 6, 12, 18]
B. [4, 10, 18]
C. [4, 5, 6, 8, 10, 12, 12, 15, 18]
D. [18, 12, 6, 15, 10, 5, 12, 8, 4]
Correct : C. [4, 5, 6, 8, 10, 12, 12, 15, 18]
72. 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]
Correct : D. [x for x in l if x<0]
73. =[x+y for x, y in zip(l1, l2)] l3
A. error
B. 0
C. [-20, -60, -80]
D. [0, 0, 0]
Correct : D. [0, 0, 0]
74. Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9].
A. [x**3 for x in l]
B. [x^3 for x in l]
C. [x**3 in l]
D. [x^3 in l]
Correct : A. [x**3 for x in l]
75. )*5/9) for x in t]
A. [0]
B. 0
C. [0.00]
D. error
Correct : D. error
76. 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)]
Correct : B. [x for x in range(1000) if x%3==0]
77. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].
A. [(2**x) for x in range(0, 13)]
B. [(x**2) for x in range(1, 13)]
C. [(2**x) for x in range(1, 13)]
D. [(x**2) for x in range(0, 13)]
Correct : A. [(2**x) for x in range(0, 13)]
78. , x is even} (including zero)
A. [x for x in range(1, 20) if (x%2==0)]
B. [x for x in range(0, 20) if (x//2==0)]
C. [x for x in range(1, 20) if (x//2==0)]
D. [x for x in range(0, 20) if (x%2==0)]
Correct : D. [x for x in range(0, 20) if (x%2==0)]
79. , i)]
A. a list of prime numbers up to 50
B. a list of numbers divisible by 2, up to 50
C. a list of non prime numbers, up to 50
D. error
Correct : C. a list of non prime numbers, up to 50
80. ] for row in (0, 1, 2)]
A. [7, 8, 9]
B. [4, 5, 6]
C. [2, 5, 8]
D. [1, 4, 7]
Correct : C. [2, 5, 8]
81. for col in row] for row in A]
A. [[11, 12, 13], [14, 15, 16], [17, 18, 19]]
B. error
C. [11, 12, 13], [14, 15, 16], [17, 18, 19]
D. [11, 12, 13, 14, 15, 16, 17, 18, 19]
Correct : A. [[11, 12, 13], [14, 15, 16], [17, 18, 19]]
82. -i] for i in range(len(A))]
A. [1, 5, 9]
B. [4, 5, 6]
C. [3, 5, 7]
D. [2, 5, 8]
Correct : C. [3, 5, 7]
83. ) 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
Correct : A. [3, 6, 9, 16, 20, 24, 35, 40, 45]
84. , row2)] for (row1, row2) in zip(A, B)]
A. [0, 30, 60, 120, 160, 200, 300, 350, 400]
B. [[3, 6, 9], [16, 20, 24], [35, 40, 45]]
C. no output
D. error
Correct : B. [[3, 6, 9], [16, 20, 24], [35, 40, 45]]
85. >>>t[1:3]
A. (1, 2)
B. (1, 2, 4)
C. (2, 4)
D. (2, 4, 3)
Correct : C. (2, 4)
86. >>>t[1:-1]
A. (1, 2)
B. (1, 2, 4)
C. (2, 4)
D. (2, 4, 3)
Correct : C. (2, 4)
87. >>>[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)
Correct : C. [1, 4, 8]
88. d["john"]
A. 40
B. 45
C. “john”
D. “peter”
Correct : A. 40
89. >>>t1 < t2
A. true
B. false
C. error
D. none
Correct : B. false
90. >>>print len(my_tuple)
A. 1
B. 2
C. 5
D. error
Correct : D. error
91. What is the data type of (1)?
A. tuple
B. integer
C. list
D. both tuple and integer
Correct : B. integer
92. What type of data is: a=[(1,1),(2,4),(3,9)]?
A. array of tuples
B. list of tuples
C. tuples of lists
D. invalid type
Correct : B. list of tuples
93. 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)
Correct : C. no, too many values to unpack
94. Tuples can’t be made keys of a dictionary.
A. true
B. false
Correct : B. false
95. 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
Correct : D. all of the mentioned
96. d = {"john":40, "peter":45}
A. “john”, 40, 45, and “peter”
B. “john” and “peter”
C. 40 and 45
D. d = (40:”john”, 45:”peter”)
Correct : B. “john” and “peter”
97. "john" in d
A. true
B. false
C. none
D. error
Correct : A. true
98. d1 == d2
A. true
B. false
C. none
D. error
Correct : B. false
99. d1 > d2
A. true
B. false
C. error
D. none
Correct : C. error
100. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use?
A. d.delete(“john”:40)
B. d.delete(“john”)
C. del d[“john”]
D. del d(“john”:40)
Correct : C. del d[“john”]