What will be output for the code?
❌ 2 GFGNO
❌ 2.33 GFGNOG
❌ 2.33 GFGNONoneGTrue
✅ 2.33 GFGNO
❌ Error
❌ None of mentioned
Explanation:
val1 will only have integer and floating values val1 = 1 + 1.33 + 0 = 2.33 and val2 will have string values val2 =’GFG’ + ‘NO’ = ‘GFGNO’. String ‘G’ will not be part of val2 as the for loop will break at None, thus ‘G’ will not be added to val2.
❌ 2 GFGNO
❌ 2.33 GFGNOG
❌ 2.33 GFGNONoneGTrue
✅ 2.33 GFGNO
❌ Error
❌ None of mentioned
Explanation:
val1 will only have integer and floating values val1 = 1 + 1.33 + 0 = 2.33 and val2 will have string values val2 =’GFG’ + ‘NO’ = ‘GFGNO’. String ‘G’ will not be part of val2 as the for loop will break at None, thus ‘G’ will not be added to val2.
What will be output for the code?
❌ 1 1 1
❌ 2 2 2
❌ 3 3 3
❌ 1 2 3
✅ 3 2 3
❌ Error
❌ None of mentioned
Explanation:
In Python, class variables are internally handled as dictionaries. If a variable name is not found in the dictionary of the current class, the class hierarchy (i.e., its parent classes) are searched until the referenced variable name is found, if the variable is not found error is being thrown.
The output 3 2 3 may be surprising. Instead of 3 3 3, here B.val reflects 2 instead of 3 since it is overridden earlier.
❌ 1 1 1
❌ 2 2 2
❌ 3 3 3
❌ 1 2 3
✅ 3 2 3
❌ Error
❌ None of mentioned
Explanation:
In Python, class variables are internally handled as dictionaries. If a variable name is not found in the dictionary of the current class, the class hierarchy (i.e., its parent classes) are searched until the referenced variable name is found, if the variable is not found error is being thrown.
The output 3 2 3 may be surprising. Instead of 3 3 3, here B.val reflects 2 instead of 3 since it is overridden earlier.
What will be output for the code?
❌ {0: 0, 1: 0, 2: 0}
✅ {0: 1, 1: 1, 2: 1}
❌ {0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
❌ TypeError: Immutable object
❌ Compilation error
❌ None of the mentioned
Explanation:
1st loop will give 3 values to i 0, 1 and 2. In the empty dictionary, valued are added and overwritten in j loop, for eg. D[0] = [0] becomes D[0] = 1, due to overwriting.
❌ {0: 0, 1: 0, 2: 0}
✅ {0: 1, 1: 1, 2: 1}
❌ {0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
❌ TypeError: Immutable object
❌ Compilation error
❌ None of the mentioned
Explanation:
1st loop will give 3 values to i 0, 1 and 2. In the empty dictionary, valued are added and overwritten in j loop, for eg. D[0] = [0] becomes D[0] = 1, due to overwriting.
What is the output of the code?
❌ TypeError
❌ Code doesn't compile
❌ (1, 5, 3, 4)
❌ 1 TypeError
✅ 6 TypeError
❌ None of the mentioned
Explanation:
T1 is an integer while T2 is tuple. Thus T1 will become 1 + 5 = 6. But an integer and tuple cannot be added, it will throw TypeError.
❌ TypeError
❌ Code doesn't compile
❌ (1, 5, 3, 4)
❌ 1 TypeError
✅ 6 TypeError
❌ None of the mentioned
Explanation:
T1 is an integer while T2 is tuple. Thus T1 will become 1 + 5 = 6. But an integer and tuple cannot be added, it will throw TypeError.
What is the output of the code?
❌ D C
❌ E B
❌ D B
❌ KeyError B
✅ E KeyError
❌ None of the mentioned
Explanation:
Key-Value Indexing is used in the example above. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][2] = “B”, D[D[1][2]] = D[“B”] = “D” and D[“D”] = “E”. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][“A”] = {1 : “A”} and D[1][“A”][2] doesn’t exists, thus KeyError.
❌ D C
❌ E B
❌ D B
❌ KeyError B
✅ E KeyError
❌ None of the mentioned
Explanation:
Key-Value Indexing is used in the example above. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][2] = “B”, D[D[1][2]] = D[“B”] = “D” and D[“D”] = “E”. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][“A”] = {1 : “A”} and D[1][“A”][2] doesn’t exists, thus KeyError.
What is the result of the code?
❌ 3
❌ 4
✅ 5
❌ Does not compile
❌ None of mentioned
Explanation:
In Python, everything is a reference, and references are passed by value. Parameter passing in Python is the same as reference passing in Java. As a consequence, the function can modify the value referred by passed argument, i.e. the value of the variable in the caller’s scope can be changed. Here the task of the function “addToList” is to add an element 10 in the list, So this will increase the length of the list by 1. So the output of the program is 5.
❌ 3
❌ 4
✅ 5
❌ Does not compile
❌ None of mentioned
Explanation:
In Python, everything is a reference, and references are passed by value. Parameter passing in Python is the same as reference passing in Java. As a consequence, the function can modify the value referred by passed argument, i.e. the value of the variable in the caller’s scope can be changed. Here the task of the function “addToList” is to add an element 10 in the list, So this will increase the length of the list by 1. So the output of the program is 5.