ExamVeda
Login
Home
41
What is the order of namespaces in which Python looks for an identifier?
Discuss
Answer & Solution
Answer: Option B
Solution:
When Python encounters an identifier (variable or function name), it searches for it in a specific order across different namespaces.
The order of namespaces that Python looks for an identifier is:
Built-in namespace: Python first searches the built-in namespace, which contains the names of all built-in functions, exceptions, and constants.
Local namespace: Next, Python searches the local namespace, which contains names defined within the current function or code block.
Global namespace: If the identifier is not found in the local namespace, Python searches the global namespace, which contains names defined at the top level of the current module or in the global scope.
If the identifier is not found in any of these namespaces, Python raises a NameError.
Therefore, the correct order of namespaces in which Python looks for an identifier is: built-in namespace, local namespace, global namespace.
42
What will be the output of the following Python function?
min(max(False,-3,-4), 2,7)
Discuss
Answer & Solution
Answer: Option D
Solution:
In Python, the boolean value False evaluates to 0 in numeric contexts.
The max() function returns the largest of the given arguments.
Here, we have the expression max(False, -3, -4).
Since False is equivalent to 0, and -3 and -4 are numeric values, the largest value among them is 0.
Therefore, the output of max(False, -3, -4) is False.
The min() function then evaluates min(False, 2, 7), and since False evaluates to 0 and 2 and 7 are greater than 0, the minimum value among them is False.
Hence, the overall output is False.
43
What will be the output of the following Python function?
len(["hello",2, 4, 6])
Discuss
Answer & Solution
Answer: Option C
Solution:
The len() function returns the number of items in an object.
Here, we have a list ["hello", 2, 4, 6].
The list contains 4 items: a string, two integers, and another integer.
Therefore, the output of len(["hello", 2, 4, 6]) will be 4.
44
Which of the following is not a core data type in Python programming?
Discuss
Answer & Solution
Answer: Option C
Solution:
In Python, the core data types include:
1. Tuples
2. Lists
3. Dictionary
However, Classes are not considered a core data type in Python.
Classes are used to create user-defined data structures that bundle data attributes and methods.
Therefore, the correct option is Option C: Class.
45
Which one of the following is not a keyword in Python language?
Discuss
Answer & Solution
Answer: Option B
Solution:
In Python, keywords are reserved identifiers that have special meanings and purposes within the language.
eval is not a keyword in Python; rather, it is a built-in function used to evaluate a Python expression stored in a string.
Keywords like pass, assert, and nonlocal have specific meanings in Python and are reserved for special purposes within the language.
Therefore, the correct option is Option B: eval.
46
What will be the output of the following Python code?
x = 'abcd'
for i in x:
    print(i.upper())
Discuss
Answer & Solution
Answer: Option D
Solution:
In the given code, a string 'abcd' is assigned to the variable x.
Then, a for loop iterates over each character in the string x.
For each character i, the upper() method is called to convert it to uppercase.
The upper() method converts a string to uppercase.
Therefore, the output will be each character of the string 'abcd' converted to uppercase and printed on separate lines:
A
B
C
D
.
47
What will be the output of the following Python code?
x = [[0], [1]]
print((' '.join(list(map(str, x))),))
Discuss
Answer & Solution
Answer: Option D
Solution:
In the given code, the variable x is assigned a list containing two sublists: [[0], [1]].
The map() function is used to apply the str() function to each element of x, converting them to strings.
Then, list() is used to convert the resulting map object to a list.
The join() function concatenates these strings with a space separator.
Finally, the result is enclosed in a tuple using (...) and printed.
Therefore, the output will be: ('[0] [1]',).
48
What will be the output of the following Python code?
def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)
Discuss
Answer & Solution
Answer: Option C
Solution:
In the given code, the function foo() is defined.
Inside the function, there is a try-finally block.
In the try block, return 1 is encountered.
However, before returning, the finally block executes, which contains return 2.
As per the behavior of finally, it always executes, regardless of whether there is an exception or not.
Therefore, the return 2 statement in the finally block is executed, and 2 is returned from the function.
When foo() is called and its return value is assigned to k, k will be 2.
Hence, the output will be 2.
49
To add a new element to a list we use which Python command?
Discuss
Answer & Solution
Answer: Option C
Solution:
In Python, to add a new element to the end of a list, the append() method is used.
The syntax for using append() is list_name.append(element), where list_name is the name of the list and element is the value to be added.
append() adds the specified element to the end of the list, increasing the list's length by one.
Therefore, list1.append(5) will add the element 5 to the end of list1.
50
The following python program can work with . . . . . . . . parameters.
def f(x):
    def f1(*args, **kwargs):
           print("Examveda")
           return x(*args, **kwargs)
    return f1
Discuss
Answer & Solution
Answer: Option A
Solution:
The program defines a function f that takes another function x as an argument.
Inside f, there is a nested function f1 defined with variable-length arguments *args and keyword arguments **kwargs.
The use of *args allows f1 to accept any number of positional arguments, and **kwargs allows it to accept any number of keyword arguments.
Inside f1, "Examveda" is printed, and then the function x is called with the received arguments using (*args, **kwargs).
Therefore, the program can work with any number of parameters.