stud
bud
Sign up
Log in
Home
Library
Explore
SAT
More
W24 - TRAFALGAR - Final exam review Flashcards | StudBud
Explore
/
W24 - TRAFALGAR - Final exam review
W24 - TRAFALGAR - Final exam review
70 terms
·
StudBud Administration
·
149 plays
Kick of your course with a quiz that can help set the tone and expectations for the coming semester.
Flashcards
70 cards
F
Practice quiz
P
Host live
L
Questions
70
Answers
1
What is the correct way to close a file object named file in Python?
file.close()
file.closeFile()
close_file(file)
close(file)
2
How do you add a new key-value pair to a dictionary in Python?
By using the insert() method
By using the add() method
By using square brackets [new_key] = value
By using the append() method
3
Which keyword is used to define a class in Python?
def
class
struct
obj
4
What happens if you try to access a key that does not exist in a dictionary?
Python returns None.
Python returns an empty dictionary.
Python raises an error.
Python creates a new key-value pair with the provided key.
5
What does the self keyword represent in Python class methods?
It refers to the current class instance
It is used for typecasting within the method
It is used for conditional statements within the method
It is a reserved keyword for private variables
6
How do you create a tuple with a single element in Python?
(item)
[item]
(item,)
item,
7
What is the process of creating an instance of a class called in Python?
Inheritance
Instantiation
Definition
Execution
8
What is the syntax for a for loop in Python?
for variable from sequence:
for variable in sequence:
for (condition):
for condition:
9
How do you access the attributes or methods of an object in Python?
Using dot notation
Using square brackets
Using parentheses
Using curly braces
10
How does the binary search algorithm work?
It halves the list recursively comparing the middle element with the target
Repeatedly compares adjacent elements, swapping them when out of order.
it compares adjacent elements and swaps them if they are in the wrong order
It randomly rearranges elements until the list is sorted
11
What is the bubble sort algorithm used for?
Creating permutations of elements
Sorting elements in ascending order
Removing duplicates from a list
Searching for elements in a list
12
What is the primary purpose of using nested loops?
To avoid using conditional statements
To perform different iterations over different sequences
To iterate over nested data structures like lists of lists
To perform multiple iterations over the same sequence
13
Which of the following statements about getters and setters is true?
They provide a way to encapsulate class attributes
They can only be defined within built-in classes
They are not commonly used in Python
They are automatically generated by Python for every class
14
How does the bubble sort algorithm work?
Repeatedly compares adjacent elements, swapping them when out of order.
It recursively sorts the list by selecting the smallest element each time
It randomly rearranges elements until the list is sorted
It divides the list into smaller sublists and sorts each sublist separately
15
Which of the following is true regarding the break statement?
It can only be used inside a for loop.
It can be used in both for and while loops.
It can only be used inside a function.
It can only be used inside a while loop.
16
Which of the following is a correct syntax to create an instance of a class named Car with attributes color and model?
car = Car(color, model)
car = create Car(color, model)
car = new Car(color, model)
car = Car(color=color, model=model)
17
What is the syntax for a while loop in Python?
while loop_condition:
while (loop_condition):
while {loop_condition}:
while [loop_condition ]:
18
Is __init__ always required to be written?
True
False
19
What happens to a variable passed to a function by value in Python?
The variable's value is directly modified within the function
The function receives a reference to the variable's memory location
The function receives a copy of the variable's value
The variable's memory location is modified within the function
20
What happens if you try to modify a global variable inside a function without explicitly declaring it as global?
Python raises a syntax error
The global variable remains unchanged
Python creates a new local variable with the same name
Python automatically promotes the variable to a global scope
21
Which of the following methods is used to read the entire contents of a file in Python at once?
All of the above
readlines()
read()
readline()
22
Which data type can be used as a value in a dictionary?
Lists
Dictionaries
All of the above
Tuples
23
In Python, how do you typically define a getter method for a class attribute "name"?
def name(self):
def get_name(self):
def get_name():
def name():
24
How do you close a file object in Python after you're done with it?
Files are automatically closed when the script finishes executing
Using the end() method
Using the exit() method
Using the close() method
25
What is a module in Python?
A data type used for storing text
A named block of code that performs a specific task
A file containing Python code that can be reused in other Python files
A built-in function for mathematical operations
26
What is a dictionary in Python?
A data structure used for sorting elements in ascending order
A type of loop used for iteration
A built-in function for mathematical operations
A collection of elements presented as key-pair
27
How do you terminate a loop prematurely in Python?
By using the exit statement
By using the break statement
By using the terminate statement
By using the stop statement
28
Which of the following is true about the while loop in Python?
It repeats until a certain condition evaluates to False
It is more suitable for iterating over a fixed number of elements
It always executes its body at least once
It is less efficient compared to the for loop
29
Which variable takes precedence if there is a local variable and a global variable with the same name in Python?
The global variable
The local variable
It depends on the scope of the function where the variable is accessed
Python raises an error
30
What is the primary requirement for using the binary search algorithm?
The list must contain only unique elements
The list must be unsorted
The list must be sorted
The list must be of fixed length
31
How do you import a module named my_module in Python?
import my_module
use my_module
module my_module
load my_module
32
What is the purpose of the range() function in a for loop?
To specify the starting and ending points of the loop
To define the step size for the loop
To generate a list of numbers
To generate a sequence of numbers that can be iterated over
33
What is the main purpose of parameters in a function definition?
To specify the return type of the function
To define local variables within the function
To provide input values to the function when it is called
To specify the function's name
34
Can a dictionary have multiple values for the same key?
Yes, but it requires special handling.
No, each key can only have one associated value.
Only if the values are of different data types.
Yes, dictionaries can store multiple values for the same key by default.
35
When should you use global variables in Python?
When you need to share data across multiple functions or modules
When you want to avoid potential naming conflicts
When you want to limit the visibility of variables within a function
When you want to improve code encapsulation and maintainability
36
What function is used to open a file in Python?
file_open()
fopen()
open()
read()
37
How do you create an empty tuple in Python?
empty_tuple = ()
empty_tuple = {}
empty_tuple = None
empty_tuple = []
38
What is the purpose of the continue statement in Python?
Skip the rest of the code block and continue the next iteration of the loop
To terminate the loop prematurely
To restart the loop from the beginning
To exit the loop completely
39
How do you add a new key-value pair to an existing dictionary?
By using parentheses and assigning a value to the new key.
By using the add() method.
By using square brackets and assigning a value to the new key.
By using the insert() method.
40
In Python, how are primitive data types (e.g., integers, floats) passed to functions?
It depends on the size of the data type
By reference
By value
It depends on the function signature
41
Which of the following data types are passed by reference in Python?
Tuples
List
Integers
Strings
42
Which of the following is true about dictionary values in Python?
Values can be mutable objects like lists
Values must be unique within the dictionary
Values can be accessed using integer indices
All of the above
43
what is the purpose of the __init__ method within a class?
To define class methods
To delete class instances
To initialize the class variables
To check the class inheritance
44
Which function is used to write data to a file in Python?
output()
write()
fwrite()
print()
45
What does the "import" keyword do in Python?
Makes functions and variables from one module available in the current file
Executes the code inside the specified module
Loads the module into memory
Defines a new module
46
Which is the correct syntax for using the with open statement to open a file named "example.txt" in read mode?
with open(file="example.txt", mode="r"):
with open("example.txt", mode="r") as file:
with open("example.txt", "r") as file:
with open("example.txt", "read") as file:
47
What happens if you try to modify an element within a tuple?
You can modify elements within a tuple without any error.
Tuples automatically convert to lists for modification.
Modifying a tuple requires using the modify() function.
Python raises an error since tuples are immutable.
48
What is the default mode for opening a file in Python's open() function if no mode is specified?
write
all of them
read
append
49
What is the binary search algorithm used for?
Creating permutations of elements
Sorting elements in ascending order
Finding the median of a list
Searching for a target value in a sorted list
50
How do you open a file named "example.txt" in Python for reading?
file_open("example.txt", "r")
open("example.txt", "r")
open("example.txt", "read")
open_file("example.txt", "read")
51
Which of the following statements about tuples in Python is true?
Tuples are created using square brackets [].
Tuples are ordered collections of elements, similar to lists.
Tuples are mutable, meaning their elements can be changed after creation.
Tuples can only contain 1 data type
52
How do you call a function named my_function in Python?
function my_function()
call my_function()
my_function()
execute my_function()
53
Which of the following is the correct way to access elements in a tuple?
By using parentheses and the index of the element.
By using curly braces and the index of the element.
By using the get() method.
By using square brackets and the index of the element.
54
Which of the following is a characteristic of a getter method?
It modifies the value of a class attribute
It deletes a class attribute
It is used to initialize class attributes
It returns the value of a class attribute
55
How do you append data to an existing file in Python?
Open the file in write mode and use write()
Open the file in read mode and use write()
Open the file in append mode and use write()
Use append() function
56
What is a tuple in Python?
A data structure similar to a list but immutable
A collection of key-value pairs
A collection with ( )
A collection with { }
57
What is the primary advantage of using the with open statement over explicitly opening and closing files?
It allows for more control over file operations
It provides better error handling
It simplifies file access syntax
It ensures that files are always closed, even if exceptions occur
58
What is a local variable?
A variable defined outside of any function
A variable defined within a function
A variable with a global scope
A variable accessible from anywhere in the code
59
What is the purpose of the with open statement in Python?
To create a new file
To ensure proper handling closing the file when done
To delete a file
To open a file and return its contents
60
What is the primary advantage of using modules in Python?
They allow for code reuse and organization
They speed up the execution of Python programs
They improve code readability
They automatically handle memory management
61
Which of the following is true about function arguments in Python?
They are required and must be specified in function definitions
They can only be of a specific data type
They are optional and can be omitted in function calls
They provide values to parameters when calling a function
62
How do you access elements in a tuple in Python?
By using square brackets [index]
By using curly braces {index}
By using parentheses (index)
By using angle brackets <index>
63
What is a return statement used for in a function?
To define the function's name
To exit the function and return a value to the caller
To specify the function's parameters
To print output to the console
64
How do you access values in a dictionary in Python?
By using the get() method
By using angle brackets <key>
By using square brackets [key]
By using parentheses (key)
65
What is the result of this code?
[(1, 2, 3), (4, 5, 6)]
(1, 2, 3, 4, 5, 6)
(1, 2, 3) + (4, 5, 6)
(1, 4, 2, 5, 3, 6)
66
Which of the following statements best describes the continue statement?
It terminates the loop entirely.
It restarts the loop from the beginning.
It has no effect on loops.
It skips the current iteration and proceeds to the next iteration.
67
What is a function in Python?
A named block of code that performs a specific task
A built-in module for mathematical operations
A keyword used for defining loops
A data type used for storing text
68
What happens to a mutable object passed to a function by reference in Python?
The object's value is directly modified within the function
The function receives a reference to the object's memory location
The function receives a copy of the object's value
The object's memory location is modified within the function
69
What is the primary advantage of using a while loop over a for loop in Python?
It is more suitable for iterating over a fixed range of values
It allows for more complex iteration conditions
It is easier to understand and implement
It provides better performance
70
What is the purpose of a default parameter in a function definition?
Provide a default value if no argument is passed when calling the function
To define the function's name
To specify the function's parameters
To specify the function's return type