Python Certification Questions
immutable
A data structure that can't be changed after its creation is known as _____?
duck-typed
A data structure where the type or the class is less important than the methods it defines is known as what?
ValueError
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 dunder method
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?
ValueError
A programmer tries to run the following code. What error is likely to be raised in this instance?
r'Hi \w{4}'
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'
total=total+a
Add the line of code needed to accumulate the total of all values of a and then print the result. Output 15
count=count+1
Add the line of code needed to count the number of values generated for a and then print the result. Output 6
in-place operation
An operation that modifies a variable without making a copy is known as ....
import myfunctions
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 module callback
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.
Multithreading
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.
linear_model is a module of sklearn package
Consider the following piece of code. Which of the following is true?
print("Hello")
How to output the string "Hello" in Python?
Comment 3
Identify the Python comment.
Comment 3
Identify the Python docstring comment.
Both variables contain string objects Output will be True
Please select the correct answer regarding the following code
# comment
Please select the valid single line comment in Python.
1. user_input=input("Do you want to go to the beach or on a hike 2. if user_input=="beach": print("Off to the beach! Let's catch some waves.") 3. elif user_input=="hike": print("Looks like we'll have a clear view from the summit today!") 4. else: print("Okay, let's do something else.")
Reorder the blocks of code so that it is in the correct order.
Maintainability. Smaller, simpler functions are easier to maintain. Debugability. It's easier to debug simple functions than complex ones.
Suggest why a program may be broken down into individual functions
Maintainability. Smaller, simpler functions are easier to maintain. Reuseability. Encourages code reuse by moving common operations to a separate function. Debugability. It's easier to debug simple functions than complex ones.
Suggest why a program may be broken down into individual functions
Python packages
The pip command line utility allows the developer to install ________?
5.0
Using the Math module to perform a calculation, what should be output of "math.sqrt(25)"?
it does not break the loop it terminates the execution of statements while iterating the loop
What does the "continue" Statement in Python do?
10
What is output by the following code?
'I am not being careful...'
What is the output of the following code snippet?
'I got a second chance at this!'
What is the output of the following code snippet?
0
What is the output of the following code?
1, 2
What is the output of the following code?
1.0
What is the output of the following code?
10
What is the output of the following code?
100
What is the output of the following code?
2
What is the output of the following code?
20
What is the output of the following code?
5, 1
What is the output of the following code?
True
What is the output of the following code?
Terminate the loop and transfer control to the statement following the loop
What is the purpose of the break statement in Python?
Trap an invalid integer value
What is the purpose of the following code?
Integer
What is the type of the variable c in the code snippet?
This will raise an error.
What is the type of the variable c in the code snippet?
25
What is the value of j after the following calculation?
6
What is the value of the variable 'a' after it passes through the loops?
9
What is the value of the variable 'a' after it passes through the loops?
-4
What is the value of the variable 'c' in the following code snippet?
11
What is the value of the variable 'c' in the following code snippet?
12
What is the value of the variable 'c' in the following code snippet?
15
What is the value of the variable 'c' in the following code snippet?
4
What is the value of the variable 'c' in the following code snippet?
9
What is the value of the variable 'c' in the following code snippet?
False
What is the value of the variable 'c' in the following code snippet?
35
What is the value of the variable j after the following code is executed?
<class 'bool'>
What is the value returned from this code?
<class 'int'>
What is the value returned from this code?
<class 'str'>
What is the value returned from this code?
1
What would be output by this code?
11
What would be the highest value printed by this code?
0
What would be the lowest value printed by this code?
2
What would be the output for the following code?
5
What would be the output of the round() function when this code is evaluated?
#!/usr/bin/env python3
Whats is the proper shebang to indicate the script is using Python version 3?
__init__()
When defining an object with a class, what function is used to establish values of an instance?
#
Which is the special symbol used in python to add comments?
random.randint() random.uniform() random.random()
Which methods would be used to generate random numbers in Python?
a module
Which of the following best describes 're'? 're' (regular expression) provides regular expression matching operations similar to those found in Perl.
a package
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
producing a random integer between 1-5 (inclusive of 1 and 5)
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 2-5 (inclusive of 2 and 5)
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 string cast as an int
Which of the following casts could result in a run time error?
int("j")
Which of the following casts could result in a run time error?
#
Which of the following characters is used for single-line comments in Python?
Option 1
Which of the following code snippets would be the correct way to import everything from a module
Option 2
Which of the following code snippets would be the correct way to import everything from a module
Indentation
Which of the following is used to define a block of code in Python?
not
Which of the following keywords is able to turn a False into a True and is also known as a 'logical inverter'
# print('Content')
Which of the following lines represents a valid comment?
An interpreter translates one statement of the source code at a time into machine code 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 -EVERYTHING except none of the above
Which of the following statements are true?
Python code is structured and indented blocks must be used
Which of the following statements are true?
.py
Which one of the following is the correct extension of the Python file?
class D3Point(Point):
Which statement would indicate that the D3Point class inherits characteristics from the Point class?
True
Will this program cause an error? If not, what will the value be of you_can_pass?
fl=int(fl) fl=str(fl) return list(fl)
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'].
a=min(tup) b=max(tup) return [a,b]
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]
if i==0: return False else: return True
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
if i==1: return False else: return True
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
if value==6: return True else: return False
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.
if value==[6,7,9]: return True else: return False
Write a function that accepts any arbitrary argument (value) and returns True if the argument is a list, otherwise it should return False.
if value==(6,2): return True else: return False
Write a function that accepts any arbitrary argument (value) and returns True if the argument is a tuple, otherwise it should return False.
add=sum(range(a,b+1)) return add
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
add=sum(range(a,b+1)) return add/2
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
for n in tup: if n%2==1: pos=tup.index(n) break return pos
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
quotation="" for n in st: if n.isalpha(): quotation="".join([quotation,n]) return quotation
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.
price = float(input("Enter the price"))
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).
value=-1 while True: if value!=x: value=value-1 continue else: return value break
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
Object-oriented programming
____ is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects.