Python Practice Exam: Theory, Functions, and Error Handling

cmpt 120 n.w
1 / 11
Embed
Share

Practice answering Python multiple-choice questions and implementing image processing functions while understanding theory, function headers, and error identification in code fragments.

  • Python Practice Exam
  • Theory
  • Functions
  • Error Handling
  • Image Processing

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CMPT 120 Lecture 25 Practice Exam 6

  2. Goals for Today! 1. Practise answering multiple choice questions 2. Practise designing, implementing and testing an image processing function 2

  3. Part 1 - Theory and Understanding 3

  4. Part 1 - Theory and Understanding 1.Consider the list oddNumbers = [0, 1, 3, 5, 7, 9, 11] which Python statement would produce [9, 11] ? A.oddNumbers[5:len(oddNumbers)] B.oddNumbers[5:] C.oddNumbers[5:15] D.All of the above E.None of the above 2.Which Boolean expression produces an error when it executes, if age = 26 ? A.True B.age.isalpha() C."ABC".isdigit() D.All of the above E.None of the above 3.If I am creating a calculator program, capable of addition, subtraction, multiplication and division, which of these variable listings would include the most descriptive and correct variable names for me to use in this program? A.a, b, +, -, *, /, result B.x, y, +, -, *, /, 1_result C.operand1, operand2, operator, result D.All of the above E.None of the above 4.Which of the following Boolean conditions would produce a True value when aNumber is assigned an integer of odd value like 5. A.aNumber % 2 == 0 B.aNumber % 2 == 1 C.aNumber.isOdd( ) D.There is an error in the code. E.None of the above 4

  5. Part 1 - Theory and Understanding 5.Which function header would represent the most generalised version of a function called add( ) ? A.def add( ): B.def add(aValue): C.def add(operand1, operand2): D.All of the above E.None of the above 6.Which line is going to cause an error in the following Python code fragment? A.def printA(aStr): B. aStr += '?' C. print(aStr) return # Main part of program D.printA('45') E.print(aStr) 7.What does the following Python code fragment do? What is its purpose? 8.Why do we hand trace our code? A.To verify that our program is bug free. B.To figure out what our program does. C.To verify whether our program is solving the problem. D.All of the above E.None of the above num = 4 mystery = 1 for time in range(num): mystery *= num A.It raises mystery to the power of num B.It raises num to the power of num C.It produces the sequence 0 1 2 3 D.There is an error in the code. E.None of the above 5

  6. Part 1 - Theory and 9.What does the following Python code fragment produce? 10.What will the following code output? def print_special(n): if n >= 1: print_special(n-1) print(n) #Main part of the program print_special(5) var1 = True var2 = False var3 = True var4 = True var5 = "Maybe!" if var1: print('var1') if var2: print('var2') else: if var3: print('var3') print('var4') else: print(var5) A.var1 var2 B.var1 var3 C.var1 var3 var4 D.An error E.None of the above A.1 2 3 4 5 B.5 4 3 2 1 C.1 2 3 4 5 D.5 4 3 2 1 E.An error F.None of the above 6

  7. Part 1 - Theory and 11.If we wanted to completely test this Python code fragment, how many test cases would we need, i.e., how many different width values should we enter? Note that we are looking at the minimum number of test cases. What do we mean by completely ? We mean that each of our test cases will execute a section of the program, such that all of our test cases will execute all statements in our program at least once. width = int(input("Please, enter a width: ")) if width > 0 : if width > 10: if width % 2 == 0 : # then do something with width else : print("width (%s) is not even." %width) else: print("0 < width (%s) <= 10." %width) else: print("width <= 0.") A.4 B.2 C.1 D.5 E.There are no test cases that could completely test the Python code fragment above. 7

  8. Part 1 - Theory and Understanding 12.What does the following Python code fragment produce? def computeEquation(anEquation): anEquation = "2.6 * 5.6" return # Main part of the program equation = input("Please, enter an equation like '3.1 + 5.67': ") computeEquation(equation) print(equation) A.The equation the user entered. B.2.6 * 5.6 C."2.6 * 5.6" D.There is an error in the code. E.None of the above. 8

  9. Part 2 - Coding 9

  10. Question 13 a) Problem Statement: Write a function isPixelLightColour that takes as parameter a tuple named pixel, containing RGB values of a pixel (3 integers). Requirement: The function should return True if the average of the tuple values is greater than or equal to 128, and False if the average is less than 128. 10 Possible solution: https://repl.it/repls/CalmVillainousAdaware

  11. Question 13 - b) Testing: What is the minimum number of test cases we would need if we wanted to completely test our function? By completely we mean that each of our test cases will execute a section of the program, such that all of our test cases will execute all statements in our program at least once Describe your test cases: this means that for each test case, you need to List the value of the argument with which you will call your function, and List which result (returned value) you will be expecting from your function 11 See: https://repl.it/repls/CalmVillainousAdaware

More Related Content