10/20/13
Exam Feedback | Learn to Program: The Fundamentals
Feedback — Final Exam You submitted this exam on Tue 8 Oct 2013 10:50 PM EET (UTC +0200). You got a score of 13.00 out of 13.00.
Read This This exam is an assessment tool, not a teaching tool, and several of the questions are intended to be challenging. Our goal is to find out how much you know and how proficient you are with the material. You may not have time to check all of your answers. You can use any resources that you like, including running your code and using the visualizer, as long as you do not ask anyone any questions, anywhere, or otherwise communicate with anyone about the content of the exam, even after you have submitted. If you do, you will be expelled from the course. In particular: Do not ask for clarification on an exam question, either on the course forums or anywhere else. Do not ask about or post any parts of your exam solutions. Do not make comments about the exam, including the length of the exam or how hard or easy the questions are. Do not help anyone else with their exam. When you first open the exam, read all of it and look for questions that you know how to answer right away. We will be monitoring what people submit. If an exam question is somehow flawed, rather than fix the question, we will simply not count it. You can only submit once, and you will not be told your score until after the course ends: after 14 October 2013. Save frequently. We recommend that you save after every question. Even if you quit your browser, the countdown will continue. Good luck!
Question 1 Select the expression(s) that evaluate to an int value.
https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
1/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
Your Answer
Score
8.0 % 4
0.25
8+3
0.25
3/4
0.25
3 // 4
0.25
Total
Explanation
1.00 / 1.00
Question 2 Consider this code: x=2 y=3-x x=3
After the code above has been executed, what value does y refer to? You entered: 1
Your Answer 1
Score
Total
Explanation
1.00 1.00 / 1.00
Question 3 Consider this code: def f(y): x=y*3 https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
2/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
return y + x
What value is returned by a call on function f with argument 10? You entered: 40
Your Answer
Score
40
Total
Explanation
1.00 1.00 / 1.00
Question 4 Consider this code: first = 'pwn' second = 3 third = 'd'
Write an expression that evaluates to the string 'pwn3d' using only variables first, second, third,
one call on function str, and string concatenation.
Do not use unnecessary parentheses: you need them for the function call, but nothing else. You entered: first + str(second) + third
Your Answer first + str(second) + third
Score
Total
https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
Explanation
1.00
1.00 / 1.00
3/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
Question 5 Consider this function: def larger_of_smallest(L1, L2): '''(list of int, list of int) -> int Return the larger of the smallest value in L1 and the smallest value in L2. Precondition: L1 and L2 are not empty. >>> larger_of_smallest([1, 4, 0], [3, 2]) 2 >>> larger_of_smallest([4], [9, 6, 3]) 4 ''' return # CODE MISSING HERE
The expression for the return statement is missing. Write it below. Use only the parameters, one call on function max, and two calls on function min. Do not use unnecessary parentheses: you need them for the function calls, but nothing else. Do not include the word return; just write the expression. You entered: max(min(L1),min(L2))
Your Answer max(min(L1),min(L2))
Score
Total
Explanation
1.00 1.00 / 1.00
Question 6 https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
4/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
Consider this function: def same_length(L1, L2): '''(list, list) -> bool Return True if and only if L1 and L2 contain the same number of elements. ''' if len(L1) == len(L2): return True else: return False
The function works, but the if statement can be replaced with a single return statement: return # CODE MISSING HERE
Write the missing expression. Use only the parameters, two calls on function len, and operator == once. Do not use unnecessary parentheses: you need them for the function calls, but nothing else. Do not include the word return; just write the expression. You entered: len(L1) == len(L2)
Your Answer len(L1) == len(L2) Total
Score
Explanation
1.00 1.00 / 1.00
Question 7 Consider these two functions; we provide only the headers, type contracts, and a precondition: def moogah(a, b): '''(str, int) -> str''' https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
5/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
def frooble(L): '''(list of str) -> int Precondition: L has at least one element.'''
Below are code fragments that call these two functions in various ways. Select the code fragment(s) below that are valid according to the function headers and the type contracts. Your Answer
Score
0.25
0.25
0.25
0.25
Explanation
frooble(moogah('', 0))
lst = ['a', 'b', 'c'] moogah(lst[0], len(lst))
moogah(frooble(['a']), 'a')
moogah('a', frooble(['a'])) Total
1.00 / 1.00
Question 8 Consider this code: def gather_every_nth(L, n): '''(list, int) -> list Return a new list containing every n'th element in L, starting at index 0. Precondition: n >= 1 >>> gather_every_nth([0, 1, 2, 3, 4, 5], 3) [0, 3] >>> gather_every_nth(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'], 2) ['a', 'c', 'e', 'g', 'i'] ''' https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
6/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
result = [] i=0 while i < len(L): result.append(L[i]) i = # CODE MISSING HERE return result
Write the missing expression. Do not use parentheses. Do not include " i =". Just write the missing expression. You entered: i+n
Your Answer i+n
Score
Total
Explanation
1.00 1.00 / 1.00
Question 9 Consider this code: def get_keys(L, d): '''(list, dict) -> list Return a new list containing all the items in L that are keys in d. >>> get_keys([1, 2, 'a'], {'a': 3, 1: 2, 4: 'w'}) ['a', 1] ''' result = [] for # CODE MISSING HERE if k in L: result.append(k)
https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
7/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
return result
Write the missing code for the first line of the for loop — everything after the word "for", up to and including the colon :. Do not use any parentheses, and do not call any functions or methods. You entered: k in d:
Your Answer k in d:
Score
Total
Explanation
1.00 1.00 / 1.00
Question 10 Consider this code: def are_lengths_of_strs(L1, L2): '''(list of int, list of str) -> bool Return True if and only if all the ints in L1 are the lengths of the strings in L2 at the corresponding positions. Precondition: len(L1) == len(L2) >>> are_lengths_of_strs([4, 0, 2], ['abcd', '', 'ef']) True ''' result = True for i in range(len(L1)): if # CODE MISSING HERE result = False return result
https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
8/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
Write the missing code for the if statement — everything after the word "if", up to and including the colon :. Your answer should be of the form expr1 != expr2:, where expr1 and expr2 are expressions. Use only variables i, L1, L2, indexing, and function len. Do not use parentheses except for the call on len. You entered: L1[i] != len(L2[i]):
Your Answer L1[i] != len(L2[i]):
Score
Explanation
1.00
Total
1.00 / 1.00
Question 11 Consider this function: def double_values(collection): for v in range(len(collection)): collection[v] = collection[v] * 2
Strings, tuples, lists, and dictionaries can all be iterated over, and function len works with all of them. Select the code fragment(s) below that run without error.
Your Answer
Score
0.20
0.20
Explanation
s = '123' double_values(s)
d = {1: 10, 2: 20, 3: 30} https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
9/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
double_values(d)
0.20
0.20
0.20
L = [1, 2, 3] double_values(L)
t = (1, 2, 3) double_values(t)
d = {0: 10, 1: 20, 2: 30} double_values(d) Total
1.00 / 1.00
Question 12 The diagonal of a square nested list goes from the top-left to the bottom-right corner. For example, consider this square nested list: [[1, 3, 5], [2, 4, 5], [4, 0, 8]]
That nested list represents this table, where the values on the diagonal are in bold: 1 3 5 2 4 5 4 0 8
Consider this function: def get_diagonal_and_non_diagonal(L): '''(list of list of int) -> tuple of (list of int, list of int) Return a tuple where the first item is a list of the values on the diagonal of square nested list L and the second item is a list of the rest of the values in L. >>> get_diagonal_and_non_diagonal([[1, 3, 5], [2, 4, 5], [4, 0, 8]]) ([1, 4, 8], [3, 5, 2, 5, 4, 0]) '''
https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
10/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
diagonal = [] non_diagonal = [] for row in range(len(L)): for col in range(len(L)): # CODE MISSING HERE return (diagonal, non_diagonal)
Select the code fragment(s) that correctly complete this function. Your Answer
Score
0.25
0.25
0.25
0.25
Explanation
if row == col: diagonal.append(L[row][col]) non_diagonal.append(L[row][col])
if row == col: diagonal.append(L[row][row]) else: non_diagonal.append(L[row][col])
if row == col: diagonal.append(L[row][col]) else: non_diagonal.append(L[row][col])
if row == col: diagonal.append(L[row][col]) elif row != col: non_diagonal.append(L[row][col]) Total
1.00 / 1.00
Question 13 https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
11/12
10/20/13
Exam Feedback | Learn to Program: The Fundamentals
Consider this code: def add_to_letter_counts(d, s): '''(dict of {str: int}, str) -> NoneType d is a dictionary where the keys are single-letter strings and the values are counts. For each letter in s, add to that letter's count in d. Precondition: all the letters in s are keys in d. >>> letter_counts = {'i': 0, 'r': 5, 'e': 1} >>> add_to_letter_counts(letter_counts, 'eerie') >>> letter_counts {'i': 1, 'r': 6, 'e': 4} ''' for c in s: # CODE MISSING HERE
Write the missing assignment statement. Do not call any functions or methods. Do not use unnecessary parentheses. You entered: d[c] = d[c]+ 1
Your Answer d[c] = d[c]+ 1 Total
Score
Explanation
1.00 1.00 / 1.00
https://class.coursera.org/programming1-002/quiz/feedback?submission_id=2588602
12/12