1.
#!/usr/bin/env python
#What does the following code do?
import sys
in = sys.stdin.readline()
a = in[:-1]
p = a[::-1]
print a==p
Correct Answer
C. Checks if input is a palindrome (reversed string is the same string).
Explanation
in=sys.stdin.readline()
#returns input from standard, including the \n (new line) character.
a = in[:-1]
#stores everything by the last character (\n in this case)
p = a[::-1]
#stores the reverse using stride
#negative means walk backwards, 1 means very every 1th element.
print a==p
#checks to see if contents are equal, compares the strings
2.
"""
the set constructor REQUIRES:
Select all that apply
"""
Correct Answer
E. An iterable container with hashable objects
Explanation
s = set([A(), A()])
s = set(["abc", "def"])
s = set([(2, 3), (4, 5)])
s = set([frozenset([2, 3]), frozenset([4, 5])])
#s = set([ set([2, 3]), set([4, 5])]) # TypeError: set objects are unhashable
#s = set([[2, 3], [4, 5]]) # TypeError: list objects are unhashable
3.
"""
Given any two sets s1 and s2,
Select all that apply:
"""
Correct Answer(s)
A. S1|s2 == (s1^s2) | (s1&s2)
B. S1^s2 == (s2|s1)-(s1&s2)
C. S1-s1 == s1^s1
D. S1-s1 == s2^s2
E. S1&s2
F. S1
G. S1&s2
I. S1&s2 = (s1|s2) - (s1^s2)
Explanation
The given answer is correct because it accurately represents the properties of set operations. The first statement states that the union of s1 and s2 is equal to the symmetric difference of s1 and s2, combined with the intersection of s1 and s2. The second statement states that the symmetric difference of s1 and s2 is equal to the union of s2 and s1, minus the intersection of s1 and s2. The third statement states that the difference between s1 and itself is equal to the symmetric difference of s1 and s1, which is an empty set. The fourth statement states that the difference between s1 and itself is equal to the symmetric difference of s2 and s2, which is also an empty set. The fifth statement states that s1 and s2 have a non-empty intersection. The sixth statement states that s1 is a subset of s1. The seventh statement states that s1 and s2 have a non-empty intersection. The eighth statement states that the union of s1 and s2 is equal to the symmetric difference of s1 and s2, combined with the intersection of s1 and s2.
4.
"""
What is the output of the following program?
"""
#!/usr/bin/env python
x = [1, 2, 3]
y = [4, 5, 6, 7]
print [v + w for v in x if v % 2 for w in y if w % 3]
Correct Answer
B. [5,6,8,7,8,10]
Explanation
The program uses list comprehension to iterate through the elements of list x and list y. It first checks if the element in x is divisible by 2 using the condition "if v % 2", and then checks if the element in y is divisible by 3 using the condition "if w % 3". If both conditions are true, it adds the sum of the two elements to the new list. In this case, the elements [5, 6, 8, 7, 8, 10] satisfy both conditions, so they are added to the output list.
5.
"""
What is the output of this program? Or does it cause an error?
"""
#!/usr/bin/env python
a = '\
\
\
abc\
\
\
'
print len(a)
Correct Answer
3
Explanation
The program assigns the string value '\abc\' to the variable 'a'. The backslashes before and after 'abc' are used to escape the newlines, so the string is actually 'abc'. The len() function is then used to determine the length of the string, which is 3. Therefore, the output of the program is 3.
6.
"""
After the following code, which of the following are valid? Select all that apply.
"""
a = [1]
Correct Answer(s)
B. A.append(1)
C. A[0]=3
D. A.append([1,2,3])
F. A.extend([1,2,3])
G. A.append([])
7.
"""
Which of the following are iterable?
"""
Correct Answer(s)
A. Range(0,5)
B. Range(5)
C. List([0,1,2,3,4])
D. Set([0,1,2,3,4])
E. Dict({1:a})
F. "abcde"
G. Set([0,1,2,3,4])
Explanation
The correct answer includes all the options that are iterable. An iterable is an object that can be looped over or iterated upon. In this case, range() objects, list, set, dict, string, and set objects are all iterable. Therefore, the options range(0,5), range(5), list([0,1,2,3,4]), set([0,1,2,3,4]), dict({1:a}), "abcde", and set([0,1,2,3,4]) are all iterable.
8.
Which of the following are hashable?
Select all that apply.
Correct Answer(s)
B. Tuple(1,2,3)
D. Frozenset([1,2,3])
E. "abc123"
F. 12345
G. A() (where A is a valid class)
Explanation
Hashable objects are immutable objects that have a hash value that never changes during its lifetime. The objects that are hashable in this case are: tuple(1,2,3), frozenset([1,2,3]), "abc123", 12345, and A() (where A is a valid class). The set([1,2,3]) and list([1,2,3]) are not hashable because they are mutable objects and their hash value can change if their elements are modified.
9.
Which of the following can make a copy of a frozenset f
f = frozenset([1,"abc",3.45])
Correct Answer(s)
B. C = set()
c |= f
C. C = set(f)
Explanation
The correct answer is c = set() because the set() function creates an empty set, and then the |= operator performs an in-place union of the empty set with the frozenset f, resulting in a copy of f being made. Similarly, the set(f) function creates a new set containing the elements of f, also resulting in a copy of f being made.
10.
What is the output of the following program?
[x, y] = z = 2, 3
print x, y, z
Correct Answer(s)
2 3 (2, 3)
Explanation
The program assigns the values 2 and 3 to the variables x and y, and also assigns the tuple (2, 3) to the variable z. When the print statement is executed, it displays the values of x, y, and z, which are 2, 3, and (2, 3) respectively.
11.
"""
What is the output to the following program?
"""
def f (x, y, *z) :
return [x, y, z]
def g (x, y, **z) :
return [x, y, z]
t = (2, 3, 4)
print f(*t)
d = {"u" : 4, "y" : 3, "x" : 2}
print g(**d)
d = {"u" : 4, "y" : 3, "v" : 5}
print g(2, **d)
Correct Answer(s)
[2, 3, (4,)]
[2, 3, {'u': 4}]
[2, 3, {'u': 4, 'v': 5}]
Explanation
The program defines two functions, "f" and "g". The function "f" takes in three arguments, "x", "y", and "*z", where "*z" represents a variable number of arguments. It returns a list containing "x", "y", and "z". The function "g" takes in two arguments, "x" and "y", and "**z", which represents a variable number of keyword arguments. It returns a list containing "x", "y", and "z".
In the first print statement, the tuple "t" is unpacked using the "*" operator, so it is passed as separate arguments to the function "f". Since "t" contains three elements, "x" is assigned 2, "y" is assigned 3, and "*z" is assigned (4,). Therefore, the output is [2, 3, (4,)].
In the second print statement, the dictionary "d" is unpacked using the "**" operator, so it is passed as keyword arguments to the function "g". "x" is assigned 2, "y" is assigned 3, and "**z" is assigned {"u": 4, "y": 3}. Therefore, the output is [2, 3, {"u": 4}].
In the third print statement, the integer 2 is passed as the first argument to the function "g", and the dictionary "d" is unpacked using the "**" operator. "x" is assigned 2, "y" is assigned 3, and "**z" is assigned {"u": 4, "y": 3, "v": 5}. Therefore, the output is [2, 3, {"u": 4, "v": 5}].
12.
"""
What is the output of the following program?
"""
s=abCbA
print s[4:-6:-2]
Correct Answer(s)
ACa
Explanation
The program is using slicing to get a substring of the variable 's'. The slicing starts at index 4 and goes up to index -6 (excluding -6) with a step of -2. This means it will select every second character in reverse order from index 4 to index -6. In the given string "abCbA", the characters at index 4, 2, and 0 are 'A', 'C', and 'a' respectively. Therefore, the output of the program is "ACa".
13.
"""What is the output of the following program?"""
x = set((1, 2, 3, 5))
y = set([1, 2, 4, 6])
z = (x & y) - (x ^ y)
t = (y ^ x) - (y & x)
print z
print t
Correct Answer(s)
set([1, 2]), any order
set([3, 4, 5, 6]), any order
Explanation
The program creates two sets, x and y, with different elements. The expression (x & y) finds the intersection of the two sets, which is {1, 2}. The expression (x ^ y) finds the symmetric difference of the two sets, which is {3, 4, 5, 6}. Therefore, z is set([1, 2]). Similarly, (y ^ x) finds the symmetric difference of y and x, which is {3, 4, 5, 6}. (y & x) finds the intersection of y and x, which is {1, 2}. Therefore, t is set([3, 4, 5, 6]). The output of the program is set([1, 2]) and set([3, 4, 5, 6]), in any order.
14.
Define the function arraycopy(), iteratively, such that it mimics
Java's System.arraycopy().
a = [2, 3, 4]
b = [2, 3, 4]
arraycopy(a, 0, b, 1, 2)
assert b == [2, 2, 3]
a = [2, 3, 4]
arraycopy(a, 0, a, 1, 2)
assert a == [2, 2, 3]
a = [2, 3, 4]
b = [2, 3, 4]
arraycopy(a, 1, b, 0, 2)
assert b == [3, 4, 4]
a = [2, 3, 4]
arraycopy(a, 1, a, 0, 2)
assert a == [3, 4, 4]
15.
Which properties of this container are true? (These containers are equivalent)
(language Container)
c++ vector
java ArrayList
python list/tuple
haskell array
let b = beginning
m = middle
e = end
Correct Answer(s)
A. Inserting at b and m are O(n)
B. Deleting from b or m are O(n)
D. Adding to e is ~O(1)
G. Indexing is O(1)
Explanation
~O(1) means amortized constant, meaning occasionally it might be linear, but will usually be O(1)
16.
Which properties of this container are true? (These containers are equivalent)
(language Container)
c++ deque
java Deque
python deque
haskell n/a
let b = beginning
m = middle
e = end
Correct Answer(s)
B. Adding to b and e are ~O(1)
D. Deleting from b and e are O(1)
G. Indexing is O(1)
Explanation
~O(1) means amortized constant, meaning occasionally it might be linear, but will usually be O(1)
17.
Which properties of this container are true? (These containers are equivalent)
(language Container)
c++ list
java LinkedList
python n/a
haskell list/tuple
Note: assume you already have a pointer to the location you want to insert or remove
Correct Answer(s)
A. Inserting is O(1)
D. Deleting is O(1)
J. Indexing is O(n)
Explanation
~O(1) means amortized constant, meaning occasionally it might be linear, but will usually be O(1)
18.
"""
What does the following code output?
"""
a = [[0,0]]*2
a[0][0] = 1
print a
Correct Answer
C. [[1,0], [1,0]]
Explanation
The address of the inner list is being duplicated, not the list. So both a[0] and a[1] point to the same list.
If one is change, both are changed.
19.
Suppose l is a list. What does it mean to say
if l:
Correct Answer
D. If the list is not empty
Explanation
Think of an empty list as 0, which is also equivalent to False
Anything other than empty is anything other than 1 is True
20.
"""
What is the output to the output of the following code?
If more than one error is present, select all that apply
"""
L = [None]*10
L[0] = 123
L[1] = "abc"
L[2] = 2.75
L[3:] = dict({"k":"v"})
print L
Correct Answer
C. [123, 'abc', 2.75, 'k']
Explanation
The code initializes a list L with 10 elements, all set to None. Then, the code assigns specific values to the first three elements of the list. However, when trying to assign a dictionary to a slice of the list (L[3:]), an error occurs because a list slice assignment must be of the same length. Additionally, the code tries to assign a non-iterable object ('k') to the fourth element of the list, which is not allowed. Therefore, the correct answer is [123, 'abc', 2.75, 'k'].