Interview Questions
&
Answers

Python Interview questions and Answers

In simple way Meaning of Analogy is “Similarity ” or compassion between one thing and other thing base d on some similarity 

250 + Python interview Question and Answer

1. What type of a language is python? Interpreted or Compiled?

Beginner’s Answer:
Python is an interpreted, interactive, object­oriented programming language.
Expert Answer:
Python is an interpreted language, as opposed to a compiled one, though the
distinction can be blurry because of the presence of the bytecode compiler. This means
that source files can be run directly without explicitly creating an executable which is
then run.

2.What do you mean by python being an “interpreted language”? (Continues from previous question)

An interpreted language is a programming language for which most of its implementations execute instructions directly, without previously compiling a program into machine­ language instructions. In context of Python, it means that Python program runs directly from the source code.

3. What is python’s standard way of identifying a block of code?  

Indentation.   

4.Please provide an example implementation of a function called “my_func”  that returns the square of a given variable “x”. (Continues from previous  question) 

def my_func ( x ):  

 return x **2 

5. Is python statically typed or dynamically typed? 

  Dynamic.  

 In a statically typed language, the type of variables must be known (and usually  declared) at the point at which it is used. Attempting to use it will be an error. In a  dynamically typed language, objects still have a type, but it is determined at runtime.  You are free to bind names (variables) to different objects with a different type. So long  as you only perform operations valid for the type the interpreter doesn’t care what type  they actually are.   

6. Is python strongly typed or weakly typed language? 

 Strong. 

 In a weakly typed language a compiler / interpreter will sometimes change the  type of a variable. For example, in some languages (like  JavaScript ) you can add  strings to numbers ‘x’ + 3 becomes ‘x3’. This can be a problem because if you have  made a mistake in your program, instead of raising an exception execution will continue  but your variables now have wrong and unexpected values. In a strongly typed  language (like Python) you can’t perform operations inappropriate to the type of the  object ­ attempting to add numbers to strings will fail. Problems like these are easier to  diagnose because the exception is raised at the point where the error occurs rather than  at some other, potentially far removed, place.   

7.Create a unicode string in python with the string “This is a test string”? 

some_variable = u ‘ This is a test string ‘  

Or

  some_variable = u ” This is a test string

8. What is the python syntax for switch case statements? 

Python doesn’t support switch­case statements. You can use if­else statements  for this purpose.  

9. What is a lambda statement? Provide an example. 

A lambda statement is used to create new function objects and then return them at  runtime. 

Example:  my_func = lambda x : x **2  

 creates a function called my_func that returns the square of the argument  passed.

10..What are the rules for local and global variables in Python? 

If a variable is defined outside function then it is  implicitly global . If variable is  assigned new value inside the function means it is local . If we want to make it global we need to explicitly define it as global. Variable referenced inside the function are implicit  global 

 

11..What is the output of the following program? 
#!/usr/bin/python   
  def fun1 ( a ): 
 print 'a:' , a 
 a = 33; 
  print 'local a: ' , a  
 a = 100  
 fun1 ( a)
 print 'a outside fun1:' , a      

#!/usr/bin/python    

 def fun1 ( a ): 

 print ‘a:’ , a   a = 33; 

  print ‘local a: ‘ , a 

   a = 100 

  fun1 ( a)  

 print ‘a outside fun1:’ , a    

 Ans. 

 Output:  a : 100

 local a : 33  

 a outside fun1 : 100

12..What is the output of the following program? 

#!/usr/bin/python

def fun2():

        global b

         print ‘b: ‘ , b 

         b   =  33

         print ‘global b:’ , b

b = 100

fun2()

print ‘b outside fun2’ , b

Output: 

 b : 100   global 

b : 33

b outside fun2 : 33 

13.What is the output of the following program? 

#!/usr/bin/python

def foo(x, y):

           global a

            a = 42

            x,y = y,x

            b  =33

            b  = 17

            c = 100

            print(a,b,x,y)

a,b,x,y=1,15,3,4foo

(

17

,

4)

print

(

a

,

b

,

x

,

y

Output: 

 b : 100   global 

b : 33

b outside fun2 : 33 

9) What is difference between c++ and Java ?

Interface

  •  Java is platform independent
  • There are no pointers in java
  • There is no operator overloading in java
  • There is garbage collection in java
  • Supports multithreading
  • There are no templates in java
  • There are no global variables in java

Abstract Class

  • C++ is platform dependent.
  • There are pointers in C++
  • C ++ has operator overloading.
  • There is no garbage collection
  • Does’nt support multithreading
  • There are templates in java
  • There are global variables in c++

12) Difference between this() and super() in java ?

this() is used to access one constructor from another with in the same class while super() is used to access superclass constructor. Either this() or super() exists it must be the first statement in the constructor.