Sail E0 Webinar

MCQs

Total Questions : 10
Question 1. Which of the following statements is used to create an empty set?
  1.    { }
  2.    set()
  3.    [ ].
  4.    ( )
 Discuss Question
Answer: Option B. -> set()


{ } creates a dictionary not a set. Only set() creates an empty set.


Question 2. What is the output of the code shown below?
s=set([1, 2, 3])s.union([4, 5])s|([4, 5])
  1.    {1, 2, 3, 4, 5}{1, 2, 3, 4, 5}
  2.    Error{1, 2, 3, 4, 5}
  3.    {1, 2, 3, 4, 5}Error
  4.    ErrorError
 Discuss Question
Answer: Option C. -> {1, 2, 3, 4, 5}Error


The first function in the code shown above returns the set {1, 2, 3, 4, 5}. This is because the method of the function union allows any iterable. However the second function results in an error because f unsupported data type, that is list and set.


Question 3. Which of the following lines of code will result in an error?
  1.    s={abs}
  2.    s={4, 'abc', (1,2)}
  3.    s={2, 2.2, 3, 'xyz'}
  4.    s={san}
 Discuss Question
Answer: Option D. -> s={san}


The line: s={san} will result in an error because 'san' is not defined. The line s={abs} does not result in an error because abs is a built-in function. The other sets shown do not result in an error because all the items are hashable.


Question 4. What is the output of the line of code shown below, if s1= {1, 2, 3}?s1.issubset(s1)
  1.    True
  2.    Error
  3.    No output
  4.    False
 Discuss Question
Answer: Option A. -> True


Every set is a subset of itself and hence the output of this line of code is true.


Question 5. What is the output of the following piece of code when executed in the python shell?a={1,2,3} a.intersection_update({2,3,4,5}) a
  1.    {2,3}
  2.    Error, duplicate item present in list
  3.    Error, no method called intersection_update for set data type
  4.    {1,4,5}
 Discuss Question
Answer: Option A. -> {2,3}


The method intersection_update returns a set which is an intersection of both the sets.


Question 6. Suppose t = (1, 2, 4, 3), which of the following is incorrect?
  1.    print(t[3])
  2.    t[3] = 45
  3.    print(max(t))
  4.    print(len(t))
 Discuss Question
Answer: Option B. -> t[3] = 45


Values cannot be modified in the case of tuple, that is, tuple is immutable.


Question 7. What will be the output?d = {"john":40, "peter":45}d["john"]
  1.    40
  2.    45
  3.    john"
  4.    peter"
 Discuss Question
Answer: Option A. -> 40


Execute in the shell to verify.


Question 8. Is the following piece of code valid?a=2,3,4,5 a
  1.    Yes, 2 is printed
  2.    Yes, [2,3,4,5] is printed
  3.    No, too many values to unpack
  4.    Yes, (2,3,4,5) is printed
 Discuss Question
Answer: Option D. -> Yes, (2,3,4,5) is printed


A tuple needn't be enclosed in parenthesis.


Question 9. What is the output of the following code?a,b=6,7 a,b=b,a a,b
  1.    (6,7)
  2.    Invalid syntax
  3.    (7,6)
  4.    Nothing is printed
 Discuss Question
Answer: Option C. -> (7,6)


The above piece of code illustrates the unpacking of variables.


Question 10. What is the output of the following piece of code when executed in Python shell?a=("Check")*3 a
  1.    ('Check','Check','Check')
  2.    * Operator not valid for tuples
  3.    ('CheckCheckCheck')
  4.    Syntax error
 Discuss Question
Answer: Option C. -> ('CheckCheckCheck')


Here ("Check) is a string not a tuple because there is no comma after the element.


Latest Videos

Latest Test Papers