December 28, 2018

Srikaanth

Wirecard Most Frequently Asked Latest Python Interview Questions Answers

What is the process of compilation and linking in python?

The compiling and linking allows the new extensions to be compiled properly without any error and the linking can be done only when it passes the compiled procedure. If the dynamic loading is used then it depends on the style that is being provided with the system. The python interpreter can be used to provide the dynamic loading of the configuration setup files.

The steps required are as follows:

- Create a file with any name and in any languages supported by the compiler of your system. For example comp.c
- Place this file in the Modules/ directory of the distribution which is getting used.
- Add a line in the file Setup.local present in the Modules/ directory.
- Run the file using spam comp.o
- After successful run of this rebuild the interpreter by using the make command on the top-level directory.
- If the file is changed then run rebuildMakefile by using the command as ‘make Makefile’.

What is the procedure to extract values from the object used in python?

To extract the value it requires the object type to be defined and the values are fetched according to the object type.

The values are extracted as:

- If the object is a tuple then PyTuple_Size() method is used that returns the length of the values and the method PyTuple_GetItem() returns the data item stored at a specific index.
- If the object is a list then PyListSize() and PyList_GetItem() perform the same function as they are for tuple.
- Strings use PyString_Size() to return the length of the value and PyString_AsString() returns the pointer to its value.
- To check the type of the object and the extracted values, methods like PyString_Check(), PyTuple_Check(), PyList_Check(), etc are used.

What are the steps required to make a script executable on Unix?

The steps required to make a script executable are:

- First create a script file and write the code that has to be executed in it.

- Make the file mode as executable by making the first line starts with #! this is the line that python interpreter reads.

- Set the permission for the file by using chmod +x file. The file uses the line which is the most important line to be used:
#!/usr/local/bin/python

- This explains the pathname that is given to the python interpreter and it is independent of the environment programs.

- Absolute pathname should be included so that the interpreter can interpret and execute the code accordingly. The sample code that is written:

#! /bin/sh
# Write your code here
exec python $0 ${1+"$@"}
# Write the function that need to be included.
Wirecard Most Frequently Asked Latest Python Interview Questions Answers
Wirecard Most Frequently Asked Latest Python Interview Questions Answers

How does global value mutation used for thread-safety?

The global interpreter lock allows the running of the thread one at a time. This is used to distribute the functionality among all the virtual machines that are used. Python allows the switching between the threads to be performed by using the byte code instructions to provide platform-independence. The sys.setcheckinterval() method is used to allow the switching to occur during the implementation of the program and the instruction. This provides the understanding in the field of accounting to use the byte code implementation that makes it portable to use. The atomicity can be provided such that the shared variables can be given as built-in data types.

Write a program to read and write the binary data using python?

The module which is used to write and read the binary data is known as struct. This module allows many functionalities to be used that consist of the string class. This class contains the binary data that is in the form of numbers that gets converted in python objects for use and vice versa. The program can read or write the binary data is:

import struct
f = open(file-name, "rb")
# This Open() method allows the file to get opened in binary mode to make it portable for # use.
s = f.read(8)
x, y, z = struct.unpack(">hhl", s)
The ‘>” is used to show the format string that allows the string to be converted in big-endian data form. For homogenous list of data the array module can be used that will allow the data to be kept in more organized fashion.

What is the process to run sub-process with pipes that connect both input and output ?

The popen2() module is used to run the sub-process but due to some difficulty in processing like creation of deadlock keeping a process blocked and waiting for the output from the child and child is waiting for the input. The deadlock occurs due to the fact that parent and child doesn’t have the synchronization and both are waiting to get the processor to provide the resources to one another. Use of popen3() method allows the reading of stdout and stderr to take place where the internal buffer increases and there is no read() takes place to share the resources. popen2() takes care of the deadlock by providing the methods like wait() and waitpid() that finish a process first and when a request comes, it hands over the responsibility to the process waiting for the resources.
The program is used to show the process and run it.
import popen2
fromchild, tochild = popen2.popen2("command")
tochild.write("input\n")
tochild.flush()
output = fromchild.readline()

What are the different ways to generate random numbers?

Random module is the standard module that is used to generate the random number.

The method is defined as:

import random
random.random()

The statement random.random() method returns the floating point number that is in the range of [0, 1). The function generates the random float numbers. The methods that are used with the random class are the bound methods of the hidden instances. The instances of the Random can be done to show the multi-threading programs that creates different instance of individual threads. The other random generators that are used in this are:

- randrange(a, b): it chooses an integer and define the range in-between [a, b). It returns the elements by selecting it randomly from the range that is specified. It doesn’t build a range object.

- uniform(a, b): it chooses a floating point number that is defined in the range of [a,b).Iyt returns the floating point number

- normalvariate(mean, sdev): it is used for the normal distribution where the mu is a mean and the sdev is a sigma and used for standard deviation.

- The Random class is used to instantiate an independent multiple random number generators.
Write a program to show the singleton pattern used in python.
Singleton pattern is used to provide a mechanism that limits the number of instances that can be used by one class. It also allows the same object to be shared between many different parts of the code. This allows the global variables to be used as the actual data that is hidden by the singleton class interface. The singleton class interface can have only one public member and one class method Handle. Private constructors are not used to create an object that is used outside the class. The process waits for the static member function to create new instances and return the singleton object.

The code that is used to call the singleton object is:

Singleton& Singleton::Handle()
{
   if( !psingle )
   {
       psingle = new Singleton;
   }
   return *psingle;
}

What is Python? State some programming language features of Python.

Python is a modern powerful interpreted language with objects, modules, threads, exceptions, and automatic memory managements.
Python was introduced to the world in the year 1991 by Guido van Rossum
Salient features of Python are

- Simple & Easy: Python is simple language & easy to learn.

- Free/open source: it means everybody can use python without purchasing license.

- High level language: when coding in Python one need not worry about low-level details.

- Portable: Python codes are Machine & platform independent.

- Extensible: Python program supports usage of C/ C++ codes.

- Embeddable Language: Python code can be embedded within C/C++ codes & can be used a scripting language.

- Standard Library: Python standard library contains prewritten tools for programming.

- Build-in Data Structure: contains lots of data structure like lists, numbers & dictionaries.

Explain how python is interpreted ?

- Python program runs directly from the source code.

- Each time Python programs are executed code is required.

- Python converts source code written by the programmer into intermediate language which is again translated into the native language / machine language that is executed. So Python is an Interpreted language.

- It is processed at runtime by the interpreter.

- The program need not be compiled before its execution.

- It is similar to PERL and PHP.

- Python is also interactive where it can prompt and interact with the interpreter directly to write the programs.

- It supports the object-oriented style of the technique which encapsulates the code within the objects.

Explain the dictionary in Python ?

Python's built-in data type is dictionary, which defines one-to-one relationships between keys and values.

Dictionaries consist of pairs of keys and their corresponding values.

Dictionaries are indexed by keys.

Dictionary is similar to associative array or hash table of other languages.

As following example explains further- India, Angel & Cartoon are keys & their corresponding values are Bharat, Teresa & Mickey respectively.

>>> dict = {'India': 'Bharat', 'Angel': ‘Teresa’, 'Cartoon': 'Mickey'}
>>>print dict[India]
Bharat
>>>print dict[Angel]
Teresa.

What is the output of print list if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]?

It will print concatenated lists. Output would be [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ].

What is the output of print list + tinylist * 2 if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ] and tinylist = [123, ‘john’]?

It will print concatenated lists. Output would be [‘abcd’, 786, 2.23, ‘john’, 70.2, 123, ‘john’, 123, ‘john’].

What is tuples in Python?

A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.

What is the difference between tuples and lists in Python?

The main differences between lists and tuples are − Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.

What is the output of print tuple if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 )?

It will print complete tuple. Output would be (‘abcd’, 786, 2.23, ‘john’, 70.200000000000003).

What is the output of print tuple[0] if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 )?

It will print first element of the tuple. Output would be abcd.

Name some of the features of Python.

Following are some of the salient features of python

It supports functional and structured programming methods as well as OOP.
It can be used as a scripting language or can be compiled to byte-code for building large applications.
It provides very high-level dynamic data types and supports dynamic type checking.
It supports automatic garbage collection.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

Do you have any personal projects?
Really?

This shows that you are willing to do more than the bare minimum in terms of keeping your skillset up to date. If you work on personal projects and code outside of the workplace then employers are more likely to see you as an asset that will grow. Even if they don’t ask this question I find it’s useful to broach the subject.

Is python a case sensitive language?

Yes! Python is a case sensitive programming language.

What are the supported data types in Python?

Python has five standard data types −

Numbers
String
List
Tuple
Dictionary

What is the output of print str if str = ‘Hello World!’?

It will print complete string. Output would be Hello World!.

What is the output of print str[0] if str = ‘Hello World!’?

It will print first character of the string. Output would be H.

What is the output of print str[2:5] if str = ‘Hello World!’?

It will print characters starting from 3rd to 5th. Output would be llo.

What is the output of print tuple[1:3] if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 )?

It will print elements starting from 2nd till 3rd. Output would be (786, 2.23).

What is the output of print tuple[2:] if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 )?

It will print elements starting from 3rd element. Output would be (2.23, ‘john’, 70.200000000000003).

What is the output of print tinytuple * 2 if tinytuple = (123, ‘john’)?

It will print tuple two times. Output would be (123, ‘john’, 123, ‘john’).

What is the output of print tuple + tinytuple if tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2) and tinytuple = (123, ‘john’)?

It will print concatenated tuples. Output would be (‘abcd’, 786, 2.23, ‘john’, 70.200000000000003, 123, ‘john’).

What are Python’s dictionaries?

Python’s dictionaries are kind of hash table type. They work like associative arrays or hashes found in Perl and consist of key-value pairs. A dictionary key can be almost any Python type, but are usually numbers or strings. Values, on the other hand, can be any arbitrary Python object.

How will you create a dictionary in python?

Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).
dict = {}
dict[‘one’] = “This is one”
dict[2]     = “This is two”
tinydict = {‘name’: ‘john’,’code’:6734, ‘dept’: ‘sales’}

How will you get all the keys from the dictionary?

Using dictionary.keys() function, we can get all the keys from the dictionary object.
print dict.keys()   # Prints all the keys

How will you get all the values from the dictionary?

Using dictionary.values() function, we can get all the values from the dictionary object.
print dict.values()   # Prints all the values

How will you convert a string to an int in python?

int(x [,base]) – Converts x to an integer. base specifies the base if x is a string.

How will you create a dictionary using tuples in python?

dict(d) − Creates a dictionary. d must be a sequence of (key,value) tuples.

How will you convert a string to a frozen set in python?

frozenset(s) − Converts s to a frozen set.

How will you convert an integer to a character in python?

chr(x) − Converts an integer to a character.

What is the purpose of ** operator?

** Exponent − Performs exponential (power) calculation on operators. a**b = 10 to the power 20 if a = 10 and b = 20.

What is the purpose of // operator?

// Floor Division − The division of operands where the result is the quotient in which the digits after the decimal point are removed.

What is the purpose of is operator?

is − Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. x is y, here is results in 1 if id(x) equals id(y).

What is the purpose of not in operator?

not in − Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. x not in y, here not in results in a 1 if x is not a member of sequence y.

What is the purpose break statement in python?

break statement − Terminates the loop statement and transfers execution to the statement immediately following the loop.

What is the purpose continue statement in python?

Continue statement − Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

How will you remove all leading whitespace in string?

strip() − Removes all leading whitespace in string.

What is the difference between del() and remove() methods of list?

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know.

What is the output of len([1, 2, 3])?

3.

What is the output of L[-2] if L = [1,2,3]?

 L[-1] = 3, L[-2]=2, L[-3]=1

How will you get the index of an object in a list?

list.index(obj) − Returns the lowest index in list that obj appears.

Subscribe to get more Posts :