CMPUT 101 Practice MT2 Solutions

docx

School

University of Alberta *

*We aren’t endorsed by this school

Course

101

Subject

Computer Science

Date

Jan 9, 2024

Type

docx

Pages

4

Uploaded by DrCloverPorcupine11

Report
CMPUT 101 A2: Midterm 1 Name: Instructor: Marianne Morris Percentage: 15% ID: Date: Oct 14, 2021 This midterm exam has 3 questions. This is a closed book exam. No calculators or any other electronic devices allowed. Question 1 / 14 Question 2 / 12 Question 3 / 14 Total / 40
Question 1 : (14 marks) Boolean Logic: Prove the following DeMorgan’s law using the below truth table: (4 marks) NOT(A OR B) = NOT A AND NOT B A B NOT A NOT B A OR B NOT(A OR B) NOT A AND NOT B 0 0 1 1 0 1 1 0 1 1 0 1 0 0 1 0 0 1 1 0 0 1 1 0 0 1 0 0 Use the sum of products to derive the expression E from the following truth table: (4 marks) A B E 0 0 0 0 1 1 1 0 1 1 1 0 Evaluate the following expression when A = True and B = True: (2 marks) (A AND B) OR (NOT A AND NOT B) = (T and T) or (F and F) = T or F = True Evaluate the following statements, given the following list of strings, and write the correct Boolean value after evaluating each statement below: (4 marks) galaxy = ['sun', 'moon', 'star', 'planet', 'comet'] a) 'star' in galaxy or 'planet' not in galaxy = T or F = T b) 'moon' in galaxy and 'mars' not in galaxy = T and T = T c) 'planet' not in galaxy or 'sun' not in galaxy = F or F = F d) 'comet' in galaxy and 'earth' in galaxy = T and F = F E = (NOT A AND B) OR (A AND NOT B)
Question 2 : (12 marks) Trace through the following program and fill the trace table below, then answer the questions that follow. (6 marks) mylist = [5, 2, 1, 3] for i in range(len(mylist)): if i % 2 == 0: mylist[i] = mylist[i] * mylist[i] else: mylist[i] = mylist[i] * 2 print(mylist) i i%2 i%2==0 mylist[i] 0 0 True 25 1 1 False 4 2 0 True 1 3 1 False 6 What is the output after the program finishes executing? In other words, what does the program print in terms of the contents of mylist? Write down the updated list. (2 marks) [25, 4, 1, 6] How does the program determine how many loop iterations to run? How does this change if you use a while loop instead of a for loop ? (2 marks) The for loop iterations are determined by the size of mylist. If we use a while loop, we use a condition instead of a delimiting value (size) but we have to ensure the condition is able to stop the loop. For example, the condition would be i < len(mylist) and i must be incremented every loop iteration. Based on which condition does the program multiply a number in the list by itself? And based on which condition does it multiply a number in the list by 2? (2 marks) If the list index is even it would multiply by itself; otherwise it would multiply by 2.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Question 3 : (14 marks) Trace through the following deciphering program and fill the table below then answer the questions that follow. (10 marks) mylist = [0, 3, 6, 8, 7] mystring = 'cklmnoptuwxyz' decipher = '' # empty string for i in range(len(mylist)): index = mylist[i] decipher = decipher + mystring[index] decipher = decipher + '#' print(decipher) i index mystring[index] decipher 0 0 ‘c’ ‘c’ 1 3 ‘m’ ‘cm’ 2 6 ‘p’ ‘cmp’ 3 8 ‘u’ ‘cmpu’ 4 7 ‘t’ ‘cmput’ What is the final decipher string that gets printed out? What condition decides the length, i.e., the size, of the decipher output string? (2 marks) ‘cmput#’ The size of the output string is determined by the size of my list. Explain how the program generates this decipher string and all the steps that it takes to generate the decipher string. (2 marks) 1) We initialize an empty string called decipher 2) We loop over the list mylist to obtain the value that represents an index in mystring 3) We obtain the character from mystring at the index obtained from mylist 4) We concatenate this character to the new decipher string every loop iteration