Python Programming Language Interview Questions with Answers
Python Programming Language Interview Questions with Answers
Here you will find top 50 Python programming language interview questions with answers. These Python interview questions you’ll most likely be asked.
1. What do you know about python?
Python is a high-level programming language
Python is
- interpreted
- interactive
- object-oriented scripting language
- highly readable
2. What makes Python differ from other programming languages?
In python English keywords are used frequently whereas punctuation is used in other languages, and it has fewer syntactical constructions than other languages.
3. Python is interpreted and interactive. How?
Python is processed at runtime by the interpreter. It does not need compilation before execution. It is interactive as one can actually sit at a Python prompt.
4. What is the use of Python?
It is used in GIS programming and as a scripting language (for ArcGIS, and Quantum GIS). Python is true general purpose language.
5. What is a board standard library of python?
A board standard library: Python’s bulk of the library is cross-platform compatible on Windows, UNIX, and Macintosh. It is portable.
6. What makes python portable?
Python can run on a wide variety of h-platforms and has the same interface for every platform.
7. How does it manage the memory?
A combination of a cycle-detecting garbage collector and reference counting and dynamic typing is used for memory management. Its memory is managed by its private heap space. All objects and data structures are placed in a private heap. The programmer is not allowed to access the heap but interpreter takes care of this private heap.
8. What is pickling and unpickling?
Pickle is a module which accepts any Python object. Converts this object into a string and dumps it into a file by using dump function, this is called pickling. While retrieving its original objects from the stored string is called unpickling.
9. How are arguments passed?
In Python everything is an object and its variables hold references to the objects. The references values are assigned according to the functions; as a result the value of the references cannot be changed. However, mutable objects can do so.
10. What is python namespace?
It is like any box where a name of variable is mapped to the object being placed. To get corresponding object the box is searched every time when the variable is searched out.
11. What is PYTHONSTARTUP environment variable?
A variable which contains the path of an initialization file which contains Python source code, is executed whenever the interpreter is started and is named as .pythonrc.py in Unix and contains commands that modify PYTHONPATH or load utilities.
12. Is python a case sensitive language?
Yes!.
13. What are the supported data types in Python?
There are Immutable and mutable types of Pythons built in types.
Immutable:
- Strings
- Tuples
- Numbers
Mutable:
- List
- Sets
- Dictionaries
14. What are tuples in Python?
A tuple is any other sequence data type which is similar to the list and consists of a number of values separated by commas, enclosed within parentheses.
15. What is the difference between tuples and lists in Python?
- List is belongs to mutable type while tuple belongs to immutable types. Tuple can be hashed for e.g. as a key for dictionaries.
- Brackets ( [ ] ) are used for enclosing list while parentheses ( ( ) ) are used to enclose tuples.
- Elements and size of lists can be changed, while tuples be update and can be thought of as read-only lists.
16. What are python dictionaries?
It is kind of hash table type. Working process is just like associative arrays or Perl hashes and consist of key-value pairs. A dictionary key can be of any Python type, but are numbers or strings usually. On the other hand, values can be any arbitrary Python object.
17. What is pass in Python?
Pass means, no-operation Python statement. It is used when a one does not want any command or code to execute but is required syntactically. The pass statement is a null operation; nothing will happens when it gets executed. It is useful in those places where the code will eventually go, but nothing has been written yet
18. What is the output of print str * 2 if str = ‘Hello!’?
It will print string two times. Output would be Hello!Hello!.
19. How will you convert an object to a string in python?
str(x) − object x is converted to a string representation.
20. How will you convert a string to a set in python?
set(s) − s is converted to a set.
21. What is the purpose of is operator?
is − Evaluates to true if the variables on both sides of the operator are pointing to the same object and false otherwise. y is x, here is results in 1 if id(y) equals id(x).
22. How can you get a random number in python?
random() − it returns a random float r, such that 0 <= r and r <1.
23. How will you check in a string that it contains all digits?
isdigit() − it returns true if string contains only digits else false.
24. How will you merge elements in a sequence?
join(seq) − concatenates the string representations of elements in seq sequence with separator string, into a string.
25. How will you reverse a list?
list.reverse() − reverses list objects.
26. What is the output of print tuple [1:3] if tuple = ( ‘ahjd’, 786 , 2.23, ‘sean’, 70.2 )?
It will print elements starting from 2nd to3rd. Output is (786, 2.23).
27. What is PYTHONHOME environment variable?
It is an alternative module search path, embedded usually in the PYTHONSTARTUP or PYTHONPATH directories making switching module libraries easy.
28. What are python regular expressions?
It is a special sequence of characters that helps in matching or finding sets of strings or other strings held in a pattern using a specialized syntax. These are used widely in the world of UNIX.
29. What is the difference between Xrange and range?
Range returns the list and the same memory is used no matter what the range size is.
Xrange returns the xrange object
30. What is module and package in Python?
Module is the way program is structured. Each program file is a module, in which other modules like objects and attributes can be imported. The folder of program is a package of modules like in other programming languages. A package can have subfolders or modules.
31. What do you know about unittest?
A unit testing framework of python is called unittest. It supports automation testing, sharing of setups, aggregation of tests into collections, shutdown code for tests, etc.
32. Given a function that does not return any value, when executed in shell what value is thrown by it by default?
NoneType object is thrown back by python shell.
33. Which core datatype is used for the storage of values in terms of key and value?
Dictionary stores values in terms of values and keys.
34. What is the order of precedence in python?
Parentheses, exponential, division, multiplication, addition and subtraction.
35. What is the maximum possible length of an identifier?
Identifiers can be of any length.
36. Why are local variable names beginning with an underscore discouraged?
Leading underscores are used to indicate variables as Python has no concept of private variables that must not be accessed from outside the class.
37. What is the output of the following?
print(”’
”’.isspace())
|
True, a newline character is considered as space.
38. How many except statements can a try-except block have?
More than zero, there has to be at least one except statement.
39. Which module in the python standard library is used for parsing options received from the command line?getopt is used for parsing options received from the command line.
40. What is returned by math.ceil() function?
The ceil function returns the smallest integer >= to the number itself.
41. Is the function abs() same as math.fabs()?
The return type of function abs() is determined by the type of value which is passed to it whereas math.fabs() always returns a float and does not work with complex numbers
42. What does os.name contain?
It contains operating system dependent module name imported such as ‘posix’, ‘java’ etc.
43. What List nd Dict comprehensions are?
Syntax constructions based on existing iterable to ease the creation of a Dictionary or List.
44. Define iterators.
To iterate a group of “containers” or “elements”.
45. Define generators.
A generator is simply a function that returns an object on which you can call next
until it raises a StopIteration exception, such that for every call it returns some value, signaling that all values have been generated.
46. Define slicing.
A process of selecting a range of items from sequence types like tuple, strings, list etc. is called as slicing.
47. What is the output of print list + nlist * 2 if list = [ ‘ajhd’, 786 , 2.97, ‘sean’, 70.2 ] and nlist = [123, ‘sean’]?
It will print concatenated lists. Output would be [‘ajhd’, 786, 2.97, ‘sean’, 70.200000000000003, 123, ‘sean’].
48. How will you get all the keys from the dictionary?
dictionary.keys() function does the work here, all the keys from the dictionary object can be retrieved by using it.
print dict.keys() # Prints all the keys
49. How will you convert an object to a regular expression in python?
repr(x) − object x is converted to an expression string.
50. What is the purpose of // operator?
// represents floor division which corresponds to the division of operands where the result is the quotient such that after the decimal point are removed.