Download as xlsx, pdf, or txt
Download as xlsx, pdf, or txt
You are on page 1of 64

S No QUESTIONS

Q1 What does an x = y or z assignment do in Python?

What does the Python nonlocal statement do (in Python


Q2 3.0 and later)?
Q3 What is the python with statement designed for?

Q4 Explain Closures (as they relate to Python)?

How do I write a function with output parameters (call by


Q5 reference)

Is it a good idea to use multi-thread to speed your Python


Q6 What are the advantages code?
of NumPy over regular Python
Q7 lists?
Q8 What
What is Monkey
is the purposePatching? How
of the single to use it in_ Python?
underscore variable in
Q9 Python?
What are generator functions? Write your own version of
Q10 range
Q11 What is pickling and unpickling in Python?
Q12 What are *args and **kwargs in Python functions?
Q13 If a function doesn’t have a return statement, is it valid?
Q14 When would you use triple quotes as a delimiter?
Q15 Define
Write Python code to self
checkin the
Python?
given sequence is a
Q16 palindrome or not?
How to generate custom sequences in numpy without
Q17 hardcoding?
How to get the common items between two python numpy
Q18 arrays?

How Do I Make 3d Plots/visualizations Using


Q19 Numpy/scipy?
List the advantages NumPy Arrays have over (nested)
Q20 Python lists?

How can you identify the datatype of a given


Q21 NumPy array?

Q22 What is Scope Resolution in Python?

Q23 What are Dict and List comprehensions?

Q24 How do you copy an object in Python?


What is the difference between xrange and range in
Q25 Python?

Q26 What is the use of help() and dir() functions?

Q27 What is the difference between .py and .pyc files?

Q28 What do you understand by reindexing in pandas?

While importing data from different sources, can the


Q29 pandas library recognize dates?

Q30 How will you efficiently load data from a text file?

Q31 Differentiate between deep and shallow copies.


Q32 What is main function in python? How do you invoke it?

How will you access the dataset of a publicly shared


Q33 spreadsheet in CSV format stored in Google Drive?

Write a Program to add two integers >0 without using the


Q34 plus operator.

Write a program for counting the number of every


Q35 character of a given text file.

Q36 How is memory managed in Python?

How are compile-time and run-time code checking done


Q37 in Python?

Q38 How are instance variables different from class variables?


How to find undefined g++ symbols __builtin_new or
Q39 __pure_virtual?

What is the function to randomize the items of a list in-


Q40 place?

Q41 When would you use a break statement in a for loop?

Q42 Illustrate the proper use of Python error handling.

Q43 Do runtime errors exist in Python? Give an example.

What are the different stages of the life cycle of a


Q44 thread?
Python supports negative indexes. What are they
Q45 and why are they used?

Q46 What is the map() function used for?

What are python modules? Name some commonly


Q47 used built-in modules in Python?

Q48 What is self in Python?

How can you randomize the items of a list in place in


Q49 Python?

What is the usage of help() and dir() function in


Q50 Python?

Q51
Q52

Q53

Q54

Q55

Q56

Q57

Q58

Q59

Q60

Q61

Q62

Q63

Q64

Q65

Q66

Q67

Q68

Q69

Q70

Q71

Q72

Q73

Q74

Q75

Q76

Q77

Q78

Q79

Q80

Q81

Q82

Q83

Q84

Q85
Q86

Q87

Q88

Q89

Q90

Q91

Q92

Q93

Q94

Q95

Q96

Q97

Q98

Q99

Q100

Q101

Q102

Q103

Q104

Q105

Q106
Q107
Q108
Q109
Q110
Q111
Q112
Q113
Q114
Q115
Q116
Q117
Q118

Q119
Q120

Q121
Q122

Q123
Q124
Q125

Q126
Q127

Q128

Q129
Q130
Q131
Q132
Q133

Q134
Q135

Q136
Q137
Q138
Q139

Q140
Q141

Q142
Q143
Q144
Q145
Q146
Q147
Q148
Q149
Q150
Q151
Q152
Q153
Q154
Q155
Q156
Q157
Q158
Q159
Q160
Q161
Q162
Q163
Q164
Q165
Q166
Q167
Q168
Q169
Q170
Q171
Q172

Q173

Q174

Q175

Q176

Q177

Q178

Q179

Q180

Q181
Q182
Q183
Q184
Q185
Q186
Q187
Q188
Q189
Q190
Q191
Q192
Q193
Q194
Q195
Q196
Q197
Q198
Q199
Q200
Q201
Q202
Q203
Q204
Q205
Q206
Q207
Q208
Q209
Q210
Q211
Q212
Q213
Q214
Q215
Q216
Q217
Q218
Q219
Q220
Q221
Q222
Q223
Q224
Q225
Q226
Q227
Q228
Q229
Q230
Q231
Q232
Q233
Q234
Q235
Q236
Q237
Q238
Q239
Q240
Q241
Q242
Q243
Q244
Q245
Q246
Q247
Q248
Q249
Q250
Q251
Q252
Q253
Q254
Q255
Q256
Q257
Q258
Q259
Q260
Q261
Q262
Q263
Q264
Q265
Q266
Q267
Q268
Q269
Q270
Q271
Q272
Q273
Q274
Q275
Q276
Q277
Q278
Q279
Q280
Q281
Q282
Q283
Q284
Q285
Q286
Q287
Q288
Q289
Q290
Q291
Q292
Q293
Q294
Q295
Q296
Q297
Q298
Q299
Q300
Q301
Q302
Q303
Q304
Q305
Q306
Q307
Q308
Q309
Q310
Q311
Q312
Q313
Q314
Q315
Q316
Q317
Q318
Q319
Q320
Q321
Q322
Q323
Q324
Q325
Q326
Q327
Q328
Q329
Q330
Q331
Q332
Q333
Q334
Q335
Q336
Q337
Q338
Q339
Q340
Q341
Q342
Q343
Q344
Q345
Q346
Q347
Q348
Q349
Q350
Q351
Q352
Q353
Q354
Q355
Q356
Q357
Q358
Q359
Q360
Q361
Q362
Q363
Q364
Q365
Q366
Q367
Q368
Q369
Q370
Q371
Q372
Q373
Q374

Q375

Q376

Q377

Q378

Q379

Q380

Q381

Q382

Q383

Q384

Q385

Q386

Q387

Q388

Q389

Q390

Q391

Q392

Q393

Q394

Q395

Q396

Q397

Q398

Q399

Q400

Q401

Q402

Q403

Q404

Q405

Q406
Q407

Q408

Q409

Q410

Q411

Q412

Q413

Q414

Q415

Q416

Q417

Q418

Q419
Type of Question
Coding

Coding
Syntax

Syntax

Coding

Theory
Theory
Theory
Coding
Coding
Theory
Theory
Syntax
Theory
Theory
Coding
Coding

Coding

Theory
Theory

Coding

Theory

Theory

Coding
Theory

Theory

Syntax

Theory

Theory

Theory

Theory
Syntax

Coding

Coding

Coding

Theory

Theory

Theory
Coding

Theory

Theory

Theory

Theory

Theory
Syntax

Syntax

Theory

Theory

Coding

Syntax
Difficulty Level
Advanced

Advanced
Advanced

Advanced

Advanced

Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced

Advanced

Advanced
Advanced

Advanced

Advanced

Advanced

Advanced
Advanced

Advanced

Advanced

Advanced

Advanced

Advanced

Advanced
Advanced

Advanced

Advanced

Advanced

Advanced

Advanced

Advanced
Advanced

Advanced

Advanced

Advanced

Advanced

Advanced
Advanced

Advanced

Advanced

Advanced

Advanced

Advanced
Answer
The with statement simplifies exception handling by encapsulating common
preparation and cleanup
If bool(a) returns tasks
False, then in assigned
x is so-called the
context
valuemanagers.
of b.
In
Forshort, it letsthe
instance, youopen
assign values to
statement is aa variable in an outer
context manager in (but
itself,non-global)
which lets
scope.
you open a file, keep it open as long as the execution is in the context of the
The
with nonlocal
statementstatement
where youcauses theand
used it, listed identifiers
close to refer
it as soon as youto leave
previously
the
bound variables in the nearest enclosing scope excluding globals.
context, no matter whether you have left it because of an exception or during
regular control flow.
Objects are data with methods attached, closures are functions with data
attached. The method of binding data to a function without actually passing
them as parameters is called closure.

In Python arguments are passed by assignment. When you call a function


with a parameter, a new reference is created that refers to the object passed
in. This is separate from the reference that was used in the function call, so
there's no way to update that reference and make it refer to a new object.

If you pass a mutable object into a method, the method gets a reference to
that same object and you can mutate it to your heart's delight, but if you
rebind the reference in the method (like b = b + 1), the outer scope will know
nothing about it, and after you're done, the outer reference will still point at
the original object.
NumPy's
So to achieve arrays theare more compact
desired effect your thanbest Python
choicelists is to- areturn
list ofalists tupleas you
describe, in Python,
containing the multiple results: would take at least 20 MB or so, while a NumPy 3D
array with single-precision floats in the cells would fit in 4 MB. Access to
reading and writing items is also faster with NumPy.
has 4 main conventional uses in Python:
Python
The differencedoesn't is allow
mostlymulti-threading
due to "indirectness"in the truest sense of
- a Python listtheis anword.arrayIt has
of a
multi-threading
To hold the
pointers package
result ofobjects,
to Python but if
the lastatexecutedyou want
least 4 bytes to multi-thread
expression(/statement) to
per pointer plus 16 in speed your code
an for even
bytes
up,
the then
interactive
smallest it'sinterpreter
usually
Python not a good
session.
object (4 for idea
This
type topointer,
use it. 4was
precedent for set by the count,
reference standard 4 for
CPython
value - and interpreter,
the memory andallocators
other interpreters
rounds uphaveto followed
16). A NumPy suit array is an
A MonkeyPatch
Python is a piece of Python thecode thatdocumentation
extends or (GIL).
modifies other
For
array
code ofhas
translation
at uniform
runtime
a construct
lookup
valuesin--
(typically
called
i18n the Global
(see
single-precision
at startup).
Interpreter
gettext
numbers Lock
take 4 bytes forThe GIL
example),
each,
makes
as sure
in code like: raise
double-precision that only
ones, one of your 'threads'
forms.ValidationError(_("Please
8 bytes. Lesson flexible,can execute
but you at
enter any one
a correct for
pay substantially time. A
It is
thread often
username")) used
acquires to replace
the GIL, a
does method
a little the
work, module
then or
passes class
the level
GIL with
onto a
the
the
customflexibility of standard Python lists.
implementation.
nexta thread.
As general This
purpose happens very quickly
"throwaway" so toname
variable the human eye itthat
to indicate maypart seemof alike
your
function
NumPy threadsresult
is common areis
not just moreexecuting
being in
deliberately
efficient; parallel, but
ignored
it is also they are really
(Conceptually,
more convenient. just
it taking
is
You being
get turns
a lot ofor
The
using most
the
discarded.), same as in CPU usecase
code core.
like: Allis this
label,adding apassing
GILwhich
has_label,workaround
_= adds for a bug
overhead
text.partition(':'). intoa module
execution.
vector
Generator
class and
when matrix
functions
you operations
are
can'tdefinition
replace the for
functions free,
that
original can sometimes
suspend
code. In this their allows
execution
case where one
you replace to avoid
after the
As part
unnecessary
returningcode of a function
a value,work. And
in order they are(using
to resume also either def
efficiently
it atwith
some or lambda),
implemented.
later time and return the another
"wrong"
signature through monkey patching an implementation inside your
value.
own Thisisisfixed
module/package. made (e.g. by a callback
possible by the yield or parent
keyword, classwhichAPI), you but thisuse particular
in place of
function
return. implementation
The most common doesn't needfunction
all of the parameters, as in code like:
Pickling
callback is
= just
lambda the Python
_: True way of saying serializing. Pickling lets youis the
generator you have worked with
range.
serialize Here is one into
an object wayaofstringimplementing
(or anything it (only
elseworks with positive
you choose) in order step,
to beI
will leave it as an exercise to make one
persisted on storage or sent over network. Unpickling is the process of that supports negative steps):
These
restoring deal theclosely
originalwith unpacking.
object from a pickledIf you put  *args in function’s parameter list, all
string.
unnamed
Yes, it isarguments
valid. By will be stored
default, allinthe
the args array. **kwargs works
functions in Python the same
return way, but for
the particular
Triple quotes ‘’” or ‘“ are string delimiters that can span
named parameters: multiple lines in
value
Python. Triple quotes are usually used when spanning multiple lines,aor
“None” unless an explicit return statement is present to return value
that’s not “None”.
enclosing a string that has a mix of single and double quotes contained
therein.

Like 2D plotting, 3D graphics is beyond the scope of NumPy and SciPy, but
just as in the 2D case, packages exist that integrate with NumPy. Matplotlib
provides basic 3D plotting in the mplot3d subpackage, whereas Mayavi
provides a wide range of high-quality 3D visualization features, utilizing the
powerful VTK engine.
Python’s lists, even though hugely efficient containers capable of a number
of functions, have several limitations when compared to NumPy arrays. It is
not possible to perform vectorised operations which includes element-wise
addition and multiplication.

They also require that Python store the type information of every element
since they support objects of different types. This means a type dispatching
code must be executed each time an operation on an element is done. Also,
each iteration would have to undergo type checks and require Python API
bookkeeping resulting in very few operations being carried by C loops.

Use the following sequence of codes to identify the datatype of a NumPy


array.

print(‘\n data type num 1 ‘,num.dtype)

print(‘\n data type num 2 ‘,num2.dtype)

print(‘\n data type num 3 ‘,num3.dtype)

Sometimes objects within the same scope have the same name but function
differently. In such cases, scope resolution comes into play in Python
automatically. A few examples of such behavior are:

Python modules namely 'math' and 'cmath' have a lot of functions that are
common to both of them - log10(), acos(), exp() etc. To resolve this
ambiguity, it is necessary to prefix them with their respective module, like
math.exp() and cmath.exp().
Consider the code below, an object temp has been initialized to 10 globally
and then to 20 on function call. However, the function call didn't change the
value of the temp globally. Here, we can observe that Python draws a clear
line between global and local variables, treating their namespaces as
separate identities.

Python comprehensions, like decorators, are syntactic sugar constructs that


help build altered and filtered lists, dictionaries, or sets from a given list,
dictionary, or set. Using comprehensions saves a lot of time and code that
might be considerably more verbose (containing more lines of code). Let's
check out some examples, where comprehensions can be truly beneficial:

In Python, the assignment statement (= operator) does not copy objects.


Instead, it creates a binding between the existing object and the target
variable name. To create copies of an object in Python, we need to use the
copy module. Moreover, there are two ways of creating copies for the given
object using the copy module -

Shallow Copy is a bit-wise copy of an object. The copied object created has
an exact copy of the values in the original object. If either of the values is a
reference to other objects, just the reference addresses for the same are
copied.
Deep Copy copies all values recursively from source to target object, i.e. it
even duplicates the objects referenced by the source object.
xrange() and range() are quite similar in terms of functionality. They both
generate a sequence of integers, with the only difference that range() returns
a Python list, whereas, xrange() returns an xrange object.

So how does that make a difference? It sure does, because unlike range(),
xrange() doesn't generate a static list, it creates the value on the go. This
technique is commonly used with an object-type generator and has been
termed as "yielding".

Yielding is crucial in applications where memory is a constraint. Creating a


static list as in range() can lead to a Memory Error in such conditions, while,
xrange() can handle it optimally by using just enough memory for the
generator (significantly less in comparison).

help() function in Python is used to display the documentation of modules,


classes, functions, keywords, etc. If no parameter is passed to the help()
function, then an interactive help utility is launched on the console.
dir() function tries to return a valid list of attributes and methods of the object
it is called upon. It behaves differently with different objects, as it aims to
produce the most relevant data, rather than the complete information.

.py files contain the source code of a program. Whereas, .pyc file contains
the bytecode of your program. We get bytecode after compilation of .py file
(source code). .pyc files are not created for all the files that you run. It is only
created for the files that you import.
Before executing a python program python interpreter checks for the
compiled files. If the file is present, the virtual machine executes it. If not
found, it checks for .py file. If found, compiles it to .pyc file and then python
virtual machine executes it.
Having .pyc file saves you the compilation time
Reindexing is the process of conforming a dataframe to a new index with
optional filling logic. If the values are missing in the previous index, then
NaN/NA is placed in the location. A new object is returned unless a new
index is produced that is equivalent to the current one. The copy value is set
to False. This is also used for changing the index of rows and columns in the
dataframe
Yes, they can, but with some bit of help. We need to add the parse_dates
argument while we are reading data from the sources. Consider an example
where we read data from a CSV file, we may encounter different date-time
formats that are not readable by the pandas library. In this case, pandas
provide flexibility to build our custom date parser with the help of lambda
functions

We can use the method numpy.loadtxt() which can automatically read the
file’s header and footer lines and the comments if any.

This method is highly efficient and even if this method feels less efficient,
then the data should be represented in a more efficient format such as CSV
etc. Various alternatives can be considered depending on the version of
NumPy used.
Shallow copy does the task of creating new objects storing references of
original elements. This does not undergo recursion to create copies of
nested objects. It just copies the reference details of nested objects.
Deep copy creates an independent and new copy of an object and even
copies all the nested objects of the original element recursively.
the main is considered as an entry point of execution for a program. But in
python, it is known that the interpreter serially interprets the file line-by-line.
This means that python does not provide main() function explicitly. But this
doesn't mean that we cannot simulate the execution of main. This can be
done by defining user-defined main() function and by using the __name__
property of python file. This __name__ variable is a special built-in variable
that points to the name of the current module.
We can use the StringIO module from the io module to read from the Google
Drive link and then we can use the pandas library using the obtained data
source

def add_nums(num1, num2):


while num2 != 0:
data = num1 & num2
num1 = num1 ^ num2
num2 = data << 1
return num1
print(add_nums(2, 10))
import collections
import pprint
with open("sample_file.txt", 'r') as data:
count_data = collections.Counter(data.read().upper())
count_value = pprint.pformat(count_data)
print(count_value)

Memory management in Python is handled by the Python Memory Manager.


The memory allocated by the manager is in form of a private heap space
dedicated to Python. All Python objects are stored in this heap and being
private, it is inaccessible to the programmer. Though, python does provide
some core API functions to work upon the private heap space.
Additionally, Python has an in-built garbage collection to recycle the unused
memory for the private heap space.

Python has a unique way of performing compile-time and run-time code


checking. A small portion checking is carried out during compile-time
checking, but most of the checks such as type, name, etc are postponed
until code execution. If the Python code references a user-defined function
that does not exist, the code will compile successfully and the code will fail
with an exception only when the code execution path references the function
which does not exist.

Instance variables are variables that are created locally within a class to
refer to an object of the class. A class variable is one that is created globally
in a class and is accessible within all instance of that class.

Class variables are declared with keyword static and Instance variables are
declared without static keyword.

Class variables can be accessed anywhere within that class whereas an


instance variable can only be accessed within the particular object of the
class.

As class variables are common to all objects of a class, changes made to


these variables through one object will reflect in another. As each object will
have its own copy of instance variables, changes made to these variables
through one object will not reflect in another object.

Class variables can be accessed using either class name or object


reference. Instance variables can be accessed only through an object
reference.
To load g++ extension modules dynamically, we have to:

Recompile Python

Re-link it using g++ (change LINKCC in the python Modules Makefile)

Link the extension module using g++ (e.g., “g++ -shared -o mymodule.so
mymodule.o”).

Python has a built-in module called <random>. It exports a public method


<shuffle(<list>)> which can randomize any input sequence.

import random
list = [2, 18, 8, 4]
print “Prior Shuffling – 0”, list
random.shuffle(list)
print “After Shuffling – 1”, list
random.shuffle(list)
print “After Shuffling – 2”, list
The break statement states that the function of the loop is over and to move
on to the next block of code. For example, when the item being searched is
found, there is no need to keep looping. The break statement comes into
play here and the loop terminates and the execution moves on to the next
section of the code.

Code Example:

try:

….#Any code

except:

…# error handling code

finally.

…# code that will be executed regardless of exception handling goes here.


Yes, runtime errors exist in Python. For example, if you are duck typing and
things look like a duck, then it is considered a duck even if that is just a flag
or stamp. The code, in this case, would be a run-time error. For example,
Print “Hackr io” would result in the runtime error of the missing parenthesis
that is required by print ( ).

Stage 1: Creating a class where we can override the run method of the
Thread class.
Stage 2: We make a call to start() on the new thread. The thread is taken
forward for scheduling purposes.
Stage 3: Execution takes place wherein the thread starts execution, and it
reaches the running state.
Stage 4: Thread waits until the calls to methods including join() and sleep()
take place.
Stage 5: After the waiting or execution of the thread, the waiting thread is
sent for scheduling.
Stage 6: Running thread is done by executing the terminates and reaches
the dead state.
The sequences in Python are indexed. It consists of positive and negative
numbers. Positive numbers use 0 as the first index, 1 as the second index,
and so on. Hence, any index for a positive number n is n-1.

Unlike positive numbers, index numbering for the negative numbers starts
from -1 and it represents the last index in the sequence. Likewise, -2
represents the penultimate index. These are known as negative indexes.
Negative indexes are used for:

Removing any new-line spaces from the string, thus allowing the string to
except the last character, represented as S[:-1]
Showing the index to representing the string in the correct order

The map() function applies a given function to each item of an iterable. It


then returns a list of the results. The value returned from the map() function
can then be passed on to functions to the likes of the list() and set().

Typically, the given function is the first argument and the iterable is available
as the second argument to a map() function. Several tables are given if the
function takes in more than one argument.

Python modules are files containing Python code. This code can either be
functions classes or variables. A Python module is a .py file containing
executable code.

Some of the commonly used built-in modules are:

os
sys
math
random
data time
JSON

Self is an instance or an object of a class. In Python, this is explicitly


included as the first parameter. However, this is not the case in Java where
it’s optional. It helps to differentiate between the methods and attributes of a
class with local variables.

The self variable in the init method refers to the newly created object while in
other methods, it refers to the object whose method was called.

Consider the example shown below:

1
2
3
4
from random import shuffle
x = ['Keep', 'The', 'Blue', 'Flag', 'Flying', 'High']
shuffle(x)
print(x)

Help() and dir() both functions are accessible from the Python interpreter and
used for viewing a consolidated dump of built-in functions.

Help() function: The help() function is used to display the documentation


string and also facilitates you to see the help related to modules, keywords,
attributes, etc.
Dir() function: The dir() function is used to display the defined symbols.

You might also like