Python Exam (BIA 3470)
Which of the following expressions best describe concatenation?
"a" + "b"
Identify which of the following is a type of error that occurs when a program is executing:
'Index out of bounds' error
What is the output of the following script? >>> a, b, c = 1, 2, 3 >>> print(a) >>> print(b) >>> print(c)
1 2 3
How many values do Boolean data types contain?
2
What is the output of running the following script? >>> print(5*4)
20
Which of the following expressions will most often result in a floating-point value?
3.0/2
How do you write an inline comment in Python?
>>> print(ans) #this is the answer
What do you call a saved file containing Python instructions?
A module
Which one of the following successfully adds two complex numbers, given 'a' and 'b' are numbers?
Complex(a + b)
Which method returns the number of occurrences of an item in a tuple?
Count()
Which of the following statement is true about frozen sets?
Frozen sets are immutable in nature and do not support adding or removing of items
Which of the following statements is true about frozen sets?
Frozen sets are immutable in nature and do not support adding or removing of items
Which of the following are examples of Python language elements that can be referenced from a module?
Functions, modules, global variables
What is the output of the following script? def myfunc(val): print("Hello" + val) my func("C++") my func("Perl") my func("Java")
Hello C++ Hello Perl Hello Java
What is the output of the following script: def myfunc(val): print("Hello" + val) myfunc("Python") myfunc("Ruby") myfunc("Java")
Hello Python Hello Ruby Hello Java
What is an example of a programming paradigm?
Imperative programming
What is the output of the following script? x = 11 y = 22 if x == y: print("Inputs are equal"); else: print("inputs are different");
Inputs are different
What are two additional actions that are often taken when importing modules?
Name imports using the 'as' keyword and group many imports by using parenthesis
What is the output from the following for loop? for i in range(3): total = 1 + i if i == 2: break else: print("Done")
No output
How are ordered dictionaries (OrderedDict) different from normal dictionaries?
OrderedDict maintains the insertion order of keys while a normal dictionary does not
What is the output of the following script? for value in "Python": if value == "t": break print(value)
P y
What is the order of operations while evaluating an expression?
PEMDAS
What command should you type in the interpreter to check your Python installation?
Python
When does a type error not occur?
You attempt to invoke a valid integer method on a literal integer, such as 2.denominator()
What is the output of the following script? >>> things = ["second"] >>> things.insert(0, "first") >>> print(things)
["first", "second"]
What is the result of replacing all occurrences of the letter 'r' with 'z' within each element of the list ["qrx","r", "z"]?
["qzx", "z", "z"]
What is the output of the following script? >>> l = list(range(10)) >>> print(l)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Which is the correct way to display an empty list?
[]
How do you check whether two objects are of the same instance?
a is b
Which operation would you use to add a data "F" to a given set? myset = {"A", "B", "C", "D", "E"}
add()
which built-in method identifies iterable elements in a tuple: count(), any(), max(), len()?
any()
What is the output of the following script? sports = ["baseball", "cricket", "soccer"] for x in sports: print(x)
baseball cricket soccer
Which function should you use to convert an integer into a binary number?
bin()
Which statements are used to exit out of a loop?
break, continue, pass
How do you introduce a new instance attribute to a class instance? class MyCls(object): pass c = MyCls()
c.newattr = None
What is the output of the following script? >>> string = "championships" >>> print(string[0:5])
champ
Which function is used to remove all items from a list?
clear()
Which method returns the number of occurrences of an item in a tuple?
count()
What is the correct syntax of a Python function?
def function_name(parameter_one, parameter_two, parameter_n): #Logic return
Which of the following is an instance method that returns an instance attribute? class Divider(object): def __init__(self): self.numerator = 1 self.denominator = 2
def get_numerator(self): return self.numerator
Which of the following is not a built-in list method: append(), insert(), destroy(), clear()?
destroy()
How do you create an anonymous function with no arguments?
f = lambda: 'result'
Predict the correct script to get the following output: * ** ***
for a in range(1,4): for b in range(a): print("*", end=' ') print()
Identify ways in which tuples are accessible: indexing, slicing, popping
indexing and slicing
Which function can be used to convert strings to integers?
int()
What is a dictionary?
it is a data type that holds data or information in a key-value order
What is an if statement?
it is a statement that allows you to execute a block of code if a condition is true
What is a control statement?
it is a structure that conditionally changes program flow
How can a while statement be used to go through all members of an iterable?
iterable = [1,2,3,4,5] count = 0 while not count >= len(iterable): print(iterable[count]) count += 1
What are anonymous functions called in Python?
lambda functions
Which function do you call to check the number of items in a list?
len()
Which function do you call when you want to append an element at the end of a list?
list.append()
What is the output of the following script? >>> fruits = ["apples", "bananas", "strawberries", "mangoes", "pears"] >>> print(fruits[3])
mangoes
How can you write the expression "n = n * 2" using short-hand operators?
n *= 2
Elements in a list are:
ordered and changeable
Which operator is used to concatenate two lists?
plus (+)
How are exceptions or errors thrown in Python?
raise ValueError
In the following code a = {1, 2, 3}, which method should we use to remove the last item from the set but not return it?
remove()
Identify the default arguments in the following script: >>> def division(first, second = 2): >>> return first/second >>> quotient = division(10)
second
Identify which tuple method is used in the following script: >>> a[1:5]
slicing
Which method allows you to access a subset or a slice of a tuple?
slicing
Which method allows you to access a subset, or a slice, of a tuple?
slicing
Which string function do you call when you want to convert the first letter in the string to uppercase?
str.capitalize()
Which set of statements is suitable for removing all elements from a list?
to_be_removed = ['a','b','c','b'] while len(to_be_removed) > 0: to_be_removed.pop()
What are local variables?
variables that are defined inside a function
From the following list, identify the function that is not a built-in function in Python: print(), input(), help(), write()
write()
What is the syntax for assigning a variable, x, to the value 8?
x = 8
Identify local and global variables in the following script: >>> def myfunc(): >>> x = 10 >>> y = 20
x is the local variables, y is the global variable
What is the output of the following script? dictionary_masher = { "name": "Amos", "age": 100 }
{'name': 'Amos', 'age': 100}
What is the output of the following script? mydict = { "name": "Mike" "age": 10 }
{'name': 'Mike', 'age': 10}
What is the output of the following script? a = {1,2,3,4,5,6} b = {1,2,3,7,8,9,10} a - b
{4, 5, 6}
What is the output of the following script? a = {10, 20, 30, 40, 50, 60} b = {10, 20, 30, 70, 80, 90, 100} a - b
{40, 50, 60}
What is the output of the following script? >>> a = {"name": "Skandar Keynes", "age": "24", "sex": "male"} >>> b = a.setdefault("name") >>> print(b)
Skandar Keynes
Identify which tuple method is used in the following script: >>> a[1:5]
Slicing
What is the name of the output you get when an error occurs in your program?
Stack trace or traceback
What type of value is returned by the input function?
String
Which module needs to be imported to access the list of arguments passed by a user to a script?
The 'sys' module
Which of the following best describes what happens when a modules is imported?
The code in the module is executed exactly once, and populates the current scope with the names of accessible resources inside the module.
Which statement is true about the copy() method of dictionaries?
The copy() method is used to create shallow copies of dictionaries
What is the main difference between if and while?
The if block is executed only once. The while block is executed multiple times
Which statement is used in Python to return something to the caller of the function?
The return statement
What happens when you call a function that returns a value in the Python Interactive Shell but does not assign the return value to a variable?
The return value is printed as output
What are parameters?
They are pieces of information that need to be passed to a function for it to do its work
What are variables?
They refer to values in a memory
Identify the incorrect statement:
Undefined function and method arguments are defined through args
Which operation would you use on the following two sets to get the expected output? a = {1,2,3,4,5,6} b = {1,2,3,7,8,9,10} Output: {1,2,3,4,5,6,7,8,9,10}
Union
