Wednesday, 4 December 2024

9 Python function-based quiz questions


1. Basic Function Syntax

What will be the output of the following code?



def greet(name="Guest"): return f"Hello, {name}!"
print(greet())
print(greet("John"))

a. Hello, Guest!, Hello, John!
b. Hello, John!, Hello, Guest!
c. Hello, Guest!, Hello, Guest!
d. Error


2. Positional and Keyword Arguments

What does the following function call print?

def calculate(a, b=5, c=10):
return a + b + cprint(calculate(3, c=7))

a. 15
b. 20
c. 25
d. Error


3. Function with Variable Arguments

What will be the output of this code?


def add_all(*args): return sum(args)print(add_all(1, 2, 3, 4))

a. 10
b. [1, 2, 3, 4]
c. Error
d. 1, 2, 3, 4


4. Returning Multiple Values

What will print(result) output?


def divide(a, b): quotient = a // b remainder = a % b return quotient, remainder result = divide(10, 3)
print(result)

a. 10, 3
b. (3, 1)
c. 3.1
d. Error


5. Scope of Variables

What will the following code print?


x = 5 def update_value(): x = 10 print(x) update_value()
print(x)

a. 10, 5
b. 10, 10
c. 5, 5
d. Error


6. Default and Non-Default Arguments

Why does this code throw an error?


def example(a=1, b): return a + b

a. b is not assigned a default value
b. Default arguments must come after non-default arguments
c. Both a and b must have default values
d. No error


7. Lambda Functions

What will the following code print?

double = lambda x: x * 2print(double(4))

a. 2
b. 4
c. 8
d. Error


8. Nested Functions

What will the following code output?

def outer_function(x):
def inner_function(y): return y + 1 return inner_function(x) + 1
print(outer_function(5))

a. 6
b. 7
c. 8
d. Error


9. Anonymous Functions with map()

What is the result of the following code?

numbers = [1, 2, 3, 4]
result = list(map(lambda x: x ** 2, numbers))print(result)

a. [1, 4, 9, 16]
b. [2, 4, 6, 8]
c. None
d. Error


1. Basic Function Syntax

Answer: a. Hello, Guest!, Hello, John!

Explanation:

  • Default value Guest is used when no argument is passed.
  • Passing "John" overrides the default value.

2. Positional and Keyword Arguments

Answer: b. 20

Explanation:

  • a = 3, b = 5 (default), c = 7 (overrides the default value of 10).
  • Result: 3 + 5 + 7 = 20.

3. Function with Variable Arguments

Answer: a. 10

Explanation:

  • *args collects all arguments into a tuple.
  • sum(args) calculates the sum: 1 + 2 + 3 + 4 = 10.

4. Returning Multiple Values

Answer: b. 

(3, 1)

Explanation:

  • The function returns a tuple (quotient, remainder).
  • 10 // 3 = 3 (quotient), 10 % 3 = 1 (remainder).

5. Scope of Variables

Answer: a. 10, 5

Explanation:

  • x = 10 inside the function is local and does not affect the global x.
  • Outside the function, x = 5.

6. Default and Non-Default Arguments

Answer: b. Default arguments must come after non-default arguments

Explanation:

  • In Python, arguments with default values (like a=1) must appear after those without defaults (like b).

7. Lambda Functions

Answer: c. 8

Explanation:

  • The lambda function doubles the input: 4 * 2 = 8.

8. Nested Functions

Answer: b. 7

Explanation:

  • inner_function(5) returns 5 + 1 = 6.
  • Adding 1 in outer_function: 6 + 1 = 7.

9. Anonymous Functions with map()

Answer: a. [1, 4, 9, 16]

Explanation:

  • The lambda function squares each number in the list:
    [1^2, 2^2, 3^2, 4^2] = [1, 4, 9, 16].

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (12) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (156) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (121) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (53) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (914) Python Coding Challenge (298) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses