Shared Flashcard Set

Details

Software Engineering
Downing's class
24
Computer Science
Undergraduate 3
09/21/2011

Additional Computer Science Flashcards

 


 

Cards

Term
Explain the following code snippet:
sum_1 :: Num a => [a] -> a
sum_1 a = foldl (+) 0 a
Definition
sum_1 takes in an array of type a and returns an element of type a.
Produces a sum by adding them together from left (fold l) to right. If the (+) parameter were a different operator, it would perform a different operation
Term
Explain the following code snippet:
sum_2 :: Num a => [a] -> a
sum_2 [] = 0
sum_2 (x : xs) = x + sum_2 xs
Definition
This is a for each loop that recursively adds up the sum; xs is rest(x) as I understand
Term
How do you declare an iterator in python? How would you employ that iterator in a try/except block?
Definition
Example:

def sum_2 (a) :
s = 0
p = iter(a)
while True :
try :
s += p.next()
except StopIteration :
break
return s
Term
Write a sum function over a collection using a for each loop in python.
Definition
def sum_1 (a) :
s = 0
i = 0
while i != len(a) :
s += a[i]
i += 1
return s

def sum_2 (a) :
s = 0
p = iter(a)
while True :
try :
s += p.next()
except StopIteration :
break
return s

def sum_3 (a) :
s = 0
for v in a :
s += v
return s
Term
Write a sum function using an operator in python.
Definition
def sum_4 (a) :
return reduce(operator.add, a, 0)
Term
Write a recursive sum function in python that uses the code snippet (a[1:])
Definition
def sum_5 (a) :
if not a :
return 0
return a[0] + sum_5(a[1:])
Term
Write a recursive sum function in python that uses an iterator (you will use an aux func)
Definition
def sum_6_aux (p) :
try :
v = p.next()
except StopIteration :
return 0
return v + sum_6_aux(p)

def sum_6 (a) :
return sum_6_aux(iter(a))
Term
What are the different ways of saying if/else in Haskell?
Definition
f :: Int -> Int
f n =
if n < 0 then
-1
else if n > 0 then
1
else
0

| n < 0 = -1
| n > 0 = 1
| otherwise = 0

h (-2) = -1
h 3 = 1
h _ = 0
Term
private static void g (int[] p) {
++p[1];}

int a[] = {2, 3, 4};
g(a);
assert a[1] == ?????;

What evaluates to true?
Definition
????? = 4
Term
How did Beck at Chrysler eliminate the need for exhaustive
specifications and a large 'quality assurance' team?
Definition
writing tests before writing code
Term
What is the output of the following program?

a = [2, 3, 4]
b = a
b += [5]
print a is b

a = (2, 3, 4)
b = a
b += (5,)
print a is b
Definition
True
False
Term
What is the Java code for accessing an IllegalAccessException's superclass? What is that superclass? Name the classes that are 1 and 2 places up the inheritance hierarchy from IllegalAccessException's superclass
Definition
IllegalAccessException.class.getSuperclass() == Exception.class

Exception.class.getSuperclass() == Throwable.class;
Throwable.class.getSuperclass() == Object.class;
Term
Name the three class hierarchical levels above NameError(the top one is object)?
Definition
NameeError
Term
What is the complexity of inserting an element into a sorted array? What about a binary heap?
Definition
O(n)
O(log n)
Term
What are the two most common problems with releasing changes?
Definition
slow merges
lost changes
Term
What is required to be able to refer to a non-static member of class
(e.g. a non-static data member, a non-static method)?
Definition
an instance of the class
Term
What is required to be able to refer to a static member of class
(e.g. a static data member, a static method)?
Definition
the name of the class
Term
In Python, are parameters passed by value or by reference? What about Java? Define the differences
Definition
Both are passed by value.

Pass-by-value
The actual parameter (or argument expression) is fully evaluated and the resulting value is copied into a location being used to hold the formal parameter's value during method/function execution. That location is typically a chunk of memory on the runtime stack for the application (which is how Java handles it), but other languages could choose parameter storage differently.
Pass-by-reference
The formal parameter merely acts as an alias for the actual parameter. Anytime the method/function uses the formal parameter (for reading or writing), it is actually using the actual parameter.
Term
What does the following code print?

def try_to_change_list_contents(the_list):
print 'got', the_list
the_list.append('four')
print 'changed to', the_list

outer_list = ['one', 'two', 'three']

print 'before, outer_list =', outer_list
try_to_change_list_contents(outer_list)
print 'after, outer_list =', outer_list
Definition
List is mutable and we are changing the object we are referring to.

before, outer_list = ['one', 'two', 'three']
got ['one', 'two', 'three']
changed to ['one', 'two', 'three', 'four']
after, outer_list = ['one', 'two', 'three', 'four']
Term
What happens in python when we try to change the reference that was passed in as a parameter?

def try_to_change_list_reference(the_list):
print 'got', the_list
the_list = ['and', 'we', 'can', 'not', 'lie']
print 'set to', the_list

outer_list = ['we', 'like', 'proper', 'English']

print 'before, outer_list =', outer_list
try_to_change_list_reference(outer_list)
print 'after, outer_list =', outer_list
Definition
The original list remains unchanged.

before, outer_list = ['we', 'like', 'proper', 'English']
got ['we', 'like', 'proper', 'English']
set to ['and', 'we', 'can', 'not', 'lie']
after, outer_list = ['we', 'like', 'proper', 'English']
Term
In Java, what is the following statement equivalent to?


return (n < 0) ? -1 : (n > 0) ? 1 : 0;}
Definition
if (n < 0)
return -1
else if (n > 0)
return 1
else return 0
Term
What defines a tuple vs. a list?
Definition
A tuple is an immutable list.
Term
According to Extreme Programming, what should you do when you are working on a story unlike anything you have done before? Move through it very quickly or plan it out slowly and try to get it perfect the first time?
Definition
Move through it quickly
Term
In XP you should release _____ and _____
Definition
early and often
Supporting users have an ad free experience!