What will be the result of the following code?
x = [1, 2, 3, 4, 5]
y = x[1:4]
print(y)
Correct answer:
B) [2, 3, 4]
Explanation: The slice x[1:4] returns a sublist starting from index 1 up to, but not including, index 4.
What will be the result of the following code?
x = [1, 2, 3, 4, 5]
y = x[1:4]
print(y)
Correct answer:
B) [2, 3, 4]
Explanation: The slice x[1:4] returns a sublist starting from index 1 up to, but not including, index 4.
Which of the following is used to create a function in Python?
Correct answer:
A) def
Explanation: The def keyword is used to define a function in Python.
How do you reverse a list x in Python?
x = [1, 2, 3, 4, 5]
Correct answer:
D) All of the above
Explanation: All options can be used to reverse a list in Python. x.reverse() modifies the list in place, x[::-1] creates a reversed copy, and reversed(x) returns an iterator.
What will this Python code output?
print(“Python” + ” is fun”)
Correct answer:
A) Python is fun
Explanation: The + operator concatenates two strings with no space between them unless explicitly added.
Which Python data type is used to store ordered, mutable collections of items?
Correct answer:
C) list
Explanation: A list in Python is an ordered collection of elements that can be modified after creation (mutable).
How would you remove duplicates from a list x in Python?
x = [1, 2, 3, 1, 2, 4]
Correct answer:
C) list(set(x))
Explanation: Converting the list to a set removes duplicates, and converting it back to a list gives a list without duplicates.
What is the result of this code?
a = [1, 2, 3]
Correct answer:
B) 4
Explanation: The append method adds a single element (in this case, a list [4, 5] ) to the end of the list, so the length of the list becomes 4.
What is the difference between remove() and pop() in Python?
Correct answer:
B) remove() deletes by value, pop() by index
Explanation: remove() deletes the first occurrence of the specified value, while pop() removes an element at the specified index.
What is the output of this code?
x = [10, 20, 30, 40, 50]
print(x.pop(2))
Correct answer:
B) 30
Explanation: The pop() function removes the element at index 2, which is 30.
What is the output of the following Python code?
x = “Hello World”
print(x.lower())
Correct answer:
B) hello world
Explanation: The lower() method converts all the characters in the string to lowercase.
How do you check the length of a list x in Python?
Correct answer:
A) len(x)
Explanation: The len() function returns the number of elements in a list.
What will be the output of this code?
x = [1, 2, 3, 4, 5]
print(sum(x))
Correct answer:
A) 15
Explanation: The sum() function returns the sum of all elements in the list.
How do you sort a list x in ascending order?
Correct answer:
C) Both a and b
Explanation: The x.sort() method sorts the list in place, while sorted(x) returns a new sorted list without modifying the original.
What is the result of this code?
x = [10, 20, 30, 40]
y = x.copy()
print(x == y and x is y)
Correct answer:
B) False
Explanation: x == y checks if the lists have the same elements, while x is y checks if they are the same object in memory. The lists have the same elements, but are different objects.
Which of the following methods can add an element to the end of a list in Python?
Correct answer:
A) append()
Explanation: The append() method adds an element to the end of the list. insert() adds an element at a specific index.
What will this Python code output?
x = [1, 2, 3]
x.insert(1, 5)
print(x)
Correct answer:
A) [1, 5, 2, 3]
Explanation: The insert() method inserts an element at a specified index. In this case, it inserts 5 at index 1.
What will this Python code output?
x = [1, 2, 3]
print(x.index(2))
Correct answer:
B) 1
Explanation: The index() method returns the index of the first occurrence of the specified value, which is 2, located at index 1.
How do you find the maximum value in a list x in Python?
Correct answer:
A) max(x)
Explanation: The max() function returns the maximum value in a list.
What is the output of this code?
x = [1, 2, 3, 4, 5]
x.remove(3)
print(x)
Correct answer:
A) [1, 2, 4, 5]
Explanation: The remove() method removes the first occurrence of the specified value from the list, which is 3 in this case.
Which of the following statements is true about Python dictionaries?
Correct answer:
C) They store key-value pairs
Explanation: Python dictionaries are collections of key-value pairs. They are unordered and mutable.
How do you access the value associated with the key “name” in a Python dictionary x ?
x = {“name”: “Alice”, “age”: 25}
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.
What will be the result of this code?
x = {“a”: 1, “b”: 2, “c”: 3}
x[“d”] = 4
print(x)
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.
What is the output of this code?
x = {“a”: 1, “b”: 2, “c”: 3}
print(x.get(“d”))
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.
Which method can be used to remove a key-value pair from a Python dictionary?
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.
What will this Python code output?
x = {1, 2, 3}
x.add(4)
print(x)
Correct answer:
B) {1, 2, 3, 4}
Explanation: The add() method adds an element to a set if it is not already present.
How do you find the length of a dictionary x in Python?
Correct answer:
A) len(x)
Explanation: The len() function returns the number of key-value pairs in a dictionary.
What is the output of the following code?
x = {“a”: 1, “b”: 2, “c”: 3}
print(“b” in x)
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 .
Which of the following is not a valid Python data type?
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 ).
What will this Python code output?
x = [1, 2, 3, 4]
print(x[3:])
Correct answer:
A) [4]
Explanation: The slice x[3:] returns a list starting from index 3 up to the end of the list.
What is the correct syntax for defining a class in Python?
Correct answer:
B) class ClassName:
Explanation: The class keyword is used to define a new class in Python.
What is the output of this Python code?
x = 10
def func():
global x
x = 5
func()
print(x)
Correct answer:
B) 5
Explanation: The global keyword allows the function to modify the global variable x .
How do you create a new instance of a class MyClass in Python?
Correct answer:
C) MyClass()
Explanation: To create an instance of a class, you call the class name followed by parentheses.
Which method is called automatically when a new object is created in Python?
Correct answer:
B) __init__()
Explanation: The __init__() method is called when a new instance of a class is created. It initializes the object.
What is the output of this Python code?
class MyClass:
def __init__(self):
self.value = 10
obj = MyClass()
print(obj.value)
Correct answer:
A) 10
Explanation: The __init__() method assigns the value 10 to the value attribute when an instance of MyClass is created.
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)
Correct answer:
B) 10
Explanation: The update() method changes the value of the value attribute to 10.
How do you define a method inside a Python class?
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.
What is the purpose of the self parameter in Python class methods?
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.
What is the result of this code?
class MyClass:
def __init__(self):
self.value = 10
obj = MyClass()
del obj
print(obj)
Correct answer:
B) Error
Explanation: After the object is deleted using del , trying to access obj will raise an error.
Which of the following can be used to check if an object obj is an instance of class 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.
What is the purpose of inheritance in Python?
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.
How do you inherit a class in Python?
Correct answer:
B) class SubClass(SuperClass):
Explanation: In Python, inheritance is achieved by specifying the parent class in parentheses after the subclass name.
What will this Python code output?
class Parent:
def __init__(self):
self.value = 5
class Child(Parent):
def __init__(self):
Parent.__init__(self)
self.value += 5
obj = Child()
print(obj.value)
Correct answer:
B) 10
Explanation: The Child class inherits from Parent and modifies the value attribute, adding 5 to it.
How can you override a method in a subclass in Python?
Correct answer:
C) By defining a method with the same name in the subclass
Explanation: You can override a method in a subclass by defining a method with the same name as in the parent class.
What will this Python code output?
class MyClass:
def __init__(self):
self.value = 5
def method(self):
print(self.value)
obj = MyClass()
obj.value = 10
obj.method()
Correct answer:
B) 10
Explanation: The value of the value attribute is changed to 10 before calling the method, so it prints 10.
What is the result of this code?
class MyClass:
def __init__(self):
self.value = 5
obj = MyClass()
obj.new_value = 10
print(obj.new_value)
Correct answer:
B) 10
Explanation: You can dynamically add new attributes to an object in Python.
What is the correct syntax to call a parent class method in a subclass?
Correct answer:
A) super().method_name()
Explanation: The super() function allows you to call methods from a parent class in a subclass.
How do you define a private attribute in Python?
Correct answer:
B) By prefixing the attribute with two underscores ( __ )
Explanation: Python does not have strict private attributes, but prefixing an attribute with two underscores makes it harder to access from outside the class (name mangling).
What is the result of this Python code?
class MyClass:
def __init__(self):
self.__value = 5
obj = MyClass()
print(obj.__value)
Correct answer:
B) Error
Explanation: The attribute __value is private and cannot be accessed directly from outside the class.
What is method overloading in Python?
Correct answer:
D) Python does not support method overloading
Explanation: Python does not support method overloading. If multiple methods with the same name are defined, the last one will override the previous ones.
What is the difference between is and == in Python?
Correct answer:
B) is checks for reference equality, while == checks for value equality
Explanation: The is operator checks whether two references point to the same object, while == checks if their values are equal.
If this exam is giving you a hard time, let us be your helping hand. Contact us, and we'll provide the assistance you need to pass and move forward.