Python MCQ1
Python MCQ1
in Python involves hiding the complex implementation details and exposing only the necessary parts
of the data. This is typically done using classes and objects. Here's a simple example to illustrate data
abstraction:
We'll create a BankAccount class that abstracts away the details of how the balance is managed,
while providing a simple interface for deposit, withdrawal, and checking the balance.
python
Copy code
class BankAccount:
if amount > 0:
self.__balance += amount
print(f"Deposited: ${amount}")
else:
self.__balance -= amount
print(f"Withdrew: ${amount}")
else:
def get_balance(self):
return self.__balance
# Usage
Explanation
Public Methods: The deposit, withdraw, and get_balance methods provide a controlled way
to interact with the account. Users of the BankAccount class can deposit, withdraw, and
check the balance without needing to know how the balance is managed internally.
This example demonstrates data abstraction by providing a clean interface for interacting with a bank
account while keeping the details of how the balance is handled hidden from the user.
Encapsulation in Python involves bundling data and methods that operate on that data into a single
unit, typically a class, and restricting access to some of the object's components. This helps in hiding
the internal state of the object and only exposing what is necessary.
We’ll create a Person class where certain details are private, and access to these details is controlled
via public methods.
python
Copy code
class Person:
def get_name(self):
return self.__name
self.__name = name
else:
print("Invalid name.")
def get_age(self):
return self.__age
self.__age = age
else:
print("Invalid age.")
# Usage
Explanation
Private Attributes: The attributes __name and __age are private (indicated by double
underscores), meaning they cannot be accessed directly from outside the class. This helps
protect the internal state of the object.
Public Methods: The methods get_name, set_name, get_age, and set_age are public and
provide controlled access to the private attributes. They allow external code to read or
modify the private attributes in a controlled manner.
In this example, encapsulation ensures that the internal representation of a Person object is hidden,
and changes to the name or age are controlled and validated through public methods. This helps
maintain the integrity of the object’s state and prevents invalid data from being set directly.
polymorphism python
Polymorphism in Python refers to the ability of different objects to be treated as instances of the
same class through a common interface, typically using methods. It allows methods to do different
things based on the object it is acting upon, even though they share the same name.
1. Method Overloading: In Python, method overloading (i.e., defining multiple methods with
the same name but different parameters) is not supported directly as in some other
languages. However, Python’s dynamic typing allows you to achieve similar results by using
default arguments or variable-length arguments.
python
Copy code
class Animal:
def speak(self):
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
def make_animal_speak(animal):
print(animal.speak())
# Usage
dog = Dog()
cat = Cat()
Explanation
Subclasses (Dog and Cat): Each subclass provides its own implementation of the speak
method.
This allows make_animal_speak to work with any object that has a speak method, regardless of the
specific class of the object, as long as the object follows the expected interface.
4o mini
Inheritance in Python allows a class (child or subclass) to inherit attributes and methods from
another class (parent or superclass). This promotes code reuse and a hierarchical organization of
classes. Python supports several types of inheritance:
1. Single Inheritance
Example:
python
Copy code
class Animal:
def speak(self):
class Dog(Animal):
def speak(self):
return "Woof!"
# Usage
dog = Dog()
2. Multiple Inheritance
In multiple inheritance, a class inherits from more than one superclass. This can be used to combine
behaviors from multiple classes.
Example:
python
Copy code
class Flyer:
def fly(self):
return "Flying"
class Swimmer:
def swim(self):
return "Swimming"
return "Quack!"
# Usage
duck = Duck()
3. Multilevel Inheritance
Example:
python
Copy code
class Animal:
def speak(self):
class Mammal(Animal):
def has_fur(self):
return True
class Dog(Mammal):
def speak(self):
return "Woof!"
# Usage
dog = Dog()
4. Hierarchical Inheritance
python
Copy code
class Animal:
def speak(self):
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
# Usage
dog = Dog()
cat = Cat()
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance. It can sometimes lead to
complex scenarios, especially with multiple inheritance.
Example:
python
Copy code
class Flyer:
def fly(self):
return "Flying"
class Swimmer:
def swim(self):
return "Swimming"
class Animal:
def eat(self):
return "Eating"
def quack(self):
return "Quack!"
# Usage
duck = Duck()
6. Abstract Inheritance
Abstract inheritance involves creating an abstract base class with abstract methods that must be
implemented by subclasses. Python uses the abc module for this.
Example:
python
Copy code
class AbstractAnimal(ABC):
@abstractmethod
def speak(self):
pass
class Dog(AbstractAnimal):
def speak(self):
return "Woof!"
class Cat(AbstractAnimal):
def speak(self):
return "Meow!"
# Usage
dog = Dog()
cat = Cat()
Single Inheritance: Simplest form where one class inherits from one superclass.
Abstract Inheritance: Uses abstract base classes to define a common interface for
subclasses.
These types of inheritance help in organizing and structuring code in a flexible and reusable way.
exception handling
Exception handling in Python is used to manage errors and exceptions that occur during the
execution of a program. It allows you to handle errors gracefully without crashing the program.
Python uses try, except, else, and finally blocks to handle exceptions.
Basic Syntax
python
Copy code
try:
pass
except ExceptionType as e:
pass
else:
pass
finally:
pass
Examples
python
Copy code
try:
result = 10 / 0
except ZeroDivisionError:
In this example, dividing by zero raises a ZeroDivisionError, which is caught and handled by the
except block.
python
Copy code
try:
result = int("string")
except ValueError:
Here, ValueError and ZeroDivisionError are handled separately. If an exception is raised, the
corresponding except block executes.
3. Using else
python
Copy code
try:
result = 10 / 2
except ZeroDivisionError:
else:
The else block runs if no exceptions are raised in the try block.
4. Using finally
python
Copy code
try:
content = file.read()
except FileNotFoundError:
finally:
file.close()
The finally block runs regardless of whether an exception occurred or not. It’s commonly used for
cleanup actions.
python
Copy code
try:
result = 10 / 0
except Exception as e:
Using Exception will catch all exceptions that inherit from the base Exception class. Be cautious with
this approach, as it may catch unintended exceptions.
6. Raising Exceptions
You can also raise exceptions manually using the raise keyword.
python
Copy code
if b == 0:
return a / b
try:
result = divide(10, 0)
except ValueError as e:
print(e)
Key Points
except Block: Code to handle the exception if it occurs. You can specify multiple except
blocks for different exceptions.
finally Block: Code that runs no matter what, often used for cleanup actions like closing files.
Effective exception handling can improve the robustness and user-friendliness of your programs by
managing errors gracefully and providing meaningful feedback.
4o mini
Sure! Here’s a sample Python program that demonstrates various aspects of exception handling:
python
Copy code
def read_file(file_path):
try:
content = file.read()
print("File content:")
print(content)
except FileNotFoundError:
except IOError:
else:
finally:
def main():
read_file(file_path)
if __name__ == "__main__":
main()
Explanation
1. try Block:
o The try block contains code that may raise exceptions, such as opening and reading a
file.
2. except Blocks:
o FileNotFoundError: Catches the exception if the file specified does not exist.
o IOError: Catches general I/O errors, such as issues with file permissions.
3. else Block:
o The else block executes if no exceptions are raised in the try block. It indicates that
the file was read successfully.
4. finally Block:
o The finally block always executes, regardless of whether an exception was raised or
not. It is typically used for cleanup actions, but in this case, it just prints a completion
message.
1. Create a file named example.txt (or another name you provide) with some content to test
successful file reading.
2. Run the program. If the file exists, it will print the content and the success message. If the
file does not exist or another I/O error occurs, it will handle the error and print the
corresponding error message.
This sample program illustrates how to handle file operations robustly and manage various
exceptions that might occur during file processing.
4o mini
o A) 6
o B) 8
o C) 9
o D) 10
Answer: B) 8
o A) tuple
o B) string
o C) list
o D) int
Answer: C) list
o A) 5
o B) 6
o C) 7
o D) 8
Answer: B) 6
4. Which method can be used to remove whitespace from both ends of a string?
o A) strip()
o B) trim()
o C) remove()
o D) delete()
Answer: A) strip()
o A) list
o B) tuple
o C) dict
o D) set
Answer: A) list
o A) func
o B) define
o C) function
o D) def
Answer: D) def
o A) dict = {}
o B) dict = []
o C) dict = ()
o D) dict = set()
o A) True
o B) False
o C) Error
o D) None
Answer: A) True
o A) handle
o B) try
o C) catch
o D) except
Answer: B) try
o A) +
o B) -
o C) *
o D) &
Answer: A) +
12. What is the default value of max() function if no arguments are provided?
o A) 0
o B) None
o C) -1
o D) Inf
Answer: B) None
o A) add()
o B) append()
o C) insert()
o D) extend()
Answer: B) append()
o C) Does nothing
15. Which of the following operators is used for floor division in Python?
o A) /
o B) %
o C) //
o D) ^
Answer: C) //
o A) True
o B) False
o C) Error
o D) None
Answer: A) True
o A) //
o B) /*
o C) #
o D) --
Answer: C) #
18. Which data type is immutable in Python?
o A) list
o B) dict
o C) set
o D) tuple
Answer: D) tuple
o A) length()
o B) size()
o C) len()
o D) count()
Answer: C) len()
o A) 1
o B) 1.5
o C) 1.0
o D) 2
Answer: B) 1.5
Feel free to adjust or expand on these questions as needed. If you need more questions or specific
topics covered, just let me know!
4o mini
Certainly! Here are some advanced Python multiple-choice questions that cover topics like
decorators, context managers, metaclasses, concurrency, and more.
o D) To create a thread
o B) To handle exceptions
3. Which of the following statements is true about Python's Global Interpreter Lock (GIL)?
Answer: B) It prevents Python programs from utilizing multiple CPU cores effectively
o A) [2, 4, 6]
o B) [1, 2, 3]
o C) [2, 4, 6, 8]
o D) None
Answer: A) [2, 4, 6]
o A) @classmethod
o B) @staticmethod
o C) @property
o D) @method
Answer: A) @classmethod
python
Copy code
b.append(a)
return b
print(func(1))
print(func(2, []))
print(func(3))
o A) asyncio
o B) threading
o C) multiprocessing
o D) concurrent
Answer: A) asyncio
12. Which of the following statements is correct about Python's super() function?
o A) [1, 2, 3, 4]
o B) (1, 2, 3, 4)
o C) {1, 2, 3, 4}
o D) {1, 2, 2, 3, 4}
Answer: C) {1, 2, 3, 4}
o A) type()
o B) id()
o C) object()
o D) class()
Answer: A) type()
python
Copy code
class A:
def __init__(self):
self.value = 10
class B(A):
def __init__(self):
super().__init__()
self.value = 20
obj = B()
print(obj.value)
o A) 10
o B) 20
o C) None
o D) Error
Answer: B) 20
o D) To handle exceptions
19. Which method is used to serialize a Python object into JSON format?
o A) json.dump()
o B) json.load()
o C) json.dumps()
o D) json.loads()
Answer: C) json.dumps()
python
Copy code
def outer():
x = 10
def inner():
nonlocal x
x = 20
inner()
return x
print(outer())
o A) 10
o B) 20
o C) None
o D) Error
Answer: B) 20
Lists
o A) []
o B) ()
o C) {}
o D) <>
Answer: A) []
o A) [1, 2, 3, 4, 5]
o B) [0, 1, 2, 3, 4]
o C) [0, 1, 2, 3, 4, 5]
o D) range(5)
Answer: B) [0, 1, 2, 3, 4]
o A) my_list[2]
o B) my_list[3]
o C) my_list.get(3)
o D) my_list[1]
Answer: A) my_list[2]
o A) pop()
o B) remove()
o C) del()
o D) discard()
Answer: A) pop()
o A) 3
o B) 4
o C) 2
o D) 5
Answer: A) 3
o A) a + b
o B) a.append(b)
o C) a.extend(b)
o D) a.concat(b)
Answer: A) a + b
o A) my_list.sort()
o B) sort(my_list)
o C) my_list.order()
o D) my_list.sorted()
Answer: A) my_list.sort()
o A) [1, 2, 3, 1, 2, 3]
o B) [2, 4, 6]
o C) [1, 2, 3, 2, 4, 6]
o D) [1, 2, 3, 4, 5, 6]
Answer: A) [1, 2, 3, 1, 2, 3]
o A) my_list.insert(index, item)
o B) my_list.add(index, item)
o C) my_list.append(index, item)
o D) my_list.add(item, index)
o A) ()
o B) []
o C) {}
o D) <>
Answer: A) ()
o A) 2
o B) 3
o C) 4
o D) None
Answer: B) 3
o A) Yes
o B) No
Answer: B) No
o A) my_tuple[0]
o B) my_tuple[1]
o C) my_tuple.get(0)
o D) my_tuple.first()
Answer: A) my_tuple[0]
o A) tuple(list)
o B) list.to_tuple()
o C) tuple.convert()
o D) convert(tuple)
Answer: A) tuple(list)
16. What will (1, 2) + (3, 4) result in?
o A) (1, 2, 3, 4)
o C) (1, 2) * (3, 4)
o D) (1, 2, 3, 4, 3, 4)
Answer: A) (1, 2, 3, 4)
o A) my_tuple.count(item)
o B) my_tuple.frequency(item)
o C) my_tuple.occurrences(item)
o D) my_tuple.find(item)
Answer: A) my_tuple.count(item)
o A) my_tuple.index(item)
o B) my_tuple.find(item)
o C) my_tuple.locate(item)
o D) my_tuple.position(item)
Answer: A) my_tuple.index(item)
o A) Yes
o B) No
Answer: A) Yes
o A) (2,)
o B) (2, 3)
o C) 2
o D) (1, 2)
Answer: A) (2,)
Sets
21. How do you create a set in Python?
o A) set()
o B) []
o C) {}
o D) ()
Answer: A) set()
o A) {1, 2, 3}
o B) [1, 2, 3]
o C) (1, 2, 3)
o D) {1, 2, 2, 3}
Answer: A) {1, 2, 3}
o A) my_set.add(element)
o B) my_set.insert(element)
o C) my_set.append(element)
o D) my_set.put(element)
Answer: A) my_set.add(element)
o A) 5
o B) 4
o C) 3
o D) 2
Answer: B) 4
o B) a.intersection(b)
o C) a.intersect(b)
o D) Both A and B
28. How do you create a set with elements from another set?
o A) set(a)
o B) a.copy()
o C) a.clone()
o D) set.copy(a)
Answer: A) set(a)
29. Which method removes and returns an arbitrary element from a set?
o A) pop()
o B) remove()
o C) discard()
o D) delete()
Answer: A) pop()
o A) Yes
o B) No
Answer: B) No
Dictionaries
o B) []
o C) ()
o D) <>
Answer: A) {}
o A) my_dict[key]
o B) my_dict.get(key)
o C) my_dict[key].value
o D) Both A and B
o A) my_dict.update(key, value)
o B) my_dict.add(key, value)
o C) my_dict[key] = value
o D) my_dict.set(key, value)
o A) my_dict.keys()
o B) my_dict.get_keys()
o C) my_dict.all_keys()
o D) my_dict.list_keys()
Answer: A) my_dict.keys()
o A) my_dict.values()
o B) my_dict.get_values()
o C) my_dict.all_values()
o D) my_dict.list_values()
Answer: A) my_dict.values()
o A) my_dict.clear()
o B) my_dict.delete()
o C) my_dict.remove()
o D) my_dict.discard()
Answer: A) my_dict.clear()
o D) A list of keys
o A) key in my_dict
o B) my_dict.has_key(key)
o C) my_dict.contains(key)
o D) key.exists(my_dict)
o A) Yes
o B) No
Answer: B) No
Advanced Questions
o A) [1, 3, 5]
o B) [2, 4]
o C) [1, 2, 3]
o D) [2, 3, 4]
Answer: A) [1, 3, 5]
o A) tuple(element)
o B) tuple((element,))
o C) tuple([element])
o D) tuple.add(element)
Answer: B) tuple((element,))
o A) a | b
o B) a.union(b)
o C) a + b
o D) Both A and B
45. Which method removes a specified element from a set if it exists, without raising an error
if it does not?
o A) discard()
o B) remove()
o C) pop()
o D) delete()
Answer: A) discard()
o A) {'a': 1, 'b': 2}
o B) {'a': 1, 'b': 1}
47. Which method can be used to merge two dictionaries in Python 3.9 and above?
o A) dict1 | dict2
o B) dict1.update(dict2)
o C) dict1.merge(dict2)
o D) dict1.concat(dict2)
48. How do you get the value associated with a key, or a default value if the key is not present
in a dictionary?
o A) my_dict.get(key, default)
o B) my_dict.fetch(key, default)
o C) my_dict.value(key, default)
o D) my_dict.default(key, default)
49. Which method returns a view object that displays a list of all the keys in a dictionary?
o A) keys()
o B) values()
o C) items()
o D) view()
Answer: A) keys()
50. How can you remove a key-value pair from a dictionary while also getting the value?
o A) pop()
o B) remove()
o C) discard()
o D) del()
Answer: A) pop()
o A) {1}
o B) {1, 2, 3}
o C) {2}
o D) {1, 2}
Answer: A) {1}
o A) copy()
o B) clone()
o C) duplicate()
o D) new()
Answer: A) copy()
53. How do you get the unique elements from a list using a set?
o A) set(my_list)
o B) list(set(my_list))
o C) unique(my_list)
o D) my_list.unique()
Answer: B) list(set(my_list))
54. How do you create a dictionary with default values for new keys?
o A) defaultdict
o B) default_dict
o C) dict_default
o D) default_values
Answer: A) defaultdict
o A) (1, 2, 3, 1, 2, 3)
o B) [1, 2, 3, 1, 2, 3]
o C) (1, 2, 3) * 2
o D) [1, 2] * 3
Answer: A) (1, 2, 3, 1, 2, 3)
56. Which method removes an item from a set and returns it?
o A) pop()
o B) remove()
o C) discard()
o D) delete()
Answer: A) pop()
o A) not my_dict
o B) my_dict.empty()
o C) len(my_dict) == 0
o D) my_dict.is_empty()
o A) Raises a TypeError
o C) Merges a and b
o D) Both A and B
o A) my_dict.items()
o B) my_dict.to_list()
o C) list(my_dict)
o D) dict.list_items()
Answer: A) my_dict.items()
o A) my_tuple[0]
o B) my_tuple.first()
o C) my_tuple[1]
o D) my_tuple.get(0)
Answer: A) my_tuple[0]
61. Which method is used to remove all occurrences of an item from a set?
o A) discard()
o B) remove()
o C) clear()
o D) delete()
Answer: A) discard()
o B) {'hello'}
o A) my_dict.values()
o B) my_dict.all_values()
o C) my_dict.view_values()
o D) my_dict.get_values()
Answer: A) my_dict.values()
o A) del my_dict[key]
o B) my_dict.remove(key)
o C) my_dict.pop(key)
o D) Both A and C
o A) len(set)
o B) count(set)
o C) size(set)
o D) len(set)
Answer: A) len(set)
o C) A set of characters
o D) None
68. How do you create a dictionary with default values for new keys using defaultdict?
o A) defaultdict(default_factory)
o B) dict.default(default_factory)
o C) defaultdict(key, default_factory)
o D) defaultdict.default_factory()
Answer: A) defaultdict(default_factory)
o A) (4, 3, 2, 1)
o B) (1, 2, 3, 4)
o C) (2, 3, 4)
o D) (3, 4)
Answer: A) (4, 3, 2, 1)
70. Which method is used to return the set of differences between two sets?
o A) difference()
o B) subtract()
o C) diff()
o D) remove()
Answer: A) difference()
71. How do you get the dictionary of all items (key-value pairs) in a dictionary?
o A) items()
o B) dict()
o C) get_items()
o D) all_items()
Answer: A) items()
72. What does the dict.fromkeys(seq, value) method do?
o A) Creates a new dictionary with keys from seq and value as their value
o B) Creates a new dictionary with value as keys and seq as their value
o C) Updates dict with keys from seq and value as their value
Answer: A) Creates a new dictionary with keys from seq and value as their value
73. Which set operation returns a new set with elements that are only in one of the sets?
o A) symmetric_difference()
o B) exclusive()
o C) only_in_one()
o D) unique()
Answer: A) symmetric_difference()
o A) [1, 2, 3, 4]
o B) [1, 2, 4]
o C) [1, 3, 2, 4]
o D) [2, 3, 4]
Answer: A) [1, 2, 3, 4]
75. Which method would you use to add multiple items to a set?
o A) update()
o B) add_all()
o C) insert()
o D) merge()
Answer: A) update()
76. How do you remove an element from a set without raising an error if the element does not
exist?
o A) discard()
o B) remove()
o C) pop()
o D) delete()
Answer: A) discard()
77. What is the output of my_dict.get('key', 'default') if 'key' does not exist in my_dict?
o A) 'default'
o B) None
o C) 'key'
o D) Error
Answer: A) 'default'
o A) dict(list_of_tuples)
o B) dict.from_tuples(list_of_tuples)
o C) list_to_dict(list_of_tuples)
o D) dict.convert(list_of_tuples)
Answer: A) dict(list_of_tuples)
o A) (element,) * n
o B) (element * n)
o C) repeat(element, n)
o D) tuple.repeat(element, n)
Answer: A) (element,) * n
o A) my_dict.update(another_dict)
o B) my_dict.add(another_dict)
o C) my_dict.merge(another_dict)
o D) my_dict.concat(another_dict)
Answer: A) my_dict.update(another_dict)
o B) common()
o C) shared()
o D) overlap()
Answer: A) intersection()
o A) Removes and returns the value of 'key', or 'default' if 'key' does not exist
Answer: A) Removes and returns the value of 'key', or 'default' if 'key' does not exist
o A) set(list)
o B) list.set()
o C) set.add(list)
o D) create_set(list)
Answer: A) set(list)
o A) union()
o B) add()
o C) combine()
o D) merge()
Answer: A) union()
o A) (1, 2, 3, 1, 2, 3)
o B) [1, 2, 3, 1, 2, 3]
o C) (1, 2, 3) * 2
o D) [1, 2] * 3
Answer: A) (1, 2, 3, 1, 2, 3)
o A) difference()
o B) subtract()
o C) remove()
o D) unique()
Answer: A) difference()
o A) my_dict.keys()
o B) my_dict.get_keys()
o C) my_dict.all_keys()
o D) my_dict.list_keys()
Answer: A) my_dict.keys()
o D) A set with elements that are in my_set but not in other_set and vice versa
Answer: A) A set with elements that are in both my_set and other_set
o A) set(string)
o B) list(string)
o C) create_set(string)
o D) string.to_set()
Answer: A) set(string)
92. What does dict.get(key, default) do if the key exists in the dictionary?
o A) clear()
o B) remove_all()
o C) delete()
o D) discard_all()
Answer: A) clear()
o A) dict1.keys() - dict2.keys()
o B) dict1.items() - dict2.items()
o C) dict1.difference(dict2)
o D) dict1 - dict2
95. How do you create a set from a list with unique elements?
o A) set(list)
o B) unique(list)
o C) list.set()
o D) create_set(list)
Answer: A) set(list)
o C) Returns 'default'
o A) list(my_dict.values())
o B) my_dict.values().to_list()
o C) my_dict.get_values()
o D) list.from_values(my_dict)
Answer: A) list(my_dict.values())
o A) set(range())
o B) range.to_set()
o C) list(range())
o D) create_set(range())
Answer: A) set(range())
100. How do you remove and return the last item of a dictionary? –
A) popitem()
B) remove_last()
C) pop_last()
D) last()
Data Types
o A) int
o B) str
o C) tuple
o D) list
Answer: D) list
o A) <class 'int'>
o B) <class 'float'>
o C) <class 'str'>
o D) <class 'complex'>
o A) []
o B) {}
o C) ()
o D) <>
Answer: B) {}
o A) 4
o B) 5
o C) 6
o D) None
Answer: B) 5
o A) list()
o B) str()
o C) split()
o D) to_list()
Answer: A) list()
o A) tuple(list)
o B) list(tuple)
o C) convert(list)
o D) to_tuple(list)
Answer: A) tuple(list)
o A) True
o B) False
o C) None
o D) 0
Answer: B) False
o A) {}
o B) set()
o C) []
o D) empty_set()
Answer: B) set()
o A) append()
o B) add()
o C) insert()
o D) extend()
Answer: A) append()
o A) 6
o B) 9
o C) 27
o D) None
Answer: B) 9
Operators
o A) /
o B) %
o C) //
o D) **
Answer: C) //
o A) 2
o B) 1
o C) 0
o D) 5
Answer: B) 1
14. Which operator is used to check if two values are not equal?
o A) ==
o B) !=
o C) >
o D) <
Answer: B) !=
o A) 10
o B) 7
o C) 12
o D) 8
Answer: B) 7
o A) True
o B) False
o C) None
o D) Error
Answer: B) False
o A) True
o B) False
o C) None
o D) Error
Answer: A) True
o A) in
o B) is
o C) not
o D) !=
Answer: A) in
o A) 512
o B) 64
o C) 256
o D) None
Answer: A) 512
o A) 3
o B) 3.33
o C) 4
o D) 2
Answer: C) 3
Decision Making
o A) if
o B) elif
o C) else
o D) switch
Answer: A) if
o A) elif
o B) or
o C) and
o D) else
Answer: A) elif
24. What does the following code return: x = 5; y = 10; print(x > y)?
o A) True
o B) False
o C) None
o D) Error
Answer: B) False
o A) 5 <= x <= 10
o B) x in (5, 10)
26. What will be the output of the following code? x = 3; if x > 2: print("yes") else: print("no")
o A) yes
o B) no
o C) None
o D) Error
Answer: A) yes
o A) continue
o B) break
o C) exit
o D) stop
Answer: B) break
28. What does the continue statement do in a loop?
o C) if !condition: ...
o A) 0 == False
o B) None == False
o C) 1 == True
o D) [] == True
Answer: C) 1 == True
Looping
o A) for
o B) while
o C) do-while
o D) repeat
Answer: A) for
32. How do you iterate over a list my_list using a for loop?
o A) 0 1 2
o B) 1 2 3
o C) 0 1 2 3
o D) 1 2
Answer: A) 0 1 2
36. What is the output of the following code? i = 1; while i < 4: print(i); i += 1
o A) 1 2 3
o B) 1 2 3 4
o C) 1 2
o D) 1 2 3 4 5
Answer: A) 1 2 3
o A) break
o B) continue
o C) exit
o D) stop
Answer: A) break
38. Which of the following is the correct way to use break in a loop?
o A) for i in range(10): if condition: break
o C) if condition: break
o A) while True:
o C) while False:
o D) for i in range(10):
40. Which loop will not execute if the condition is initially False?
o A) for
o B) while
o C) do-while
o D) repeat
Answer: B) while
o A) 30
o B) 1020
o C) 10 20
o D) Error
Answer: B) 1020
o A) 123
o B) '123'
o C) None
o D) Error
Answer: A) 123
43. Which method removes whitespace from the start and end of a string?
o A) strip()
o B) trim()
o C) clean()
o D) remove()
Answer: A) strip()
o A) len()
o B) length()
o C) size()
o D) count()
Answer: A) len()
o A) sort()
o B) order()
o C) arrange()
o D) list_sort()
Answer: A) sort()
o A) my_list[2]
o B) my_list[3]
o C) my_list.get(3)
o D) my_list.third()
Answer: A) my_list[2]
o A) [1, 2, 3, 4]
o B) [1, 2, [3, 4]]
o C) [4, 3, 2, 1]
o D) [1, 2, 4, 3]
Answer: A) [1, 2, 3, 4]
o A) key in dict
o B) dict.has_key(key)
o C) dict.contains(key)
o D) key.exists(dict)
o A) 16
o B) 13
o C) 10
o D) 8
Answer: B) 11
o A) &&
o B) &
o C) and
o D) &&
Answer: C) and
o A) Integer division
o B) Modulus operation
o D) Exponentiation
o A) ==
o B) !=
o C) >
o D) is
Answer: A) ==
o A) +
o B) *
o C) **
o D) //
Answer: C) **
o A) aaa
o B) a a a
o C) aaaaa
o D) Error
Answer: A) aaa
o A) 9
o B) 81
o C) 27
o D) None
Answer: A) 9
o A) &
o B) &&
o C) and
o D) |
Answer: A) &
61. What will be the result of the following code? x = 4; while x < 8: print(x); x += 2
o A) 4 6 8
o B) 4 6
o C) 4 6 8 10
o D) 4 6 8 10 12
Answer: B) 4 6
62. How do you execute a block of code multiple times with a for loop?
o B) while condition:
64. How do you create a loop that executes exactly five times?
o A) for i in range(5):
o B) while i < 5:
o C) repeat 5 times:
o D) loop 5 times:
o A) 1 2 3
o B) 1 2 3 4
o C) 1 2
o D) 0 1 2 3
Answer: A) 1 2 3
o A) function
o B) def
o C) func
o D) define
Answer: B) def
67. What is the default return value of a function that does not have a return statement?
o A) None
o B) 0
o C) False
o D) Error
Answer: A) None
68. How do you call a function named my_function with arguments 1 and 2?
o A) my_function(1, 2)
o B) my_function{1, 2}
o C) my_function[1, 2]
o D) call my_function(1, 2)
Answer: A) my_function(1, 2)
o A) for
o B) while
o C) do-while
o D) repeat
Answer: C) do-while
70. What will the following code print? for i in range(2): print(i ** 2)
o A) 0 1
o B) 0 1 4
o C) 1 4
o D) 2 4
Answer: A) 0 1
Additional Questions
o A) try-except
o B) catch-finally
o C) try-catch
o D) error-handler
Answer: A) try-except
o A) <class 'list'>
o B) <class 'tuple'>
o C) <class 'dict'>
o D) <class 'set'>
73. Which method can be used to find the maximum value in a list?
o A) max()
o B) highest()
o C) top()
o D) largest()
Answer: A) max()
o A) set(list)
o B) list.to_set()
o C) create_set(list)
o D) convert_to_set(list)
Answer: A) set(list)
o A) 3 4 5
o B) 3 4 5 6
o C) 3 6
o D) 3 4 5 6 7
Answer: A) 3 4 5
o B) [123]
o C) [1, 2, 3]
o D) ['123']
78. Which function can be used to get the sum of all elements in a list?
o A) sum()
o B) total()
o C) add_all()
o D) accumulate()
Answer: A) sum()
o A) s.lower()
o B) s.to_lower()
o C) s.lowercase()
o D) s.convert_to_lower()
Answer: A) s.lower()
o B) Performs an intersection
o A) discard()
o B) remove()
o C) pop()
o D) delete()
Answer: B) remove()
o A) (1, 2, 3, 1, 2, 3)
o B) [1, 2, 3, 1, 2, 3]
o C) (1, 2, 3) * 2
o D) [1, 2] * 3
Answer: A) (1, 2, 3, 1, 2, 3)
o A) difference()
o B) subtract()
o C) remove()
o D) unique()
Answer: A) difference()
84. How do you get the keys from a dictionary?
o A) my_dict.keys()
o B) my_dict.get_keys()
o C) my_dict.all_keys()
o D) my_dict.list_keys()
Answer: A) my_dict.keys()
o D) A set with elements that are in my_set but not in other_set and vice versa
Answer: A) A set with elements that are in both my_set and other_set
o A) set(string)
o B) list(string)
o C) create_set(string)
o D) string.to_set()
Answer: A) set(string)
88. What does dict.get(key, default) do if the key exists in the dictionary?
o B) Use list.remove_duplicates()
o C) Use list.unique()
o D) Use list.discard()
o A) [0, 2]
o B) [1, 3]
o C) [0, 1, 2, 3]
o D) [2, 4]
Answer: A) [0, 2]
o A) list[-1]
o B) list.last()
o C) list[len(list) - 1]
o D) list.end()
Answer: A) list[-1]
o A) if not list:
o B) if list == []:
o C) if len(list) == 0:
o A) type(object)
o B) check_type(object)
o C) object.type()
o D) object.get_type()
Answer: A) type(object)
o A) {1, 2, 3}
o B) [1, 2, 3]
o C) {1, 2, 2, 3}
o D) None
Answer: A) {1, 2, 3}
o A) tuple(list)
o B) list.to_tuple()
o C) convert_to_tuple(list)
o D) tuple.create(list)
Answer: A) tuple(list)
o A) not list
o B) list.is_empty()
o C) list.empty()
o D) list.length() == 0
o A) HELLO
o B) hello
o C) Hello
o D) HELlo
Answer: A) HELLO
100. How do you get the number of items in a dictionary? - A) len(dict) - B) dict.count() -
C) dict.size() - D) dict.length()
python
Copy code
**Answer: A) `len(dict)`**