Q56. How, can you pass optional or keyword parameters from one function to another function?
Ans: You have to collect the arguments as * and ** , which will give you positional arguments as a tuple and the keyword arguments as a dictionary. You can pass these arguments when calling another function using the * and **
def fun1(param1, *args, *kwargs): kwargs[key]=value fun2(param1, *args, *kwargs) # Calling another function |
Q57. You have the following code
list1 = [ ] list2 = list1
list2.append(“hadoopexam.com”) |
What all the values are in list1 and list2?
Ans : Both list1 and list2 will hold the same value. Because, list2 is pointing to the same list as created by list1. Hence, any modification you do either through list1 or list2 , changes will be reflected in both. In fact there is only one list and pointed by two variables named list1 and list2.
Q58. You have been given below code
X=100 y=x x=x+1 #there new object of x will be created as x itself cannot be mutated. |
What is the value hold by variable x and y?
Ans : Here x will have 101 and y will be 100. Because, integers are immutable and when you do x=x+1 , it will be creating new int object and variable x will be assigned that variable. However, y will still hold the old value.
Q59. You have a list y as below
y=["hadoopexam" , "learning" ,"resource" , "spark" , "learning" , "resources"] x=y.sort() |
When you call y.sort() , what will happen?
Ans: In this case y will hold the sorted list , but variable x is pointing to None. Most of the cases the operation which mutate object itself return None. Here, y itself sorted.
Learn Python in Less than 8 Hours sitting at Home/@Desk
Q60. You have below code
x=(1,2,3) x+=(100,) |
What will happen to the x?
Ans : In this case x is immutable, hence new object of x will be created.