Worksheets101 suraq. (by Dias Bolat)
Total questions: 101
Worksheet time: 51mins
. What is the output of the following code?
text = "Python"
for letter in text:
print(letter, end=",")
a) P,y,t,h,o,n
b) P y t h o n
c) Python
d) P,y,t,h,o,n
What does the `input()` function do in Python?
Prints output to the console
b) Reads input from the user
Imports modules
Performs mathematical operations
What is the result of the following code? ```python
x = 10
while x > 0:
print(x) x -= 2
a) 10 8 6 4 2
b) 10 9 8 7 6 5 4 3 2 1
) 10 8 6 4 2 0
d) 10 8 6 4
Which of the following is a correct Python syntax for an if statement?
if x = 5:
`if x == 5`
`if x equals 5:`
`if x == 5 then:`
`if x == 5:
In Python, the break statement can only be used within loops.
True
False
What does the `range(3, 10, 2)` function in Python represent?
A range from 3 to 10 with a step of 2
A range from 3 to 9 with a step of 2
) A range from 3 to 10 with a step of 1
A range from 3 to 11 with a step of 2
A range from 2 to 10 with a step of 3
Which of the following is the correct way to define a variable in Python?
variable_name = value
value = variable_name
var = value
value = var
) All of the above
What does the `print()` function do in Python?
Reads input from the user
Performs mathematical operations
Imports modules
) Prints output to the console
What is the output of the following code snippet?
```python
word = "Python"
print(word[0:3])
Py
) Pyt
pyt
) P
. The continue statement in Python can only be used within loops.
Which of the following is a correct way to declare a string variable in Python?
string_variable = "Hello"
string variable = "Hello"
stringVariable = "Hello"
) STRING_VARIABLE = "Hello"
. In Python, which loop is used when the number of iterations is known?
while loop
for loop
nested loop
do-while loop
What is the result of the following code?
```python
x = 10
while x > 0:
print(x) x -= 2
10 8 6 4 2
10 9 8 7 6 5 4 3 2 1
10 8 6 4 2 0
) 10 8 6 4
. Which of the following is a correct Python syntax for an if statement?
`if x = 5:`
`if x == 5`
`if x equals 5:`
`if x == 5 then:`
`if x == 5:
. In Python, a string can be enclosed in single quotes (`'`) or double quotes (`"`).
True
False
What does IDE stand for?
Integrated Design Environment
Integrated Development Environment
Intelligent Design Execution
Interface Development Environment
Which of the following is NOT a valid Python variable name?
my_variable
1st_variable
variable_one
_variable_two
What is the result of "Python"[::-1] in Python?
Python
nohtyP
n
Error
What is the result of `"hello" + "world"` in Python?
hello world
helloworld
hello+world
Error
. In Python, a variable name must start with a letter or an underscore.
True
False
Which of the following is NOT a comparison operator in Python?
==
!=
<>
>=
What will be the output of the following code?
```python
x = 5
if x > 10:
print("Greater than 10")
else:
print("Less than or equal to 10")
```
Greater than 10
Less than or equal to 10
Error
None
What does the `str()` function do in Python?
Converts a number to a string
Converts a string to a number
Checks if a string contains only alphabetic characters
Returns the length of a string
What is the result of `"Hello"[-1:]` in Python?
Hello
H
o
olleH
. The range() function in Python generates a list of integers starting from 1 by default.
True
False
What is the result of the expression `3 * "Python"` in Python?
PythonPythonPython
9
Error
none
What does the `break` statement do in Python?
Exits the loop completely
Skips the rest of the code block and moves to the next iteration of the loop
Allows the loop to continue to the next iteration
None of the above
What is the result of `"hello".upper()` in Python?
"hello"
"HELLO"
"Hello"
Error
. How many times will the following loop iterate?
```python
for i in range(5):
print(i)
```
4
5
6
0
The else statement can be used without an if statement in Python
True
False
. What will be the output of the following code?
list_1 = [1, 2, 3]
list_2 = list_1 * 2
print(list_2)2)
[1, 2, 3, 1, 2, 3]
[2, 4, 6]
[1, 1, 2, 2, 3, 3]
[1, 2, 3, 2, 4, 6]
What is the purpose of recursion in programming?
Repeating a process until a specific condition is met
Implementing iterative loops
Breaking down a problem into smaller, similar sub-problems
Generating random numbers
. In a 2D list in Python, how would you access the element in the second row and third column?
list[2][3]
list[1][2]
list[3][2]
list[1][3]
Which of the following operations can be performed on sets in Python?
Union
Intersection
Subtraction
All of the above
. What is the output of the following code snippet?
my_dict = {"apple": 2, "banana": 3, "orange": 1}
print(my_dict["banana"])
"banana"
KeyError
2
3
. What will be the output of the following code snippet?
my_list = [1, 2, 3, 4]
print(my_list[2])
Which of the following methods can be used to add an element at the end of a list in Python?
append()
add()
insert()
extend()
What is the purpose of a return statement in a function?
To halt the execution of the function
To print a value to the console
To return a value from the function
To define a recursive call
What is the base case in a recursive function?
The case where the function returns a value
The case where the function calls itself
The case where the function terminates without making a recursive call
The case where the function has no parameters
Which of the following methods can be used to initialize a 2D list in Python?
[[0]*n]*m
[[]]*n
[[]]*m
[[0]*m]*n
What will be the output of the following code snippet?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])
Which of the following statements about 2D lists in Python is true?
All rows in a 2D list must have the same length
2D lists cannot be passed as arguments to functions
2D lists cannot contain lists as elements
Accessing elements in a 2D list requires nested loops
What is a set in Python?
An ordered collection of elements
A collection of key-value pairs
A mutable collection of elements with duplicate values allowed
An unordered collection of unique elements
What is a tuple in Python?
An ordered collection of elements
An unordered collection of unique elements
A mutable collection of elements
An immutable collection of elements
How do you concatenate two tuples in Python?
Using the + operator
Using the concat() method
Using the extend() method
Using the append() method
.Which of the following methods can be used to add an element at the end of a list in Python?
append()
add()
insert()
extend()
What does the list.pop() method do in Python?
Adds an element to the end of the list
Removes and returns the last element from the list
Inserts an element at the specified position
Retrieves the index of a specified element
Consider the following code:
my_list = [1, 2, 3, 4]
del my_list[2]
print(my_list)
What will be printed?
[1, 2, 4]
[1, 2, 3]
[2, 3, 4]
[1, 3, 4]
Which of the following statements about Python lists is false?
Lists can contain elements of different data types
Lists are mutable
Lists can be nested
Lists are always sorted
Which of the following statements about recursion is true?
Recursion is always more efficient than iteration
Recursion involves a function calling itself
Recursion is limited to a fixed number of iterations
Recursion cannot be used in Python
What is the base case in a recursive function?
The case where the function returns a value
The case where the function calls itself
The case where the function terminates without making a recursive call
The case where the function has no parameters
How would you access the element in the second row and third column of a 2D list in Python?
list[2][3]
list[1][2]
list[3][2]
list[1][3]
How would you add a new row to a 2D list in Python?
Use the append() method with a list containing the new row
Use the insert() method with the index set to the desired row number
Use the extend() method with a list containing the new row
Use the add() method with a list containing the new row
How would you remove an element from a set in Python?
Using the remove() method
Using the pop() method
Using the discard() method
All of the above
.What will be the output of the following code snippet?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))
{1, 2, 3, 4, 5}
{3}
{1, 2, 4, 5}
{}
.What is the purpose of packing and unpacking tuples in Python?
Packing allows you to combine multiple values into a single tuple, while unpacking allows you to extract values from a tuple into separate variables
Packing allows you to create empty tuples, while unpacking allows you to remove elements from a tuple
Packing allows you to convert tuples to lists, while unpacking allows you to convert lists to tuples
Packing allows you to sort tuples, while unpacking allows you to reverse the order of elements in a tuple
.How would you access the last element of a tuple in Python?
By using negative indexing
By using positive indexing
By using the last() method
By converting the tuple to a list
What is the difference between a tuple and a list in Python?
Tuples are mutable, while lists are immutable
Tuples are ordered collections, while lists are unordered collections
Tuples can contain elements of different data types, while lists cannot
Tuples are immutable, while lists are mutable
How would you find the length of a tuple in Python?
Using the count() method
Using the size() function
Using the length() function
Using the len() function
What will be the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
print(2 in my_list)
True
False
[2]
2
Which of the following statements about Python lists is false?
Lists can contain elements of different data types
Lists are mutable
Lists can be nested
Lists are always sorted
What will be the output of the following code snippet?
my_list = [1, 2, 3, 4, 5]
print(my_list[2:4])
[1, 2]
[2, 3]
[3, 4]
[2, 4]
Which of the following methods can be used to remove an element from a list by its value?
remove()
delete()
pop()
erase()
What is the purpose of packing and unpacking tuples in Python?
Packing allows you to combine multiple values into a single tuple, while unpacking allows you to extract values from a tuple into separate variables
Packing allows you to create empty tuples, while unpacking allows you to remove elements from a tuple
Packing allows you to convert tuples to lists, while unpacking allows you to convert lists to tuples
Packing allows you to sort tuples, while unpacking allows you to reverse the order of elements in a tuple
.How would you reverse the elements of a list in Python?
Using the reverse() method
Using the invert() method
Using the flip() method
Using the revert() method
What is the output of the following recursive function?
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1) p
rint(factorial(4))
120
What is a set in Python?
An ordered collection of elements
A collection of key-value pairs
A mutable collection of elements with duplicate values allowed
An unordered collection of unique elements
How do you access the third element of a tuple in Python?
By using the index 3
By using the index -3
By using the index 2
By using the index -2
What is the primary difference between a set and a tuple in Python?
Sets are mutable, while tuples are immutable
Sets are ordered, while tuples are unordered
Sets can contain duplicate elements, while tuples cannot
Sets can only contain numerical elements, while tuples can contain any data type
.How do you iterate over all elements in a 2D list named matrix in Python?
Using a single for loop
Using nested for loops
Using the map() function
Using the enumerate() function
Which data structure in Python is suitable for storing a collection of elements with no duplicates and no specific order requirement?
List
Tuple
Set
Dictionary
.What is the purpose of the in and not in operators in Python?
They are used to check if a variable is assigned a value or not
they are used to check if a key exists in a dictionary
They are used to check if an element is present in a sequence (such as a list, tuple, or string
They are used to concatenate strings
Which of the following methods can be used to convert a tuple to a list in Python?
convert()
list()
to_list()
make_list()
Which keyword is used to define a function in Python?
func
define
def
function
How do you call (invoke) a function named my_function in Python?
invoke my_function()
call my_function()
my_function()
execute my_function()
Consider the following code:
my_list = [1, 2, 3, 4]
del my_list[2]
print(my_list)
What will be printed?
[1, 2, 4]
[1, 2, 3]
[2, 3, 4]
[1, 3, 4]
What is the purpose of recursion in programming?
Repeating a process until a specific condition is met
Implementing iterative loops
Breaking down a problem into smaller, similar sub-problems
Generating random numbers
Which of the following statements about recursion is true?
Recursion is always more efficient than iteration
Recursion involves a function calling itself
Recursion is limited to a fixed number of iterations
Recursion cannot be used in Python
How would you reverse the elements of a list in Python?
Using the reverse() method
Using the invert() method
Using the flip() method
Using the revert() method
How do you access the third element of a tuple in Python?
By using the index 3
) By using the index -3
By using the index 2
By using the index -2
What is the primary difference between a set and a tuple in Python?
Sets are mutable, while tuples are immutable
Sets are ordered, while tuples are unordered
) Sets can contain duplicate elements, while tuples cannot
Sets can only contain numerical elements, while tuples can contain any data type
How would you add a new row to a 2D list in Python?
Use the append() method with a list containing the new row
Use the insert() method with the index set to the desired row number
Use the extend() method with a list containing the new row
Use the add() method with a list containing the new row
How would you access the element in the second row and third column of a 2D list in Python?
list[2][3]
list[1][2]
) list[3][2]
list[1][3]
What will be the output of the following code snippet?
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))
{1, 2, 3, 4, 5}
{3}
{1, 2, 4, 5}
{}
What will be the output of the following code snippet?
my_list = [1, 2, 3, 4]
print(my_list[2])
3
2
4
1
What is a set in Python?
An ordered collection of elements
A collection of key-value pairs
A mutable collection of elements with duplicate values allowed
An unordered collection of unique elements
What is the base case in a recursive function?
The case where the function returns a value
The case where the function calls itself
The case where the function terminates without making a recursive call
The case where the function has no parameters
How would you find the length of a tuple in Python?
Using the count() method
Using the size() function
Using the length() function
Using the len() function
Which of the following statements about Python lists is false?
Lists can contain elements of different data types
Lists are mutable
Lists can be nested
Lists are always sorted
Which data structure in Python is suitable for storing a collection of elements with no duplicates and no specific order requirement?
List
Tuple
set
Dictionary
What is the difference between a tuple and a list in Python?
Tuples are mutable, while lists are immutable
Tuples are ordered collections, while lists are unordered collections
Tuples can contain elements of different data types, while lists cannot
Tuples are immutable, while lists are mutable
.What is the purpose of the in and not in operators in Python?
They are used to check if a variable is assigned a value or not
They are used to check if a key exists in a dictionary
They are used to check if an element is present in a sequence (such as a list, tuple, or string)
They are used to concatenate strings
What will be the output of the following code?
list_1 = [1, 2, 3]
list_2 = list_1 * 2
print(list_2)
[1, 2, 3, 1, 2, 3]
[2, 4, 6]
[1, 1, 2, 2, 3, 3]
[1, 2, 3, 2, 4, 6]
What is the purpose of packing and unpacking tuples in Python?
Packing allows you to combine multiple values into a single tuple, while unpacking allows you to extract values from a tuple into separate variables
Packing allows you to create empty tuples, while unpacking allows you to remove elements from a tuple
Packing allows you to convert tuples to lists, while unpacking allows you to convert lists to tuples
Packing allows you to sort tuples, while unpacking allows you to reverse the order of elements in a tuple
Which of the following operations can be performed on sets in Python?
Union
Intersection
Subtraction
All of the above
What will be the output of the following code snippet?
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[1][2])
1
2
3
6
How do you iterate over all elements in a 2D list named matrix in Python?
Using a single for loop
Using nested for loops
Using the map() function
Using the enumerate() function
What does the list.pop() method do in Python?
Adds an element to the end of the list
Removes and returns the last element from the list
Inserts an element at the specified position
Retrieves the index of a specified element
What is the purpose of a return statement in a function?
To halt the execution of the function
To print a value to the console
To return a value from the function
To define a recursive call
How do you concatenate two tuples in Python?
Using the + operator
Using the concat() method
Using the extend() method
Using the append() method
Which of the following statements about 2D lists in Python is true?
All rows in a 2D list must have the same length
2D lists cannot be passed as arguments to functions
2D lists cannot contain lists as elements
Accessing elements in a 2D list requires nested loops
