Code Buzz (Language : Python)

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 Aniket Mondal
A
Aniket Mondal
Community Contributor
Quizzes Created: 1 | Total Attempts: 211
| Attempts: 211 | Questions: 30
Please wait...
Question 1 / 30
0 %
0/100
Score 0/100
1. 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=" ")

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

Submit
Please wait...
About This Quiz
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... see morelanguages?
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. see less

2. 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()

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

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

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.

Submit
4. 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 _____________________

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.

Submit
5. 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))

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.

Submit
6. 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()

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

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

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.

Submit
8. What happens if the base condition isn't defined in recursive programs

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.

Submit
9. 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))

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.

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

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

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

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.

Submit
12. Which function overloads the // operator?

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.

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

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.

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

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.

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

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

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

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.

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

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.

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

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

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

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.

Submit
20. 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)

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

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

Explanation

The correct answer is the fourth option: class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x

Submit
22. 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)

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.

Submit
23. 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]]

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.

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

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.

Submit
25. 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()

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.

Submit
26. 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()

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.

Submit
27. 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()

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.

Submit
28. 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))

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.

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

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.

Submit
30. 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))

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 ([]).

Submit
View My Results

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

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

  • Current Version
  • Mar 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 06, 2020
    Quiz Created by
    Aniket Mondal
Cancel
  • All
    All (30)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
Fill in the line of the following Python code for calculating the...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What type of inheritance is illustrated in the following Python code? ...
What happens if the base condition isn't defined in recursive programs
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code and state the...
Which function overloads the // operator?
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
Observe the following Python code? ...
Which of the following Python code will print True? ...
What will be the output of the following Python code? ...
Which type of copy is shown in the following python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
What will be the output of the following Python code? ...
Alert!

Advertisement