CS150 - Sample Final

Similar documents
CS150 Sample Final. Name: Section: A / B

CS150 Sample Final Solution

CS 1301 Exam 1 Fall 2010

CS 1301 Exam 1 Fall 2010

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY

YOUR NAME PLEASE: *** SOLUTIONS ***

: Intro Programming for Scientists and Engineers Final Exam

Midterm Exam 2B Answer key

CSCI 101 Midterm Sample Questions

APPM 2460 Matlab Basics

Short Answer Questions (40 points)

A Function that Calls Itself. It Doesn t Do Anything! Defining a Recursive Function. 2) Base Case. 1) Divide and Conquer 11/22/2010

Structure and Interpretation of Computer Programs Spring 2017 Mock Midterm 1

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Intro. Scheme Basics. scm> 5 5. scm>

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

CS 115 Exam 3, Spring 2014

Admin. How's the project coming? After these slides, read chapter 13 in your book. Quizzes will return

CS 115 Exam 2 (Section 1) Spring 2017 Thu. 03/31/2017

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

CDA 3103 Computer Organization Exam 1 (Sep. 22th, 2014)

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

CS112 Spring 2012 Dr. Kinga Dobolyi. Exam 2. Do not open this exam until you are told. Read these instructions:

The Beauty and Joy of Computing 1 Lab Exercise 9: Problem self-similarity and recursion - Python version


You will not be tested on JUnit or the Eclipse debugger. The exam does not cover interfaces.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

An Introduction to Python

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

CS150 - Assignment 5 Data For Everyone Due: Wednesday Oct. 16, at the beginning of class

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Exam 1 Format, Concepts, What you should be able to do, and Sample Problems

This exam has 10 pages including the title page. Please check to make sure all pages are included.

Final Exam Practice Questions

UNIVERSITY OF TORONTO SCARBOROUGH. Wnter 2016 EXAMINATIONS. CSC A20H Duration 2 hours 45 mins. No Aids Allowed

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Practical Questions CSCA48 Week 6

CS150 - Final Cheat Sheet

Text Input and Conditionals

Math Exam 2 Solutions. Problem 1 (8 points) Problem 2 (8 points) Problem 3 (12 points) Problem 4 (26 points) Problem 5 (14 points)

Structure and Interpretation of Computer Programs

You must bring your ID to the exam.

15110 PRINCIPLES OF COMPUTING SAMPLE EXAM 2

Final Exam in Algorithms and Data Structures 1 (1DL210)

COS 126 General Computer Science Fall Written Exam 1

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

University of Washington CSE 140 Introduction to Data Programming Winter Midterm exam. February 6, 2013

Control Structures 1 / 17

Practice Exam 2. CS112 Computation for the Sciences

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Practice Midterm Examination

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS 455 Final Exam Fall 2013 [Bono] December 12, 2013

Structure and Interpretation of Computer Programs

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE. Sample Final Exam

CS 115 Exam 2 (Section 1) Fall 2017 Thu. 11/09/2017

Q1 Q2 Q3 Q4 Q5 Q6 Total

CS1 Lecture 22 Mar. 6, 2019

Computer Science AP/X CSCI-140/242 Polygons Lab 2 8/30/2018

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Control Flow: Loop Statements

NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.

a b c d a b c d e 5 e 7

AP Computer Science Principles Programming Question Tips. 1: Which algorithm/code segment achieves some result?

CS1800 Discrete Structures Final Version A

EECS 140/141 Introduction to Digital Logic Design Fall Semester 2016 Exam #1 Date: 3 October 2016

CS100 Spring 2017 Final

CSE 247 Data Structures and Algorithms Fall Exam I

UNIVERSITY OF TORONTO SCARBOROUGH. December 2017 EXAMINATIONS. CSCA20H3 Duration 3 hours. Examination Aids: Instructor: Bretscher

Python lab session 1

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

Number Sense Workbook 7, Part 1: Unit 3

University of Washington CSE 140 Data Programming Winter Final exam. March 11, 2013

CS 1301 Exam 2 Fall 2010

n! = 1 * 2 * 3 * 4 * * (n-1) * n

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

ADD AND NAME WORKSHEETS

ICS 31 UC IRVINE. ############## YOUR UCINET #### K E Y #### ############## Second Midterm YOUR NAME YOUR STUDENT ID (8 DIGITS)

CS 1110 Prelim 2 November 14th, 2013

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

Repetition Through Recursion

(b) If a heap has n elements, what s the height of the tree?

My dear students, Believe in yourselves. Believe in your abilities. You can DO this! -Dr. M

Lab 7: OCaml 12:00 PM, Oct 22, 2017

CS 251 Intermediate Programming Java Basics

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

Read this before starting!

CS 1301 Exam 1 Fall 2014

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

1 Format. 2 Topics Covered. 2.1 Minimal Spanning Trees. 2.2 Union Find. 2.3 Greedy. CS 124 Quiz 2 Review 3/25/18

Problem Solving for Intro to Computer Science

PYTHON FOR MEDICAL PHYSICISTS. Radiation Oncology Medical Physics Cancer Care Services, Royal Brisbane & Women s Hospital

CS 1301 Exam 1 Fall 2014

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

Transcription:

CS150 - Sample Final Name: Honor code: You may use the following material on this exam: The final exam cheat sheet which I have provided The matlab basics handout (without any additional notes) Up to two pages of notes with 10 point font or larger (or comparable hand-written) Beyond that, this exam is closed book, closed notes, closed computer, closed calculator, etc. You do not need to include comments or constants in your code. For grading: http://www.xkcd.com/986/ 1

1: Odd and ends 18 2: Matlab 10 3: Dictionaries 9 4: Recursion 13 5: We ve got problems 7 6: Sequences 12 Total 69 1. [18 points] Odds and ends (a) [5 points] T/F: For each statement below, indicate whether the statement is true or false. If you make an assumption about something in the statement, please note it underneath your answer. For all possible inputs insertion sort is faster than selection sort. 01101 in binary is equal to 13. * in R for vectors is equivalent to.* in Matlab for vectors. If we typed the two first statements below, s would have the value shown below: >>> s = "this is a test" >>> s.upper() THIS IS A TEST >>> s THIS IS A TEST For an O(n 2 ) algorithm, if it takes 10 seconds to run on 1000 items we would expect it to take approximately 40 seconds on 2000 items. 2

(b) [3 points] Add the following two binary numbers: 01101101 with 00111101. Make sure to show your work (i.e. carries, etc.). (c) [5 points] Given the following code in a file called my file.py: # some code up here if name == " main ": print "bananas " * 2 print "apples"[:-3] print "End of " + name What would happen if the file were run with the green arrow in Wing? What would happen if we then typed import my file in the python shell? 3

(d) [5 points] The xor (exclusive or) of two boolean values is True if either value is True, but is False if they are both True or both False. The following is the truth table for xor: a b xor(a,b) F F F F T T T F T T T F Write a function xor that calculates the exclusive or of two boolean variables. Remember to use good boolean style! 4

2. [10 points] Matlab (a) [4 points] If we initialized x in Matlab to be the following matrix: 10 4 3 1 7 15 6 4 2 What would be displayed by the following: i. disp(x(:, 1)) ii. disp(sum(x)) iii. disp(sum(x > 5)) iv. disp(sum(sum(x.* (x > 5))) (b) [6 points] Write a Matlab function that takes a matrix as a parameter and returns the difference between the largest and smallest values in that matrix. 5

3. [9 points] Dictionaries (a) [6 points] Write a function called dictionary add that takes two dictionaries whose values are numbers and returns a dictionary containing the keys found in both dictionaries. The value associated with these keys should be the sum of the values in the two dictionaries. If a key does not occur in BOTH dictionaries, then it should not occur in the returned dictionary. For example: >>> d1 = {"a":1, "b":2, "c":3} >>> d2 = {"a":4, "c":5, "d": 6} >>> dictionary_add(d1, d2) { a : 5, c : 8} (b) [3 points] In one sentence, what does the following function do if dict1 and dict2 are dictionaries? Be concise, but precise. def mystery(dict1, dict2): for key in dict1: if not key in dict2: return False return True 6

4. [13 points] Recursion (a) [5 points] Draw what the following function would draw on the screen for the call mystery(80, 5) assuming the turtle started at the center of the screen (0,0) and was facing to the right. In addition, annotate your drawing with the final turtle location. from turtle import * def mystery(length, levels): if levels == 0: dot() forward(length) left(90) mystery(length/2, levels-1) right(90) backward(length) 7

(b) [3 points] What does the following function return for the input CS class : def mystery(s): if s == "": return s if s[0].isupper(): return s[0].lower() + mystery(s[1:]) return s[0].upper() + mystery(s[1:]) (c) [5 points] Write a function called sum squared that takes a list of numbers as a parameter and returns the sum of the each of the numbers squared. For example, sum squared([1, 2, 3]) would return 14 (that is, 1*1 + 2*2 + 3*3=14). If you write the function recursively you will receive up to 5 points. If do not use recursion you can only receive a maximum of 3.5 points. 8

5. [7 points] We ve got problems... (a) [4 points] The following program expects two command-line arguments. The program is supposed to print the usage if the user doesn t supply exactly two arguments, or run the function my function(...) if the user does supply two arguments. However, the program has two general problems. Fix the problems so that it works as expected. You don t have to rewrite the function, just mark-up the one below. import sys def my_function(a, b): # some function stuff def print_usage(): print "my_program.py <number> <number>" number1 = sys.argv[0] number2 = sys.argv[1] if len(sys.argv)!= 2: print_usage() my_function(number1, number2) (b) [3 points] The following function attempts to check if a number is prime or not, but it has a mistake. Correct the function: import math def isprime(num): """Returns True if the input is a prime number, False otherwise""" for i in range(2, int(math.sqrt(num)+1)): if num % i == 0: return False return True 9

6. [12 points] Sequences (a) [6 points] Write a function called unique that takes a string as a parameter and returns True if all of the characters in the string are unique (i.e no repeats) or False if there are duplicate letters. You will only get partial credit if your function is not efficient. 10

(b) [6 points] Write a function reverse sentence that takes a string representing a sentence and returns a new string where all the words in the sentence are in reverse order. You can assume a word is anything separated by a space. For example: >>> reverse_sentence("this is a sentence") sentence a is this Have a great break! http://www.gardyloocomics.com/2008/06/21/huddling/ 11