Python questions for retrospection
Sep 5, 2018 · 7 min read
Are you a python newbie or preparing for interview ?? Try answering these questions
Note: Dont just limit yourself to answering these question explore the hidden pythons :)
- what is the difference between getattribute and getattr
- What is GIL?
- what are partial functions?
- What are abstract base class / ABC?
- What is the use of collections.counter?
- what is deque?
- what is defaultdict?
- Have you used namedtuple ?
- what is the use of contains magic method?
- Can you write a class which behaves as iterator?
- What is deep copy n shallow copy?
- When will you use dict of an object?
- What is the output of [‘one’, ‘two’, ‘three’][True] ?
- set([1,2]) == set([2,1]) whats d output?
- whats the difference between iterator and iterable?
- x = [[]];x = x\*5;x[0].append(2) Whats d output ?
- what are discriptors ?
- what is the use of set and get ?
- what is the builtin function to get an atribute safely ?
- how do you safely access a value frm dictionary which may or may not present in dict ?
- how do you merge two list ?
- how do you merge two dicts ?
- what are slots ?
- what is the use of getitem and setitem
- what is this @some_function_name is generally called?
- what are generators?
- what is the use of global keyword ?
- what is the difference between global and globals?
- how to access doc string of an object ?
- what is the use of repr?
- how to reverse a string ?
- what is hashing ? and which is the data structure which uses hasing ?
- what should be the property of a key of dictionary?
- Can tuple be a key of dictionary?
- Can (1,2, [1]) be a key of dictionary ?
- what is the output? and how ? what are the addresses of x and y? x=’hi’;y=’hi’ if x is y:print ‘yeyyy’
- what is the difference between == and ‘is’ ?
- what are new style classes ?
- what are metaclasses and what is the use of it ?
- what is the use of with statement?
- How do you write a class which supports with statement?
- what is init.py used for?
- How to create a module in python?
- What is the value of -4 ** 2 ? (it’s a bug)
- What happens when True = False ?
- What is new ?
- What is type ?
- Who is the mother of all classes ?
- What is the type(object)?
- How do you create generator without using yield keyword?
- Can you do dictionary comprehension?
- How garbage collection works in python?
- How to sort list? without using list.sort()
- How do you see all the local variables and values of a scope?
- How list comprehensions (and know when not to abuse them?)
- How to upack tuple?
- Diference between range and xrange ?
- What is the use of builtin function enumerate ?
- What is MRO ?
- What is constructor chaining? Or how do you call parent class init frm child class?
- String building. Do they use “+=” or do they build a list and use .join() to recombine them efficiently?
- Truth-value testing questions and observations (do they write “if x == True” or do they just write “if x”)?
- Basic file-processing (iterating over a file’s lines)
- Basic understanding of exception handling
- Questions about the standard library (”do you know if there’s a standard library for doing X?”, or “in which
- library would you find [common functionality Y]?”) Most of these are related to the more common
- libraries such as os/os.path/sys/re/itertools
- Questions about iterators/generators
- Questions about map/reduce/sum/etc family of functions
- Questions about “special” methods (foo)
- Can they manipulate functions as first-class objects (Python makes it easy, but do they know how)
- More detailed questions about the std. libraries (such as datetime/email/csv/zipfile/networking/
- optparse/unittest)
- Questions about testing (unittests/doctests)
- What python duck typing?
- Questions about docstrings vs. comments, and the “Why” of them
- More detailed questions about regular expressions
- Questions about mutability
- Keyword/list parameters and unpacked keyword arguments
- Questions about popular 3rd-party toolkits (BeautifulSoup, pyparsing…mostly if they know about them
- and when to use them, not so much about implementation details)
- Questions about monkey-patching
- Questions about PDB
- Questions about properties vs. getters/setters
- Questions about classmethods
- Questions about scope/name-resolution
- Use of lambda ?
- Decorators added in which version?
- SQL-capable DB in which version?
- The difference between “class Foo” and “class Foo(object)”
- Questions frm “import this” about pythonic code
- About various Python web frameworks (knowing a few names is usually good enough, though knowledge about the frameworks is a nice plus) such as Django, TurboGears, Zope, etc.
- Python GUI frameworks and the pros/cons of them (tkinter, wx, pykde, etc)
- What is used to create Unicode string in Python?
- Describe how to send mail frm a Python script.
- Explain how to overload constructors (or methods) in Python.
- What is a negative index in python?
- How can I find the methods or attributes of an object in python?
- Explain how to copy an object in Python.
- What is the use of virtual env.
- How is memory managed in python?
- Explain pickling and unpickling.
- How can we pass optional or keyword parameters frm one function to another in Python?
- Explain indexing and slicing operation in sequences.
- How do I interface to C++ objects frm Python?
- How do I catch the output frm PyErr_Print() (or anything that prints to stdout/stderr)?
- How do I read (or write) binary data?
- questions about os module, file rename, file delete, folder operations, execute command on console. etc..
- Questions about threads, processes and subprocess.
- difference between pipe and queue?
- Modules used for unittesting python code.
- When I edit an imported module and reimport it, the changes dont show up. Why does this happen?
- How do I find the current module name?
- How do I call a method defined in a base class frm a derived class that overrides it?
- What is self?
- How do I check if an object is an instance of a given class or of a subclass of it?
- How do you set a global variable in a function?
- what is tuple unpacking?
- Explain different ways of string concatination, and best solution to concatinate string.
- How do you write your own exception?
- what are iterators?
- What are generators? and write class based and function based generators.
- Explain map/reduce/sum/filter family of functions.
- what are magic methods ?
- what are “special” methods (____)
- what are first-class objects
- Not a question, but get to know some std. libraries (such as datetime/email/csv/zipfile/networking/optparse/unittest/pytz/smtp/unittest/mysqldb/pyssh)
- what is docstring testing ?
- Questions about regular expressions.
- Questions about mutability/immutablity.
- what are args and kwargs ? how do u use them ?
- questions about popular 3rd-party toolkits (BeautifulSoup, urllib, minidom, sax pyparsing…mostly if they know about them and when to use them, not so much about implementation details)
- difference between sax dom parser and dom parser.
- what is monkey-patching?
- Difference between classmethods and static methods?
- what is the difference between “class Foo” and “class Foo(object)”
- Questions frm “import this” about pythonic code
- what is pep8 ?
- How do you sort dictionary by value.
- Find the missing number in list of sequence, numbers eg. [1,2,3…..100]
- How to use map function when you need to add two lists value
- Find the largest integer in List.:
- Print odd numbers in List.
- Networking programming with python.
- Division and Multiplication without using default operators.
- Swap two variables.
- Remove duplicate variables in list.
- Find middle element of the linked list in one pass.
- Explain difference between DOM and SAX parsers.
- Reverse a String in Python
- Find sub string inside a string
- Create a dictionary which capitalizes the string values before it actually saves.
- Write a class which works with ‘with’.
- What order is a hash table lookup?
- Find the largest integer in Python.
- What should be the property of key in dectionary.
- Can a tuple be the key of dictionary ?
- Can (‘a’, [1,2]) be the key of a dictionary ?
- Write a python program that will take the following list of words as input and output a dictionary with a the frequency of each word
- x =[‘apple’, ‘nokia’, ‘htc’, ‘apple’, ‘samsung’, ‘nokia’, ‘LG’, ‘apple’]
- Given an array A[N] containing N numbers. Crate an array Output[N] where Output[i] is equal to the product of all the elements of A[N] except A[i]. For example Output[0] is the product of A[1] to A[N-1] and Output[1] is the product of A[0] and frm A[2] to A[N-1].
- Do this without using the division operator. Do it in O(n).
- soln: Get product of list using reduce(lambda a,b:a\*b), divide product using left shift.
- How are arguments passed — by reference or by value?
- Abstract classes in python.
- What is the use of Virtual environment.
- Difference between python 2 and python 3.
- What is Big O notation ?
- What is the use of future module?
- what is MRO?
- How garbage collection happens in python?
- Do you know about GIL?
- Whats wrong with this def hello(x=10, y):pass ?
- What is pass ?
- How to find the frequency of repeated items in list ?
- In try and finally if return statement exected inside try, will code inside finally executes ?
- Write a metaclass to convert all the methods name to uppercase.
- write a class which automatically converts its methods name to uppercase, without using metaclasses.
- what are the uses of getstate and setstate methods?
- Implement ordered dict.
- What are the advantages of immutable datatypes ?
- Difference between import and import?
- Can you do x++ ?
- What are the advantages of new style classes?
- How MRO computed ?
- What is the advantages of super in inheritance ?
- How do you print “123456789” to “97531” using slicing ?
- What is extended tuple unpacking?
- Does python have notations ? like java notations ?
- Does python supports weak references ? if so how do you do?
- what is the sorting algo used in sorted or list.sort ?
- Threading and multiprocessing modules/ difference between mutex lock/ semaphore/ pipe/ queue.
- Difference between descriptor and property.
- What is the simple way to get combinations of [‘apple’, ‘pear’, ‘orange’] ?
- how do you group rows by given value ?
- How python list and arrays are different ?
- What are closures in python?
- Can Python really hold private variables? (learn about name mangling ?)
- What are multimethods?
- What do you think the output is
- print “word” in [] == False
- How about this ?
units = [1, 2] tens = [10, 20] nums = (a + b for a in units for b in tens) units = [3, 4] tens = [30, 40] print nums.next()
units = [1, 2]
tens = [10, 20]
nums = (a + b for a in units for b in tens)
units = [3, 4]
tens = [30, 40]
print nums.next()- Do you know about prepare method ?
- Is it possible to list all functions in a module(only functions) ?
- Get actuall class object, given class name(string).
- How are list,tuple,dict implemented in python?
