Python Output Questions with Explanation for KVS PGT Computer Science

Post a Comment

The KVS PGT Computer Science exam is an important milestone for individuals looking to pursue a career in teaching computer science. This article provides a set of challenging python output questions with explanations to help you prepare for the KVS PGT Computer Science exam.

Related: KVS Admit Card

kvs cs python output questions


1. What is the output of the following code snippet?

def func(lst):

    return [i for i in lst if i % 2 == 0]

print(func([1, 2, 3, 4, 5, 6]))

Answer: [2, 4, 6]


Explanation: The function func takes a list as input and returns a new list that consists of only the even numbers from the input list. The output of the code will be [2, 4, 6].


2. What will be the output of the following code snippet?


def f(n):

    if n == 0:

        return 0

    elif n == 1:

        return 1

    else:

        return f(n-1) + f(n-2)


print(f(4))

Answer: 3


Explanation: The function f is an implementation of the Fibonacci sequence. It uses recursion to calculate the nth number in the sequence. When n=4, the function returns f(3) + f(2), which equals 2 + 1 and the output will be 3.


3. What will be the output of the following code snippet?

def g(n):

    if n == 0:

        return 0

    else:

        return g(n-1) + 1


print(g(2))

Answer: 2


Explanation: The function g takes an integer n as input and returns the sum of n and all the integers from 0 to n-1. In the example, g(2) returns g(1) + 1 which equals 1 + 1 and the output will be 2.


4. What is the output of the following code snippet?


def h(n):

    if n == 1:

        return 1

    else:

        return h(n-1) * n


print(h(3))

Answer: 6


Explanation: The function h takes an integer n as input and calculates the factorial of n using recursion. When n=3, the function returns h(2) * 3 which equals 2 * 3 and the output will be 6.


5. What will be the output of the following code snippet?

def func(x):

    return x * 2


lst = [1, 2, 3, 4, 5]

result = map(func, lst)

print(list(result))

Answer: [2, 4, 6, 8, 10]


Explanation: The function func takes an integer x as input and returns x * 2. The map function applies the func function to each element of the list lst. The list function is then used to convert the map object to a list, which is printed. The output will be [2, 4, 6, 8, 10], a list of the doubled values of the elements of lst.


6. What will be the output of the following code snippet?

lst = [1, 2, 3, 4, 5]

result = [i * 2 for i in lst if i % 2 == 0]

print(result)

Answer: [4, 8]


Explanation: This code uses a list comprehension to double the values of only the even numbers in the list lst. The if i % 2 == 0 clause filters out the odd numbers from lst and only the even numbers are processed. The output will be [4, 8], a list of the doubled values of the even numbers in lst.


7. What is the output of the following code snippet?


lst = [1, 2, 3, 4, 5]

result = filter(lambda x: x % 2 == 0, lst)

print(list(result))

Answer: [2, 4]


Explanation: The filter function takes two arguments, a function lambda x: x % 2 == 0 and a list lst. The function lambda x: x % 2 == 0 returns True if x is even and False if x is odd. The filter function applies this function to each element of the list lst and returns a filter object containing only the even numbers from lst. The list function is then used to convert the filter object to a list, which is printed. The output will be [2, 4], a list of only the even numbers from lst.


8. What is the output of the following code snippet?

def outer(x):

    def inner(y):

        return x + y

    return inner


closure = outer(10)

print(closure(5))

Answer: 15


Explanation: The code defines two functions, outer and inner. The outer function takes a single argument x and returns the inner function. The inner function takes a single argument y and returns the sum of x and y. The variable closure is assigned the value of outer(10), which returns the inner function. Then, closure(5) is called, which calls the inner function with an argument of 5. The output will be 15, which is the sum of 10 and 5.


9. What is the output of the following code snippet?

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

b = a[:]

b[0] = 10

print(a)

Answer: [1, 2, 3, 4, 5]


Explanation: The code creates a list with the values [1, 2, 3, 4, 5]. Then, it creates a new list b by slicing a. The slice of a creates a new list with the same elements as a, but the two lists are not connected, i.e., they are separate objects in memory. When the first element of b is modified to be 10, the original list a is unchanged and the output will be [1, 2, 3, 4, 5].


10. What is the output of the following code snippet?

def foo(x, y=[]):

    y.append(x)

    return y


print(foo(1))

print(foo(2))

print(foo(3))

Answer:

[1]

[1, 2]

[1, 2, 3]

Explanation: The function foo takes an optional argument y which has a default value of an empty list []. When the function foo is called for the first time with an argument 1, the default value of y is used, and y is set to [1]. When foo is called again with an argument 2, the default value of y is still the same list [1], so y is appended with 2 and becomes [1, 2]. Similarly, when foo is called with an argument 3, y becomes [1, 2, 3]. This behavior occurs because the default value of y is only created once and is used every time foo is called.


Related Posts

Post a Comment

Subscribe Our Newsletter