wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Programming Quiz

Total questions: 40

Worksheet time: 3600secs

Name
Class
Date
1.

Which of the following is an immutable data type in Python?

a)

List

b)

Dictionary

c)

Set

d)

Tuple

2.

In Python, which operator is used for floor division?

a)

/

b)

//

c)

%

d)

**

3.

What is the purpose of the __init__ method in a Python class?

a)

To initialize class variables

b)

To destroy class instances

c)

To import required modules

d)

To define class methods

4.

Which of the following is a correct way to define a class method in Python?

a)

def method(self, args):

b)

def method(cls, args):

c)

@classmethod

def method(self, args):

d)

@classmethod

def method(cls, args):

5.

In the context of object-oriented programming, what does "inheritance" mean?

a)

Creating multiple instances of the same class

b)

A class acquiring the properties of another class

c)

Defining private methods in a class

d)

Creating a class without any attributes

6.

What is encapsulation in object-oriented programming?

a)

Binding code and data together

b)

Creating multiple instances of a class

c)

Converting one data type to another

d)

Running multiple processes simultaneously

7.

Which of the following is used to create a private attribute in a Python class?

a)

Using the private keyword before the attribute name

b)

Prefixing the attribute name with two underscores (__)

c)

Enclosing the attribute in parentheses

d)

Using the @private decorator

8.

What is the output of the following code?

my_list = [1, 2, 3, 4, 5]

print(my_list[-2])

a)

5

b)

4

c)

-2

d)

Error

9.

Which method is used to add an element to a set in Python?

a)

append()

b)

add()

c)

insert()

d)

extend()

10.

What does the following code do?

my_dict = {'a': 1, 'b': 2, 'c': 3}

print(my_dict.get('d', 0))

a)

Raises a KeyError

b)

None

c)

0

d)

Adds a new key 'd' with value 0

11.

Which of the following is NOT a characteristic of Python sets?

a)

Sets are unordered

b)

Sets can contain duplicates

c)

Sets can only contain immutable elements

d)

Sets are mutable

12.

What is a generator in Python?

a)

A function that returns multiple values

b)

A function that creates a sequence of values using yield

c)

A built-in method to generate random numbers

d)

A class that generates instances of other classes

13.

What is the output of the following code?

def gen():

yield 1

yield 2

yield 3




g = gen()

print(next(g))

a)

[1, 2, 3]

b)

1

c)

gen object at 0x...

d)

StopIteration error

14.

Which statement correctly describes a Python generator expression?

a)

It is the same as a list comprehension but uses parentheses instead of square brackets

b)

It creates a list of values immediately

c)

It is used to generate random numbers

d)

It is a class method that creates new instances

15.

What is the main advantage of using generators over lists?

a)

Generators are always faster

b)

Generators consume less memory for large sequences

c)

Generators can store more elements

d)

Generators have more built-in methods

16.

How do you create an infinite sequence using generators?

a)

It's not possible in Python

b)

By using a while loop with yield

c)

By using the infinite() function

d)

By returning a large list

17.

Which data structure follows the LIFO (Last In, First Out) principle?

a)

Queue

b)

Stack

c)

Linked List

d)

Heap

18.

In the context of graph theory, what is a directed acyclic graph (DAG)?

a)

A graph with no cycles and directed edges

b)

A graph where all nodes have the same number of edges

c)

A graph where edges can only go in one direction

d)

A tree structure with multiple root nodes

19.

What is dynamic programming?

a)

A programming paradigm that uses objects

b)

A method to solve complex problems by breaking them into simpler subproblems

c)

A technique for writing code that changes during runtime

d)

An approach to create user interfaces

20.

What is recursion in programming?

a)

A function that takes a long time to execute

b)

A loop that runs a fixed number of times

c)

A function that calls itself

d)

A process that uses multiple CPU cores

21.

What is the base case in a recursive function?

a)

The most complex case that the function handles

b)

The condition where the function stops calling itself

c)

The initial input to the function

d)

The maximum recursion depth allowed

22.

What potential issue can occur with recursive functions if not properly designed?

a)

Memory overflow

b)

Stack overflow

c)

Syntax errors

d)

Type errors

23.

The Fibonacci sequence can be efficiently computed using:

a)

Only loops

b)

Only recursion

c)

Recursion with memoization

d)

Only iteration

24.

In a graph, what is the degree of a vertex?

a)

The number of edges connected to it

b)

The number of vertices adjacent to it

c)

The distance from the root node

d)

The weight of all connected edges

25.

Which traversal method explores as far as possible along each branch before backtracking?

a)

Breadth-First Search

b)

Depth-First Search

c)

Dijkstra's Algorithm

d)

A* Search

26.

What does it mean for a graph to be connected?

a)

Every vertex has at least one edge

b)

There is a path between every pair of vertices

c)

The graph has no cycles

d)

All vertices have the same degree

27.

Which data structure is commonly used to implement Breadth-First Search?

a)

Stack

b)

Queue

c)

Linked List

d)

Binary Tree

28.

Which data structure is most commonly used to evaluate infix expressions?

a)

Queue

b)

Stack

c)

Dictionary

d)

List

29.

What is operator precedence?

a)

The order in which operators appear in an expression

b)

The relative priority of operators that determines the order of evaluation

c)

The associativity of operators in an expression

d)

The number of operands an operator takes

30.

What is a dependency graph in the context of scheduling?

a)

A graph where edges represent tasks that can be executed simultaneously

b)

A graph where vertices represent resources and edges represent tasks

c)

A graph where vertices represent tasks and edges represent dependencies

d)

A graph showing the execution time of each task

31.

In event-driven simulation, what is an event queue?

a)

A list of all possible events that could occur

b)

A priority queue that orders events by their scheduled time

c)

A stack of events that have already occurred

d)

A random selection of events to simulate

32.

What is the main constraint in Sudoku puzzles?

a)

Each row, column, and 3x3 box must contain the digits 1-9 without repetition

b)

The sum of each row and column must equal 45

c)

Each diagonal must contain unique numbers

d)

The center square must always be 5

33.

Which of the following best describes Generative AI?

a)

AI systems that can generate new content based on patterns learned from training data

b)

AI systems that can only classify existing content

c)

AI systems that generate random outputs

d)

AI systems that require human input for every decision

34.

Which algorithm is best suited for computing a running average on a stream of data?

a)


Batch processing

b)

Full sorting

c)

Sliding window

d)

Recursive accumulation

35.

In scheduling, what does the presence of a cycle in the dependency graph indicate?

a)

A valid scheduling order

b)

Optimal task sequencing

c)

An error or unschedulable set of tasks

d)

Redundant tasks

36.

What is the output of the code snippet:

print(type(3/2))

a)

int

b)

float

c)

string

d)

complex

37.

Which data structure is best suited for implementing an undo functionality in applications?

a)

Queue

b)

Stack

c)

Dictionary

d)

Set

38.

Which of the following best defines a mathematical expression in the context of symbolic computation?

a)

A sequence of random numbers

b)

A combination of constants, variables, and operators representing a value

c)

Only numerical constants

d)

A method to solve equations

39.

What is the purpose of an evaluation function in the context of expression trees?

a)

To convert an expression into its tree representation

b)

To compute the numerical value of the expression

c)

To generate a random expression

d)

To print the expression in infix notation

40.

Which of the following conditions is necessary and sufficient for an undirected graph with n vertices to be a tree?

a)

Graph is connected and has n edges

b)

Graph is acyclic and has n edges

c)

Graph is connected and has n - 1 edges

d)

Graph is acyclic and has n - 1 edges