41. What is the output of print([1, 2, 3][::-1])?
- a) [3, 2, 1]
- b) [1, 2, 3]
- c) [3]
- d) Error
[::-1] reverses the list (step value of -1).
Test your knowledge of Python with these interactive multiple-choice questions.
41. What is the output of print([1, 2, 3][::-1])?
[::-1] reverses the list (step value of -1).
42. Which method returns the number of occurrences of an element in a list?
my_list.count(item) returns the count.
43. What does the any() function do?
any([False, True, False]) returns True.
44. Which module is used for working with JSON data?
json module handles JSON serialization/deserialization.
45. What is the output of print('Python'.center(10, '*'))?
46. Which method removes and returns the last element of a list?
my_list.pop() removes and returns the last item.
47. What is the output of print(0.1 + 0.2 == 0.3)?
0.1 + 0.2 equals 0.30000000000000004.
48. Which function returns a list of tuples containing index and value?
enumerate(['a','b']) yields (0,'a'), (1,'b').
49. What does __str__ method do?
__str__ is called by str() and print().
50. Which module is used for command-line argument parsing?
argparse is the modern recommended approach.
51. What is the output of print('Python'.isidentifier())?
52. Which method returns a shallow copy of a dictionary?
my_dict.copy() creates a new dictionary with the same key-value pairs.
53. What is the output of print('Hello'.partition('l'))?
54. Which function returns the largest item in an iterable?
max([3,1,4]) returns 4.
55. What is the output of print('Python'.endswith('on'))?
56. Which module is used for working with operating system interfaces?
os module provides OS-dependent functionality.
57. What is the output of print('Python'.swapcase())?
58. Which method removes whitespace from both ends of a string?
' hello '.strip() returns 'hello'.
59. What is the output of print('Python'.index('th'))?
find() but raises ValueError if not found.
60. Which function returns a sequence of numbers starting from 0?
range(5) generates numbers from 0 to 4.