Best Python Quizzes for Intermittent Level


Intermittent Level Python Quizzes

Dive deeper into Python mastery with our intermediate-level Python quizzes! This quiz is designed for those who have a solid foundation in Python and are ready to take their skills to the next level. Sharpen your problem-solving abilities and gain confidence in handling more complex coding scenarios. Here we are covering topics like functions, object-oriented programming, exception handling and more.

Python Quizzes for Intermittent Level

Question 301.

What is the output of the following code?

my_list = [1, 2, 3, 4, 5]
numbers = [i for i in my_list if i%2 == 0]
print(numbers)
  • [2,4]
  • [1,3]
  • [1, 2, 3, 4, 5]
  • Error

a. [2,4]


Question 302.

What is the output of the following code?

def logical(x):
    if x == 1:
        return 1
    else:
        return(x + logical(x-1))
    
result = logical(3)

print(result)
  • 6
  • 5
  • 15
  • None of the above

a. 6


Question 303.

What is the output of the following code?

for i in range(1):
    for j in range(1):
        print(i,j)
  • 1 1
  • 0 0
  • Error
  • None of the above

Answer:

b. 0 0

Explanation:

1. for i in range(1) – The outer loop:

  • Here, range(1) creates a range starting from 0 and ending before 1 (exclusive), so it generates a single iteration where ‘i’ will take the value of 0.
  • The loop iterates only once because there is only one element in the range.
    The inner loop:

2. for j in range(1) – The inner loop:

  • Similar to the outer loop, range(1) creates a range starting from 0 and ending before 1, resulting in a single iteration where j will also take the value of 0.
  • Again, this loop iterates only once.

Within the nested loops, print(i, j) is called. At each iteration, it prints the values of i and j. As both loops iterate only once, the output of this code is ‘0 0’.


Question 304.

What is the output of the following code?

def outer_function():
    x = 5
    def inner_function():
        x = 2
        x += 2
        
    inner_function()
    print(x)

outer_function()
  • 5
  • 2
  • 4
  • Error

Answer:

a. 5

Explanation:

  1. Inside outer_function(), a variable x is initialized with the value 5. This x is a local variable for outer_function().
  2. Then inner_function() is defined within outer_function().
  3. Inside inner_function(), a local variable x is initialized with the value 2. This x is local to inner_function() and does not affect the variable x in the outer scope.
  4. x += 2 increments the value of the local variable x (which is 2), making it 4. However, this change does not affect the x variable in the outer scope because it’s a separate variable.
  5. inner_function() is called within outer_function().
  6. At last , print(x) is called inside outer_function() after calling inner_function(). This will print the value of the variable x in the scope of outer_function(), which remains 5 because the x inside inner_function() is a separate variable with its own scope, and changes made to it do not affect the outer scope x.


Question 305.

What is the output of the following code?

my_list = [1, 2, 3, 4, 5] 
for i in my_list:
    if i == 5:
        break
    print(i)
else:
    print("Logical Python")
  • 1 2 3
  • 1 2 3 4
  • 1 2 3 4 5
  • 1 2 3 4 Logical Python

Answer:

b. 1 2 3 4

Explanation:

  1. for i in my_list:: This is a loop that iterates over each element in the list my_list. In each iteration, the current element is assigned to the variable i.
  2. if i == 5:: Inside the loop, this line checks if the current element i is equal to 5.
  3. break: If the current element i is equal to 5, the break statement is executed, which terminates the loop prematurely. This means that if the loop encounters the number 5 in the list, it will stop executing and move to the next part of the code.
  4. print(i): This line prints the current element i if it’s not equal to 5. In other words, it prints all elements of the list up to (but not) 5.
  5. else:: This is an optional part of the for loop in Python. It executes when the loop completes all its iterations without encountering a break statement.

Question 306.

What is the output of the following code?

my_list = [1, 2, 3, 4, 5] 
for i in my_list:
    if i == 5:
        continue
    print(i)
else:
    print("Logical Python")
  • 1 2 3 4 5 Logical Python
  • 1 2 3 4 Logical Python
  • 1 2 3 4 5
  • 1 2 3 4

b. 1 2 3 4 Logical Python


Question 307.

What is the output of the following code?

def my_args(a, b=9, *args):
    print(a, b, args)

my_args(1, 2, 3, 4, 5)
  • 1 2 (3, 4, 5)
  • 1, 2, 3, 4, 5
  • 1, 9, 3, 4, 5
  • None of the above

a. 1 2 (3, 4, 5)


Question 308.

What is the output of the following code?

def my_args(a, b=9,  **kwargs):
    print(a, b, kwargs)

my_args(1, 2, c=3, d=4)
  • 1 2 {‘c’: 3, ‘d’: 4}
  • 1, 2, 3, 4
  • 1 9 {‘c’: 3, ‘d’: 4}
  • None of the above

a. 1 2 {‘c’: 3, ‘d’: 4}


Question 309.

What is the output of the following code?

def my_args(a, b=9, *args, **kwargs):
    print(a, b, args, kwargs)

my_args(1, 2, 3, 4, 5, x=6, y=7)
  • 1 2 (3, 4, 5) {‘x’: 6, ‘y’: 7}
  • 1 9 (3, 4, 5) {‘x’: 6, ‘y’: 7}
  • 1 2 (3, 4, 5)
  • 1 2 (3, 4, 5, 6, 7)

a. 1 2 (3, 4, 5) {‘x’: 6, ‘y’: 7}


Question 310.

What is the output of the following code?

x = [1,2,3]
y = [4,5,6,7]

print(list(zip(x,y)))
  • [(1, 4), (2, 5), (3, 6)]
  • [(1, 4), (2, 5), (3, 6) (7)]
  • [(1, 4), (2, 5), (3, 6) (,7)]
  • Error

a. [(1, 4), (2, 5), (3, 6)]


Question 311.

What is the output of the following code?

x = 5

def func(): 
    global x
    x = 10

print(x)
func() 
print(x)
  • 5 10
  • 5 5
  • 10 10
  • None of the above

a. 5 10


Question 312.

What is the output of the following code?

def outer_function():
    x = 5
    def inner_function():
        nonlocal x
        x += 2
        
    inner_function()
    print(x)

outer_function()
  • 5
  • 2
  • 7
  • Error

c. 7


Question 313.

What is the output of the following code?

numbers = {1 : 'One', 2 : 'Two', 3 : 'Three', 4 : 'Four'}
num = {i: j for i, j in numbers.items() if i % 2 == 0} 
print(num)
  • {2: ‘Two’, 4: ‘Four’}
  • {1 : ‘One’, 3 : ‘Three’}
  • {1 : ‘One’, 2 : ‘Two’, 3 : ‘Three’, 4 : ‘Four’}
  • None of the above

a. {2: ‘Two’, 4: ‘Four’}


Question 314.

What is the output of the following code?

from statistics import median
numbers = [1,100,90,80,45,100,2]
print(median(numbers))
  • 60
  • 70
  • 80
  • 100

c. 80


Question 315.

What is the output of the following code?

x = [1,2,3]
y = []

print(list(zip(x,y)))
  • []
  • ()
  • Error
  • None of the above

a. []


Question 316.

What is the output of the following code?

numbers = [1,2,3,4,5]
x = slice(1,3)
print(numbers[x])
  • [1,2]
  • [2,3]
  • (2,3)
  • Error

b. [2,3]


Question 317.

What is the output of the following code?

numbers = [1,2,3,4,5]
x = slice(None,None,-1)
print(numbers[x])
  • [5, 4, 3, 2, 1]
  • [1,2,3,4,5]
  • Error
  • None of the above

a. [5, 4, 3, 2, 1]


Question 318.

What is the output of the following code?

numbers = [1,2,3,4,5]
x = slice(,,-1)
print(numbers[x])
  • [1,2,3,4,5]
  • [5, 4, 3, 2, 1]
  • Error
  • None of the above

c. Error


Question 319.

What is the output of the following code?

def my_exception():
    try:
        print("Logical Python")
        return
    finally:
        print("is the Best")
        
my_exception()
  • Logical Python \n is the Best. (Note: \n here means new line)
  • is the Best
  • Logical Python
  • None of the above

Answer:

a. Logical Python \n is the Best. (Note: \n here means new line)

Explanation:

  1. try: – This is the start of a try block. Code inside this block will be executed, and if any exceptions occur, they will be caught by the corresponding except block. We have not used any except block here.
  2. finally: – This block follows the try block. Code inside the finally block is always executed, regardless of whether an exception occurs in the try block or not. It’s often used for cleanup tasks.

Question 320.

What is the output of the following code?

x, *y = (1, 2, 3, 4)

print(y, type(y))
  • [2, 3, 4] <class ‘list’>
  • (2, 3, 4) <class ‘tuple’>
  • {2, 3, 4} <class ‘set’>
  • Error

Answer:

a. [2, 3, 4] <class ‘list’>

Explanation:

The expression on the left side of the assignment unpacks this tuple into variables x and y. The * operator is used to collect multiple values into a list. Here, it’s used to collect multiple values into y.

  • x will be assigned the first value of the tuple, which is 1.
  • *y collects the remaining values of the tuple into a list assigned to y.

Since y is assigned a list of values ([2, 3, 4]), it will print [2, 3, 4], and the type(y) will return <class 'list'>, indicating that y is of type list.


Question 321.

What is the output of the following code?

class sum:
    def __init__(self, num):
        self.num = num

    def __add__(self, num2):
        return sum(self.num + num2.num)

x = sum(1)
y = sum(2)
z = x + y
print(z.num)
  • 3
  • 2
  • 1
  • 12

Answer:

a. 3

Explanation:

  1. The init method is a constructor that initializes an object of the sum class with a parameter num.
  2. The add method is defined to overload the + operator for instances of the sum class.
  3. In the add method, a new sum object is returned with the sum of self.num and num2.num.
  4. x + y is performed, which calls the add method of the sum class. This results in a new sum object where num is the sum of x.num (1) and y.num (2), so z will have num set to 3.

Question 322.

What is the output of the following code?

lst = [-25, -5, -2, 0, 2, 5, 25]
print(list(filter(lambda x : x %5 == 0, lst)))
  • [-25, -5, 0, 5, 25]
  • [-2, 0, 2]
  • [0]
  • None of the above

Answer:

a. [-25, -5, 0, 5, 25]

Explanation:

  1. filter() is a built-in Python function that takes two arguments: a function and an iterable (in this case, the list lst). It returns an iterator with those items of the iterable for which the function returns true.
  2. lambda x: x % 5 == 0 is a lambda function that takes one argument x and returns True if x is divisible by 5 (i.e., x modulo 5 is equal to 0), and False otherwise.
  3. The filter() function applies this lambda function to each element in the list lst. It keeps only those elements for which the lambda function returns True.
  4. The list() function is used to convert the iterator returned by filter() back into a list.

Question 323.

What is the output of the following code?

l1 = [1,2,3]
l2 = []

print(list(zip(l1, l2)))
  • []
  • [(1), (2), (3)]
  • [(1,()), (2,()), (3,())]
  • None of the above

Answer:

a. []

Explanation:

The zip() function in Python takes iterables as arguments and returns an iterator of tuples where the i-th tuple contains the i-th element from each of the input iterable. If the input iterables are of different lengths, zip() stops when the shortest input iterable is exhausted.

In your code:

  1. l1 is a list [1, 2, 3].
  2. l2 is an empty list [].
  • When you call zip(l1, l2), the function pairs elements from both lists based on their corresponding indices. Since l2 is empty, there are no elements to pair with the elements of l1. So, the resulting iterator will also be empty.
  • However, you are then converting this iterator into a list using list(), which converts it into list. So, the output of print(list(zip(l1, l2))) will be an empty list [].

Question 324.

What is the output of the following code?

lst = ["aa", "ab", "ac", "ad"]
print(lst[1: ][ : 2])
  • [“aa”, “ab”, “ac”, “ad”]
  • [“aa”, “ab”, “ac”]
  • [“ab”, “ac”, “ad”]
  • [‘ab’, ‘ac’]

Answer:

d. [‘ab’, ‘ac’]

Explanation:

  1. lst[1:]: This is slicing the list lst. It starts from index 1 (which is the second element “ab”) and goes till the end of the list. So, it selects all elements from index 1 to the last element.
  2. [ : 2]: This further slices the result obtained from the previous step. It selects elements from the beginning (index 0) up to, but not including, index 2. So, it selects the first two elements from the sliced list obtained in step 2.

Question 325.

What is the output of the following code?

lst = [1,-1,"Python", []]
print(all(lst), any(lst))
  • False True
  • True False
  • True True
  • None of the above

Answer:

a. False True

Explanation:

  1. all(lst): This function returns True if all elements in the iterable lst evaluate to True. Otherwise, it returns False. In Python, non-zero numbers and non-empty containers (lists, tuples, sets, dictionaries) evaluate to True. Here, the list lst contains elements 1, -1, “Python”, and an empty list []. Since an empty list[] is False, all(lst) evaluates to False.
  2. any(lst): This function returns True if at least one element in the iterable lst evaluates to True. Otherwise, it returns False. In Python, non-zero numbers and non-empty containers (lists, tuples, sets, dictionaries) evaluate to True. Here, the list lst contains elements 1, -1, “Python”, and an empty list []. Since all of these elements except the empty list [] are truthy, any(lst) evaluates to True.


Question 326.

What is the output of the following code?

names = ["Bill", "Elon"]
print(list(enumerate(names, start=50)))
  • [(‘Bill’, 50), (‘Elon’, 51)]
  • [(50, ‘Bill’), (51, ‘Elon’)]
  • [(‘Bill’), (‘Elon’)]
  • None of the above

Answer:

b. [(50, ‘Bill’), (51, ‘Elon’)]

Explanation:

  1. enumerate(names, start=50): This function takes an iterable names and an optional start parameter (which defaults to 0) specifying the starting index for enumeration. It returns an iterator that yields tuples containing the index (starting from the specified start value) and the corresponding element from the iterable. In this case, it starts enumeration from index 50.
  2. list(): This converts the enumeration iterator generated by enumerate() into a list.

Question 327.

What is the output of the following code?

for i, j in enumerate(['a', 'b','c']): 
    print(i,j,sep='-', end=' >> ')
  • 0-a >> 1-b >> 2-c >>
  • a >> b >> c >>
  • 0 >> 1 >> 2 >>
  • 0-‘a’ >> 1-‘b’ >> 2-‘c’ >>

Answer:

a. 0-a >> 1-b >> 2-c >>

Explanation:

  1. The enumerate() function is used to iterate over a sequence while keeping track of the index of each item. It returns pairs of (index, item).
  2. The loop for i, j in enumerate([‘a’, ‘b’,’c’]): iterates over each element in the list [‘a’, ‘b’, ‘c’] while simultaneously tracking their index. In each iteration:
    • i represents the index of the current element.
    • j represents the value of the current element.
  3. Inside the loop: print(i, j, sep=’-‘, end=’ >> ‘) Prints the current index i, the current element j, with a separator of ‘-‘ between them, and ‘ >> ‘ as the end parameter for the print statement.

Here:

  1. 0-a: 0 is the index of the first element ‘a’.
  2. 1-b: 1 is the index of the second element ‘b’.
  3. 2-c: 2 is the index of the third element ‘c’.
  4. The >> is the custom end parameter specified in the print() function.