python interview questions

1. What is the output of below code.

test = [1,7,9,0,4,2]

print (test[-2:-1])

 

A :  [0]

B :  [4,2]

C : [4]

D : [9,0,4,2]

Answer : C

2. What is output of below Code.

a=5

a,b=a+4,a*2

print(a,b)

 

A : 9 18

B : 18 9

C : 9,18

D : 9 10

Answer : D

3. What is output of below Code.

test=”hello world”

substr=test.find(‘a’,0,len(test))

print(substr)

 

A : 0

B : -1

C : Exception

D : Error

Answer : B

4. What is output of below Code.

def extendList1(val, list=[]):

list.append(val)

return list

list1 = extendList1(10)

list2 = extendList1(123,[])

list3 = extendList1(‘a’)

print(list1,list2,list3)

 

A : [10] [123] [‘a’]

B : [10,’a’][123][‘a’]

C : [10,123,’a’][10,123,’a’][10,123,’a’]

D : [10,’a’][123][10,‘a’]

Answer : D

5. What is output of below Code.

>>> from collections import Counter

>>> Counter([1,3,2,1,4,2,1,3,1])

 

A:Counter({1: 4, 2: 2, 3: 2, 4: 1})

B:{1: 4, 2: 2, 3: 2, 4: 1}

C:Counter({4,  2,  2, 4 1})

D:{4,  2,  2, 4 1}

Answer : A

6. How can I find the methods or attributes of an object?

Answer : Using dir function

7. What is a Python module?

Answer : A module is a Python script that generally contains import statements, functions, classes and variable definitions, and Python runnable code and it “lives” file with a ‘.py’ extension.

8. Which of the following is an invalid statement?

a) abc = 1,000,000

b) a b c = 1000 2000 3000

c) a,b,c = 1000, 2000, 3000

d) a_b_c = 1,000,000

Answer : B

9. Is Python object oriented? what is object oriented programming?

Yes. Python is Object Oriented Programming language. OOP is the programming paradigm based on classes and instances of those classes called objects. The features of OOP are:

Encapsulation, Data Abstraction, Inheritance, Polymorphism.

10. Is Python object oriented? what is object oriented programming?

Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.

11. By Default String is a List?

True or False.

Answer : True.

12. In String Formatting %x represents

A : Decimal

B : Integer

C : hexadecimal integer

D : Octal Integer

Answer : C

13. If I want to remove the last element from list which is the most suitable method on can use.

A : remove

B : pop

C : del

D : None

Answer : B

14. What is the statement that can be used in Python if a statement is required syntactically but the program requires no action?

A : pass

B : continue

C : break

D : skip

Answer : A

15. pick odd one out

A : openpyxl

B : xlsxwriter

C : xlrd

D : re

Answer : D

16. What is the difference between “Xrange” and “range”?

“Xrange” returns the “Xrange” object while range returns the “list” irrespective of the size of the “range”.

17. In a class definition, what does the __ init_O function do?

It overrides the any initialization from an inherited class, and is called when the class is instantiated

18. Below Function uses what type of arguments

def printinfo( name, age = 35 ):

print (“Name: “, name)   print (“Age “, age)

return

printinfo( name = “miki” )

 

A : Required argument

B : Keyword arguments

C : Default arguments

D : Variable-length arguments

Answer : C

19. If I want to matches any non-whitespace character which of below can be used.

A : \s

B :  \S

C :  \b

D :  \W

Answer : B

20. How is memory managed in Python?

Memory management in python is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have access to this private heap. The python interpreter takes care of this instead.

21. How is Python executed in command line?

Answer: By entering filename.py

22. what is output following code:

def test(n = []):

A=n[0]

A=A+5

Print(n)

test([1, 2, 3])

test([1,2])

A : [1,2,3]

B : [1,2]

C : Error

D : None

Answer : C

23. Is it valid if a function doesn’t have a return statement?

Answer :  Yes

24. Is it possible have multiple returns in function?

Answer : Yes using yield keyword.

25. In Python what is slicing?

Answer : A mechanism to select a range of items from sequence types like list, tuple, strings etc. is known as slicing.

26. What are generators in Python?

Answer : The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.

27. What is output of below code.

>>> a=8.5//3

>>> print(a)

 

A:2.0

B: 2.5

C:2

D: Error

Answer : A

28.Can True=False be possible in Python?

Answer : No

29. Is String in Python are immutable?

Answer : Yes

30.What is lambda function in python?

Answer : ‘lambda’ is a keyword in python which creates an anonymous function. Lambda does not contain block of statements. It does not contain return statements.

Share

Recommended Posts

Leave a Reply

Your email address will not be published. Required fields are marked *