Python Interview Questions

Learn Python in Less than 8 Hrs Sitting @ Home /Desk with HandsOn

Q51. Why should you avoid below import statement

from nameofmodule import *

Ans : Because, if you have created a variable with the same name as defined in import module as well, then it will overwrite your variable and it will be difficult to debug, what happened to the variable.

Q52. What is the best order to import the modules?

Ans : It is considered best practice, if you import the modules in following order

- First import standard library module e.g. sys, os, re

- Then third-party modules e.g. mx.DateTime

- Then modules developed by you locally.

Q53. What is the circular import and which form of circular import will not create a problem?

Ans : When you have import statement at the top and uses the “import nameofmodule” it is fine.

import b

import a

This is circular import and they will work fine, without any issue.

Q54. Which form of circular import will not work?

Ans : When you put circular import in the form of “from nameofmodule import something” and you will have circular dependencies than it will fail.

from b import x

from a import y

Here, module a is busy importing module b and in module b value y from module a is not yet available. Hence, this kind of circular import should be avoided at the top level and needs to move import to function or class level.

Only move imports into a local scope, such as inside a function definition, if it’s necessary to solve a problem such as avoiding a circular import or are trying to reduce the initialization time of a module. This technique is especially helpful if many of the imports are unnecessary depending on how the program executes. You may also want to move imports into a function if the modules are only ever used in that function. Note that loading a module the first time may be expensive because of the one time initialization of the module, but loading a module multiple times is virtually free, costing only a couple of dictionary lookups. Even if the module name has gone out of scope, the module is probably available in sys.modules.

Q55. When you define a function with default shared value like “def function_name(mydict={}):” what issue you will see?

Ans: When you define a function as below

def fun1(my_dict={}):
                … do something…
                return my_dict

Here, it is expected that every time you call the function fun1, it will create a new my_dict instance with no values. But this is not correct, because default values are created only once. And any subsequent call to that function will use the already created my_dict object.

To avoid such scenario, you should have used None as a default value and inside the function you should check whether the value is None then create a new my_dict.

def fun1(my_dict=None):
                if my_dict is None:
                  my_dict={} #now it is a local dictionary object

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):
         fun2(param1, *args, *kwargs) # Calling another function

Q57. You have the following code

list1 = [ ]
list2 = list1

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=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"]

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.

Q60. You have below code


What will happen to the x?

Ans : In this case x is immutable, hence new object of x will be created.

Learn Python in Less than 8 Hrs Sitting @ Home /Desk with HandsOn

Premium Training : Spark Full Length Training : with Hands On Lab

Previous Next

Home Spark Hadoop NiFi Java

Disclaimer :

1. Hortonworks® is a registered trademark of Hortonworks.

2. Cloudera® is a registered trademark of Cloudera Inc

3. Azure® is aregistered trademark of Microsoft Inc.

4. Oracle®, Java® are registered trademark of Oracle Inc

5. SAS® is a registered trademark of SAS Inc

6. IBM® is a registered trademark of IBM Inc

7. DataStax ® is a registered trademark of DataStax

8. MapR® is a registered trademark of MapR Inc.

2014-2017 © | Dont Copy , it's bad Karma |