Saturday, May 1, 2021

51 PYTHON INTERVIEW QUESTIONS - (Level 0)


1) What is Python? What are the benefits of using Python?

 Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source. 


2) What is PEP 8? 

 PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.

 3) What is pickling and unpickling? 

 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. 

 4) How Python is interpreted?


 Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed. 

 5) How memory is managed in Python?


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

 The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.

 Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.

6) What are the tools that help to find bugs or perform static analysis?


 PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.

 7) What are Python decorators?


 A Python decorator is a specific change that we make in Python syntax to alter functions easily.

 8) What is the difference between list and tuple?



 The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries. 

 9) How are arguments passed by value or by reference?


 Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result you cannot change the value of the references. However, you can change the objects if it is mutable. 


 10) What is Dict and List comprehensions are?


 They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable. 


 11) What are the built-in type does python provides?


 There are mutable and Immutable types of Pythons built in types 
Mutable built-in types
 List
 Sets
 Dictionaries

 Immutable built-in types
 Strings
 Tuples
 Numbers 


 12) What is namespace in Python?


 In Python, every name introduced has a place where it lives and can be hooked for. This is known as namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get corresponding object. 

 13) What is lambda in Python?


 It is a single expression anonymous function often used as inline function. 


 14) Why lambda forms in python does not have statements?


 A lambda form in python does not have statements as it is used to make new function object and then return them at runtime. 

 15) What is pass in Python?


 Pass means, no-operation Python statement, or in other words it is a place holder in compound statement, where there should be a blank left and nothing has to be written there.

 16) In Python what are iterators?


 In Python, iterators are used to iterate a group of elements, containers like list.

 17) What is unittest in Python?


 A unit testing framework in Python is known as unittest. It supports sharing of setups, automation testing, shutdown code for tests, aggregation of tests into collections etc. 

 18) In Python what is slicing?


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

 19) What are generators in Python?


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

 20) What is docstring in Python?


 A Python documentation string is known as docstring, it is a way of documenting Python functions, modules and classes.

 21) How can you copy an object in Python?


 To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

 22) What is negative index in Python?


 Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth. 


 23) How you can convert a number to a string?



 In order to convert a number into a string, use the inbuilt function str(). If you want a octal or hexadecimal representation, use the inbuilt function oct() or hex(). 

 24) What is the difference between Xrange and range?


 Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is. 

 25) What is module and package in Python?


 In Python, module is the way to structure program. Each Python program file is a module, which imports other modules like objects and attributes.

 The folder of Python program is a package of modules. A package can have modules or subfolders. 


 26) Mention what are the rules for local and global variables in Python?


 Local variables: If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local.

 Global variables: Those variables that are only referenced inside a function are implicitly global.

 27) How can you share global variables across modules?


 To share global variables across modules within a single program, create a special module. Import the config module in all modules of your application. The module will be available as a global variable across modules. 

 28) Explain how can you make a Python Script executable on Unix?


 To make a Python Script executable on Unix, you need to do two things,

 Script file’s mode must be executable and

 the first line must begin with # ( #!/usr/local/bin/python) 


 29) Explain how to delete a file in Python?


 By using a command os.remove (filename) or os.unlink(filename) 

30) Explain how can you generate random numbers in Python?


 To generate random numbers in Python, you need to import command as import random random.random()

 This returns a random floating point number in the range [0,1) 


 31) Explain how can you access a module written in Python from C?


 You can access a module written in Python from C by following method,


 Module = =PyImport_ImportModule(“”); 


 32) Mention the use of // operator in Python?


 It is a Floor Divisionoperator , which is used for dividing two operands with the result as quotient showing only digits before the decimal point. For instance, 10//5 = 2 and 10.0//5.0 = 2.0. 


 33) Mention five benefits of using Python?


 Python comprises of a huge standard library for most Internet platforms like Email, HTML, etc.

 Python does not require explicit memory management as the interpreter itself allocates the memory to new variables and free them automatically
 Provide easy readability due to use of square brackets

 Easy-to-learn for beginners

 Having the built-in data types saves programming time and effort from declaring variables

 34) Mention the use of the split function in Python?


 The use of the split function in Python is that it breaks a string into shorter strings using the defined separator. It gives a list of all words present in the string. 

 35) Is python the right choice for Web based Programming?


 Python is another open source programming that has become popular for creating web-related applications and large programs. Scripts written in Python are often very clear to read; the language is also known for its flexibility. Whether you are looking for database tools, image manipulation scripts, or something else entirely, if it is written in Python, you will find it here. 

 36) What are uses of lambda?


 It used to create small anonymous functions at run time. 
Like e.g.
 def fun1(x):
                  
 return x**2
 
                   print fun1(2)
 
it gives you answer 4
 
the same thing can be done using
 
sq=lambda x: x**2
 print sq(2)
 it gives the answer 4 

 37) When you need ordered container of things, which will be manipulated, use lists.


 Dictionary is key, value pair container and hence is not ordered. Use it when you need fast access to elements, not in ordered fashion. Lists are indexed and index of the list cannot be “string” e.g. list [‘myelement’] is not a valid statement in python. 

 38) When do you use list vs. tuple vs. dictionary vs. set?


 List and Tuple are both ordered containers. If you want an ordered container of constant elements use tuple as tuples are immutable objects. 

 39) Do they know a tuple/list/dict when they see it?


 Dictionaries are consisting of pair of keys and values.like {’key’:’value’}.

 book={’cprog’:’1024′,’c++’:’4512′}

 Keys are unique but values can be same. 
The main difference between list and tuple is you can change the list but you cannot change the tuple. Tuple can be used as keys in mapping where list is not. 

 40) What is used to represent Strings in Python? Is double quotes used for String representation or single quotes used for String representation in Python?

 Using Single Quotes (‘)

 You can specify strings using single quotes such as ‘Quote me on this’ . All white space i.e. spaces and tabs are preserved as-is.

 Using Double Quotes (“)

 Strings in double quotes work exactly the same way as strings in single quotes. An example is “What’s your name?”

 Using Triple Quotes (”’ or “””)

 You can specify multi-line strings using triple quotes. You can use single quotes and double quotes freely within the triple quotes. An example is

 ”’This is a multi-line string. This is the first line.

 This is the second line.
 “What’s your name?,” 
I asked.
 He said “Bond, James Bond.” 

 41) Who created the Python programming language?


 Python programming language was created by Guido van Rossum.

 42) Why cannot lambda forms in Python contain statements?


 A lambda statement is used to create new function objects and then return them at runtime that is why lambda forms in Python did not contain statement. 

 43) How is memory managed in Python?


 Memory is managed through private heaps. Private heap is managed by python memory manager. 

 44) Which of the languages does Python resemble in its class syntax?


 C++ is the appropriate language that Python resemble in its class syntax. 


 45) Why was the language called as Python?


 At the same time he began implementing Python, Guido van Rossum was also reading the published scripts from “Monty Python’s Flying Circus” (a BBC comedy series from the seventies, in the unlikely case you didn’t know). It occurred to him that he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python.

 46) Does Python support strongly for regular expressions? What are the other languages that support strongly for regular expressions?


 Yes, python strongly support regular expression. Other languages supporting regular expressions are: Delphi, Java, Java script, .NET, Perl, Php, Posix, python, Ruby, Tcl, Visual Basic, XML schema, VB script, Visual Basic 6. 


 47) Why is not all memory freed when Python exits?


 Objects referenced from the global namespaces of Python modules are not always de-allocated when Python exits. This may happen if there are circular references. There are also certain bits of memory that are allocated by the C library that are impossible to free (e.g. a tool like the one Purify will complain about these). Python is, however, aggressive about cleaning up memory on exit and does try to destroy every single object. If you want to force Python to delete certain things on de- allocation, you can use the at exit module to register one or more exit functions to handle those deletions. 

 48) What are the disadvantages of the Python programming language?


 One of the disadvantages of the Python programming language is it is not suited for fast and memory intensive tasks. 

 49) What is the language from which Python has got its features or derived its features?


 Most of the object oriented programming languages to name a few are C++, CLISP and Java is the language from which Python has got its features or derived its features. 

 50) Does python support switch or case statement in Python? If not what is the reason for the same?


 No. You can use multiple if-else, as there is no need for this.

51) If you have module imported in your code, still your program fails with error - "Imported module not found" ?


 Please check your python path if it's pointing to the correct path and set the path as below

EXPORT PYTHONPATH='<location to imported python path>'

Please like, comment and share if you find this useful.

1 comment:

  1. Casinos in NJ 2021: Top 10 casinos for slots & roulette
    › casinos-in-nj-us- 안성 출장마사지 › casinos-in-nj-us- 화성 출장샵 The Top 10 상주 출장샵 NJ Online 문경 출장안마 Casinos ✓ List of New Jersey Casinos in 2021. We have the top 10 casino sites in NJ. See 과천 출장샵 our bonus offers and best no deposit bonuses!

    ReplyDelete

Tricky Python Interview Questions and Answers Code - (Level 1)

1. Compress the String as below using Python code  :  Sample Input - 1222311 Sample Output - (1, 1) (3, 2) (1, 3) (2, 1) Explanation - First...