wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Fundamentals of Python Programming

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

What are the basic data types in Python?

a)

int, float, complex, set

b)

char, list, tuple, dict

c)

str, bool, array, object

d)

int, float, str, bool

2.

How do you declare a variable in Python?

a)

You declare a variable in Python by using the syntax: variable_name = value.

b)

You declare a variable in Python using the syntax: value = variable_name.

c)

To declare a variable in Python, use the syntax: variable_name : value.

d)

Variables in Python are declared with the command: declare variable_name as value.

3.

What is the difference between a list and a tuple in Python?

a)

Tuples are mutable; lists are immutable.

b)

Lists can contain only numbers; tuples can contain any data type.

c)

Lists are mutable; tuples are immutable.

d)

Lists are faster than tuples in all cases.

4.

How can you convert a string to an integer in Python?

a)

Use int('string') to convert a string to an integer.

b)

Use eval('string') to evaluate and convert a string to an integer.

c)

Apply float('string') to convert a string to a float.

d)

Use str('integer') to convert an integer to a string.

5.

What is a function in Python?

a)

A function in Python is a type of variable.

b)

A function in Python is a built-in data structure.

c)

A function in Python is a reusable block of code defined with the 'def' keyword.

d)

A function in Python is a way to store data permanently.

6.

How do you define a function in Python?

a)

function_name(parameters) =>

b)

create function_name(parameters)

c)

function_name: parameters()

d)

def function_name(parameters):

7.

What is the purpose of the return statement in a function?

a)

The return statement allows a function to run indefinitely without stopping.

b)

The return statement pauses the function until further input is received.

c)

The purpose of the return statement is to exit a function and return a value to the caller.

d)

The return statement is used to define a function's parameters.

8.

How can you import a module in Python?

a)

Import modules using 'load module_name'.

b)

Use the 'require' statement, e.g., 'require(module_name)'.

c)

Use the 'import' statement, e.g., 'import module_name'.

d)

You can include a module with 'include module_name'

9.

What is exception handling in Python?

a)

Exception handling in Python is a way to manage errors and exceptions that occur during program execution.

b)

Exception handling in Python is used to improve user interface design.

c)

Exception handling in Python is a technique for data encryption.

d)

Exception handling in Python is a method to optimize code performance.

10.

How do you handle exceptions using try and except?

a)

Wrap all code in 'except' without a 'try' block.

b)

Use 'try' to ignore all exceptions without handling them.

c)

Only use 'try' for code that is guaranteed to succeed.

d)

Use 'try' to wrap code that may raise an exception, and 'except' to handle the exception.

11.

What is the purpose of the finally block in exception handling?

a)

To define the type of exception that can be caught.

b)

To handle exceptions before they are thrown.

c)

To optimize performance by skipping error checks.

d)

To ensure that code runs regardless of whether an exception occurs.

12.

What is the difference between a syntax error and a runtime error?

a)

Runtime errors can be fixed by changing the syntax of the code.

b)

Both syntax errors and runtime errors are detected at compile time.

c)

Syntax errors occur during program execution, while runtime errors are detected at compile time.

d)

Syntax errors are detected at compile time, while runtime errors occur during program execution.

13.

How can you raise an exception in Python?

a)

raise 'Error message'

b)

raise Exception()

c)

throw Exception('Error message')

d)

raise Exception('Error message')

14.

What are default arguments in a function?

a)

Default arguments can only be used in class methods.

b)

Default arguments must always be specified by the user.

c)

Default arguments are only used for optional parameters.

d)

Default arguments are predefined values for function parameters that are used when no argument is provided.

15.

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

a)

Use *args in Python to accept a variable number of arguments.

b)

Use **kwargs** to pass a fixed number of arguments.

c)

Use a list to pass arguments instead of unpacking them.

d)

Declare all arguments explicitly in the function definition.

16.

What is the purpose of the __name__ variable in a Python script?

a)

The __name__ variable tracks the execution time of a script.

b)

The __name__ variable helps identify if a script is run directly or imported as a module.

c)

The __name__ variable stores the main function of a script.

d)

The __name__ variable is used to define global constants.

17.

How do you create a module in Python?

a)

Create a .exe file and run it directly.

b)

Create a .py file with your code and import it in other scripts.

c)

Use a .txt file to store your code.

d)

Write your code directly in the Python interpreter.

18.

What is the significance of the pass statement in Python?

a)

The pass statement serves as a placeholder in Python code.

b)

The pass statement is a way to define a function without any implementation.

c)

The pass statement is used to raise exceptions in Python.

d)

The pass statement is used to terminate a loop in Python.

19.

How can you read a file in Python?

a)

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

b)

Use 'with open(filename, 'w') as file: content = file.read()' to read a file.

c)

Use 'open(filename) as file: content = file.read()' to read a file.

d)

Read a file by calling 'file.read(filename)' directly.

20.

What is the use of the with statement when handling files?

a)

The 'with' statement is necessary for reading files in binary mode.

b)

The 'with' statement is a way to format text files.

c)

The 'with' statement is used to create new files only.

d)

The 'with' statement is used to ensure proper acquisition and release of resources, like automatically closing files.