-
web.groovymark@gmail.com
- December 8, 2024
Question 21
Which of the following creates a dictionary in Python?
a) []
b) {}
c) {key: value}
d) ()
Correct Answer: c) {key: value}
Explanation: Dictionaries in Python are created using curly braces {} with key-value pairs inside, separated by colons.
Question 22
Which Python method is used to add an item to the end of a list?
a) insert()
b) append()
c) add()
d) extend()
Correct Answer: b) append()
Explanation: The append() method is used to add an element to the end of a list.
Question 23
Which operator is used to check if an element exists in a list?
a) exists
b) in
c) has
d) contains
Correct Answer: b) in
Explanation: The in operator is used to check if an element exists in a list or any other iterable.
Question 24
What is the output of the following code?
print(“Hello” + “World”)
a) HelloWorld
b) Hello World
c) Error
d) HelloWorld without space
Correct Answer: a) HelloWorld
Explanation: The + operator concatenates strings, so "Hello" + "World" results in "HelloWorld".
Question 25
How would you access the value associated with the key “name” in the dictionary below?
person = {“name”: “John”, “age”: 30}
a) person[“John”]
b) person[“name”]
c) person(“name”)
d) name
Correct Answer: b) person["name"]
Explanation: In Python dictionaries, you access values using the key inside square brackets.
Question 26
Which Python method is used to combine two lists?
a) add()
b) combine()
c) extend()
d) merge()
Correct Answer: c) extend()
Explanation: The extend() method adds the elements of one list to the end of another list.
Question 27
What is the output of this Python code?
numbers = [1, 2, 3]
numbers[0] = 10
print(numbers)
a) [1, 2, 3]
b) [10, 2, 3]
c) [1, 2, 10]
d) Error
Correct Answer: b) [10, 2, 3]
Explanation: The first element of the list (at index 0) is reassigned to 10, so the list becomes [10, 2, 3].
Question 28
What will this code return?
my_str = “Hello World”
print(my_str[0:5])
a) Hello World
b) Hello
c) World
d) Hell
Correct Answer: b) Hello
Explanation: The slicing my_str[0:5] returns the first 5 characters of the string, which is "Hello".
Question 29
What does this Python code output?
print(2 * 3 ** 2)
a) 12
b) 18
c) 36
d) 16
Correct Answer: b) 18
Explanation: According to the order of operations, 3 ** 2 = 9, and then 2 * 9 = 18.
Question 30
How can you create a tuple in Python?
a) [1, 2, 3]
b) (1, 2, 3)
c) {1, 2, 3}
d) tuple[1, 2, 3]
Correct Answer: b) (1, 2, 3)
Explanation: A tuple is created by using parentheses () with comma-separated values inside.
Question 31
Which of the following is not a valid Python data type?
a) list
b) set
c) array
d) dictionary
Correct Answer: c) array
Explanation: Arrays are not a built-in data type in Python. Python has lists, sets, and dictionaries as built-in data structures.
Question 32
Which keyword is used to define a class in Python?
a) class
b) def
c) object
d) func
Correct Answer: a) class
Explanation: The class keyword is used to define a class in Python.
Question 33
What does the following Python expression return?
len([10, 20, 30])
a) 10
b) 20
c) 3
d) 30
Correct Answer: c) 3
Explanation: The len() function returns the number of items in the list, which is 3.
Question 34
Which of the following Python keywords is used to create a loop?
a) if
b) else
c) for
d) break
Correct Answer: c) for
Explanation: The for keyword is used to create a loop that iterates over a sequence.
Question 35
How do you remove an element from a list in Python?
a) del()
b) remove()
c) discard()
d) pop()
Correct Answer: b) remove()
Explanation: The remove() method removes the first occurrence of a specified element from the list.
Question 36
What does the continue statement do in Python loops?
a) Exits the loop
b) Skips the current iteration and moves to the next
c) Repeats the current iteration
d) Terminates the loop
Correct Answer: b) Skips the current iteration and moves to the next
Explanation: The continue statement skips the current iteration and proceeds with the next iteration of the loop.
Question 37
What is the output of this code?
my_str = “Hello”
print(my_str[-1])
a) H
b) e
c) o
d) l
Correct Answer: c) o
Explanation: Negative indexing starts from the end, so my_str[-1] returns the last character, which is "o".
Question 38
What does this Python code return?
x = 7
y = 5
print(x % y)
a) 2
b) 5
c) 1
d) 0
Correct Answer: a) 2
Explanation: The modulus operator % returns the remainder when 7 is divided by 5, which is 2.
Question 39
How do you declare an empty set in Python?
a) {}
b) []
c) set()
d) empty()
Correct Answer: c) set()
Explanation: set() is used to create an empty set in Python. {} is used for dictionaries.
Question 40
What is the output of the following Python code?
x = 5
y = 10
print(x == y)
a) True
b) False
c) 5
d) 10
Correct Answer: b) False
Explanation: The == operator checks if x is equal to y. Since 5 is not equal to 10, the result is False.