Code Buzz (Language : Python)

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 Aniket Mondal
A
Aniket Mondal
Community Contributor
Quizzes Created: 1 | Total Attempts: 195
Questions: 30 | Attempts: 195

SettingsSettingsSettings
Code Buzz (Language : Python) - Quiz

"A good programmer is someone who always looks both ways before crossing a one-way street"
Do you have a wide vision on programming?
Do you have control on several languages?
Let's check !Techtix, KGEC in association with Coding Ninjas presents Code Buzz, the competition to test your technical knowledge in Java, C & Python. Code Buzz this year has got total prizes worth Rs. 4,500. Take this challenge and register for Code Buzz this year.


Questions and Answers
  • 1. 

    What will be the output of the following Python code? a = [1, 2, 3, 4, 5] b = lambda x: (b (x[1:]) + x[:1] if x else [])  print(b (a))

    • A.

      1 2 3 4 5

    • B.

      [5,4,3,2,1]

    • C.

      []

    • D.

      Error, lambda functions can’t be called recursively

    Correct Answer
    C. []
    Explanation
    The code defines a lambda function named "b" that takes a list as input. The lambda function recursively calls itself by slicing the input list and concatenating it with the first element of the list. This process continues until the input list becomes empty. In this case, the lambda function returns an empty list. Therefore, the output of the code will be an empty list ([]).

    Rate this question:

  • 2. 

    What will be the output of the following Python code? def check(n):     if n < 2:         return n % 2 == 0     return check(n - 2) print(check(11))

    • A.

      False

    • B.

      True

    • C.

      1

    • D.

      An exception is thrown

    Correct Answer
    A. False
    Explanation
    The code defines a recursive function called "check" that takes an integer "n" as input. The function checks if "n" is less than 2. If it is, the function returns the result of the expression "n % 2 == 0", which checks if "n" is an even number. If "n" is not less than 2, the function calls itself with "n - 2" as the argument.

    In this case, the initial value passed to the function is 11. Since 11 is not less than 2, the function calls itself with the argument 9. This process continues until "n" becomes 1, which is less than 2. At this point, the function returns False because 1 is not an even number. Therefore, the output of the code will be False.

    Rate this question:

  • 3. 

    What will be the output of the following Python code? l1=[1, 2, 3, (4)] l2=l1.copy() l2 l1

    • A.

      [1, 2, 3, (4)] [1, 2, 3, 4]

    • B.

      [1, 2, 3, 4] [1, 2, 3, (4)]

    • C.

      [1, 2, 3, 4] [1, 2, 3, 4]

    • D.

      [1, 2, 3, (4)] [1, 2, 3, (4)]

    Correct Answer
    C. [1, 2, 3, 4] [1, 2, 3, 4]
    Explanation
    The code creates a list l1 with elements [1, 2, 3, (4)]. Then, it creates a copy of l1 and assigns it to l2. Finally, it prints the contents of l2 and l1. Since l2 is a copy of l1, any changes made to l1 will not affect l2. Therefore, the output will be [1, 2, 3, 4] for l2 and [1, 2, 3, (4)] for l1.

    Rate this question:

  • 4. 

    What will be the output of the following Python code? l1=[10, 20, 30, [40]] l2=copy.deepcopy(l1) l1[3][0]=90 l1 l2

    • A.

      [10, 20, 30, [40]] [10, 20, 30, 90]

    • B.

      Error

    • C.

      [10, 20, 30 [90]] [10, 20, 30, [40]]

    • D.

      [10, 20, 30, [40]] [10, 20, 30, [90]]

    Correct Answer
    C. [10, 20, 30 [90]] [10, 20, 30, [40]]
    Explanation
    The code first creates a list l1 with elements [10, 20, 30, [40]]. Then, using the deepcopy() function from the copy module, it creates a deep copy of l1 and assigns it to l2. A deep copy creates a completely independent copy of the object, including any nested objects.

    Next, the code modifies the first element of the nested list in l1 to 90. When we print l1, it will show the modified list [10, 20, 30, [90]].

    However, since l2 is a deep copy of l1, it remains unchanged and prints the original list [10, 20, 30, [40]].

    Rate this question:

  • 5. 

    What will be the output of the following Python code? l1=[10, 20, 30] l2=l1 id(l1)==id(l2)   l2=l1.copy() id(l1)==id(l2)

    • A.

      True, False

    • B.

      False, True

    • C.

      True, True

    • D.

      False, False

    Correct Answer
    A. True, False
    Explanation
    The first line of code assigns the list l1 to the variable l2, so both variables refer to the same list object. The expression id(l1)==id(l2) compares the memory addresses of l1 and l2, which will be True.

    However, in the second line of code, l2 is assigned a copy of l1 using the copy() method. This creates a new list object for l2, so the memory addresses of l1 and l2 will be different. Therefore, the expression id(l1)==id(l2) will be False.

    Rate this question:

  • 6. 

    What will be the output of the following Python code and state the type of copy that is depicted? l1=[2, 4, 6, 8] l2=[1, 2, 3] l1=l2 l2

    • A.

      [2, 4, 6, 8], shallow copy

    • B.

      [2, 4, 6, 8], deep copy

    • C.

      [1, 2, 3], shallow copy

    • D.

      [1, 2, 3], deep copy

    Correct Answer
    C. [1, 2, 3], shallow copy
    Explanation
    The code assigns the value of l2 to l1, so l1 and l2 refer to the same list object. Therefore, any changes made to l1 will also affect l2. This is an example of a shallow copy, where the reference to the original object is copied rather than creating a new independent copy. The output of the code will be [1, 2, 3], which is the value of l2.

    Rate this question:

  • 7. 

    What will be the output of the following Python code? l=[2, 3, [4, 5]] l2=l.copy() l2[0]=88 l l2

    • A.

      [88, 2, 3, [4, 5]] [88, 2, 3, [4, 5]]

    • B.

      [2, 3, [4, 5]] [88, 2, 3, [4, 5]]

    • C.

      [88, 2, 3, [4, 5]] [2, 3, [4, 5]]

    • D.

      [2, 3, [4, 5]] [2, 3, [4, 5]]

    Correct Answer
    B. [2, 3, [4, 5]] [88, 2, 3, [4, 5]]
    Explanation
    The code creates a list `l` with three elements: 2, 3, and another list [4, 5]. Then, the code creates a copy of `l` called `l2` using the `copy()` method. The first element of `l2` is then changed to 88. However, this change does not affect the original list `l`. Therefore, when `l` and `l2` are printed, `l` remains unchanged while `l2` has the first element as 88.

    Rate this question:

  • 8. 

    Which type of copy is shown in the following python code? l1=[[10, 20], [30, 40], [50, 60]] ls=list(l1) ls [[10, 20], [30, 40], [50, 60]]

    • A.

      Shallow copy

    • B.

      Deep copy

    • C.

      Memberwise

    • D.

      All of the mentioned

    Correct Answer
    A. Shallow copy
    Explanation
    The given python code is creating a new list `ls` by using the `list()` function and passing the existing list `l1` as an argument. This type of copy is known as a shallow copy. In a shallow copy, a new object is created, but the elements inside the object are still references to the original elements. Therefore, if any changes are made to the original elements, it will also reflect in the copied object.

    Rate this question:

  • 9. 

    What will be the output of the following Python code? def a(n):     if n == 0:         return 0     elif n == 1:         return 1     else:         return a(n-1)+a(n-2) for i in range(0,4):     print(a(i),end=" ")

    • A.

      0 1 2 3

    • B.

      An exception is thrown

    • C.

      0 1 1 2 3

    • D.

      0 1 1 2

    Correct Answer
    D. 0 1 1 2
    Explanation
    The given Python code defines a recursive function 'a' that calculates the nth Fibonacci number. The function returns 0 if n is 0, 1 if n is 1, and otherwise returns the sum of the (n-1)th and (n-2)th Fibonacci numbers.

    In the for loop, the function 'a' is called for each value of i in the range 0 to 3. So, the output will be the Fibonacci numbers for n=0, n=1, n=2, and n=3, which are 0, 1, 1, and 2 respectively. Therefore, the correct answer is "0 1 1 2".

    Rate this question:

  • 10. 

    What happens if the base condition isn’t defined in recursive programs

    • A.

      Program gets into an infinite loop

    • B.

      Program runs once

    • C.

      Program runs n number of times where n is the argument given to the function

    • D.

      An exception is thrown

    Correct Answer
    A. Program gets into an infinite loop
    Explanation
    If the base condition is not defined in recursive programs, the program will keep calling itself indefinitely, resulting in an infinite loop. This means that the program will continue executing the recursive function without any termination condition, causing it to run indefinitely until it is manually stopped or encounters a stack overflow error.

    Rate this question:

  • 11. 

    What will be the output of the following Python code? def fun(n):     if (n > 100):         return n - 5     return fun(fun(n+11));   print(fun(45))

    • A.

      50

    • B.

      100

    • C.

      74

    • D.

      Infinite Loop

    Correct Answer
    B. 100
    Explanation
    The code defines a recursive function called "fun" that takes an argument "n". If "n" is greater than 100, it subtracts 5 from "n" and returns the result. Otherwise, it calls the "fun" function again with the argument "n+11". This process continues until "n" becomes greater than 100. In this case, the initial value of "n" is 45. When the "fun" function is called with 45, it calls itself with 56, then 67, and finally 78. When 78 is passed to the function, it subtracts 5 and returns 73. This value is then passed to the previous call, which subtracts 5 again and returns 68. Finally, the initial call to "fun(45)" receives the value 68 and subtracts 5, resulting in 63. Therefore, the output of the code is 63.

    Rate this question:

  • 12. 

    Observe the following Python code? def a(n):     if n == 0:         return 0     else:         return n*a(n - 1) def b(n, tot):     if n == 0:         return tot     else:         return b(n-2, tot-2)

    • A.

      Both a() and b() aren’t tail recursive

    • B.

      Both a() and b() are tail recursive

    • C.

      B() is tail recursive but a() isn’t

    • D.

      A() is tail recursive but b() isn’t

    Correct Answer
    C. B() is tail recursive but a() isn’t
    Explanation
    The given code consists of two functions, a() and b(). In order for a function to be tail recursive, the recursive call should be the last operation in the function.

    In function a(), the recursive call to a() is not the last operation. It multiplies the current value of n with the result of the recursive call. Therefore, function a() is not tail recursive.

    In function b(), the recursive call to b() is the last operation. It subtracts 2 from both n and tot, and then calls b() with the updated values. Therefore, function b() is tail recursive.

    Hence, the correct answer is "b() is tail recursive but a() isn't."

    Rate this question:

  • 13. 

    What will be the output of the following Python code? l=[] def convert(b):     if(b==0):         return l     dig=b%2     l.append(dig)     convert(b//2) convert(6) l.reverse() for i in l:     print(i,end="")

    • A.

      011

    • B.

      110

    • C.

      3

    • D.

      Infinite Loop

    Correct Answer
    B. 110
    Explanation
    The given code defines a function named "convert" that takes an integer "b" as input. Inside the function, it checks if "b" is equal to 0. If it is, then it returns the list "l". Otherwise, it calculates the remainder of "b" divided by 2 and appends it to the list "l". Then, it recursively calls the "convert" function with "b" divided by 2.

    The main code calls the "convert" function with the input 6. After that, it reverses the list "l" and prints each element of the list without any space in between.

    Therefore, the output of the code will be "110".

    Rate this question:

  • 14. 

    What will be the output of the following Python code? def test(i,j):     if(i==0):         return j     else:         return test(i-1,i+j) print(test(4,7))

    • A.

      13

    • B.

      7

    • C.

      Infinite Loop

    • D.

      17

    Correct Answer
    D. 17
    Explanation
    The code defines a recursive function called "test" that takes two parameters, i and j. If i is equal to 0, the function returns the value of j. Otherwise, it calls itself with the parameters i-1 and i+j.

    In the given code, the function is called with test(4,7). Since i is not equal to 0, the else statement is executed. The function calls itself with test(3,11). Again, i is not equal to 0, so the function calls itself with test(2,14). This process continues until i becomes 0.

    When i becomes 0, the function returns the value of j. In this case, when i is 0, j is 17. Therefore, the output of the code will be 17.

    Rate this question:

  • 15. 

    Fill in the line of the following Python code for calculating the factorial of a number. def fact(num):     if num == 0:          return 1     else:         return _____________________

    • A.

      Num*fact(num-1)

    • B.

      (num-1)*(num-2)

    • C.

      Num*(num-1)

    • D.

      Fact(num)*fact(num-1)

    Correct Answer
    A. Num*fact(num-1)
    Explanation
    The correct answer is "num*fact(num-1)". This is the correct line of code to calculate the factorial of a number. It multiplies the current number (num) with the factorial of the previous number (num-1), recursively calculating the factorial until it reaches 0.

    Rate this question:

  • 16. 

    What will be the output of the following Python code? def f(): x=4 x=1 f() x

    • A.

      Error

    • B.

      4

    • C.

      Junk Value

    • D.

      1

    Correct Answer
    D. 1
    Explanation
    The output of the code will be 1. The variable x is initially assigned the value 1. Then, the function f is called, but it only creates a local variable x with the value 4, which does not affect the global variable x. Therefore, when the code reaches the last line, the value of x is still 1.

    Rate this question:

  • 17. 

    What will be the output of the following Python code? a=10 globals()['a']=25 print(a)

    • A.

      10

    • B.

      25

    • C.

      Junk Value

    • D.

      Error

    Correct Answer
    B. 25
    Explanation
    The code assigns the value 10 to the variable 'a'. Then, it uses the globals() function to access the global namespace and assigns the value 25 to a new variable with the name 'a'. Finally, it prints the value of the original variable 'a', which is now 25.

    Rate this question:

  • 18. 

    What will be the output of the following Python code? x=1 def cg():     global x     x=x+1     cg() x

    • A.

      2

    • B.

      1

    • C.

      0

    • D.

      Error

    Correct Answer
    A. 2
    Explanation
    The output of the code will be 2. The code defines a global variable x with a value of 1. Then, a function cg is defined, which increments the value of x by 1. Finally, the function cg is called, which updates the value of x to 2. Therefore, when x is printed, the output will be 2.

    Rate this question:

  • 19. 

    What will be the output of the following Python code? class A:     def test(self):         print("test of A called") class B(A):     def test(self):         print("test of B called")         super().test()   class C(A):     def test(self):         print("test of C called")         super().test() class D(B,C):     def test2(self):         print("test of D called")       obj=D() obj.test()

    • A.

      Test of B called test of C called test of A called

    • B.

      Test of C called test of B called

    • C.

      Test of B called test of C called

    • D.

      Error, all the three classes from which D derives has same method test()

    Correct Answer
    A. Test of B called test of C called test of A called
    Explanation
    The code defines four classes: A, B, C, and D. Class A has a method called "test" which prints "test of A called". Class B and C both inherit from class A and override the "test" method. Class B's "test" method prints "test of B called" and then calls the "test" method of its superclass using the "super()" function. Class C's "test" method prints "test of C called" and also calls the "test" method of its superclass using the "super()" function. Class D inherits from both class B and C and has its own method called "test2" which prints "test of D called".

    When the code creates an instance of class D and calls the "test" method on that instance, it first looks for the "test" method in class D. Since class D does not have its own "test" method, it looks in its superclass, class B. Class B's "test" method is called and it prints "test of B called". Then, the "super().test()" call in class B's "test" method looks for the "test" method in the next superclass, class C. Class C's "test" method is called and it prints "test of C called". Finally, the "super().test()" call in class C's "test" method looks for the "test" method in the next superclass, class A. Class A's "test" method is called and it prints "test of A called".

    So, the output of the code will be:
    test of B called
    test of C called
    test of A called

    Rate this question:

  • 20. 

    What will be the output of the following Python code? class A:     def test1(self):         print(" test of A called ") class B(A):     def test(self):         print(" test of B called ") class C(A):     def test(self):         print(" test of C called ") class D(B,C):     def test2(self):         print(" test of D called ")         obj=D() obj.test()

    • A.

      Test of B called test of C called

    • B.

      Test of C called test of B called

    • C.

      Test of B called

    • D.

      Error, both the classes from which D derives has same method test()

    Correct Answer
    C. Test of B called
    Explanation
    The code defines four classes: A, B, C, and D. Class A has a method called test1, class B has a method called test, class C also has a method called test, and class D has a method called test2. Class D inherits from both class B and class C.

    When the code creates an object of class D and calls the test method on it, the output will be "test of B called" followed by "test of C called". This is because class D inherits the test method from both class B and class C, but since class B is listed first in the inheritance hierarchy, its version of the test method is called first. Then, class D's own test2 method is not called in this code.

    Rate this question:

  • 21. 

    What will be the output of the following Python code? class A:     def __init__(self,x=3):         self._x = x         class B(A):     def __init__(self):         super().__init__(5)     def display(self):         print(self._x) def main():     obj = B()     obj.display()   main()

    • A.

      5

    • B.

      Error, class member x has two values

    • C.

      3

    • D.

      Error, protected class member can’t be accessed in a subclass

    Correct Answer
    A. 5
    Explanation
    The output of the code will be 5.


    The code defines two classes, A and B. Class A has a constructor that initializes a variable _x with a default value of 3. Class B is a subclass of A and it overrides the constructor to call the constructor of its superclass (A) with an argument of 5.

    In the main function, an object of class B is created (obj = B()), and the display method of obj is called. The display method prints the value of _x, which is 5 because it was set in the constructor of B. Therefore, the output of the code will be 5.

    Rate this question:

  • 22. 

    What will be the output of the following Python code? class A:     def __init__(self):         self._x = 5        class B(A):     def display(self):         print(self._x) def main():     obj = B()     obj.display() main()

    • A.

      Error, invalid syntax for object declaration

    • B.

      Nothing is printed

    • C.

      5

    • D.

      Error, private class member can’t be accessed in a subclass

    Correct Answer
    C. 5
    Explanation
    The code defines a class A with a private variable _x initialized to 5. Then, a class B is defined as a subclass of A. Class B has a method display that prints the value of _x. In the main function, an object obj of class B is created and its display method is called. Since obj is an instance of class B, it inherits the variable _x from class A and can access its value. Therefore, the output of the code will be 5.

    Rate this question:

  • 23. 

    What will be the output of the following Python code? class A:     def __init__(self):         self.__x = 1 class B(A):     def display(self):         print(self.__x) def main():     obj = B()     obj.display() main()

    • A.

      1

    • B.

      0

    • C.

      Error, invalid syntax for object declaration

    • D.

      Error, private class member can’t be accessed in a subclass

    Correct Answer
    D. Error, private class member can’t be accessed in a subclass
    Explanation
    The output of the code will be "Error, private class member can't be accessed in a subclass". This is because the attribute __x in class A is a private member, indicated by the double underscore prefix. Private members cannot be accessed directly in subclasses. Therefore, when the display method in class B tries to print self.__x, it will result in an error.

    Rate this question:

  • 24. 

    What will be the output of the following Python code? >>> class A:     pass >>> class B(A):     pass >>> obj=B() >>> isinstance(obj,A)

    • A.

      True

    • B.

      False

    • C.

      Wrong syntax for isinstance() method

    • D.

      Invalid method for classes

    Correct Answer
    A. True
    Explanation
    The code defines two classes, A and B. Class B is a subclass of class A. An object of class B is created using the obj variable. The isinstance() function is then used to check if the obj object is an instance of class A. Since class B is a subclass of class A, the output will be True.

    Rate this question:

  • 25. 

    What will be the output of the following Python code? class A:     def __init__(self,x):         self.x = x     def count(self,x):         self.x = self.x+1 class B(A):     def __init__(self, y=0):         A.__init__(self, 3)         self.y = y     def count(self):         self.y += 1      def main():     obj = B()     obj.count()     print(obj.x, obj.y) main()

    • A.

      3 0

    • B.

      3 1

    • C.

      0 1

    • D.

      An exception in thrown

    Correct Answer
    B. 3 1
    Explanation
    The code defines two classes, A and B. Class A has an initializer that takes a parameter x and sets the instance variable self.x to the value of x. It also has a method count that increments self.x by 1. Class B is a subclass of A and has an initializer that takes an optional parameter y, which defaults to 0. It calls the initializer of A with the argument 3 and sets the instance variable self.y to the value of y. It also has a method count that increments self.y by 1.

    In the main function, an object obj of class B is created. The count method of obj is called, which increments self.y by 1. Finally, the values of obj.x and obj.y are printed, resulting in the output "3 1".

    Rate this question:

  • 26. 

    What will be the output of the following Python code? class A:      def __init__(self):          self.__i = 1          self.j = 5        def display(self):          print(self.__i, self.j) class B(A):      def __init__(self):          super().__init__()          self.__i = 2          self.j = 7   c = B() c.display()

    • A.

      2 7

    • B.

      1 5

    • C.

      1 7

    • D.

      2 5

    Correct Answer
    C. 1 7
    Explanation
    The code defines two classes, A and B. Class A has a constructor that initializes two instance variables, __i and j, with the values 1 and 5 respectively. Class A also has a display method that prints the values of __i and j. Class B is a subclass of A and it overrides the constructor to call the constructor of A using the super() function. Class B also initializes its own __i variable with the value 2 and its j variable with the value 7.

    When the code creates an instance of class B and calls the display method, it will print the values of __i and j in that instance, which are 1 and 7 respectively. Therefore, the output will be "1 7".

    Rate this question:

  • 27. 

    What type of inheritance is illustrated in the following Python code? class A():     pass class B(A):     pass class C(B):     pass

    • A.

      Multi-level inheritance

    • B.

      Multiple inheritance

    • C.

      Hierarchical inheritance

    • D.

      Single-level inheritance

    Correct Answer
    A. Multi-level inheritance
    Explanation
    The given Python code demonstrates multi-level inheritance. In multi-level inheritance, a class inherits from another class, which in turn inherits from another class. In this code, class B inherits from class A, and class C inherits from class B. This creates a chain of inheritance, where each class inherits the properties and methods of the class above it in the hierarchy.

    Rate this question:

  • 28. 

    Which function overloads the // operator?

    • A.

      __div__()

    • B.

      __ceildiv__()

    • C.

      __floordiv__()

    • D.

      __truediv__()

    Correct Answer
    C. __floordiv__()
    Explanation
    The correct answer is __floordiv__(). The double forward slash operator (//) is used for floor division, which returns the largest integer less than or equal to the division result. The __floordiv__() function is the specific function in Python that overloads the // operator and performs the floor division operation.

    Rate this question:

  • 29. 

    Which of the following Python code will print True? a = foo(2) b = foo(3) print(a < b)

    • A.

      Class foo:     def __init__(self, x):         self.x = x     def __lt__(self, other):         if self.x < other.x:             return False         else:             return True

    • B.

      Class foo:     def __init__(self, x):         self.x = x     def __less__(self, other):         if self.x > other.x:             return False         else:             return True

    • C.

      Class foo:     def __init__(self, x):         self.x = x     def __lt__(self, other):         if self.x < other.x:             return True         else:             return False

    • D.

      Class foo:     def __init__(self, x):         self.x = x     def __less__(self, other):         if self.x < other.x:             return False         else:             return True

    Correct Answer
    C. Class foo:     def __init__(self, x):         self.x = x     def __lt__(self, other):         if self.x < other.x:             return True         else:             return False
    Explanation
    The correct answer is the fourth option: class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return True else: return False. This is because the "__lt__" method is the special method for the less than (

    Rate this question:

  • 30. 

    What will be the output of the following Python code? def sum(*args):    '''Function returns the sum     of all values'''    r = 0    for i in args:       r += i    return r print sum.__doc__ print sum(1, 2, 3) print sum(1, 2, 3, 4, 5)

    • A.

      615

    • B.

      6100

    • C.

      12312345

    • D.

      None of these

    Correct Answer
    A. 615
    Explanation
    The code defines a function called "sum" that takes in any number of arguments and returns the sum of all the values. The variable "r" is initialized as 0 and then each argument is added to "r" using a for loop. The function is then called with different sets of arguments.

    The output of the code will be 615. This is because when the function is called with the arguments (1, 2, 3), the sum of these values is 6. When the function is called with the arguments (1, 2, 3, 4, 5), the sum of these values is 15. Therefore, the output will be the sum of these two values, which is 615.

    Rate this question:

Related Topics

Back to Top Back to top
Advertisement