WorksheetsPython Fundamentals Assessment
Total questions: 20
Worksheet time: 10mins
What are the basic data types in Python?
char, float, array
int, float, string, boolean
int, float, str, bool, list, tuple, set, dict
list, tuple, array, object
How do you declare a variable in Python?
You declare a variable in Python by using the syntax: variable_name = value.
declare variable_name as value
variable_name : value
value = variable_name
What is the difference between a list and a tuple?
A list can only contain numbers, while a tuple can contain any data type.
A list is defined with {}, while a tuple is defined with []
A list is mutable and defined with [], while a tuple is immutable and defined with ().
A list is immutable and defined with [], while a tuple is mutable and defined with ().
How do you convert a string to an integer in Python?
Use str('string') to convert a string to an integer.
Use int('string') to convert a string to an integer.
Use eval('string') to convert a string to an integer.
Apply float('string') for conversion to an integer.
What is the purpose of the 'if' statement in Python?
The 'if' statement is used to create loops.
The 'if' statement allows conditional execution of code blocks.
The 'if' statement is used to define functions.
The 'if' statement is for importing libraries.
How do you create a for loop in Python?
for i in range(5): print(i)
for i in 5: print(i)
for (i = 0; i < 5; i++): print(i)
for i in range(0, 5, 2): print(i)
What is a while loop and how does it work?
A while loop is a control structure that repeatedly executes a block of code as long as a specified condition is true.
A while loop is a function that executes only once.
A while loop is a data structure that stores multiple values.
A while loop is a type of variable that holds a single value.
Explain the concept of indentation in Python.
Indentation is a feature that allows for variable declaration in Python.
Indentation is only used for comments in Python code.
Indentation in Python defines code blocks and is crucial for the structure of the program.
Indentation is optional in Python and does not affect code execution.
What is a function in Python?
A function is a variable that stores data.
A function in Python is a defined block of code that performs a specific task and can take inputs and return outputs.
A function is a type of loop in Python.
A function is a built-in library for data analysis.
How do you define a class in Python?
class ClassName() {}
class ClassName[]:
class ClassName: pass
define ClassName as a function
What is inheritance in object-oriented programming?
Inheritance allows a superclass to inherit attributes from a subclass.
Inheritance allows a subclass to inherit attributes and methods from a superclass.
Inheritance is a way to create new classes without any attributes.
Inheritance is a method for data encryption.
What is the purpose of the __init__ method in a class?
To create a new class from an existing one.
To define a class's methods and functions.
To initialize an object's attributes when a class instance is created.
To delete an object's attributes when a class instance is destroyed.
How do you create an instance of a class?
create ClassName instance = ()
Use the syntax: instance = ClassName()
ClassName.instance()
instance = new ClassName()
What is polymorphism in Python?
Polymorphism is the ability to present the same interface for different data types in Python.
Polymorphism is a design pattern used exclusively in object-oriented programming.
Polymorphism refers to the ability to change variable types dynamically in Python.
Polymorphism is a method for optimizing code performance in Python.
What is encapsulation and how is it achieved in Python?
Encapsulation is a method to increase the performance of Python code.
Encapsulation is only achieved through the use of classes in Python.
Encapsulation allows all attributes to be accessed freely without restrictions.
Encapsulation in Python is achieved through private and protected attributes, using underscores to restrict access.
How do you handle exceptions in Python?
Use if-else statements to handle exceptions in Python.
Ignore exceptions and continue execution.
Use print statements to debug exceptions in Python.
Use try-except blocks to handle exceptions in Python.
What is a dictionary in Python?
A dictionary in Python is a collection of key-value pairs.
A dictionary in Python is a single value.
A dictionary is a collection of ordered pairs in Python.
A dictionary is a type of list in Python.
How do you iterate over a dictionary?
Use a while loop to access keys.
Convert the dictionary to a list and iterate.
Use recursion to traverse the dictionary.
Use a for loop: 'for key in dictionary:', 'for value in dictionary.values():', or 'for key, value in dictionary.items():'.
What is the difference between '==' and 'is' in Python?
The '==' operator checks for value equality, while 'is' checks for object identity.
Both '==' and 'is' check for value equality.
'==' checks for object identity, while 'is' checks for value equality.
'==' is used for comparing types, while 'is' is used for checking values.
How do you import a module in Python?
Modules can be imported using the 'load' command.
Use the 'import' statement followed by the module name.
You can import a module by typing its name directly in the code.
Use the 'require' statement to load the module.
