WorksheetsPCSE
Total questions: 57
Worksheet time: 43mins
What is the value returned from this code?
print(type(4))
<type 'bool'>
<class 'int'>
<type 'str'>
Undefined
Which line of code inserted in the program will add a random item from the list each time the loop runs?
prize_list = random.choice(rand_item)
rand_item = option_list.random(prize_list)
rand_item = random.choice(option_list)
rand_item = option_list.random()
Which collection structure allows items that are ordered and unchangeable?
Tuple
Dictionary
List
Set
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
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 would be used to check for the presence of the file in the current working directory.
os module
file module
re module
sys module
Consider the following piece of code. Which of the following is true?
from sklearn.linear_model import LinearRegression
sklearn is a module and kernel_approximation is a package
sklearn and linear_model are both packages
LinearRegression is a module, which comes the linear_model package
linear_model is a module of the sklearn package
The pip command line utility allows the developer to install _______?
Python modules
Invalid
Python libraries
Python packages
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
Which code would allocate and initialize a list of 10 integers?
for i in range(11) nlist.append(i)
None of these
nlist = [0,0,0,0,0,0,0,0]
nlist = [0] * 10
Which of the following best describes 're'? 're' (regular expression) provides regular expression matching operations similar to those found in Perl.
an environment
a module
a package
a suite
Please select the correct answer regarding the following code
Both variables contain string objects
Output will be 'Kim'
Output will be True
Output will be False
How many except statements can a try-except block have?
one
more than one
zero
at least one
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
append()
extend()
insert()
pop()
Which of the following best describes the built-in set class?
A mutable, iterable collection able to hold any type
ordered collection, mutable, non-iterable collection able to hold any type
unordered collection, iterable, mutable and has no duplicate elements
An immutable, iterable collection able to hold any type
A data structure that can't be changed after its creation is known as _____?
duck-typed
strongly-typed
immutable
mutable
A programmer tries to run the following code. What error is likely to be raised in this instance?
#Example:
print(int('cat'))
SyntaxError
GrammarError
EOLerror
ValueError
Which of the following shows the correct way to initialize a multidimensional array?
arr = []
arr = ['Jack].extend(['Kan','Ryne','Ema'])
arr = [[]]
arr = [][]
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.
re
r
w
a
What is the value of the variable 'c' in the following code snippet?
4
1
3
2
Which methods would be used to generate random numbers in Python?
random.uniform()
uniform.random()
random.randint()
random.random()
What is the output of the following code snippet?
'I got a second chance at this!'
The execution will not enter the if-else statement.
'I am not being careful...'
'I am being careful!'
What will happen when the user enters 12?
There will be an error caused by trying to access index 12
The program will print 'Your favorite month is January!'
The program will print 'Your favorite month is !'
The program will print 'Your favorite month is December!'
What is the type of the variable c in the code snippet?
Floating point
This will raise an error.
Integer
Decimal
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?
ValueError
AttributeError
TypeError
NameError
What would be the output of the following code?
['Glass']
['Mug', 'Plate', 'Bowl', 'Saucer']
['Saucer']
['Plate', 'Bowl', 'Saucer']
A programmer wants to match a specific string using a regular expression. What would be most appropriate to match the following string? 'Hello 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}'
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?
TypeError
ValueError
AttributeError
NameError
What data type would be created by this code?
arr = ('four', 'one', 'seven')
Tuple
List
Set
Dictionary
What is the purpose of the following code?
Trap an invalid integer value
Convert b to a string
Invalid syntax
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"
using myfunctions
import myfunctions
copy myfunctions
What is the value returned from this code?
print(type(True))
Undefined
<type 'int'>
<class 'bool'>
<type 'str'>
____ is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects.
Encapsulation
Object-oriented programming
Instantiation
Inheritance
What are Runtime Errors in Python? (Select all that apply)
An error which occurs while loading the script
Errors that occur when you break the rules defined in Python while creating a script
Errors that occur because of misspelled or incorrectly capitalized variable and function names
Errors that show up when the script is attempting to perform operations on data of the wrong type
Which of the following methods allows to insert a specified value on a specified position? Use the example below for context
append()
extend()
pop()
insert()
Which of the following best describes the randint function of the random module. Consider the example below to see the code in context.
x = random.randint(1, 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)
producing a random integer between 1-5 (excluding 1 and 5)
Which of the following statements are true?
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 compiler scans the entire source code file and translate the whole of it into machine code at once
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
os module
re module
sys module
Which collection structure allows items that are ordered, changeable, and does not allow duplicate values?
Tuple
Dictionary
Set
List
The open() function opens a file in text format by default. Which mode opens the file in binary format for reading?
'b'
'rb'
'a'
'r'
Which of the following is a collection type that implements a method called 'insert()'?
dictionary
tuple
list
set
What is the value of the variable 'c' in the following code snippet?
11
3
8
10
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 of the company. What is best to use for this situation?
An instance attribute
A class attribute
A property decorator
A dunder method
Which collection structure allows items that are ordered and changeable and contain duplicate values?
Dictionary
List
Tuple
Set
Which of the following is a collection type that implements a method called 'keys()'?
list
tuple
dictionary
set
Will this program cause an error? If not, what will the value be of you_can_pass?
False
SyntaxError
ValueError
True
Identify the Python docstring comment.
Comment 3
Comment 4
Comment 2
Comment 1
What is the value of the variable 'c' in the following code snippet?
-5
-3
-2
-4
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 overwritten
An OS error
The file is appended to, with the original contents saved
A duplicate is created with a modified filename
When defining an object with a class, what function is used to establish values of an instance?
__init__()
Instantiation()
try()
__default__()
Which of the following shows the correct way to initialize a single dimension array?
arr = []
arr = ['Kan', 'Ryne', 'Em']
arr = {}
arr = [][]
What is the value of the variable 'c' in the following code snippet?
7
8
3
15
An operation that modifies a variable without making a copy is known as ...
in-place operation
static typed operation
constant time operation
mutability operation
What is the result of the following if statement?
when j is 10 the result is True
when j is 20 the result is False
when j is 15 the result is False
when j is 15 the result is True
Identify the Python comment.
"""My comment."""
""#Another comment.""
#Some comment.
// Just comment.
What would be the output of the round() function when this code is evaluated?
4
7
6
5
Which of the following best describes the built-in tuple class?
An immutable, iterable collection able to hold any type
A mutable, iterable collection able to hold any type
A mutable, non-iterable collection able to hold any type
A mutable, iterable collection to hold only one specific type
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 chapter
A module callback
Multithreading
A request loop
