Amdocs Python Latest Interview Questions Answers

Amdocs Most Frequently Asked Python Latest Interview Questions Answers

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.
Amdocs Most Frequently Asked Python Latest Interview Questions Answers
Amdocs Most Frequently Asked Python Latest 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[0] if list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]?

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

What is the output of print list[1:3] if list = [ ‘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 list[2:] if list = [ ‘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 tinylist * 2 if tinylist = [123, ‘john’]?

It will print list two times. Output would be [123, ‘john’, 123, ‘john’].

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’].

Post a Comment

Previous Post Next Post