# MCQ Quiz: Python Programming!

Approved & Edited by ProProfs Editorial Team
At ProProfs Quizzes, our dedicated in-house team of experts takes pride in their work. With a sharp eye for detail, they meticulously review each quiz. This ensures that every quiz, taken by over 100 million users, meets our standards of accuracy, clarity, and engagement.
| Written by Yogi0001122
Y
Yogi0001122
Community Contributor
Total Contribution - 2 | Total attempts - 1,326
Questions: 10 | Attempts: 313  Settings  .

• 1.

### How to change global variables locally?

• A.

Use global <variable name> in the function

• B.

Use local <variable name> in the function

• C.

Use global <variable name> outside the function

• D.

None of the above

A. Use global <variable name> in the function
Explanation
Using the "global" keyword followed by the variable name inside a function allows you to change the value of a global variable locally within that function. This means that any changes made to the variable inside the function will also affect the variable outside of the function.

Rate this question:

• 2.

### What will be printed: >>> def f(a, b):              print(a, b) >>> f(b=1, *(2,))

• A.

Error

• B.

1 1

• C.

2 1

• D.

1 2

C. 2 1
Explanation
The function f takes two arguments, a and b. In the function call f(b=1, *(2,)), the value of b is explicitly set to 1 using keyword argument syntax. The *(2,) syntax is used to unpack the tuple (2,) and pass its elements as positional arguments to the function. Since there is only one element in the tuple, it will be passed as the value of a. Therefore, the output will be 2 1, with 2 being the value of a and 1 being the value of b.

Rate this question:

• 3.

### Below syntax is correct or not? def f(a):     pass f(a for a in [1,2])

• A.

Yes

• B.

No

A. Yes
Explanation
The given syntax is correct. The function "f" is defined with a single parameter "a", and the body of the function is empty (indicated by the "pass" statement). The function is then called with an argument that uses a generator expression to iterate over the list [1,2] and pass each element as the value of "a". This syntax is valid in Python.

Rate this question:

• 4.

### What does this function return? >>> def f(a,b):             return a,b >>> f(**{'b':2,'a':1})

• A.

Error

• B.

1,1

• C.

2,1

• D.

1,2

D. 1,2
Explanation
The function returns the values of the variables a and b. In this case, the function is called with keyword arguments using the double asterisks (**), which unpacks the dictionary { 'b': 2, 'a': 1 } and assigns the values to the corresponding variables a and b. Therefore, the function returns 1, 2.

Rate this question:

• 5.

### Import random print random.randint(0, 5) This will output either 0,1, 2, 3, 4 or 5.

• A.

TRUE

• B.

FALSE

A. TRUE
Explanation
The given code uses the random module in Python to generate a random integer between 0 and 5 (inclusive). The randint() function is used, which returns a random integer from the specified range. Therefore, the code will output a random number between 0 and 5, including both 0 and 5. Hence, the answer is TRUE.

Rate this question:

• 6.

### What is the output of this program? >>>colors = ["blue", "lavender", "red", "yellow"] >>>for color in sorted(colors, key=lambda color: len(color), reverse=True):       print(color)

• A.

Lavender yellow blue Red

• B.

Yellow blue Red

• C.

Yellow blue Red lavender

• D.

None of the above

A. Lavender yellow blue Red
Explanation
The program sorts the list of colors in descending order based on the length of each color. The lambda function is used as the key to determine the length of each color. Therefore, the output will be "lavender", "yellow", "blue", and "Red" in separate lines.

Rate this question:

• 7.

### What is the output of this program? >>>furniture = {"table" : 1, "chair" : 2, "desk" : 4} >>>s = sorted(furniture.keys()) >>>for key in s:         print(key, furniture[key])

• A.

Chair 2 desk 4 table 1

• B.

Error

• C.

"chair 2" "desk 4" "table 1"

• D.

None of the above

D. None of the above
Explanation
The output of the program will be:
chair 2
desk 4
table 1
The program creates a dictionary called "furniture" with keys and values representing different furniture items. It then sorts the keys of the dictionary and assigns them to the variable "s". The program then iterates over each key in "s" and prints the key followed by its corresponding value from the "furniture" dictionary. Therefore, the output will be the keys and values of the "furniture" dictionary in sorted order.

Rate this question:

• 8.

### Def func(n):    if(n==1):       return 1;    else:       return(n+func(n-1)) print(func(4))

• A.

12

• B.

10

• C.

9

• D.

11

B. 10
Explanation
The given code defines a function called "func" which takes an input "n". If the value of "n" is equal to 1, the function returns 1. Otherwise, it returns the sum of "n" and the result of calling the function recursively with the argument "n-1". In the given code, the function is called with the argument 4. So, the function calculates the sum of 4 and the result of calling the function with the argument 3, which is 6. Then, it calculates the sum of 4 and 6, which is 10. Therefore, the correct answer is 10.

Rate this question:

• 9.

### When will the else part of try-except-else be executed?

• A.

Always

• B.

When an exception occurs

• C.

When no exception occurs

• D.

When an exception occurs in to except block

C. When no exception occurs
Explanation
The else part of a try-except-else block will be executed when no exception occurs. This means that if the code inside the try block runs successfully without raising any exceptions, the code inside the else block will be executed. If an exception is raised within the try block, the code inside the except block will be executed instead.

Rate this question:

• 10.

### Is the following Python code valid? try: # Do something except: # Do something finally: # Do something

• A.

no, there is no such thing as finally

• B.

No, finally cannot be used with except

• C.

No, finally must come before except

• D.

Yes Back to top