Search Header Logo
Python - 5

Python - 5

Assessment

Presentation

Professional Development

Professional Development

Practice Problem

Hard

Created by

Mani Ak

Used 1+ times

FREE Resource

5 Slides • 5 Questions

1

Multiple Choice

Question image

What will be the output of the following Python code?

1

Yes, the Value is 6
Result: 1

2

Yes, the Value is 6
Result: 6

3

Yes, the Value is 6
Result: None

4

Result: 1

2

  1. The function funct_exp takes three positional arguments (a, b, c) and variable keyword arguments (**args).

  2. When the function is called, it's given the values 1, 2, and 3 for a, b, and c respectively, and two keyword arguments: action="true" and number="false".

  3. The first if statement checks if args.get("action") == "true", which is true. So it prints "Yes, the Value is 6" (because 1 + 2 + 3 = 6).

  4. The second if statement checks if args.get("number") == "false", which is also true. So it returns the value of a, which is 1.

  5. The returned value (1) is assigned to the variable result.

  6. Finally, "Result: 1" is printed.

3

Multiple Choice

Question image

What is the output of the following code?

1

The code will run successfully and print the values of all parameters.

2

The code will raise a SyntaxError.

3

The code will raise a TypeError.

4

The code will run but only print the values of a and b.

4

Correct answer: B) The code will raise a SyntaxError.
The function definition in this code is incorrect and will raise a SyntaxError.

In Python, the order of parameters in a function definition is important and must follow a specific sequence:

Regular parameters

*args (if used)

Parameters with default values

**kwargs (if used)

In the given function definition, **kwargs comes before args, which violates this order. Additionally, the parameter c with a default value is placed after both *kwargs and *args, which is also incorrect.

5

Multiple Choice

Question image

What is the correct code to sort a list of dictionaries data by the values of the 'age' key in ascending order without changing the original list?

1

sorted_students = sorted(students, key=lambda x: x['name'])

2

sorted_students = sorted(students, key=lambda x: x['age'])

3

sorted_students = sorted(students, key=lambda x: x['age'], reverse=True)

4

sorted_students = sorted(students, key=lambda x: x['name'], reverse=True)

6

Option A is incorrect because This code sorts the list of dictionaries by the name key in ascending order. The lambda function lambda x: x['name'] extracts the value associated with the name key from each dictionary.

Option B is correct, This code sorts the list of dictionaries by the age key in ascending order. The lambda function lambda x: x['age'] extracts the value associated with the age key from each dictionary.

Option C is incorrect, This code sorts the list of dictionaries by the age key in descending order. The lambda function lambda x: x['age'] extracts the value associated with the age key from each dictionary, and the reverse=True parameter specifies that the sorting should be in descending order.

Option D is incorrect, This code sorts the list of dictionaries by the name key in descending order. The lambda function lambda x: x['name'] extracts the value associated with the name key from each dictionary, and the reverse=True parameter specifies that the sorting should be in descending order.

7

Multiple Choice

Question image

What will be the output?

1

True

False

2

True

True

3

False

TypeError

4

True

TypeError

8

  1. First Comparison: {1, 2, 3, 4} == {1, 4, 3, 2}

    • Explanation: In Python, sets are collections of unique elements that are unordered. The equality operator (==) checks if both sets contain the same elements, regardless of the order.

    • Result: The sets {1, 2, 3, 4} and {1, 4, 3, 2} contain the same elements. Therefore, the comparison returns True.

  2. Second Comparison: {1, 2, 3, 4} > {1, 2, 3, '4'}

    • Explanation: The greater than operator (>) is used to check if one set is a proper superset of another set. However, in this case, the sets contain elements of different types (integers and a string). According to Python's comparison rules, comparing objects of different types using == and != is allowed and will generally result in False or True, respectively

9

Multiple Choice

Question image

What will be the output?

1

{3.5, 'g', 6, 3, 'h', 4, 'y'}

2

{3.5, 'g', 6, 'h', 4, 'y'}

3

{3.5, 'g', 6, 'h', 'y'}

4

{3.5, 'g', 6, 3, 'h', 'y'}

10

  • The symmetric_difference() method removes the common element 3 and combines the remaining elements from both sets.

  • The resulting set {3.5, 'g', 6, 'h', 4, 'y'} contains all unique elements from both set1 and set2 excluding the common element.


The correct answer is
B)because the symmetric difference ofset1andset2is{3.5, 'g', 6, 'h', 4, 'y'}.

Question image

What will be the output of the following Python code?

1

Yes, the Value is 6
Result: 1

2

Yes, the Value is 6
Result: 6

3

Yes, the Value is 6
Result: None

4

Result: 1

Show answer

Auto Play

Slide 1 / 10

MULTIPLE CHOICE