Font size
WorksheetsPython KP 1-2
Total questions: 120
Worksheet time: 1hrs 6mins
Which is the special symbol used in python to add comments?
/*...*/
#
//
$
Please select the valid single line comment in Python.
# comment
"comment"
'comment'
/* comment */
Which of the following statements are true?
A compiler scans the entire source code file and translates the whole of it into machine code at once
Compiled code tends to execute faster than interpreted code
An interpreter translates one statement of the source code at a time into machine code
None of the above
A data structure that can't be changed after its creation is known as _____?
immutable
mutable
duck-typed
strongly-typed
How to output the string "Hello" in Python?
System.out("Hello")
printf("Hello")
print("Hello")
echo("Hello")
Which one of the following is the correct extension of the Python file?
.p
.pyt
.python
.py
Identify the Python docstring comment.
Comment 1
Comment 2
Comment 3
Comment 4
Which of the following characters is used for single-line comments in Python?
!
#
/*
//
An operation that modifies a variable without making a copy is known as ....
in-place operation
static typed operation
mutability operation
constant time operation
Identify the Python comment.
Comment 1
Comment 2
Comment 3
Comment 4
Which of the following lines represents a valid comment?
print /*('Hello World')*/
fibbonacci_code = //[1, 1, 2, 3, 5, 8]
x,y, z, number = 10, 15, 16, 21 "" More Content ""
# print('Content')
Which of the following statements are true?
Python code is structured and indented blocks must be used
Python is insensitive to case
The ! character is used to denote a comment in Python
All of the above
Which one of the following is the correct extension of the Python file?
.py
.python
.p
None of these
A data structure where the type or the class is less important than the methods it defines is known as what?
immutable
strongly-typed
mutable
duck-typed
Which of the following is used to define a block of code in Python?
Indentation
Brackets
Key
All the above
Whats is the proper shebang to indicate the script is using Python version 3?
#!/usr/bin/env python3
#!/usr/bin/env python
!#/usr/bin/env python3
!#/usr/bin/env python
What is the value of the variable 'c' in the following code snippet?
True
3
False
0
What is the value returned from this code?
<type ‘bool’>
Undefined
<class 'int'>
<type ‘str’>
What is the output of the following code?
1, 2
2, 1
2, 2
1, 1
What is the value of the variable 'c' in the following code snippet?
11
3
10
8
What is the value of the variable 'c' in the following code snippet?
26
49
28
12
What is the value of the variable 'c' in the following code snippet?
1
2
3
4
What is the output of the following code?
0
10
20
None of these
What is the output of the following code?
1.0
10
20
None of these
What is the value of the variable j after the following code is executed?
35
55
45
75
Which of the following keywords is able to turn a True into a False, and is also known as a 'logical inverter'.
apostrophe
change
invert
not
What is the output of the following code?
10
100
1000
None of these
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
```
WIP
WIP
What is the type of the variable c in the code snippet?
Floating point
Integer
Decimal
This will raise an error.
What is the purpose of the following code?
Convert b to a string
Trap an invalid integer value
Invalid syntax
None of these
Which of the following keywords is able to turn a False into a True and is also known as a 'logical inverter'
not
change
invert
convert
What is the type of the variable c in the code snippet?
Floating point
Integer
Decimal
This will raise an error.
What is the output of the following code?
10
20
invalid syntax
None of these
Please select the correct answer regarding the following code.
Output will be 'Kim'
Output will be True
Output will be False
Both variables contain string objects
What is the output of the following code?
0
2
10
20
What would be output by this code?
0
1
Invalid operation
None of these
What is the value of j after the following calculation?
7
10
25
None of the above
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).
price = float(input(“Enter the price”))
price = int(input(“Enter the price”))
price = input(“Enter the price”)
price = flt(input(“Enter the price”))
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
```
WIP
WIP
What is the value of the variable 'c' in the following code snippet?
3
7
8
15
What is the value returned from this code?
<type 'str'>
<class 'bool'>
Undefined
<type 'int'>
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
```
WIP
WIP
What is the value of the variable 'c' in the following code snippet?
-2
-3
-4
-5
Which of the following keywords is able to turn a False into a True and is also known as a 'logical inverter'
opposite
not
bool
return inverse
What is the output of the following code?
10
True
False
None of these
Which of the following casts could result in a run time error?
str(2)
str(4.75)
float(7)
int(“j”)
Which of the following casts could result in a run time error?
A string cast as an int
A float cast as a string
An integer cast as a string
An int cast as a float
What is the output of the following code?
0
10
20
None of these
What is the output of the following code?
5, 1
1, 1
4, 1
5, 3
What is the value of the variable 'c' in the following code snippet?
0
6
9
False
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
```
WIP
WIP
What is the output of the following code snippet?
'I got a second chance at this!'
'I am being careful!'
'I am not being careful...'
The execution will not enter the if-else statement.
What is the output of the following code snippet?
'I am being careful!'
'I am not being careful...'
'I got a second chance at this!'
The execution will not enter the if-else statement.
Will this program cause an error? If not, what will the value be of you_can_pass?
True
False
SyntaxError
ValueError
What is output by the following code?
1
8
10
12
Add the line of code needed to count the number of values generated for a and then print the result.
```
Output: 6
```
WIP
WIP
What is the purpose of the break statement in Python?
Terminate the loop and transfer control to the statement following the loop
Skip the remainder of the if body and transfer control to the if condition
Skip the remainder of the loop body and transfer control to the loop condition
Terminate the if statement and transfer control to the statement following the if
What would be the highest value printed by this code?
11
12
13
None of these
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
```
WIP
WIP
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
```
WIP
WIP
What is the value of the variable 'a' after it passes through the loops?
3
8
9
10
What would be the lowest value printed by this code?
0
1
12
None of these
What is the value of the variable 'a' after it passes through the loops?
4
6
8
10
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
```
WIP
WIP
Add the line of code needed to accumulate the total of all values of a and then print the result.
```
Output: 15
```
WIP
WIP
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
```
WIP
WIP
What does the “continue” Statement in Python do?
it does not break the loop
it terminates the execution of statements while iterating the loop
it breaks the loop
None of these
What would be the output of the round() function when this code is evaluated?
4
5
6
7
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
```
WIP
WIP
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']
```
WIP
WIP
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
```
WIP
WIP
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]
```
WIP
WIP
What would be the output of the round() function when this code is evaluated?
4
5
6
7
What is the value of the variable 'c' in the following code snippet?
3
8
10
11
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'
r'Hi \W{4}''
r'Hi \d\d\d\d'
r'Hi \w{4}'
r'hi\w{4}'
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 request loop
An event
A module callback
A chapter
Which collection structure allows items that are ordered and unchangeable?
Set
List
Tuple
Dictionary
Which collection structure allows items that are ordered, changeable, and does not allow duplicate values?
List
Tuple
Set
Dictionary
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.
r
w
a
re
Which of the following best describes the built-in set class?
An immutable, iterable collection able to hold any type
unordered collection, iterable, mutable and has no duplicate elements
ordered collection, mutable, non-iterable collection able to hold any type
A mutable, iterable collection able to hold any type
Consider the following piece of code. Which of the following is true?
sklearn is a module and kernel_approximation is a package
LinearRegression is a module, which comes the linear_model package
linear_model is a module of sklearn package
sklearn and linear_model are both packages
Which statement would indicate that the D3Point class inherits characteristics from the Point class?
Class D3Point(“Point”):
Class D3Point(Point):
class D3Point(“Point”):
class D3Point(Point):
Which statement would assign the input.txt file to the handler fin?
fin.open(“input.txt”, “r”)
fin = “input.txt”, “r”
open.fin(“Input.txt”,”r”)
fin = open(“input.txt”, “r”)
What would be the output for the following code?
Saucer
Mug
Plate
Bowl
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 class attribute
A property decorator
A dunder method
An instance attribute
Print the maximum value from a list named mylist.
```
Input: [1, 3, 5, 2, 12, 10]
Output: 12
```
WIP
WIP
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?
The file is appended to, with the original contents saved
An OS Error
A duplicate is created with a modified filename
The file is overwritten
The open() function opens a file in text format by default. Which mode opens the file in binary format for reading?
'rb'
'r'
'b'
'a'
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
```
WIP
WIP
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.
os module
scikit-learn
file module
sys module
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.
insert()
append()
pop()
extend()
Suggest why a program may be broken down into individual functions
Non-Descriptive Naming of Variables.
Debugability. It's easier to debug simple functions than complex ones.
Maintainability. Smaller, simpler functions are easier to maintain.
Reuseability. Encourages code reuse by moving common operations to a separate function.
Consider the following piece of code. Which of the following is true?
LinearRegression is a module, which comes the linear_model package
sklearn and linear_model are both packages
sklearn is a module and kernel_approximation is a package
linear_model is a module of sklearn package
Which of the following best describes the randint function of the random module. Consider the example code below to see the code in context.
producing a random integer between 1-5 (excluding 1 and 5)
producing between 1 to 5 individual random integers
producing a random float between the values of 1 and 5 (inclusive of 1 and 5)
producing a random integer between 1-5 (inclusive of 1 and 5)
Which of the following best describes 're'? 're' (regular expression) provides regular expression matching operations similar to those found in Perl.
a suite
a module
a package
an environment
Which of the following code snippets would be the correct way to import everything from a module
Option 1
Option 2
Option 3
Option 4
Which of the following code snippets would be the correct way to import everything from a module
Option 1
Option 2
Option 3
Option 4
Which of the following best describes the randint function of the random module. Consider the example code below to see the code in context.
producing a random float between the values of 2 and 5 (inclusive of 2 and 5)
producing a random integer between 2-5 (inclusive of 2 and 5)
producing between 2 to 5 individual random integers
producing a random integer between 2-5 (excluding 2 and 5)
The pip command line utility allows the developer to install ________?
Invalid
Python modules
Python libraries
Python packages
Which methods would be used to generate random numbers in Python?
random.uniform()
random.randint()
random.random()
uniform.random()
Suggest why a program may be broken down into individual functions
Maintainability. Smaller, simpler functions are easier to maintain.
It helps to avoid syntax errors
Debugability. It's easier to debug simple functions than complex ones.
Non-Descriptive Naming of Variables.
Using the Math module to perform a calculation, what should be output of “math.sqrt(25)”?
10
5.0
25
None of these
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?
import myfunctions
import "myfunctions"
using myfunctions
copy myfunctions
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 package
an environment
a module
a suite
____ is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects.
Inheritance
Object-oriented programming
Encapsulation
Instantiation
When defining an object with a class, what function is used to establish values of an instance?
Instantiation()
__init__()
try()
__default__()
How is called a programming paradigm where programs are constructed by applying and composing functions?
Response Oriented Programming
Object Oriented Programming
Event Driven Programming
Functional Programming
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?
ValueError
NameError
AttributeError
TypeError
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?
EOLerror
ValueError
GrammarError
SyntaxError
A programmer tries to run the following code. What error is likely to be raised in this instance?
EOLerror
SyntaxError
ValueError
GrammarError
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?
AttributeError
NameError
ValueError
TypeError
What are Runtime Errors in Python? (Select all that apply)
An error which occurs while loading the script
Errors that occur because of misspelled or incorrectly capitalized variable and function names
Errors that occur when you break the rules defined in Python while creating a script
Errors that show up when the script is attempting to perform operations on data of the wrong type
How many except statements can a try-except block have?
more than one
one
at least one
zero
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 request loop
A module callback
Multithreading
A chapter
A programmer tries to run the following code. What error is likely to be raised in this instance?
ValueError
EOLerror
GrammarError
SyntaxError
Which statement would read the entire file contents?
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.
re module
os module
sys module
file module
Which of the following modes is most suitable for writing a binary file in Python?
'a'
'wb'
'rb'
'r'
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
w
r
re
a
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.
file module
re module
os module
sys module
