0% found this document useful (0 votes)
5 views

Week 7 Tutorial

The document contains a series of tutorial questions and answers related to Python programming, focusing on functions, data structures, and sorting methods. It includes multiple-choice questions, code snippets, and explanations for returning tuples, lists, and dictionaries from functions. Additionally, it discusses sorting techniques and provides solutions to various coding problems.

Uploaded by

rathoredaksh25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week 7 Tutorial

The document contains a series of tutorial questions and answers related to Python programming, focusing on functions, data structures, and sorting methods. It includes multiple-choice questions, code snippets, and explanations for returning tuples, lists, and dictionaries from functions. Additionally, it discusses sorting techniques and provides solutions to various coding problems.

Uploaded by

rathoredaksh25
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Tutorial Week 7 Questions

Question 1: What will be the output of the following Python code?


def calculate(a, b):
return a - b, a + b
result = calculate(8, 3)
print(result[0])

A) 5
B) 11
C) (5, 11)
D) Error

Answer: A) 5

Question 2: Which of the following methods returns a dictionary from a function in Python?

A) def get_info(): return {"name": "John", "age": 30}


B) def get_info(): return ["name", "John", "age", 30]
C) def get_info(): return (name: "John", age: 30)
D) def get_info(): return {name, age}

Answer: A) def get_info(): return {"name": "John", "age": 30}

Question 3: What will be the result of the following code snippet?

items = [(2, 'apple'), (3, 'banana'), (1, 'cherry')]

items.sort(key=lambda x: x[0])

print(items)

A) [(1, 'cherry'), (2, 'apple'), (3, 'banana')]


B) [(3, 'banana'), (2, 'apple'), (1, 'cherry')]
C) [(2, 'apple'), (3, 'banana'), (1, 'cherry')]
D) Error

Answer: A) [(1, 'cherry'), (2, 'apple'), (3, 'banana')]


Question 4: Which statement correctly sorts a list in ascending order?

A) data.sort(reverse=True)
B) sorted(data, reverse=True)
C) data.sort()
D) sorted(data, key=lambda x: x[1])

Answer: C) data.sort()

Question 5: Which of the following methods returns a tuple from a function?

A) def get_values(): return (1, 2, 3)


B) def get_values(): return [1, 2, 3]
C) def get_values(): return {1, 2, 3}
D) def get_values(): return '1, 2, 3'

Answer: A) def get_values(): return (1, 2, 3)

Question 6: What will be the output of the following code?

def process_data(a, b):

return a * b, a - b

result = process_data(6, 2)

print(result[1])

A) 8
B) 12
C) (12, 4)
D) 4

Answer: D) 4

7. Returning a Tuple: The most common way is to return a tuple by separating values with
commas.

def calculate(a, b):


sum_value = a + b

diff_value = a - b

return sum_value, diff_value # returns a tuple

result = calculate(10, 5)

print(result)

# Output: (15, 5)

Here, the calculation returns both the sum and difference as a tuple.

8. Returning a List: Functions can also return a list, which is mutable and can contain multiple
data types.
def get_data():

return [1, 'apple', 3.5]

result = get_data()

print(result) # Output: [1, 'apple', 3.5]

9. Returning a Dictionary: This method is helpful when returning values with keys to describe
each.
def create_profile(name, age):

return {"name": name, "age": age}

profile = create_profile("John", 30)

print(profile)

# Output: {'name': 'John', 'age': 30}

10.Returning a Tuple Using return Statement: When returning a tuple directly, values are separated
by commas.
def coordinates():

return 5, 10

x, y = coordinates()

print(f"x: {x}, y: {y}")

# Output: x: 5, y: 10
How to Do Sorting

Python provides several ways to sort data, such as using the sorted() function or the sort() method
for lists.

11. Using sort() Method for Lists: This method sorts the list in place (it modifies the original list).
numbers = [3, 1, 4, 1, 5, 9]

numbers.sort()

print(numbers) # Output: [1, 1, 3, 4, 5, 9]

12. Using sorted() Function: This function returns a new sorted list and works on any iterable.
numbers = [3, 1, 4, 1, 5, 9]

sorted_numbers = sorted(numbers)

print(sorted_numbers) # Output: [1, 1, 3, 4, 5, 9]

13. Sorting in Reverse Order: Both sort() and sorted() can sort in descending order using
reverse=True.
numbers = [3, 1, 4, 1, 5, 9]

numbers.sort(reverse=True)

print(numbers) # Output: [9, 5, 4, 3, 1, 1]

14. Sorting with a Key: You can use a key argument to sort complex structures like lists of tuples.
fruits = [('apple', 2), ('banana', 1), ('cherry', 3)]

fruits.sort(key=lambda fruit: fruit[1])

print(fruits) # Output: [('banana', 1), ('apple', 2), ('cherry', 3)]

MCQ Questions

15. What will be the output of the following code?

def values():

return 1, 2, 3

x, y, z = values()

print(x + y + z)

A) 6
B) (1, 2, 3)
C) [1, 2, 3]
D) Error

Answer: A) 6
16. Which of the following code snippets correctly returns a list from a function?

A) def get_numbers():

return 1, 2, 3

B) def get_numbers():

return [1, 2, 3]

C) def get_numbers():

return {1, 2, 3}

D) def get_numbers():

return 1; 2; 3

Answer: B)

def get_numbers():

return [1, 2, 3]

17. Which of the following statements will sort the list data in descending order?

data = [5, 2, 9, 1]

A) data.sort()
B) sorted(data)
C) sorted(data, reverse=True)
D) data.sort(reverse=True)

Answer: D) data.sort(reverse=True)

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

items = [(1, 'apple'), (3, 'banana'), (2, 'cherry')]

items.sort(key=lambda x: x[0])

print(items)

A) [(3, 'banana'), (2, 'cherry'), (1, 'apple')]


B) [(1, 'apple'), (2, 'cherry'), (3, 'banana')]
C) [(1, 'apple'), (3, 'banana'), (2, 'cherry')]
D) Error

Answer: B) [(1, 'apple'), (2, 'cherry'), (3, 'banana')]

19. What will be the output of the following code?

def process_data(a, b):

return a + b, a * b
result = process_data(3, 4)

print(result[1])

a) 7
b) 12
c) (7, 12)
d) 3
Answer: B) 12

20. Which of the following code snippets returns a tuple from a function?

A) def get_values():

return [1, 2, 3]

B) def get_values():

return {1, 2, 3}

C) def get_values():

return 1, 2, 3

D) def get_values():

return '1, 2, 3'

Answer: C)

def get_values():

return 1, 2, 3

21. def func1():


print("Arunabha")
def func1(x)
a) Invalid Syntax
b) Arunabha
c) Runtime Error
d) None of the above

Ans:a

22. What are the two main types of functions?


a) Custom function
b) Built-in function & User defined function
c) User function
d) All of the above

Ans:b
23. Where is function defined?
a) Module
b) Class
c) Another function
d) All of the above

24. x=str
def func1(str):
print("Hi")
y=func1(x)
Output for the above code
a) Error
b) Hi
c) No output
d) Hi Hi

Ans:b
25. Does Lambda contain return statements?
a) True
b) False
Ans:b

26. What will be the output of the code snippet?

a) 25
b) 10 15
c) 0
d) (25,0)

Ans:(d)

27. What will be the outcome of below code:


def return_Even(*args):
Even_List=[]
for x in args:
if x%2==0:
Even_List.append(x)
return Even_List
print(return_Even(1,2,3,4,5,6))

Solution: Outcome will be [2, 4, 6]

28. What will be the outcome of following code:

def return_AlphabetOnly_Strings(*args):
return [x for x in args if x.isalpha()]
print(return_AlphabetOnly_Strings("Sidharth","Sid123","12345","Bennett
University"))

Solution. ['Sidharth']

29. Will below code execute or generate error:

def greater_lesser(x,y):
if x>=y:return x,y
else:return y,x
print(greater_lesser(int(input()),int(input())))

Solution: Yes the code given above will execute perfectly

30. def generate_list(n):

return [x**2 for x in range(2,10,2) ]


print(generate_list(6))
What is the limitation of above code??

Solution: The major limitation of above code could be that in above code the outcome of function
generate_list will always generate the same outcome irrespective of the input provided

31. Modify above code to generate list depending upon input n

Solution

def generate_list(n):
return [x**2 for x in range(2,n,2)]
print(generate_list(int(input())))

32. Modify the code given in Q5 to output list of only squares of odd numbers in the
required range

Solution:

def generate_list(n):
return [x**2 for x in range(1,n,2) if x%2==1]
print(generate_list(int(input())))

Top of Form

You might also like