-
web.groovymark@gmail.com
- November 29, 2024
Question 01
Which of the following is a valid way to create a list in Python?
a) my_list = {1, 2, 3}
b) my_list = (1, 2, 3)
c) my_list = [1, 2, 3]
d) my_list = <1, 2, 3>
Correct Answer: c) my_list = [1, 2, 3]
Explanation: A list in Python is created using square brackets, like [1, 2, 3].
Question 02
What does the pop() method do in Python?
a) Adds an element to the list
b) Removes the last element from the list
c) Sorts the list
d) Reverses the list
Correct Answer: b) Removes the last element from the list
Explanation: The pop() method removes and returns the last element from a list.
Question 03
Which of the following statements about dictionaries in Python is true?
a) Keys in dictionaries must be unique
b) Values in dictionaries must be unique
c) Dictionaries are immutable
d) Dictionaries cannot contain lists
Correct Answer: a) Keys in dictionaries must be unique
Explanation: In a dictionary, keys must be unique, but values do not have to be.
Question 04
What is the output of len([1, 2, 3, 4])?
a) 3
b) 4
c) 5
d) 2
Correct Answer: b) 4
Explanation: The len() function returns the number of elements in the list, which is 4 in this case.
Question 05
What is a tuple in Python?
a) A mutable collection
b) An unordered collection
c) A mutable collection of key-value pairs
d) An immutable ordered collection
Correct Answer: d) An immutable ordered collection
Explanation: A tuple is an immutable sequence of values that cannot be changed once defined.
Question 06
How can you check if a key exists in a dictionary?
a) if key in dict:
b) if dict.has_key(key):
c) if dict.exists(key):
d) if key == dict:
Correct Answer: a) if key in dict:
Explanation: The in keyword checks whether a key exists in a dictionary.
Question 07
What is the result of 5 % 3?
a) 1
b) 2
c) 3
d) 0
Correct Answer: b) 2
Explanation: The modulus operator % returns the remainder when dividing 5 by 3, which is 2.
Question 08
How do you create an empty dictionary in Python?
a) dict = []
b) dict = {}
c) dict = ()
d) dict = “”
Correct Answer: b) dict = {}
Explanation: An empty dictionary is created using curly braces {}.
Question 09
What will be the result of print(3 * ‘abc’)?
a) abcabcabc
b) abc
c) abcabc
d) abcabcabcabc
Correct Answer: a) abcabcabc
Explanation: The * operator repeats the string 'abc' three times, resulting in 'abcabcabc'.
Question 10
What is the correct syntax for defining a set in Python?
a) my_set = [1, 2, 3]
b) my_set = {1, 2, 3}
c) my_set = (1, 2, 3)
d) my_set = <1, 2, 3>
Correct Answer: b) my_set = {1, 2, 3}
Explanation: A set in Python is created using curly braces, like {1, 2, 3}.
Question 11
What is the purpose of the is operator in Python?
a) To compare two values for equality
b) To check if two variables point to the same object
c) To add two numbers
d) To multiply two numbers
Correct Answer: b) To check if two variables point to the same object
Explanation: The is operator checks whether two variables point to the same object in memory.
Question 12
How can you remove an element from a set in Python?
a) set.pop()
b) set.remove()
c) set.delete()
d) set.drop()
Correct Answer: b) set.remove()
Explanation: The remove() method removes a specified element from a set.
Question 13
What is the purpose of the range() function in Python?
a) To return a list of values
b) To return a sequence of numbers
c) To print numbers
d) To sort numbers
Correct Answer: b) To return a sequence of numbers
Explanation: The range() function generates a sequence of numbers, which is often used in loops.
Question 14
Which of the following can be used to generate a random number in Python?
a) random.randrange()
b) math.random()
c) sys.random()
d) input.random()
Correct Answer: a) random.randrange()
Explanation: The random module provides functions such as randrange() to generate random numbers.
Question 15
How do you access the value associated with a key in a dictionary?
a) dict.key()
b) dict(key)
c) dict[key]
d) dict.get(key)
Correct Answer: c) dict[key]
Explanation: You can access the value associated with a key using the dict[key] syntax.
Question 16
Which of the following methods can be used to split a string into a list?
a) split()
b) join()
c) replace()
d) find()
Correct Answer: a) split()
Explanation: The split() method breaks a string into a list based on a delimiter.
Question 17
Which function converts a string to a floating-point number in Python?
a) int()
b) str()
c) float()
d) list()
Correct Answer: c) float()
Explanation: The float() function converts a string or integer to a floating-point number.
Question 18
What is the default return value of a function that has no return statement?
a) 0
b) None
c) True
d) False
Correct Answer: b) None
Explanation: A function that does not specify a return value returns None by default.
Question 19
How can you check if a list is empty in Python?
a) if list == {}
b) if len(list) == 0
c) if list is None
d) if list == ()
Correct Answer: b) if len(list) == 0
Explanation: The len() function returns the length of the list, and if it is 0, the list is empty.
Question 20
What is the difference between a list and a tuple in Python?
a) Lists are mutable, tuples are immutable
b) Tuples are mutable, lists are immutable
c) Both are immutable
d) Both are mutable
Correct Answer: a) Lists are mutable, tuples are immutable
Explanation: Lists can be modified, while tuples cannot be changed once created.