WorksheetsPython Programming Quiz
Total questions: 40
Worksheet time: 3600secs
Which of the following is an immutable data type in Python?
List
Dictionary
Set
Tuple
In Python, which operator is used for floor division?
/
//
%
**
What is the purpose of the __init__ method in a Python class?
To initialize class variables
To destroy class instances
To import required modules
To define class methods
Which of the following is a correct way to define a class method in Python?
def method(self, args):
def method(cls, args):
@classmethod
def method(self, args):
@classmethod
def method(cls, args):
In the context of object-oriented programming, what does "inheritance" mean?
Creating multiple instances of the same class
A class acquiring the properties of another class
Defining private methods in a class
Creating a class without any attributes
What is encapsulation in object-oriented programming?
Binding code and data together
Creating multiple instances of a class
Converting one data type to another
Running multiple processes simultaneously
Which of the following is used to create a private attribute in a Python class?
Using the private keyword before the attribute name
Prefixing the attribute name with two underscores (__)
Enclosing the attribute in parentheses
Using the @private decorator
What is the output of the following code?
my_list = [1, 2, 3, 4, 5]
print(my_list[-2])
5
4
-2
Error
Which method is used to add an element to a set in Python?
append()
add()
insert()
extend()
What does the following code do?
my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.get('d', 0))
Raises a KeyError
None
0
Adds a new key 'd' with value 0
Which of the following is NOT a characteristic of Python sets?
Sets are unordered
Sets can contain duplicates
Sets can only contain immutable elements
Sets are mutable
What is a generator in Python?
A function that returns multiple values
A function that creates a sequence of values using yield
A built-in method to generate random numbers
A class that generates instances of other classes
What is the output of the following code?
def gen():
yield 1
yield 2
yield 3
g = gen()
print(next(g))
[1, 2, 3]
1
gen object at 0x...
StopIteration error
Which statement correctly describes a Python generator expression?
It is the same as a list comprehension but uses parentheses instead of square brackets
It creates a list of values immediately
It is used to generate random numbers
It is a class method that creates new instances
What is the main advantage of using generators over lists?
Generators are always faster
Generators consume less memory for large sequences
Generators can store more elements
Generators have more built-in methods
How do you create an infinite sequence using generators?
It's not possible in Python
By using a while loop with yield
By using the infinite() function
By returning a large list
Which data structure follows the LIFO (Last In, First Out) principle?
Queue
Stack
Linked List
Heap
In the context of graph theory, what is a directed acyclic graph (DAG)?
A graph with no cycles and directed edges
A graph where all nodes have the same number of edges
A graph where edges can only go in one direction
A tree structure with multiple root nodes
What is dynamic programming?
A programming paradigm that uses objects
A method to solve complex problems by breaking them into simpler subproblems
A technique for writing code that changes during runtime
An approach to create user interfaces
What is recursion in programming?
A function that takes a long time to execute
A loop that runs a fixed number of times
A function that calls itself
A process that uses multiple CPU cores
What is the base case in a recursive function?
The most complex case that the function handles
The condition where the function stops calling itself
The initial input to the function
The maximum recursion depth allowed
What potential issue can occur with recursive functions if not properly designed?
Memory overflow
Stack overflow
Syntax errors
Type errors
The Fibonacci sequence can be efficiently computed using:
Only loops
Only recursion
Recursion with memoization
Only iteration
In a graph, what is the degree of a vertex?
The number of edges connected to it
The number of vertices adjacent to it
The distance from the root node
The weight of all connected edges
Which traversal method explores as far as possible along each branch before backtracking?
Breadth-First Search
Depth-First Search
Dijkstra's Algorithm
A* Search
What does it mean for a graph to be connected?
Every vertex has at least one edge
There is a path between every pair of vertices
The graph has no cycles
All vertices have the same degree
Which data structure is commonly used to implement Breadth-First Search?
Stack
Queue
Linked List
Binary Tree
Which data structure is most commonly used to evaluate infix expressions?
Queue
Stack
Dictionary
List
What is operator precedence?
The order in which operators appear in an expression
The relative priority of operators that determines the order of evaluation
The associativity of operators in an expression
The number of operands an operator takes
What is a dependency graph in the context of scheduling?
A graph where edges represent tasks that can be executed simultaneously
A graph where vertices represent resources and edges represent tasks
A graph where vertices represent tasks and edges represent dependencies
A graph showing the execution time of each task
In event-driven simulation, what is an event queue?
A list of all possible events that could occur
A priority queue that orders events by their scheduled time
A stack of events that have already occurred
A random selection of events to simulate
What is the main constraint in Sudoku puzzles?
Each row, column, and 3x3 box must contain the digits 1-9 without repetition
The sum of each row and column must equal 45
Each diagonal must contain unique numbers
The center square must always be 5
Which of the following best describes Generative AI?
AI systems that can generate new content based on patterns learned from training data
AI systems that can only classify existing content
AI systems that generate random outputs
AI systems that require human input for every decision
Which algorithm is best suited for computing a running average on a stream of data?
Batch processing
Full sorting
Sliding window
Recursive accumulation
In scheduling, what does the presence of a cycle in the dependency graph indicate?
A valid scheduling order
Optimal task sequencing
An error or unschedulable set of tasks
Redundant tasks
What is the output of the code snippet:
print(type(3/2))
int
float
string
complex
Which data structure is best suited for implementing an undo functionality in applications?
Queue
Stack
Dictionary
Set
Which of the following best defines a mathematical expression in the context of symbolic computation?
A sequence of random numbers
A combination of constants, variables, and operators representing a value
Only numerical constants
A method to solve equations
What is the purpose of an evaluation function in the context of expression trees?
To convert an expression into its tree representation
To compute the numerical value of the expression
To generate a random expression
To print the expression in infix notation
Which of the following conditions is necessary and sufficient for an undirected graph with n vertices to be a tree?
Graph is connected and has n edges
Graph is acyclic and has n edges
Graph is connected and has n - 1 edges
Graph is acyclic and has n - 1 edges
