Computer Science 217-University of Calgary-Basic

Computer Science (CPSC 217-UCAL) Final Exam

1. Which of the following operation is most destructive in used improperly?

A. inf = open (“file_name.txt”, “r”)
B. inf = open (“file_name.txt”, “e”)
C. inf = open (“file_name.txt”, “w”)
D. inf = open (“file_name.txt”, “a”)

2. Which type of data sorting technique is built into Python 3 functions library?

A. selection sort
B. loop sort
C. insertion sort
D. bubble sort
E. quick sort

3. If you were to take the length of the following (call in len), what will be the expected output?
s = [[0,1],[2,3],[4,5],[6,7],[8,9]

A. 1
B. 5
C. 2
D. 4
E. 10

4. Which of the following is a mutable type?

A. integer
B. float
C. string
D. lists
E. boolean

5. The SU Wellness Centre University of Calgary use a Java program for billing. (Similar to Python 3). Do you think a code like the following can be used for calculating the price for medicine in Fall 2012?
cost = 55.99
gst_multiplier = 0.07
gst = cost * gst_multiplier
total = cost + gst

A. Yes, this simple program may work.
B. No, this program will crash due to Syntax Error caused by invalid format specifiers.
C. No, this program will not work because the GST calculation is wrong for Fall 2012.
D. No, this program will generate a no print statements because the initial amount is NOT a user input variable.

6. What will be the output if you were to call in s[-2][0] from the following?
s = [[0,1],[2,3],[4,5],[6,7],[8,9]]

A. 2
B. Invalid Index Error
C. 8
D. [8,9]
E. 6

7. Can you read from a non-text(.txt) external file using a Python 3 script ?

A. Yes
B. No

8. Python 3 ONLY have mutable type.

A. True
B. False

9. You have the following string which you must split in to it’s elements describing the each unique item:
data = “Sanuja Senanayake , Calgary , , (403) 111-1111”
The elements should separate; full name, city, web address, phone number. How do you do that?

A. list =” “)
B. list =” , “)
C. list = data.split(” “)
D. data = data.split(” , “)
E. list = data.split(” , “)

10. Why do we use exceptions in advanced Python 3 programs?

A. To crash the program in an event the output is invalid.
B. To quite a program in an event the output is invalid.
C. To print out a meaningful error messages.
D. To limit errors

11. This while loop will produce a print statement…
s = 0
while s < 5:
…. s = (s + 1) % 2
…. print (s)

A. 0 1 0 1 0 1… to infinity
B. 1 0 1 0 1 0
C. 1 0 1 0 1 0… to infinity
D. 1 1 0 1 1 0 1 1 0… to infinity
E. 0 1 0 1 0 1

12. In Python 3, dictionary can be ONLY be created in one dimension.

A. False
B. True

13. In order to satisfy the Python 3 rules, what has to be true in order for a code to be a function?

A. A single function cannot have more than one result.
B. Functions must always return a result.
C. A function must take a parameter.
D. A function must be predefined in order to call it in.

14. If you were to add one number to another from 0 to 100, what type of method would you use?

A. A iteration loop
B. A while loop
C. A dictionary
D. A for loop

15. Which of the following statement is true given a for loop first line; for i in range (x,y)?

A. x and y values are both inclusive.
B. x and y values are both exclusive.
C. x is exclusive and y is inclusive.
D. x is inclusive and y is exclusive.

16. What output would you expect from the following program?

4 in [11,4,73,19,13,24]

A. 4
B. Yes
C. True
D. False

17. How do you format the output of a numeric answer to two (exactly two) decimal places like shown below?
mass conversion output

A. print (“It is %.2f” % VARIABLE, “TEXT HERE !”)
B. print (“It is %2f” VARIABLE % , “TEXT HERE !”)
C. print (“It is %.2” % VARIABLE, “TEXT HERE !”)
D. print (“It is 2f” % VARIABLE, “TEXT HERE !”)
E. print (“It is %.2” % VARIABLE, “TEXT HERE !”)

18. Most current databases use relational model to sort and organize elements.

A. True
B. Faults

19. How do you delete all the keys and values from a dictionary named test efficiently?

A. del.keys()
B. clear.test()
C. del.test()
D. test.clear()
E. delete.test()

20. If you were to add to an empty dictionary name sanuja, how do you do that? (Direct addition without variables.)
sanuja = { }

A. sanuja[“keyName”] = valueName
B. sanuja[keyName] = valueName
C. sanuja[“keyName”] = “valueName”
D. sanuja[keyName] = valueName

21. Based on the following information:
a = [ ] is a …
b = { } is a …

a = [ ] is a list.
b = { } is a dictionary.

a = [ ] is a dictionary.
b = { } is a list.

a = [ ] is ais a tuple.
b = { } is a dictionary.

a = [ ] is a dictionary.
b = { } is a tuple.

22. How is text different from data?

A. Data hold program instructions typically in machine code and text point to all global consents and variables.
B. Data points to information and text point to all global consents and variables.
C. Text hold program instructions typically in machine code and data point to all global consents and variables.
D. Data is human readable and text is not human readable.

23. Information about function calls and pointers for local variables and formal parameters are stored in as what in a program’s memory?

A. Text
B. Data
C. Heap
D. Stack

24. Which one of the following is a list?

A. myList = (1,2,3,UofC,5.5,#)
B. myList = [peace, “25”, sanuja, “666”, BBC, 2012]
C. myList = [peace, “25”, sanuja, “666”, BBC, 2012]
D. myList = {2.5, -9.9, “sanuja”, 9615, “BBC”, 12}
E. myList = [2.5, -9.9, “sanuja”, 9615, “BBC”, 12]
F. myList = {peace, “25”, sanuja, “666”, BBC, 2012}

25. A heap holds any type of objects allocated as the program runs.

A. True
B. False

26. What is 55 base 8 in base 2 ?

A. 10 1101
B. F8D5
C. 10 0010
D. D56

27. An empty Tuple can be described as a…

A. tp = [ ]
B. tp = { }
C. tp = ( )
D. tp = ” “

28. The following Python 3 code is written to print “” several times.
while k > 10:
…. print (“ha”)
…. k = k + 2
What’s wrong with the code?

A. The variable k is not defined causing a NameError
B. The usage of a while loop instead of a for loop will create inaccurate results.
C. The program will crash due to inaccurate print statement.
D. The program will NOT crash, but will also provide no print statements in the terminal.

29. How many values can a key in a dictionary can hold?

A. One
B. More than one and less than zero
C. Cannot be more than the number of keys in a particular dictionary
D. Unlimited given the condition that resources are unlimited

30. How do you add a value to the following empty list?
myList = [ ]

A. myList.append(sanuja)
B. append.myList(“sanuja”)
C. append.myList(125)
D. myList.append(“125”)

31. What is the output of the following operation if a user entered 3 (exactly, no white space or any other characters) as the value?
power = input(“Please enter a value: “)
answer = 5**power
print (answer)

A. 125
B. Error, mathematical operation with a string.
C. 10
D. 25

32. On the following list, how do you remove the number 8 ?
myList = [1,2,3,8,4,5,6]

A. myList.pop(8)
B. myList = [8]
C. append.myList(8)
D. myList.append(8)

33. Dictionaries have ____ to assign position to variables.

A. values
B. index numbers
C. multipliers
D. lengths
E. keys

34. A length of a dictionary is always defined by the number of what elements?

A. values
B. keys
C. units
D. commas

35. Select all the valid dictionary names.

A. haha
B. myDic
C. my Dictionary
D. my1998
E. c_b1999

36. How do you add a tab (like a keyboard tab entry) to a Python 3 statement?

A. “\tab text here”
B. “\t text here”
C. “\n text here”
D. “\space text here”

37. Given,
s = [[0,1],[2,3],[4,5],[6,7],[8,9]]
what will be the output if you were to call in len(s) ?

A. 2
B. 4
C. 10
D. 5
E. 1

38. Given,
s = [[0,1],[2,3],[4,5],[6,7],[8,9]]
what will be the output if you were to call in s[3] ?

A. [4,5]
B. [6,7]
C. 2
D. 3

39. What is the most efficient way to combine the following two lists ?
s1 = [0, 1, 2, 3]
s2 = [4, 5, 6, 7, 8, 9]

A. s3 = s1 + s2
B. Writing a “for” loop with an empty list called s3 = [] that will take each value of s1 and combine it with s2 using s3.append()
C. Writing a “while” loop with an empty list called s3 = [] that will take each value of s1 and combine it with s2 using s3.append()
D. Slice the either one of the lists into its elements and place them in a new list called s3.

40. Given,
s = [[0,1],[2,3],[4,5],[6,7],[8,9]]
what will be the output if you were to call in s[3][1] ?

A. An Index Error message
B. 4
C. 7
D. 6
D. 5

41. Well developed tools for databases such as MySQL, Python libraries primarily used for what purpose?

A. To deal with large amounts of data.
B. To lower failure rate of the connection between data and information.
C. To create Python code with low level programing codes.
D. To allow flexibility for multiple programming languages to interact with each other.

42. In high level programming languages such as Python 3, variables can be called in before they has been created.

A. True
B. False

43. What is the output of the following code?
myName = “Sanuja\nSenanayake”
print (myName)

A. Sanuja Senanayake
B. Senanayake Sanuja
C. SanujaSenanayake
D. Sanuja
E. Sanuja\nSenanayake

44. In order to open an external file to be used in a Python 3 program, you must import which of the following library?

A. sys
B. math
C. open
D. file
E. sttder

45. How many characters in the following string?
s = “Love Canada”

A. 10
B. 11
C. 8
D. 13
E. None of the above answers are correct

46. Which of the following statement(s) is(are) correct?

A. In a relational database, the schema describes the structure of the data in the database
B. A primary key is a unique value associated with each row in a table
C. Text files and databases are both viable options for managing large amounts of data
D. In a string, there is a unique index number associated with each value.
E. A dictionary cannot be updated using a loop (for loop or while loop) because a dictionary contains more than one dimension.

47. Python 3 can be best described as a…

A. low level programming language.
B. high level programming language.
C. C# based programming language.
D. relatively complex programming language.

48. While the position of a character in a string always counts from zero (0), the counting of the string length always start from one (1).

A. True
B. False

49. According to relational model, it is easy to analyze, process and produce meaningful outputs from large scale databases when what has been done?

A. Splitting of large scale data schemes into small scale schemes.
B. Organization of data into tables.
C. Pulling data out one at a time and comparing the values against information.
D. Replacing data with information BEFORE processing them.
E. Replacing data with information AFTER processing the initial condition.

50. Poor commenting in the source file can cause a program to crash.

A. True
B. False

51. What is the default behavior of a run time errors?

A. Crash of the program.
B. Creation of the try block.
C. Creation of the except block.
D. Quitting the program by quit() function.

52. You have the following list with too much information. To protect privacy, you were told to split and print ONLY the geographical information. How do you do that?
data = “A male who lives in Calgary, Alberta, Canada.”

A. print(data[20: ])
B. print(data[25: ])
C. print(data[ :20])
D. print(data[ :25])
E. print([data:20])
F. print([data:25])

53. To print the statement “That is not a valid divisional operation.”, a user could enter what value for the prompt ?
prompt = input(“Enter a number to be used for division: “)

…. divided = 5 / float(prompt)
…. print (divided)
…. print(“That is not a valid divisional operation.”)

A. 5
B. 9999
C. 1
D. 0

54.… sort is the method of creating a sorted list by randomly taking an item from the unsorted list and placing in the right location in the sorted list until all the items are sorted.

A. Bubble
B. Quick
C. Selection
D. Insertion

55. This while loop will produce a print statement…
def fun1():
…. fun2()
…. print (“Alberta”)

def fun2():
…. print (“Quebec”)



56. This is year 2012 and your prof wants you to write the year on your exam in base-16. If 2012 is in base-10, what is it in base-16 ?

C. 7C4
D. 7DC

57. Linux based OS versions such as MacOS 9 and up, ONE character is used for a new line and in Windows TWO characters are used for a new line.

A. True
B. False

58. A print statement can be used as a computer code for analysis.

A. True
B. False

59. In order to preform the following operation, what must be corrected in the following code?
value = 55
result = sqrt(55)
print (result)

Call in the sys library; import sys
Request the function from math; math.sqrt()
Call in the math library; import math
Call in the math library; import math
Request the function from sys; sys.sqrt()
Call in the math library; import math
Request the function from math; math.sqrt()
Call in the sys library; import sys
Request the function from sys; sys.sqrt()

60. Lists are ordered data structures with specific index values for each item. (no random index assignments)

A. True
B. False

61. When do we use SQL (Structured Query Language)?

A. It is used to take commands from a database.
B. It is used to take commands from several databases for comparison and analysis.
C. It is used to send commands to database.
D. It is a client side programming language used to display server side Python 3 language operations.

62. Which type of sorting described by the following statement ?
Find the smallest number in the unsorted list, remove that number and add it to the end o the sorted list. Repeat this sequence until there are no numbers in the unsorted list.

A. selection sort
B. bubble sort
C. insertion sort
D. quick sort

63. … sort is the method of creating a sorted list by randomly taking an item from the unsorted list and placing in the right location in the sorted list until all the items are sorted.

A. Bubble
B. Quick
C. Selection
D. Insertion

64. If you were to create a program to keep track of “moody” days of your girlfriend for each month (the same way Android/iOS period & ovulation app), what kind of Python 3 function would you use ?

A. A list
B. A dictionary
C. A string
D. A tuple

65. Which one of the following is NOT a valid Python 3 command ?

A. input()
B. Print()
C. float()
D. for i in range(9):

66. Is the following variable name is a valid name?
University of Calgary = Calgary

A. Yes
B. No

67. Dictionary key index number must always increase by one (and exactly one only) integer value.

A. True
B. False

68. On the following Python 3 code, what is “.rstrip()” do ?
data = input(“Please enter your name: “).rstrip()

A. Take the white space out of the end print statement.
B. To add the carriage return into the input statement.
C. To add the white space formatting at the end of the print statement.
D. Take the carriage return out of the input statement.

69. How do you exit a Python 3 script in an even of an error ?

A. closed()
B. quit(-1)
C. quit()
D. fclosed()
E. closed(-1)

70. How do you take a value from one function and pass it to another ?

A. Take a reading from the first function and pass it to a variable. Then pass the variable to the next function.
B. Take a return from the reading function and pass the return to a variable. Then pass the variable to the next function.
C. After the writing the two functions, make first function equals to the second function. eg. fun1() = fun2()
D. In Python 3, you cannot take data from one function and pass it to another. It is not a valid argument.

71. Why would this simple function would not work?

…. print (“Done”)


A. The function is not defined.
B. The print statement formatting is wrong.
C. The function failed to return any useful data.
D. The indentation pf the function is wrong.

72. If you want to iterate all the key values in a Python 3 dictionary name dic, how do you do that in a for loop ?

A. for k in keys.dic():
B. for j in key.dic():
C. for i in dic.key():
D. for p in dic.keys():

73. Which of the following languages can be used to write an algorithm for Python 3? (Select all that apply)

A. Any version of Python up to and including Python 3
B. English
C. Binary Code
D. Japanese
E. Machine code

======= END ========
Answers (highlight/copy the text to see the answers!): 1-C, 2-D, 3-B, 4-D, 5-C, 6-E, 7-A, 8-B, 9-E, 10-C, 11-C, 12-A, 13-D, 14-B, 15-D, 16-C, 17-A, 18-A, 19-D, 20-A, 21-A, 22-C, 23-D, 24-E, 25-A, 26-A, 27-C, 28-A, 29-D, 30-D, 31-B, 32-A, 33-E, 34-B, 35-A-B-D-E, 36-B, 37-D, 38-B, 39-A, 40-C, 41-A, 42-B, 43-D, 44-A, 45-B, 46-A-B, 47-B, 48-A, 49-B, 50-B, 51-A, 52-A, 53-D, 54-D, 55-A, 56-D, 57-A, 58-B, 59-D, 60-A, 61-C, 62-D, 63-D, 64-B, 65-B, 66-B, 67-B, 68-D, 69-C, 70-B, 71-A, 72-D, 73-B-D,….
Explanations: 12: It can be created in several dimensions including 3D and up. However, it is possible to create an empty dictionary and/or an empty key, which may be classify as a 1D system. 23: … and it’s memory may change with time as read and write cycles varies from time to time. 42: An error called, Undefined Variable will cause the program to crash (or go into an except block). 50: Comments are for humans not for machines. Strong constructive comments add value to a program by giving information about what each segment of code suppose to do when it runs. It has no value to the computer itself in Python 3. However, on certain programming languages such as PHP it can add additional parameters to the code. 58: The print() statements provide information for the end user and can NOT be used to create code itself. It adds value to the code, but often is independent of the code itself, except for display purposes. 66: Variable name appear on the left side of the assignment operator and the variable itself appear on the right side. On the left side “University of Calgary” is NOT a valid variable name because you cannot have white space(in this case space) between variable letters. You MAY fix this by replacing a the variable name with, UniversityOfCalgary 67: No, it can increase by any integer value when moving from one key to another. 72: Regardless of the letter/name we place for iteration (i,j,k,p), the right format is dicName.keys() to all in all the keys appear in the dictionary named dic.

Standing on the shoulders of giants