-
web.groovymark@gmail.com
- December 8, 2024
Question 21
How do you access the value associated with the key “name” in a Python dictionary x?
x = {“name”: “Alice”, “age”: 25}
a) x[“name”]
b) x.get(“name”)
c) Both a and b
d) None of the above
Correct Answer: c) Both a and b
Explanation: Both x["name"] and x.get("name") can be used to access the value associated with the key "name" in a dictionary.
Question 22
What will be the result of this code?
x = {“a”: 1, “b”: 2, “c”: 3}
x[“d”] = 4
print(x)
a) {“a”: 1, “b”: 2, “c”: 3, “d”: 4}
b) {“a”: 1, “b”: 2, “c”: 3}
c) Error
d) None of the above
Correct Answer: a) {"a": 1, "b": 2, "c": 3, "d": 4}
Explanation: You can add a new key-value pair to a dictionary by assigning a value to a new key.
Question 23
What is the output of this code?
x = {“a”: 1, “b”: 2, “c”: 3}
print(x.get(“d”))
a) 0
b) None
c) Error
d) False
Correct Answer: b) None
Explanation: The get() method returns None if the specified key does not exist in the dictionary, unless a default value is provided.
Question 24
Which method can be used to remove a key-value pair from a Python dictionary?
a) del
b) remove()
c) pop()
d) Both a and c
Correct Answer: d) Both a and c
Explanation: The del statement and pop() method can be used to remove a key-value pair from a dictionary.
Question 25
What will this Python code output?
x = {1, 2, 3}
x.add(4)
print(x)
a) {1, 2, 3}
b) {1, 2, 3, 4}
c) [1, 2, 3, 4]
d) Error
Correct Answer: b) {1, 2, 3, 4}
Explanation: The add() method adds an element to a set if it is not already present.
Question 26
How do you find the length of a dictionary x in Python?
a) len(x)
b) x.count()
c) x.size()
d) length(x)
Correct Answer: a) len(x)
Explanation: The len() function returns the number of key-value pairs in a dictionary.
Question 27
What is the output of the following code?
x = {“a”: 1, “b”: 2, “c”: 3}
print(“b” in x)
a) True
b) False
c) Error
d) None
Correct Answer: a) True
Explanation: The in keyword checks if a key exists in a dictionary. In this case, "b" is a key in the dictionary x.
Question 28
Which of the following is not a valid Python data type?
a) set
b) list
c) array
d) tuple
Correct Answer: c) array
Explanation: Python has sets, lists, and tuples, but array is not a built-in Python data type (although arrays can be implemented using external libraries like numpy).
Question 29
What will this Python code output?
x = [1, 2, 3, 4]
print(x[3:])
a) [4]
b) [3, 4]
c) [1, 2]
d) []
Correct Answer: a) [4]
Explanation: The slice x[3:] returns a list starting from index 3 up to the end of the list.
Question 30
What is the correct syntax for defining a class in Python?
a) function ClassName:
b) class ClassName:
c) def ClassName:
d) new ClassName:
Correct Answer: b) class ClassName:
Explanation: The class keyword is used to define a new class in Python.
Question 31
What is the output of this Python code?
x = 10
def func():
global x
x = 5
func()
print(x)
a) 10
b) 5
c) Error
d) None of the above
Correct Answer: b) 5
Explanation: The global keyword allows the function to modify the global variable x.
Question 32
How do you create a new instance of a class MyClass in Python?
a) MyClass.new()
b) MyClass.create()
c) MyClass()
d) MyClass[]
Correct Answer: c) MyClass()
Explanation: To create an instance of a class, you call the class name followed by parentheses.
Question 33
Which method is called automatically when a new object is created in Python?
a) __new__()
b) __init__()
c) __create__()
d) __start__()
Correct Answer: b) __init__()
Explanation: The __init__() method is called when a new instance of a class is created. It initializes the object.
Question 34
What is the output of this Python code?
class MyClass:
def __init__(self):
self.value = 10
obj = MyClass()
print(obj.value)
a) 10
b) 0
c) None
d) Error
Correct Answer: a) 10
Explanation: The __init__() method assigns the value 10 to the value attribute when an instance of MyClass is created.
Question 35
What will this Python code output?
class MyClass:
def __init__(self):
self.value = 5
def update(self, new_value):
self.value = new_value
obj = MyClass()
obj.update(10)
print(obj.value)
a) 5
b) 10
c) None
d) Error
Correct Answer: b) 10
Explanation: The update() method changes the value of the value attribute to 10.
Question 36
How do you define a method inside a Python class?
a) function method_name(self):
b) def method_name(self):
c) class method_name(self):
d) new method_name(self):
Correct Answer: b) def method_name(self):
Explanation: Methods in Python classes are defined using the def keyword, just like regular functions, but they must include self as the first parameter.
Question 37
What is the purpose of the self parameter in Python class methods?
a) It refers to the class itself
b) It refers to the instance of the class
c) It is a reserved keyword for functions
d) It is used for decorators
Correct Answer: b) It refers to the instance of the class
Explanation: The self parameter refers to the current instance of the class and allows access to its attributes and methods.
Question 38
What is the result of this code?
class MyClass:
def __init__(self):
self.value = 10
obj = MyClass()
del obj
print(obj)
a) None
b) Error
c) 10
d) 0
Correct Answer: b) Error
Explanation: After the object is deleted using del, trying to access obj will raise an error.
Question 39
Which of the following can be used to check if an object obj is an instance of class MyClass?
a) obj.__class__ == MyClass
b) type(obj) == MyClass
c) isinstance(obj, MyClass)
d) obj == MyClass
Correct Answer: c) isinstance(obj, MyClass)
Explanation: The isinstance() function checks if an object is an instance of a specified class or its subclass.
Question 40
What is the purpose of inheritance in Python?
a) To create private attributes
b) To enable one class to reuse the functionality of another class
c) To speed up code execution
d) To write functions within classes
Correct Answer: b) To enable one class to reuse the functionality of another class
Explanation: Inheritance allows a class to inherit attributes and methods from another class, promoting code reuse.