Software Engineering Midterm 2

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Emorenoe
E
Emorenoe
Community Contributor
Quizzes Created: 1 | Total Attempts: 162
| Attempts: 162 | Questions: 19
Please wait...
Question 1 / 19
0 %
0/100
Score 0/100
1. """ 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]

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.

Submit
Please wait...
About This Quiz
Python Quizzes & Trivia

The 'Software Engineering Midterm 2' assesses knowledge in Python programming, focusing on string manipulations, set operations, and list comprehensions. It tests practical coding skills and theoretical understanding, essential... see morefor software engineering students. see less

2. """ What is the output of the following program? """ s=abCbA print s[4:-6:-2]

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".

Submit
3. Suppose l is a list. What does it mean to say if l:

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

Submit
4. """ What does the following code output? """ a = [[0,0]]*2 a[0][0] = 1 print a

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.

Submit
5. """ What is the output of this program? Or does it cause an error? """ #!/usr/bin/env python a = '\ \ \ abc\ \ \ ' print len(a)

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.

Submit
6. #!/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

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

Submit
7. """ Which of the following are iterable? """

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.

Submit
8. Which of the following can make a copy of a frozenset f f = frozenset([1,"abc",3.45])

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.

Submit
9. """ the set constructor REQUIRES: Select all that apply """

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

Submit
10. What is the output of the following program? [x, y] = z = 2, 3 print x, y, z

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.

Submit
11. 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

Explanation

~O(1) means amortized constant, meaning occasionally it might be linear, but will usually be O(1)

Submit
12. Which of the following are hashable? Select all that apply.

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.

Submit
13. """ 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

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'].

Submit
14. 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

Explanation

~O(1) means amortized constant, meaning occasionally it might be linear, but will usually be O(1)

Submit
15. """ After the following code, which of the following are valid? Select all that apply. """ a = [1]

Explanation

not-available-via-ai

Submit
16. """ Given any two sets s1 and s2, Select all that apply: """

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.

Submit
17. 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

Explanation

~O(1) means amortized constant, meaning occasionally it might be linear, but will usually be O(1)

Submit
18. """ 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)

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}].

Submit
19. """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

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.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 20, 2011
    Quiz Created by
    Emorenoe
Cancel
  • All
    All (19)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
"""...
"""...
Suppose l is a list. What does it mean to say if l:
"""...
"""...
#!/usr/bin/env python...
"""...
Which of the following can make a copy of a frozenset f...
"""...
What is the output of the following program?...
Which properties of this container are true? (These containers are...
Which of the following are hashable? Select all that apply.
"""...
Which properties of this container are true? (These containers are...
"""...
"""...
Which properties of this container are true? (These containers are...
"""...
"""What is the output of the following...
Alert!

Advertisement