wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python KP 1-2

Total questions: 120

Worksheet time: 1hrs 6mins

Name
Class
Date
1.

Which is the special symbol used in python to add comments?

a)

/*...*/

b)

#

c)

//

d)

$

2.

Please select the valid single line comment in Python.

a)

# comment

b)

"comment"

c)

'comment'

d)

/* comment */

3.

Which of the following statements are true?

a)

A compiler scans the entire source code file and translates the whole of it into machine code at once

b)

Compiled code tends to execute faster than interpreted code

c)

An interpreter translates one statement of the source code at a time into machine code

d)

None of the above

4.

A data structure that can't be changed after its creation is known as _____?

a)

immutable

b)

mutable

c)

duck-typed

d)

strongly-typed

5.

How to output the string "Hello" in Python?

a)

System.out("Hello")

b)

printf("Hello")

c)

print("Hello")

d)

echo("Hello")

6.

Which one of the following is the correct extension of the Python file?

a)

.p

b)

.pyt

c)

.python

d)

.py

7.

Identify the Python docstring comment.

a)

Comment 1

b)

Comment 2

c)

Comment 3

d)

Comment 4

8.

Which of the following characters is used for single-line comments in Python?

a)

!

b)

#

c)

/*

d)


//

9.

An operation that modifies a variable without making a copy is known as ....

a)

in-place operation

b)

static typed operation

c)


mutability operation

d)


constant time operation

10.

Identify the Python comment.

a)


Comment 1

b)

Comment 2

c)

Comment 3

d)

Comment 4

11.

Which of the following lines represents a valid comment?

a)


print /*('Hello World')*/

b)

fibbonacci_code = //[1, 1, 2, 3, 5, 8]

c)

x,y, z, number = 10, 15, 16, 21 "" More Content ""

d)

# print('Content')

12.

Which of the following statements are true?

a)

Python code is structured and indented blocks must be used

b)

Python is insensitive to case

c)

The ! character is used to denote a comment in Python

d)

All of the above

13.

Which one of the following is the correct extension of the Python file?

a)

.py

b)

.python

c)

.p

d)


None of these

14.

A data structure where the type or the class is less important than the methods it defines is known as what?

a)

immutable

b)

strongly-typed

c)

mutable

d)

duck-typed

15.

Which of the following is used to define a block of code in Python?

a)

Indentation

b)

Brackets

c)

Key

d)

All the above

16.

Whats is the proper shebang to indicate the script is using Python version 3?

a)

#!/usr/bin/env python3

b)

#!/usr/bin/env python

c)

!#/usr/bin/env python3

d)

!#/usr/bin/env python

17.

What is the value of the variable 'c' in the following code snippet?

a)

True

b)

3

c)

False

d)

0

18.

What is the value returned from this code?

a)

<type ‘bool’>

b)

Undefined

c)

<class 'int'>

d)

<type ‘str’>

19.

What is the output of the following code?

a)

1, 2

b)

2, 1

c)

2, 2

d)

1, 1

20.

What is the value of the variable 'c' in the following code snippet?

a)

11

b)

3

c)

10

d)

8

21.

What is the value of the variable 'c' in the following code snippet?

a)

26

b)

49

c)

28

d)

12

22.

What is the value of the variable 'c' in the following code snippet?

a)

1

b)

2

c)

3

d)

4

23.

What is the output of the following code?

a)

0

b)

10

c)

20

d)

None of these

24.

What is the output of the following code?

a)

1.0

b)

10

c)

20

d)

None of these

25.

What is the value of the variable j after the following code is executed?

a)

35

b)

55

c)

45

d)

75

26.

Which of the following keywords is able to turn a True into a False, and is also known as a 'logical inverter'.

a)

apostrophe

b)

change

c)

invert

d)

not

27.

What is the output of the following code?

a)

10

b)

100

c)

1000

d)

None of these

28.

Write a function that accepts any arbitrary argument (value) and returns True if the argument is a tuple, otherwise it should return False.

```

Example:

Input: 6

Output: False

Input: [6, 7, 9]

Output: False

Input: (6, 2)

Output: True

```

a)

WIP

b)

WIP

29.

What is the type of the variable c in the code snippet?

a)

Floating point

b)

Integer

c)

Decimal

d)

This will raise an error.

30.

What is the purpose of the following code?

a)

Convert b to a string

b)

Trap an invalid integer value

c)

Invalid syntax

d)

None of these

31.

Which of the following keywords is able to turn a False into a True and is also known as a 'logical inverter'

a)

not

b)

change

c)

invert

d)

convert

32.

What is the type of the variable c in the code snippet?

a)

Floating point

b)

Integer

c)

Decimal

d)

This will raise an error.

33.

What is the output of the following code?

a)

10

b)

20

c)

invalid syntax

d)

None of these

34.

Please select the correct answer regarding the following code.

a)

Output will be 'Kim'

b)

Output will be True

c)

Output will be False

d)

Both variables contain string objects

35.

What is the output of the following code?

a)

0

b)

2

c)

10

d)

20

36.

What would be output by this code?

a)

0

b)

1

c)

Invalid operation

d)

None of these

37.

What is the value of j after the following calculation?

a)

7

b)

10

c)

25

d)

None of the above

38.

You are coding a tip calculator. The user must enter a price. Select the line of code that will make your program work whether the user enters whole numbers (for example, 10) or numbers that include decimals (for example, 30.99).

a)

price = float(input(“Enter the price”))

b)

price = int(input(“Enter the price”))

c)

price = input(“Enter the price”)

d)

price = flt(input(“Enter the price”))

39.

Write a function that takes a string (st) containing one letter surrounded by numbers. The function should iterate through the string and return only the letter as a string.

```

Example:

Input: 124f2312

Output: f

```

a)

WIP

b)

WIP

40.

What is the value of the variable 'c' in the following code snippet?

a)

3

b)

7

c)

8

d)

15

41.

What is the value returned from this code?

a)

<type 'str'>

b)

<class 'bool'>

c)

Undefined

d)

<type 'int'>

42.

Write a function that accepts any arbitrary argument (value) and returns True if the argument belongs to the integer class, otherwise it should return False.

The input can belong to the following classes: integer, string, float, boolean, list

```

Example:

Input: 6

Output: True

Input: [6, 7, 9]

Output: False

Input: 6.2

Output: False

```

a)

WIP

b)

WIP

43.

What is the value of the variable 'c' in the following code snippet?

a)

-2

b)

-3

c)

-4

d)

-5

44.

Which of the following keywords is able to turn a False into a True and is also known as a 'logical inverter'

a)

opposite

b)

not

c)

bool

d)

return inverse

45.

What is the output of the following code?

a)

10

b)

True

c)

False

d)

None of these

46.

Which of the following casts could result in a run time error?

a)

str(2)

b)

str(4.75)

c)

float(7)

d)

int(“j”)

47.

Which of the following casts could result in a run time error?

a)

A string cast as an int

b)

A float cast as a string

c)

An integer cast as a string

d)

An int cast as a float

48.

What is the output of the following code?

a)

0

b)

10

c)

20

d)

None of these

49.

What is the output of the following code?

a)

5, 1

b)

1, 1

c)

4, 1

d)

5, 3

50.

What is the value of the variable 'c' in the following code snippet?

a)

0

b)

6

c)

9

d)

False

51.

Write a function that accepts any arbitry argument (value) and returns True if the argument is a list, otherwise it should return False.

```

Example:

Input: 6

Output: False

Input: [6, 7, 9]

Output: True

Input: (6, 2)

Output: False

```

a)

WIP

b)

WIP

52.

What is the output of the following code snippet?

a)

'I got a second chance at this!'

b)

'I am being careful!'

c)

'I am not being careful...'

d)

The execution will not enter the if-else statement.

53.

What is the output of the following code snippet?

a)

'I am being careful!'

b)

'I am not being careful...'

c)

'I got a second chance at this!'

d)

The execution will not enter the if-else statement.

54.

Will this program cause an error? If not, what will the value be of you_can_pass?

a)

True

b)

False

c)

SyntaxError

d)

ValueError

55.

What is output by the following code?

a)

1

b)

8

c)

10

d)

12

56.

Add the line of code needed to count the number of values generated for a and then print the result.

```

Output: 6

```

a)

WIP

b)

WIP

57.

What is the purpose of the break statement in Python?

a)

Terminate the loop and transfer control to the statement following the loop

b)

Skip the remainder of the if body and transfer control to the if condition

c)

Skip the remainder of the loop body and transfer control to the loop condition

d)

Terminate the if statement and transfer control to the statement following the if

58.

What would be the highest value printed by this code?

a)

11

b)

12

c)

13

d)

None of these

59.

You are given a function which contains a non working while loop. The function also accepts a positive integer (x). The while loop is in an infinite loop state, which would be unwise to run. The while loop continually decreases the value variable by 1. Your task is to fix the code so that the loop exits and returns the value variable when it is equal to x.

```

Examples

Input: -7

Output: -7

Input: -15

Output: -15

```

a)

WIP

b)

WIP

60.

Write a function that accepts two integers(a and b, where a < b) and returns the sum of all numbers between a and b (inclusive of a and b).

```

Examples

Inputs: a = 2, b = 5

Output: 14

Inputs: a = 8, b = 10

Output: 27

```

a)

WIP

b)

WIP

61.

What is the value of the variable 'a' after it passes through the loops?

a)

3

b)

8

c)

9

d)

10

62.

What would be the lowest value printed by this code?

a)

0

b)

1

c)

12

d)

None of these

63.

What is the value of the variable 'a' after it passes through the loops?

a)

4

b)

6

c)

8

d)

10

64.

Write a function that iterates over a tuple(tup) and returns the position of the first odd number.

Examples:

```

Input: (5, 7, 9, 12, 44, 66)

Output: 0

Input: (50, 7, 9, 12, 44, 66)

Output: 1

```

a)

WIP

b)

WIP

65.

Add the line of code needed to accumulate the total of all values of a and then print the result.

```

Output: 15

```

a)

WIP

b)

WIP

66.

Write a function that accepts two integers(a, b), it should sum up all the numbers from a to b (inclusive of a and b) and then return the sum divided by 2 as float.

```

Examples

Inputs: a = 2, b = 5

Output: 7.0

Inputs: a = 7, b = 9

Output: 12.0

```

a)

WIP

b)

WIP

67.

What does the “continue” Statement in Python do?

a)

it does not break the loop

b)

it terminates the execution of statements while iterating the loop

c)

it breaks the loop

d)

None of these

68.

What would be the output of the round() function when this code is evaluated?

a)

4

b)

5

c)

6

d)

7

69.

Write a function that accepts an integer(i) which can only be 0 or 1 and returns the Boolean representation of that integer.

```

Examples

Input: 0

Output: False

Input: 1

Output: True

```

a)

WIP

b)

WIP

70.

Write a function that accepts a float(fl) with 2 decimal places. Convert this float to an integer, removing its decimal places, then the resulting integer as a string and then convert it to a list. The process should look like this 65.66 -> 65 -> '65' -> ['6', '5'].

Note:

* This question is designed to test your knowledge on built in functions within Python and the appropriate function should be used within the function. The presence of the function is tested for and is required to pass.

```

Example

Input: 79.99

Output: ['7', '9']

Input: 61.12

Output: ['6', '1']

```

a)

WIP

b)

WIP

71.

Write a function that accepts an integer(i) which can only be 0 or 1. The function should return the inverse Boolean representation of that integer.

```

Examples

Input: 0

Output: True

Input: 1

Output: False

```

a)

WIP

b)

WIP

72.

Write a function that accepts a tuple of positive integers (tup) and return a new ordered list containing the smallest and largest integers only.

```

Examples

Input: (5, 4, 6, 1, 10, 35)

Output: [1, 35]

Input: (10, 4, 7, 8, 21, 34, 100)

Output: [4, 100]

```

a)

WIP

b)

WIP

73.

What would be the output of the round() function when this code is evaluated?

a)

4

b)

5

c)

6

d)

7

74.

What is the value of the variable 'c' in the following code snippet?

a)

3

b)

8

c)

10

d)

11

75.

A programmer wants to match a specific string using a regular expression. What would be most appropriate to match the following string? 'Hello NAME'. The 'NAME' can be any 4 letter name containing only letters, such as 'Hi Alex', or 'Hi John'

a)

r'Hi \W{4}''

b)

r'Hi \d\d\d\d'

c)

r'Hi \w{4}'

d)

r'hi\w{4}'

76.

Consider a program that has a module that provides a framework of sorts to short declarative one-file scripts that writes. Which of the following words is suitable for describing the previous program.

a)

A request loop

b)

An event

c)

A module callback

d)

A chapter

77.

Which collection structure allows items that are ordered and unchangeable?

a)

Set

b)

List

c)

Tuple

d)

Dictionary

78.

Which collection structure allows items that are ordered, changeable, and does not allow duplicate values?

a)

List

b)

Tuple

c)

Set

d)

Dictionary

79.

Which of the following modes of file writing would be most appropriate for a program designed to update the contents of a .txt file and to overwrite the entire file if the file contains any data.


a)

r

b)

w

c)

a

d)

re

80.

Which of the following best describes the built-in set class?

a)

An immutable, iterable collection able to hold any type

b)

unordered collection, iterable, mutable and has no duplicate elements

c)

ordered collection, mutable, non-iterable collection able to hold any type

d)

A mutable, iterable collection able to hold any type

81.

Consider the following piece of code. Which of the following is true?

a)

sklearn is a module and kernel_approximation is a package

b)

LinearRegression is a module, which comes the linear_model package

c)

linear_model is a module of sklearn package

d)

sklearn and linear_model are both packages

82.

Which statement would indicate that the D3Point class inherits characteristics from the Point class?

a)

Class D3Point(“Point”):

b)

Class D3Point(Point):

c)

class D3Point(“Point”):

d)

class D3Point(Point):

83.

Which statement would assign the input.txt file to the handler fin?

a)

fin.open(“input.txt”, “r”)

b)

fin = “input.txt”, “r”

c)

open.fin(“Input.txt”,”r”)

d)

fin = open(“input.txt”, “r”)

84.

What would be the output for the following code?

a)

Saucer

b)

Mug

c)

Plate

d)

Bowl

85.

A programmer has created a class to represent Employees, and each employee has a unique name, role and ID number. The programmer also wants to create a special method to overload the len() method such that every Employee instance will return the seniority in the company. What is best to use for this situation?

a)

A class attribute

b)

A property decorator

c)

A dunder method

d)

An instance attribute

86.

Print the maximum value from a list named mylist.

```

Input: [1, 3, 5, 2, 12, 10]

Output: 12

```

a)

WIP

b)

WIP

87.

When creating a text file using the built-in open() function with 'a mode', what will occur if a file is written to a directory which contains a file having the same name?

a)

The file is appended to, with the original contents saved

b)

An OS Error

c)

A duplicate is created with a modified filename

d)

The file is overwritten

88.

The open() function opens a file in text format by default. Which mode opens the file in binary format for reading?

a)

'rb'

b)

'r'

c)

'b'

d)

'a'

89.

Add the calc function to the Point class. The function should return the value of the x variable multiplied by the value of the y variable.

```

Output: 12

```

a)

WIP

b)

WIP

90.

A programmer wants to use simple and efficient tools for predictive data analysis that are used for Machine Learning. Suggest a suitable module or package that could be used to check for this task.

a)

os module

b)

scikit-learn

c)

file module

d)

sys module

91.

Which of the following methods allows multiple items from a string to be added as individual elements to an existing list? Use the example below for context.

a)

insert()

b)

append()

c)

pop()

d)

extend()

92.

Suggest why a program may be broken down into individual functions

a)

Non-Descriptive Naming of Variables.

b)

Debugability. It's easier to debug simple functions than complex ones.

c)

Maintainability. Smaller, simpler functions are easier to maintain.

d)

Reuseability. Encourages code reuse by moving common operations to a separate function.

93.

Consider the following piece of code. Which of the following is true?

a)

LinearRegression is a module, which comes the linear_model package

b)

sklearn and linear_model are both packages

c)

sklearn is a module and kernel_approximation is a package

d)

linear_model is a module of sklearn package

94.

Which of the following best describes the randint function of the random module. Consider the example code below to see the code in context.

a)

producing a random integer between 1-5 (excluding 1 and 5)

b)

producing between 1 to 5 individual random integers

c)

producing a random float between the values of 1 and 5 (inclusive of 1 and 5)

d)

producing a random integer between 1-5 (inclusive of 1 and 5)

95.

Which of the following best describes 're'? 're' (regular expression) provides regular expression matching operations similar to those found in Perl.

a)

a suite

b)

a module

c)

a package

d)

an environment

96.

Which of the following code snippets would be the correct way to import everything from a module

a)

Option 1

b)

Option 2

c)

Option 3

d)

Option 4

97.

Which of the following code snippets would be the correct way to import everything from a module

a)

Option 1

b)

Option 2

c)

Option 3

d)

Option 4

98.

Which of the following best describes the randint function of the random module. Consider the example code below to see the code in context.

a)

producing a random float between the values of 2 and 5 (inclusive of 2 and 5)

b)

producing a random integer between 2-5 (inclusive of 2 and 5)

c)

producing between 2 to 5 individual random integers

d)

producing a random integer between 2-5 (excluding 2 and 5)

99.

The pip command line utility allows the developer to install ________?

a)

Invalid

b)

Python modules

c)

Python libraries

d)

Python packages

100.

Which methods would be used to generate random numbers in Python?

a)

random.uniform()

b)

random.randint()

c)

random.random()

d)

uniform.random()

101.

Suggest why a program may be broken down into individual functions

a)

Maintainability. Smaller, simpler functions are easier to maintain.

b)


It helps to avoid syntax errors

c)

Debugability. It's easier to debug simple functions than complex ones.

d)

Non-Descriptive Naming of Variables.

102.

Using the Math module to perform a calculation, what should be output of “math.sqrt(25)”?

a)


10

b)

5.0

c)


25

d)


None of these

103.

Assume you have written your Python code in a separate file named ‘myfunctions.py’. Which of the following statements shows the correct syntax for adding it to your new Python script so the code can be used?

a)

import myfunctions

b)

import "myfunctions"

c)

using myfunctions

d)

copy myfunctions

104.

Which of the following best describes 'sklearn'? 'sklearn' (scikit-learn) provides Machine Learning operations and it contains simple and efficient tools for predictive data analysis.

a)

a package

b)


an environment

c)


a module

d)


a suite

105.

____ is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects.

a)

Inheritance

b)

Object-oriented programming

c)

Encapsulation

d)

Instantiation

106.

When defining an object with a class, what function is used to establish values of an instance?

a)

Instantiation()

b)

__init__()

c)

try()

d)

__default__()

107.

How is called a programming paradigm where programs are constructed by applying and composing functions?

a)

Response Oriented Programming

b)

Object Oriented Programming

c)

Event Driven Programming

d)

Functional Programming

108.

A programmer accidentally attempts convert the type of an object and receives the next error: 'int() cannot convert 'dog' into an integer'. What error was likely raised in this instance?

a)

ValueError

b)

NameError

c)

AttributeError

d)

TypeError

109.

A programmer has created a string with a single speech mark on one side and double speech mark on the other. What error is likely to be raised in this instance?

a)

EOLerror

b)

ValueError

c)

GrammarError

d)

SyntaxError

110.

A programmer tries to run the following code. What error is likely to be raised in this instance?

a)

EOLerror

b)

SyntaxError

c)

ValueError

d)

GrammarError

111.

A programmer accidentally attempts to call a function or use an operator on something of the incorrect type. What error is likely to be raised in this instance?

a)

AttributeError

b)

NameError

c)

ValueError

d)

TypeError

112.

What are Runtime Errors in Python? (Select all that apply)

a)

An error which occurs while loading the script

b)

Errors that occur because of misspelled or incorrectly capitalized variable and function names

c)

Errors that occur when you break the rules defined in Python while creating a script

d)

Errors that show up when the script is attempting to perform operations on data of the wrong type

113.

How many except statements can a try-except block have?

a)

more than one

b)

one

c)

at least one

d)

zero

114.

Consider a program which manages its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer. Which of the following words is suitable for describing the previous program.

a)

A request loop

b)

A module callback

c)

Multithreading

d)

A chapter

115.

A programmer tries to run the following code. What error is likely to be raised in this instance?

a)

ValueError

b)

EOLerror

c)

GrammarError

d)

SyntaxError

116.

Which statement would read the entire file contents?

a)

fin.readall()

b)

fin.readin()

d)

fin.readline()

117.

A developer wants to read a text file but he is unsure if the file exists in the current directory. Suggest a suitable module that could be used to check for the presence of the file in the current working directory.

a)

re module

b)

os module

c)

sys module

d)

file module

118.

Which of the following modes is most suitable for writing a binary file in Python?

a)

'a'

b)

'wb'

c)

'rb'

d)

'r'

119.

Which of the following modes of file reading would be most appropriate for a program designed to update the contents of a .txt file without making any new deletion to the file

a)

w

b)

r

c)

re

d)

a

120.

A programmer wants to use various functions and variables that are used to manipulate different parts of the Python runtime environment. Suggest a suitable module that could be used to check for this task.

a)

file module

b)

re module

c)

os module

d)

sys module