Python Interview Questions part-1
https://www.computersprofessor.com/2017/08/python-interview-questions-part-1.html
1.What is python?
Python is an open source language that is getting a lot of attention from the market.
1) Created by: Guido van Rossum nearly 11 years ago
2) Python is an interpreted,high-level programming language,pure object-oriented and powerful server-side scripting language for the Web.
3) Python is a good prototype language.In just a few minutes,you can develop prototypes that would take you several hours in other languages.
2.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.
3.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
*
*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
4.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.
e.g.list [‘myelement’] is not a valid statement in python.
5.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.
6.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′}
{‘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.
7.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.”
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.”
8.Who created the Python programming language?
Python programming language was created by Guido van Rossum.
9.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.
10.How is memory managed in Python?
Memory is managed through private heaps.Private heap is managed by python memory manager.
11.Which of the languages does Python resemble in its class syntax?
C++ is the appropriate language that Python resemble in its class syntax.
12.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.
13.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.
6.
14.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.
15.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.
16.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.
17.What is the Java implementation of Python popularly known as?
Jython
18.What is the method does join() in python belong?
String method
19.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
20.How is the Implementation of Pythons dictionaries done?
Using curly brackets -> {}
E.g.: {‘a’:’123′,’b’:’456′}
E.g.: {‘a’:’123′,’b’:’456′}
21.What is the statement that can be used in Python if a statement is required syntactically but the program requires no action?
Pass is a no-operation/action statement in python.
If we want to load a module and if it does not exist,let us not bother,let us try to do other task.The following example demonstrates that.Try:
Import module1
Except:
Pass
If we want to load a module and if it does not exist,let us not bother,let us try to do other task.The following example demonstrates that.Try:
Import module1
Except:
Pass
22.What are the uses of List Comprehensions feature of Python?
List comprehensions help to create and manage lists in a simpler and clearer way than using map(),filter() and lambda.Each list comprehension consists of an expression followed by a clause,then zero or more for or if clauses.
23.What is used to create Unicode string in Python?
Add u before the string >>> u ‘test’
24.What is the optional statement used in a try except statement in Python?
There are two optional clauses used in try except statements:
1.Else clause:
It is useful for code that must be executed when the try block does not create any exception
2.Finally clause:
It is useful for code that must be executed irrespective of whether an exception is generated or not.