Docsity

Practice Exam 2 - Introduction to Computing | CS 1301, Exams of Computer Science

Toggle sidebar
bg1
Your Name: _______________________________ Your TA's Name: ________________________
1 / 8
CS 1301 CS1 with Robots Fall 2008 โ€“ Exam 2
You have 80 minutes for this exam.
1. Vocabulary Matching: (15 points)
Write the number from the correct definition in the blank next to each term on the left:
___compound data type
___slice
___traverse
___mutable
___increment
___decrement
___element
___aliases
___sequence
___nested list
___clone
___immutable type
___dictionary
___recursion
___iteration
1. A part of a string specified by a range of indices.
2. A collection of key-value pairs that maps from keys to
values.
3. A compound data type whose elements can be assigned new
values.
4. The process of calling the function that is currently
executing.
5. Repeated execution of a set of statements using either a
recursive function call or a loop.
6. Multiple variables that contain references to the same
object.
7. To iterate through the elements of a set, performing a
similar operation on each.
8. Any of the data types that consist of an ordered set of
elements, with each element identified by an index.
9. A list that is an element of another list.
10. To increase the value of a variable by one.
11. To decrease the value of a variable by one.
12. One of the values in a list (or other sequence). The bracket
operator selects an __________ of a list.
13. A type in which the elements cannot be modified.
Assignments to elements or slices of these types cause an
error.
14. To create a new object that has the same value as an
existing object.
15. A data type in which the values are made up of components,
or elements, that are themselves values.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Practice Exam 2 - Introduction to Computing | CS 1301 and more Exams Computer Science in PDF only on Docsity!

1 / 8

CS 1301 CS1 with Robots Fall 2008 โ€“ Exam 2

You have 80 minutes for this exam.

  1. Vocabulary Matching: (15 points) Write the number from the correct definition in the blank next to each term on the left:

___compound data type ___slice ___traverse ___mutable ___increment ___decrement ___element ___aliases ___sequence ___nested list ___clone ___immutable type ___dictionary ___recursion ___iteration

  1. A part of a string specified by a range of indices.
  2. A collection of key-value pairs that maps from keys to values.
  3. A compound data type whose elements can be assigned new values.
  4. The process of calling the function that is currently executing.
  5. Repeated execution of a set of statements using either a recursive function call or a loop.
  6. Multiple variables that contain references to the same object.
  7. To iterate through the elements of a set, performing a similar operation on each.
  8. Any of the data types that consist of an ordered set of elements, with each element identified by an index.
  9. A list that is an element of another list.
  10. To increase the value of a variable by one.
  11. To decrease the value of a variable by one.
  12. One of the values in a list (or other sequence). The bracket operator selects an __________ of a list.
  13. A type in which the elements cannot be modified. Assignments to elements or slices of these types cause an error.
  14. To create a new object that has the same value as an existing object.
  15. A data type in which the values are made up of components, or elements, that are themselves values.

2 / 8

  1. Write Code (15 points) Write a function return_smallest that accepts 3 parameters (x,y,z) and returns the smallest of the three. For example, return_smallest(7, -34, 23.8) should return -34. Make sure that your function works for test cases such as return_smallest(5,5,4).
  2. Program Comprehension (3 points)

def n_lines(n):

print "Line!"

if n >= 0:

n_lines(n-1)

How many times will the string โ€œLine!โ€ be printed when n_lines is called with n=4? Number____________

  1. Write Code (2 points) Write a function with infinite recursion named run_forever. Your function should have no parameters, and it should run forever when called (on an ideal computer, in a real computer it would eventually run out of memory.) You may add a print statement if you wish.

4 / 8

  1. Python Expression Evaluation (20 points) For this question, assume the following statements have already been entered and interpreted: a = [ True, 7, ["Cherry", "Apple","Plum"], 56, [4, 5, 6], 84 ] b = a c = a[0:4] d = a[2] d[2] = "Peach"

Pretend that you are the Python Interpreter (IDLE window). What do you print or return when each of the following statements are entered?

Example: a[0] Result: True

Example: a[4:6] Result: _ [ [4,5,6], 84 ]_

  1. a[4][0] Result:_____________________________
  2. d Result:_____________________________
  3. c Result: _____________________________
  4. a[2][2] Result: _____________________________
  5. b[:2] Result: _____________________________
  6. b[-2] Result: _____________________________
  7. c[-2] Result: _____________________________
  8. print "Pumpkin %.3f" %3.1459 Result: _____________________________
  9. (5 > 10) or (5 > 3) Result: _____________________________
  10. 34 % 10 Result: _____________________________

5 / 8

  1. Write Code ( 10 points) Write a function changeLetter(aString, index, newLetter) that will replace the letter stored at index in aString with the contents of newLetter and return the new string without modifying the original string! For example, changeLetter("Python is great!", 10, "G") will return the string "Python is Great!"
  2. Write Code ( 5 points) Write a function changeValue(aList, index, newValue) that will replace the element stored at index in aList with the contents of newValue. It should NOT return the list. For example after the following commands: a = [5, True,"Test",10]

changeValue( a, 3, "Hi!")

The list a will be [5, True,"Test","Hi!"]

7 / 8

  1. Write Code! (15 points)

Write a function reverseList(aList) that will return a reversed copy of aList. For example, after the following: a = [ 5, 10, True, "Hi!"]

b = reverseList(a)

The list b = ["Hi!", True, 10, 5], while a = [5, 10, True, "Hi!"].

  1. Write Code: (10 Points)

Write a function findJ( aString ) that uses a while loop to find the index of the first occurrence of the letter 'J' in the aString parameter. The function should return the index it found. For example, findJ("This is Jays String") should return the number 8. Note that you should find both UPPERCASE J's and lowercase j's! If you do not find a J or a j you should return -1.

8 / 8 Extra Credit (1 point each)

What is the one sensor on the Scribbler that does not detect light of one form or another?


What is the decimal representation of the binary number { 101101}? ____________

What is the hexadecimal representation of the decimal number 34? _____________

What does CSS stand for? C__________ S________________ S_______________