Font size
WorksheetsPractice Quiz
Total questions: 60
Worksheet time: 34mins
Which of the following commands will return the length of the list mylist?
length(mylist)
len(mylist)
mylist.length()
mylist.len()
Which of the following is the correct way to add the number 4 to mylist?
mylist + 4
mylist.append(4)
mylist.append([4])
4.append(mylist)
What will be the output?
names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1:
print(1)
else: print(2)
None
1
2
error
What is the output of the following?
x = ['ab', 'cd']
for i in x:
i.upper()
print(x)
[‘ab’, ‘cd’].
[‘AB’, ‘CD’].
None of the above
[None, None]
In programming, lists are often known as...
Arrays
Elements
Indexes
Data
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
a
w
x
see the program and fill in the blank with correct statement.
(a)
data = "No Way!" The expression len(data) evaluates to:
7
9
8
6
word = "amazing"
For the given string if we run word[2:5] what does it mean?
It will extract alphabets from index 2 to index 4
It will extract alphabets from index 2 to index 5
It will extract alphabets from position 2 to position 4
It will extract alphabets from position 2 to position 5
fruit1="kiwi"
fruit2="strawberry"
print(len(fruit1))
print(len(fruit2))
kiwi and strawberry
4
10
4 10
410
word=”amazing”
word[-7:-3]
(a)
y=str(123)
x=”hello”*3
print(x,y)
(a)
What is printed?
cheer = "Go Eagles!"
print cheer[7:]
nothing - error
s!
es!
Go Eagles
What is printed?
cheer = "Go Eagles!"
print cheer[:5]
Go Eag
Go Ea
Go E
Go
What is missing in the square braces?
cheer = "Go Eagles!"
print cheer[ ]
If the output is Eagle
[4:9]
[2:8]
[3:7]
[3:8]
What will be the output of the following code? python Copy def add(a, b): return a + b result = add(5, 3) print(result)
8
5
3
Error
What will the following code output? python Copy def multiply(x, y): return x * y result = multiply(4, 3) print(result)
7
12
3
Error
What will be the output of the following code? python Copy x = 10 def func(): x = 5 return x print(func()) print(x)
5 10
10 5
5 5
10 10
What is the output of the following code? python Copy def func(a, b=3): return a * b result = func(4) print(result)
12
7
4
Error
Which of the following statements about recursion is true? python Copy def count_down(n): if n == 0: return else: print(n) count_down(n-1)
Recursion will never terminate
Recursion is only valid for integer inputs
The function will print numbers from n down to 1
Recursion terminates before printing
What does this code output? python Copy def test(x): return x + 2 x = test(10) print(x)
10
12
2
Error
What will the following code print? python Copy def func(a, b): return a / b result = func(10, 2) print(result)
12
5
20
Error
What is the output of this function call? python Copy def calculate(x, y=3): return x + y result = calculate(2, 5) print(result)
7
5
8
Error
Which of the following correctly defines a function to calculate the square of a number?
def square(x): return x * x
def square(x) return x * x
def square(x): x * x
def square(x): return x^2
What will this code print? python Copy def calculate(x): return x ** 2 x = 3 print(calculate(x))
9
6
8
3
What is the output of the following code? python Copy def func(x, y): return x + y print(func(3, 4))
7
12
34
Error
Which of the following will give a syntax error?
def foo(x): return x + 1
def foo(x, y): return x + y
def foo(x): return x * 2
def foo(x, y,): return x * y
What is the output of the following code? python Copy x = 5 def func(): global x x = 10 func() print(x)
5
10
Error
None
What will be the result of calling sum_of_squares(4) for the following function? python Copy def sum_of_squares(n): if n == 1: return 1 else: return n**2 + sum_of_squares(n-1)
10
30
15
30
Which of the following is the correct function definition?
def my_func{x}: print(x)
def my_func(x): print(x)
def my_func(x) {print(x)}
def my_func(x)-> return x
What is the output of the following code? python Copy def func(a, b=3, c=2): return a + b + c print(func(1, c=4))
8
7
6
9
What is the output of this function call? python Copy def check(x): if x > 10: return "Greater" else: return "Smaller" result = check(8) print(result)
Greater
Smaller
Error
None
What does this function do? python Copy def power(a, b): return a ** b
Calculates the square of a
Adds two numbers
Raises a to the power of b
Multiplies a and b
What is the purpose of the return statement in a function?
It terminates the function
It specifies the function's output
It specifies the function's input
It is used for error handling
What is a global variable?
A variable declared inside a function
A variable declared outside all functions, accessible anywhere
A variable that cannot be modified
A variable only accessible to a specific function
What is the scope of a variable defined within a function?
Global scope
Local scope
Both global and local
None of the above
Which of these is a valid function signature?
def func(x y):
def func(x, y) ->:
def func(x, y):
def func(x, y;):
Which of the following is true for default parameters in Python?
Default parameters must always be the first in the function definition
Default parameters cannot be changed
Default parameters are optional when calling a function
Default parameters are not supported in Python
What happens if you don't use a return statement in a function?
The function will return None by default
The function will raise an error
The function will execute but not perform any action
The function will return an empty string
What is a function parameter?
A value returned by the function
A value passed into the function for processing
A variable defined inside the function
A function that returns a value
What does the following function definition mean? def func(x, y=2): return x + y
It defines a function that requires two parameters, x and y
It defines a function with a default value of 2 for y
It defines a function that can only take one argument
It defines a function that returns the value of y
Which statement is true about global variables?
Global variables cannot be accessed inside functions
Global variables can be modified inside functions using the global keyword
Global variables must be passed to the function explicitly
Global variables are always private to a function
Which type of function does not return a value?
A function with no return statement
A function that returns a string
A recursive function
A function that takes multiple parameters
How do you handle files in Python?
Using the open() function
Using the file() function
Using the read() function
Using the get() function
Which of these is used to read a file line by line?
file.read()
file.readlines()
file.write()
file.readfile()
What is the os module used for?
Handling file I/O operations
Manipulating text strings
Managing file paths and directories
Handling exceptions
Which method is used to write to a file in Python?
write()
read()
writefile()
append()
Which of these represents a valid file path in Python?
"C:\Users\Documents\file.txt"
"C:/Users/Documents/file.txt"
"/Users/Documents\file.txt"
"C://Users/Documents/file.txt"
Which function is used to close a file after opening it?
close()
end()
stop()
finish()
What does the with open() statement do in Python?
It creates a new file
It opens a file and automatically closes it when done
It opens a file in read mode
It is used to check for file existence
What is the default mode for opening a file?
'r'
'w'
'rb'
'a'
Which of the following is used to get the absolute path of a file?
os.path.abspath()
os.path.exists()
os.path.getsize()
os.getcwd()
How would you append data to a file?
Open the file in write mode 'w'
Open the file in append mode 'a'
Open the file in read mode 'r'
Open the file in binary mode
What is the purpose of using 'r' mode when opening a file?
To read data from the file
To write data to the file
To append data to the file
To create a new file
Which of the following is correct when handling files in Python?
Files need to be opened before they can be used
Files can only be opened in write mode
Files are automatically saved after closing
Files cannot be read and written simultaneously
Which function is used to get the current working directory in Python?
os.getcwd()
os.getcwd()
os.path()
os.path.getcwd()
How would you read the entire contents of a file into a string?
file.read()
file.readlines()
file.readlines()
file.content()
What will be the output of the following code? python Copy x = [1, 2, 3] def modify_list(lst): lst.append(4) modify_list(x) print(x)
[4]
[1, 2, 3, 4]
Error
[1, 2, 3]
What does the following code return? python Copy result = [x**2 for x in range(5)] print(result)
[0, 1, 2, 3, 4, 5]
[0, 1, 4, 9, 16]
[1, 4, 9, 16, 25]
[0, 1, 2, 3, 4]
What is the output of the following code? python Copy for i in range(3): print(i) else: print('Done')
Done
0 1 2 Done
0 1 2
Error
