Python Programming Question Set
Python Programming Question Set
Initially, the dictionary has three keys. After multiple manipulations, including additions and deletions of keys and values, the final dictionary has only one key with an integer number type and a newly inserted key with a string number type, making the length of the dictionary 1 .
The 'append' method adds the entire list as a single element at the end, increasing length by 1, while 'extend' iterates over the added list, inserting its elements independently, increasing length by 4 for [6, 7, 8]. Thus, the final length of the list becomes 7 .
The try-except block first catches a ZeroDivisionError when dividing by zero, printing 'Cannot divide by 0'. No exception occurs when subsequently dividing by 5, so 'GFG' is printed. This shows how Python executes only the relevant except block and how subsequent operations in the else clause proceed if no exceptions occur in the try block .
Inheritance allows class B and class C to access the class variable 'val' from class A. Initially, B.val and C.val will be the same as A.val, but changing B.val only affects class B. Later, when A.val is updated, it affects A.val and C.val but not B.val as B had an overridden value. This demonstrates the use of inheritance and how class variables behave in an inheritance chain .
Using the initial value of x = 2, the operations proceed as follows: first, applying r(x) results in 4, then applying s(x) on the result leads to 12, and finally, applying r(x) again on this result gives 24. This chain of function applications demonstrates how lambda functions can be used to sequentially transform data .
Python functions can return multiple values as a tuple by default. In this example 'return_two_values' returns True, 'hi', and 1 as a tuple, demonstrating Python's flexibility with returning multiple items using a single return statement .
In Python, the '//' operator performs floor division, which discards the fractional part of the division result. Given a = 4.5 and b = 2, the operation a//b results in 2.0 because the division of 4.5 by 2 equals 2.25, and flooring this yields 2.0 .
The global keyword allows the doThis function to modify the global variable 'count'. Every time the function is called, it increments 'count' by 3 during its loop, running a total of 3 times. The final output is 4, as the global count starts at 1 and is incremented across 3 iterations .
The outer and inner loops in the code use the same variable 'i', causing the outer loop's scope to be overwritten by the inner loop. Therefore, for each iteration of the outer loop, only numbers 4 and 5 are printed, repeated for the outer loop's range of 2. This illustrates variable scope limitations across nested loops .
The conditions in the program are evaluated sequentially. The first condition 'not a or b' evaluates to False, the second 'not a or not b and c' also evaluates to False, but the third 'not a or b or not b and a' evaluates to True because 'not b' is True and 'a' is True. Thus, it prints '3' .