Python Interview Questions 1
Python Interview Questions 1
Answer :
Python is an interpreted, interactive, object-oriented programming language. It
incorporates modules, exceptions, dynamic typing, very high level dynamic data
types, and classes. Python combines remarkable power with very clear syntax. It
has interfaces to many system calls and libraries, as well as to various window
systems, and is extensible in C or C++. It is also usable as an extension language
for applications that need a programmable interface. Finally, Python is portable: it
runs on many Unix variants, on the Mac, and on PCs under MS-DOS, Windows,
Windows NT, and OS/2.
2. Question 2. Is There A Tool To Help Find Bugs Or Perform Static Analysis?
Answer :
Yes.
PyChecker is a static analysis tool that finds bugs in Python source code and
warns about code complexity and style.
Pylint is another tool that checks if a module satisfies a coding standard, and also
makes it possible to write plug-ins to add a custom feature.
Perl Scripting Interview Questions
3. Question 3. What Are The Rules For Local And Global Variables In Python?
Answer :
In Python, variables that are only referenced inside a function are implicitly global.
If a variable is assigned a new value anywhere within the function's body, it's
assumed to be a local. If a variable is ever assigned a new value inside the
function, the variable is implicitly local, and you need to explicitly declare it as
'global'.
Though a bit surprising at first, a moment's consideration explains this. On one
hand, requiring global for assigned variables provides a bar against unintended
side-effects. On the other hand, if global was required for all global references,
you'd be using global all the time. You'd have to declare as global every reference
to a builtin function or to a component of an imported module. This clutter would
defeat the usefulness of the global declaration for identifying side-effects.
4. Question 4. How Do I Share Global Variables Across Modules?
Answer :
The canonical way to share information across modules within a single program
is to create a special module (often called config or cfg). Just import the config
module in all modules of your application; the module then becomes available as
a global name. Because there is only one instance of each module, any changes
made to the module object get reflected everywhere. For example:
config.py:
x = 0 # Default value of the 'x' configuration setting
mod.py:
import config
config.x = 1
main.py:
import config
import mod
print config.x
Perl Scripting Tutorial
5. Question 5. How Do I Copy An Object In Python?
Answer :
In general, try copy.copy() or copy.deepcopy() for the general case. Not all objects
can be copied, but most can.
Some objects can be copied more easily. Dictionaries have a copy() method:
newdict = olddict.copy()
Sequences can be copied by slicing:
new_l = l[:]
C++ Interview Questions
6. Question 6. How Can I Find The Methods Or Attributes Of An Object?
Answer :
For an instance x of a user-defined class, dir(x) returns an alphabetized list of the
names containing the instance attributes and methods and attributes defined by
its class.
7. Question 7. Is There An Equivalent Of C's "?:" Ternary Operator?
Answer :
No
C Tutorial
14. Question 14. How Do I Call A Method Defined In A Base Class From A
Derived Class That Overrides It?
Answer :
If you're using new-style classes, use the built-in super() function:
class Derived(Base):
def meth (self):
super(Derived, self).meth()
If you're using classic classes: For a class definition such as class
Derived(Base): ... you can call method meth() defined in Base (or one of Base's
base classes) as Base.meth(self, arguments...). Here, Base.meth is an unbound
method, so you need to provide the self argument.
15. Question 15. How Do I Find The Current Module Name?
Answer :
A module can find out its own module name by looking at the predefined global
variable __name__. If this has the value '__main__', the program is running as a
script. Many modules that are usually used by importing them also provide a
command-line interface or a self-test, and only execute this code after checking
__name__:
def main():
print 'Running test...'
...
if __name__ == '__main__':
main()
__import__('x.y.z') returns
Try:
__import__('x.y.z').y.z
For more realistic situations, you may have to do something like
m = __import__(s)
for i in s.split(".")[1:]:
m = getattr(m, i)
Ruby Interview Questions
16. Question 16. Where Is The Math.py (socket.py, Regex.py, Etc.) Source File?
Answer :
There are (at least) three kinds of modules in Python:
1. modules written in Python (.py);
2. modules written in C and dynamically loaded (.dll, .pyd, .so, .sl, etc);
3. modules written in C and linked with the interpreter; to get a list of these, type:
import sys
print sys.builtin_module_names
Ruby on Rails Tutorial
17. Question 17. How Do I Delete A File?
Answer :
Use os.remove(filename) or os.unlink(filename);
Django Interview Questions
18. Question 18. How Do I Copy A File?
Answer :
The shutil module contains a copyfile() function.
C Interview Questions
28. Question 28. How Do I Make Python Scripts Executable?
Answer :
On Windows 2000, the standard Python installer already associates the .py
extension with a file type (Python.File) and gives that file type an open command
that runs the interpreter (D:Program FilesPythonpython.exe "%1" %*). This is
enough to make scripts executable from the command prompt as 'foo.py'. If you'd
rather be able to execute the script by simple typing 'foo' with no extension you
need to add .py to the PATHEXT environment variable.
On Windows NT, the steps taken by the installer as described above allow you to
run a script with 'foo.py', but a longtime bug in the NT command processor
prevents you from redirecting the input or output of any script executed in this
way. This is often important.
The incantation for making a Python script executable under WinNT is to give the
file an extension of .cmd and add the following as the first line:
@setlocal enableextensions & python -x %~f0 %* & goto :EOF
wxPython Tutorial
32. Question 32. How Do I Emulate Os.kill() In Windows?
Answer :
Use win32api:
def kill(pid):
"""kill function for Win32"""
import win32api
handle = win32api.OpenProcess(1, 0, pid)
return (0 != win32api.TerminateProcess(handle, 0))
33. Question 33. Explain About The Programming Language Python?
Answer :
Python is a very easy language and can be learnt very easily than other
programming languages. It is a dynamic object oriented language which can be
easily used for software development. It supports many other programming
languages and has extensive library support for many other languages.
These lists performance is slower Tuple performance is faster when compared to lists
Syntax: list_1 = [20, ‘Mindmajix’, 30] Syntax: tup_1 = (20, ‘Mindmajix’, 30)
Dynamic In Java, we need to declare the In this case, codes are dynamica
type for each variable typed and this is also known as
duck typing
Easy to use Java is not easy to use because of In Python, it is very easy to code
its larger coding and perform very easily.
It is used for performing general and efficient This is an entire collection of tools in Python mainl
computations on numerical data which is saved in used to perform operations like differentiation,
arrays. For example indexing, reshaping, sorting, and integration and many more.
so on
There are some of the linear algebraic functions For performing algebraic computations this module
present in this module but they are not fully fledged. contain some of the fully fledged operations
60Q. How do Python arrays and lists differ from each other?
Ans: The difference between Python array and Python list are as follows:
Arrays Lists
Array is defined as a linear structure that is used to List are used to store arbitrary and heterogenous da
store only homogeneous data.
Since array stores only similar type of data so it List stores different types of data so it requires huge
occupies less amount of memory when compared to amount of memory
list.
Length of the array is fixed at the time of designing Length of the list is no fixed, and adding items in th
and no more elements can be added in the middle. middle is possible in lists.
List is returned by this range() method It only returns the generator object because it doesn
produce a static list during run time.
It occupies a huge amount of memory as it stores the It occupies less memory because it only stores one
complete list of iterating numbers in memory. number at the time in memory.
Ans: The following are the steps used to minimize the outages of the Memcached
server in your Python development, and they are.
When a single instance fails, this will impact on larger load of the database server. The client
makes the request when the data is reloaded. In order to avoid this, the code that you have
written must be used to lower cache stampedes then it will used to leave a minimal impact.
The other way is to bring out the instance of the memcached on a new machine by using the
IP address of the lost machine.
Another important option is to lower the server outages is code. This code provides you the
liberty to modify the memcached server list with minimal work
Another way is by setting timeout value that will be one of the options for memcac
1 Class Student:
2 def __init__(self, name):
3 self.name = name
4 S1=Student("XYZ")
print(S1.name)
5
hed clients to implement the memcached server outage. When the performance of the server
goes down, the client keeps on sending a request until the timeout limit is reached.