Software Engineering Midterm 2

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Emorenoe
E
Emorenoe
Community Contributor
Quizzes Created: 1 | Total Attempts: 158
Questions: 20 | Attempts: 158

SettingsSettingsSettings
Software Engineering Quizzes & Trivia

Quiz to prepare for CS373 Midterm 2


Questions and Answers
  • 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

    • A.

      Checks to see if a and p point to the same memory address.

    • B.

      Doesn't compile.

    • C.

      Checks if input is a palindrome (reversed string is the same string).

    • D.

      Stores the first character in a. Stores the first character from that and stores it in p. Checks to see if their contents are equal.

    • E.

      Prints true

    • F.

      Prints false

    • G.

      Stores the first character in input into a. Stores last character in a into p. Compares

    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

    Rate this question:

  • 2. 

    """ the set constructor REQUIRES: Select all that apply """

    • A.

      Only lists, not tuples or frozen sets

    • B.

      Only tuples, not lists or frozen sets

    • C.

      Only lists and tuples, not frozen sets

    • D.

      Any iterable container

    • E.

      An iterable container with hashable objects

    • F.

      Mutable objects

    • G.

      Hashable objects

    • H.

      Anything

    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

    Rate this question:

  • 3. 

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

    • 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

    • H.

      S1|s2

    • I.

      S1&s2 = (s1|s2) - (s1^s2)

    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.

    Rate this question:

  • 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]

    • A.

      [8]

    • B.

      [5,6,8,7,8,10]

    • C.

      [5,7,9,7]

    • D.

      [1,3,4,5,7]]

    • E.

      [2,6]

    • F.

      [5,7,6,8,8,10]

    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.

    Rate this question:

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

    Rate this question:

  • 6. 

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

    • A.

      A.append()

    • B.

      A.append(1)

    • C.

      A[0]=3

    • D.

      A.append([1,2,3])

    • E.

      A.extend(1)

    • F.

      A.extend([1,2,3])

    • G.

      A.append([])

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

    • 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])

    • H.

      01234

    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.

    Rate this question:

  • 8. 

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

    • A.

      Set([1,2,3])

    • B.

      Tuple(1,2,3)

    • C.

      List([1,2,3])

    • D.

      Frozenset([1,2,3])

    • E.

      "abc123"

    • F.

      12345

    • G.

      A() (where A is a valid class)

    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.

    Rate this question:

  • 9. 

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

    • A.

      C = set() c = f.copy

    • B.

      C = set() c |= f

    • C.

      C = set(f)

    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.

    Rate this question:

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

    Rate this question:

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

    Rate this question:

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

    Rate this question:

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

    Rate this question:

  • 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

    • A.

      Inserting at b and m are O(n)

    • B.

      Deleting from b or m are O(n)

    • C.

      Adding to e is O(n)

    • D.

      Adding to e is ~O(1)

    • E.

      Adding to e is O(1)

    • F.

      Indexing is O(n)

    • G.

      Indexing is O(1)

    • H.

      Inserting at b or m are amortized constant

    • I.

      Inserting at b or m are ~O(1)

    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)

    Rate this question:

  • 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

    • A.

      Adding to b and e are O(1)

    • B.

      Adding to b and e are ~O(1)

    • C.

      Adding to b and e are O(n)

    • D.

      Deleting from b and e are O(1)

    • E.

      Deleting from b and e are ~O(1)

    • F.

      Deleting from b and e are O(n)

    • G.

      Indexing is O(1)

    • H.

      Indexing is O(n)

    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)

    Rate this question:

  • 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

    • A.

      Inserting is O(1)

    • B.

      Inserting is ~O(1)

    • C.

      Inserting is O(n)

    • D.

      Deleting is O(1)

    • E.

      Deleting is ~O(1)

    • F.

      Deleting is O(n)

    • G.

      Indexing is O(1)

    • H.

      Indexing is ~O(1)

    • I.

      Indexing is O(log n)

    • J.

      Indexing is O(n)

    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)

    Rate this question:

  • 18. 

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

    • A.

      [[0,0], [0,0]]

    • B.

      Doesn't compile or throws an error

    • C.

      [[1,0], [1,0]]

    • D.

      [[1,1], [1,1]]

    • E.

      [[1,0], [0,0]]

    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.

    Rate this question:

  • 19. 

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

    • A.

      If the first element is true

    • B.

      If all elements are true

    • C.

      If the list is empty

    • D.

      If the list is not empty

    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

    Rate this question:

  • 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

    • A.

      {'k': 'v'}

    • B.

      Error: can't initialize list with None variables

    • C.

      [123, 'abc', 2.75, 'k']

    • D.

      Error: list must be homogeneous (all objects same time)

    • E.

      Error: can't slice list (L[3:])

    • F.

      Error: assignment of slice must be of same length

    • G.

      Error: assignment of slice must be utterable

    • H.

      [123, 'abc', 2.75, 'v']

    • I.

      [123, 'abc', 2.75, {'k':'v'}]

    • J.

      123

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

    Rate this question:

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.