wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Practice Quiz

Total questions: 60

Worksheet time: 34mins

Name
Class
Date
1.

Which of the following commands will return the length of the list mylist?

a)

length(mylist)

b)

len(mylist)

c)

mylist.length()

d)

mylist.len()

2.

Which of the following is the correct way to add the number 4 to mylist?

a)

mylist + 4

b)

mylist.append(4)

c)

mylist.append([4])

d)

4.append(mylist)

3.

What will be the output?

names1 = ['Amir', 'Bala', 'Chales']

if 'amir' in names1:

print(1)

else: print(2)

a)

None

b)

1

c)

2

d)

error

4.

What is the output of the following?

x = ['ab', 'cd']

for i in x:

i.upper()

print(x)

a)

[‘ab’, ‘cd’].

b)

[‘AB’, ‘CD’].

c)

None of the above

d)

[None, None]

5.

In programming, lists are often known as...

a)

Arrays

b)

Elements

c)

Indexes

d)

Data

6.

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","__")

a)

r

b)

a

c)

w

d)

x

7.

see the program and fill in the blank with correct statement.

(a)  

8.

data = "No Way!" The expression len(data) evaluates to:

a)

7

b)

9

c)

8

d)

6

9.

word = "amazing"

For the given string if we run word[2:5] what does it mean?

a)

It will extract alphabets from index 2 to index 4

b)

It will extract alphabets from index 2 to index 5

c)

It will extract alphabets from position 2 to position 4

d)

It will extract alphabets from position 2 to position 5

10.

fruit1="kiwi"

fruit2="strawberry"

print(len(fruit1))

print(len(fruit2))

a)

kiwi and strawberry

b)

4

10

c)

4 10

d)

410

11.

word=”amazing”

word[-7:-3]

(a)  

12.

y=str(123)

x=”hello”*3

print(x,y)

(a)  

13.

What is printed?
cheer = "Go Eagles!"
print cheer[7:]

a)

nothing - error

b)

s!

c)

es!

d)

Go Eagles

14.

What is printed?
cheer = "Go Eagles!"
print cheer[:5]

a)

Go Eag

b)

Go Ea

c)

Go E

d)

Go

15.

What is missing in the square braces?
cheer = "Go Eagles!"
print cheer[   ]
If the output is Eagle

a)

[4:9]

b)

[2:8]

c)

[3:7]

d)

[3:8]

16.

What will be the output of the following code? python Copy def add(a, b): return a + b result = add(5, 3) print(result)

a)

8

b)

5

c)

3

d)

Error

17.

What will the following code output? python Copy def multiply(x, y): return x * y result = multiply(4, 3) print(result)

a)

7

b)

12

c)

3

d)

Error

18.

What will be the output of the following code? python Copy x = 10 def func(): x = 5 return x print(func()) print(x)

a)

5 10

b)

10 5

c)

5 5

d)

10 10

19.

What is the output of the following code? python Copy def func(a, b=3): return a * b result = func(4) print(result)

a)

12

b)

7

c)

4

d)

Error

20.

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)

a)

Recursion will never terminate

b)

Recursion is only valid for integer inputs

c)

The function will print numbers from n down to 1

d)

Recursion terminates before printing

21.

What does this code output? python Copy def test(x): return x + 2 x = test(10) print(x)

a)

10

b)

12

c)

2

d)

Error

22.

What will the following code print? python Copy def func(a, b): return a / b result = func(10, 2) print(result)

a)

12

b)

5

c)

20

d)

Error

23.

What is the output of this function call? python Copy def calculate(x, y=3): return x + y result = calculate(2, 5) print(result)

a)

7

b)

5

c)

8

d)

Error

24.

Which of the following correctly defines a function to calculate the square of a number?

a)

def square(x): return x * x

b)

def square(x) return x * x

c)

def square(x): x * x

d)

def square(x): return x^2

25.

What will this code print? python Copy def calculate(x): return x ** 2 x = 3 print(calculate(x))

a)

9

b)

6

c)

8

d)

3

26.

What is the output of the following code? python Copy def func(x, y): return x + y print(func(3, 4))

a)

7

b)

12

c)

34

d)

Error

27.

Which of the following will give a syntax error?

a)

def foo(x): return x + 1

b)

def foo(x, y): return x + y

c)

def foo(x): return x * 2

d)

def foo(x, y,): return x * y

28.

What is the output of the following code? python Copy x = 5 def func(): global x x = 10 func() print(x)

a)

5

b)

10

c)

Error

d)

None

29.

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)

a)

10

b)

30

c)

15

d)

30

30.

Which of the following is the correct function definition?

a)

def my_func{x}: print(x)

b)

def my_func(x): print(x)

c)

def my_func(x) {print(x)}

d)

def my_func(x)-> return x

31.

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))

a)

8

b)

7

c)

6

d)

9

32.

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)

a)

Greater

b)

Smaller

c)

Error

d)

None

33.

What does this function do? python Copy def power(a, b): return a ** b

a)

Calculates the square of a

b)

Adds two numbers

c)

Raises a to the power of b

d)

Multiplies a and b

34.

What is the purpose of the return statement in a function?

a)

It terminates the function

b)

It specifies the function's output

c)

It specifies the function's input

d)

It is used for error handling

35.

What is a global variable?

a)

A variable declared inside a function

b)

A variable declared outside all functions, accessible anywhere

c)

A variable that cannot be modified

d)

A variable only accessible to a specific function

36.

What is the scope of a variable defined within a function?

a)

Global scope

b)

Local scope

c)

Both global and local

d)

None of the above

37.

Which of these is a valid function signature?

a)

def func(x y):

b)

def func(x, y) ->:

c)

def func(x, y):

d)

def func(x, y;):

38.

Which of the following is true for default parameters in Python?

a)

Default parameters must always be the first in the function definition

b)

Default parameters cannot be changed

c)

Default parameters are optional when calling a function

d)

Default parameters are not supported in Python

39.

What happens if you don't use a return statement in a function?

a)

The function will return None by default

b)

The function will raise an error

c)

The function will execute but not perform any action

d)

The function will return an empty string

40.

What is a function parameter?

a)

A value returned by the function

b)

A value passed into the function for processing

c)

A variable defined inside the function

d)

A function that returns a value

41.

What does the following function definition mean? def func(x, y=2): return x + y

a)

It defines a function that requires two parameters, x and y

b)

It defines a function with a default value of 2 for y

c)

It defines a function that can only take one argument

d)

It defines a function that returns the value of y

42.

Which statement is true about global variables?

a)

Global variables cannot be accessed inside functions

b)

Global variables can be modified inside functions using the global keyword

c)

Global variables must be passed to the function explicitly

d)

Global variables are always private to a function

43.

Which type of function does not return a value?

a)

A function with no return statement

b)

A function that returns a string

c)

A recursive function

d)

A function that takes multiple parameters

44.

How do you handle files in Python?

a)

Using the open() function

b)

Using the file() function

c)

Using the read() function

d)

Using the get() function

45.

Which of these is used to read a file line by line?

a)

file.read()

b)

file.readlines()

c)

file.write()

d)

file.readfile()

46.

What is the os module used for?

a)

Handling file I/O operations

b)

Manipulating text strings

c)

Managing file paths and directories

d)

Handling exceptions

47.

Which method is used to write to a file in Python?

a)

write()

b)

read()

c)

writefile()

d)

append()

48.

Which of these represents a valid file path in Python?

a)

"C:\Users\Documents\file.txt"

b)

"C:/Users/Documents/file.txt"

c)

"/Users/Documents\file.txt"

d)

"C://Users/Documents/file.txt"

49.

Which function is used to close a file after opening it?

a)

close()

b)

end()

c)

stop()

d)

finish()

50.

What does the with open() statement do in Python?

a)

It creates a new file

b)

It opens a file and automatically closes it when done

c)

It opens a file in read mode

d)

It is used to check for file existence

51.

What is the default mode for opening a file?

a)

'r'

b)

'w'

c)

'rb'

d)

'a'

52.

Which of the following is used to get the absolute path of a file?

a)

os.path.abspath()

b)

os.path.exists()

c)

os.path.getsize()

d)

os.getcwd()

53.

How would you append data to a file?

a)

Open the file in write mode 'w'

b)

Open the file in append mode 'a'

c)

Open the file in read mode 'r'

d)

Open the file in binary mode

54.

What is the purpose of using 'r' mode when opening a file?

a)

To read data from the file

b)

To write data to the file

c)

To append data to the file

d)

To create a new file

55.

Which of the following is correct when handling files in Python?

a)

Files need to be opened before they can be used

b)

Files can only be opened in write mode

c)

Files are automatically saved after closing

d)

Files cannot be read and written simultaneously

56.

Which function is used to get the current working directory in Python?

a)

os.getcwd()

b)

os.getcwd()

c)

os.path()

d)

os.path.getcwd()

57.

How would you read the entire contents of a file into a string?

a)

file.read()

b)

file.readlines()

c)

file.readlines()

d)

file.content()

58.

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)

a)

[4]

b)

[1, 2, 3, 4]

c)

Error

d)

[1, 2, 3]

59.

What does the following code return? python Copy result = [x**2 for x in range(5)] print(result)

a)

[0, 1, 2, 3, 4, 5]

b)

[0, 1, 4, 9, 16]

c)

[1, 4, 9, 16, 25]

d)

[0, 1, 2, 3, 4]

60.

What is the output of the following code? python Copy for i in range(3): print(i) else: print('Done')

a)

Done

b)

0 1 2 Done

c)

0 1 2

d)

Error