wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Mastering Python Concepts

Total questions: 16

Worksheet time: 16mins

Name
Class
Date
1.

What is the purpose of a function in Python?

a)

The purpose of a function in Python is to encapsulate code for reuse and improve code organization.

b)

To execute code only once during a program's lifetime

c)

To create variables in Python

d)

To define classes in Python

2.

How do you import a module in Python?

a)

Use the 'require' statement to load the module.

b)

Modules can only be imported using the 'load' command.

c)

You must declare the module in a configuration file before importing.

d)

Use the 'import' statement followed by the module name.

3.

What is the difference between a function and a method?

a)

A method can be called without an object; a function cannot.

b)

A function is independent; a method is associated with an object.

c)

A function is always part of a class; a method can exist independently.

d)

Functions and methods are the same and have no differences.

4.

How can you pass a variable number of arguments to a function?

a)

Use '...' in JavaScript or *args in Python to accept variable arguments.

b)

Use '...' in Python or *args in JavaScript to accept variable arguments.

c)

Use global variables to pass arguments to the function.

d)

Declare a fixed number of parameters in the function definition.

5.

What is the syntax for opening a file in Python?

a)

open('filename')

b)

file.open('filename', 'mode')

c)

open('filename', 'mode')

d)

open_file('filename', 'mode')

6.

How do you read the entire content of a file?

a)

Use 'open(filename) as file: content = file.read()' in Python.

b)

Read the file using 'file.get_contents(filename)' in Python.

c)

Use 'file.read_all(filename)' to read the entire file.

d)

Use 'with open(filename, 'r') as file: content = file.read()' in Python.

7.

What method would you use to write data to a file?

a)

Use file.write() method in Python or equivalent in other languages.

b)

Use file.read() method to write data to a file.

c)

Use print() method to display data on the screen.

d)

Use input() method to read data from a file.

8.

How can you handle exceptions in Python?

a)

Ignore exceptions and continue execution without handling them.

b)

Use print statements to debug exceptions in Python.

c)

Use try-except blocks to handle exceptions in Python.

d)

Use if-else statements to handle exceptions in Python.

9.

What is the purpose of the 'try' and 'except' blocks?

a)

To define functions and methods in Python.

b)

The purpose of 'try' and 'except' blocks is to handle exceptions and errors in a controlled manner.

c)

To declare variables and constants in a program.

d)

To create loops and iterations in code.

10.

What is the difference between 'raise' and 'assert'?

a)

'raise' can only be used in classes, while 'assert' can be used anywhere.

b)

'raise' triggers an exception, while 'assert' checks a condition and raises an error if false.

c)

'raise' is a keyword in Python, whereas 'assert' is a built-in function.

d)

'raise' is used for logging messages, while 'assert' is for debugging.

11.

What is a class in Object-Oriented Programming?

a)

A class is a blueprint for creating objects in Object-Oriented Programming.

b)

A class is a method for executing code.

c)

A class is a function that returns an object.

d)

A class is a type of variable in programming.

12.

How do you create an instance of a class?

a)

object = new ClassName;

b)

ClassName.object();

c)

Use the class name followed by parentheses, e.g., 'object = ClassName()'.

d)

create ClassName() instance;

13.

What is inheritance in Python?

a)

Inheritance is a way to create multiple classes without any relationship.

b)

Inheritance in Python is a method to delete existing classes.

c)

Inheritance allows a class to inherit only methods, not properties.

d)

Inheritance in Python is a mechanism where a new class derives properties and behavior from an existing class.

14.

What is the purpose of the '__init__' method?

a)

To define a class's methods and functions.

b)

To delete an object after it is created.

c)

To create a new instance of a class without attributes.

d)

To initialize a new object's attributes when it is created.

15.

How can you define a class method?

a)

A class method is a method that can only be called on an instance of the class.

b)

A class method is a method that is bound to the class and not the instance, defined with the @classmethod decorator.

c)

A class method is a method that is specific to an object and cannot be shared across instances.

d)

A class method is a method that does not require any decorators to be defined.

16.

What is polymorphism in the context of OOP?

a)

Polymorphism is the process of creating multiple classes with the same name.

b)

Polymorphism is the ability of different classes to be treated as instances of the same class through a common interface.

c)

Polymorphism refers to the ability of a class to inherit properties from multiple parent classes.

d)

Polymorphism is the technique of hiding the implementation details of a class.