Menu Close

Python Interview Questions

 What is Script?

  • A program that serves the application (Standalone or Web) that was developed using another programming language.
  • Script is a set of instructions that will be interpreted.
  • Script carried out by another program rather than by the computer processor.

Why Python programming language?

  • Every programming language has its scope. Different programming languages used for different types of application development.
  • Python script analyzes the data effectively using its pre-defined functionality than other programming codes.
  • Python can be used in web development, Gaming development, Embedded System applications and many more.

 

What is the difference between Script and Program?

  • A Program (Process) is a set of instructions.
  • A script is a sub program or sub process.

What is Compiler?

  • A translator that converts the source code (high level instructions set) into either machine code or interpreter understandable code.

 

Can we modify a list?

  • Yes, List is mutable collection object. Hence we can modify the object by adding or removing the elements once it has been created.

Can we modify a tuple?

  • No, tuple is immutable object.
  • Immutable objects are constants hence we cannot modify once it has created.

Can we modify a tuple element?

  • Tuple elements can be Mutable or Immutable.
  • If the element is Mutable like List, we can modify.
  • If the element is immutable like int, float , string and Tuple cannot be modified.

 

Can we store a List inside Tuple?

  • Yes allowed.
  • Tuple can able to store both Mutable and Immutable objects.
  • List is Mutable hence that cannot be stored as an element.

Differentiate Compilation and Interpretation?

  • Compiler translates the source code into machine code all at once only if all instructions are syntactically correct.
  • Interpreter translates the code line by line and generates the output for translated instruction instantly.

What is the return type of input() function?

  • Input() function reads the end user input in the program.
  • It returns the input values in String format.

How can we process the input values in application?

  • We can convert the String type elements into corresponding type using pre-defined data conversion methods such as int() , float() ….

What is the use of ‘pass’ keyword?

  • ‘pass’ keyword is used to define empty blocks, functions and classes.

What is break statement?

  • “Break” is used to terminate the flow of a loop.

Differentiate Break and Continue?

  • “Break” is used to terminate the loop completely where as “Continue” is used to end the current iteration and continue with remaining iterations execution.

Can we store a Tuple inside List?

  • List allows both Mutable and Immutable objects as elements.
  • Tuple is Immutable and it can be stored as an element.

Can we store a set inside another set?

  • No.
  • Set is Mutable.
  • Set elements must be immutable.

 

Can we store a Tuple as a key in dictionary?

  • Yes allowed, tuple is Immutable and Dictionary keys are Immutable.

 

Does python contains char data type?

  • No, python supports only string type.
  • A character in python is a String of length 1.

What is the use of chr() function in Python?

  • chr() function returns the corresponding symbol of specified integer value.
  • For example  chr(97) is ‘a’

How can we represent a String in python?

  • String is a one dimensional character array.           
  • We can represent Strings either by using single quotes or double quotes.

 

Can we add String and integer directly using + operator?

  • No, we cannot concatenate String and Integer directly. We need to convert integer into string to merge.

string = string + str(int)

 

What is the use of str( ) function?

  • Converts specified input value(int or float..) into String type.

                str(10) à “10”

Can we convert a character into integer using int() function?

  • No, int() function is used to convert the specified input(convertible integer) into int type.

For example,

                int(“12”) à 12

                int(34.56) à 34

                int(“abc”) à Error :

What is the use of ord( ) function?

  • ord( ) function is used to convert the specified symbol(A string of length 1) into integer.

 

How to convert a string into float ?

  • float() function is pre-defined and it can be used to convert String to float(if the string contains convertible float value) else it raises exception.

 

What is function?

  • Set of instructions having identity. A function takes input, process input and generates output.

 

Differentiate Function and Method?

  • Both are same.
  • We call it as a Function in Procedure oriented programming and Method in Object
  • Oriented programming.

 

What is Keyword arguments Function?

  • Calling the function by passing values along with argument names.
  • Argument names working like keys.
  • While using keys(argument identities), no need to follow the order of arguments to pass values.

What is the use of variable arguments Function?

  • Variable argument functions can able to collect different length of parameter values.
  • A pointer type variable collects all these values.

What is Default arguments function?

  • Assigning values directly to argument in the function definition.
  • Passing parameter values to these arguments is optional.
  • We can replace these values if required.

Can we place a non default argument followed by default argument?

  • No, it is not possible to place default argument preceded by non default argument.

 

What is class?

  • The complete representation of real world entity(object) is called class.
  • It is a blue print or model of object.
  • A collection of similar objects can be defined using class template.

What is object?

  • A real world entity with an identity, properties and functionality.
  • Identity is unique and it is used to access the object.
  • Properties represent information of Object.
  • Functionality describes capability of object.

 

Describe self reference variable?

  • The most recommended variable used to define object level methods.
  • It must be placed as first argument in the method definition.
  • It holds object address and it is used to invoke object level methods.

What is the need of encapsulation?

  • Protecting the object members(data and code) is the concept of encapsulation.
  • We can implement Encapsulation in object oriented programming using ‘class’.

 

What is inheritance?

  • Defining an object by extending(updating) the functionality of existing object.
  • It includes, accessing existing object functionality, adding new functionality and updating existing functionality if required.

What is the advantage of inheritance?

  • The main advantage of inheritance is code re-usability.

How interpreter differ from compiler ?

  • Compiler translates the source code all at once where as Interpreter translates the code line by line.

 

Why python is interpreted programming language?

  • Python code need not be compiled.
  • Python interpreter able to run the source code directly and generate output.

 

Describe local variable?

  • Definition of variable inside a block or function.
  • It can be accessed only within the block in which it has defined.

Is python object oriented programming language?

  • Yes. Python is fully object oriented programming language. Every data element in python is an object. It doesn’t support primitive types

 

What is global variable in Python ?

  • Defining variable outside to all the functions referred as Global variable.
  • The scope of global variable is throughout the program.

What are the advantages of functions?

  • Code re-usability
  • Easy implementation of application

What is the use of global keyword ?

  • The keyword is used to create, access and process the global variables information from the function.

 

Does python supports data types?

  • Python is dynamic programming language.
  • It stores the information in the form of object.
  • It doesn’t store primitive data. Primitive data type size is fixed but not object size.
  • If we able to store the data of any size is called dynamic language.

 

What is method overriding?

  • Defining a method in the child class with the same name and same signature of parent class method.
  • Overriding can be implemented only in Parent-Child relation.

 

What is the need of method overriding?

  • The main advantage of method overriding (re-writing) is to update Parent object functionality in the child class if it is not sufficient to Child class.

 

Define abstraction?

  • Abstraction is the concept of hiding unnecessary details of object and showing essential features to communicate.

 

What is object reference variable?

  • A user variable that holds address of Object is called Object reference variable.

 

How to initialize Parent object in the process of Child object creation?

  • By invoking the Parent class constructor(__init__) from the Child class constructor in Child object creation process.

 

What is the use of super() method?

  • super() method is used to access Parent class functionality from Child class.
  • Super() method is used to access the any super class functionality by specifying its Child reference.

Does python supports Hybrid inheritance?

  • Yes, python supports Hybrid inheritance. It is also called Diamond inheritance.

 

What is the difference between match() and search() functions in Regular expressions?

  • match() function is used to check whether the string starts with specified pattern or not.
  • search() function is used to find whether the string contains specified pattern or not.

 

Define polymorphism?

  • Defining an object(class) that shows different behavior(functionality) with the same identity.

 

What is method overloading technique?

  • Defining method more than once with the same name and different signature.
  • We can implement method overloading inside a class.

 

How can we achieve method overloading in python?

  • It is not possible to implement method overloading in python programming. Overloading is called compile time binding. Python is interpreted programming language.

 

What is Constructor?

  • A special method invokes automatically in object creation process.

 

How to access Overridden method of Parent class from Child?

  • Using super() method, we can access Parent class functionality from Child class area.

 

How to access Grandparent object level functionality from Child?

  • By invoking super(Parent, object_ref).

 

What is runtime polymorphism?

  • It is also called dynamic binding. It is method overriding technique.
  • Writing the method in the Child class with the same name and same signature with Parent class method.

What is ‘self’ ?

  • The most recommended variable to define object level methods and constructors.
  • ‘self’ holds object address hence it is also called ‘Reference variable’.

Can I access ‘self’ anywhere in the program?

  • ‘self’ is an argument of function or constructor
  • ‘arguments’ are working like local variables.
  • Local variables can be accessed only within the function.

What is Exception?

  • It is a run time error.
  • Exception either pre-defined class or user defined class.

Why handling exceptions?

  • Exception causes abnormal termination of Program execution.
  • Exception provides informal information to End user.

How to handle exceptions in Python?

  • Using try-except blocks, we can handle exceptions in pyton.

What is try ?

  • It is a keyword
  • It is a block of instructions.
  • Doubtful code that raises exception must be place inside the try block in exception handling.

What is exception object?

  • If exception has raised with in the logic, an object will be created with the complete information of error to be handled is called “Exception object”.

Describe except block?

  • Except-block contains exception handling logic.
  • Except block executes only if an exception has risen in try block.

 

What is the need of finally block?

  • Finally block contains resource releasing logic. It is recommended to shut down(release) the resources properly after use in the application.

What is the main advantage of except block?

  • It avoids abnormal termination of program in case of exception
  • It is used to provide formal information to End user.

 

When we use try with finally block?

  • We can use try with finally block without except block definition but the program terminates abnormally in case of exception.

How to create user exception class?

  • User exception can be created by extending the functionality of pre-defined exception class.

 

What is the use of raise keyword?

  • Pre-defined exception objects will be raised automatically by the python interpreter when error occurs.
  • User defined exception objects must be raised manually in the application using ‘raise’ keyword.

 

What is the use of try with multi except blocks?

  • Used to handle different types of exceptions occur in different lines of code in try block. Only one except block executes among multiple blocks we defined.

What is Multi tasking?

  • Performing more than one task simultaneously in the application.

What is the advantage of Multi tasking?

  • The optimum utilization of CPU(process) resource and complete the tasks with parallel processing.

 

Differentiate Multi tasking and Multi threading?

  • Task is a program or process
  • Thread is a sub program or sub process.
  • In multi tasking, n tasks execute in n process spaces where as in multi threading, n tasks execute in a single process space.

 

How to create thread?

  • By extending the functionality of Pre-defined Thread class. Thread class belongs to a module called ‘threading’.

 

What is the use of run() method?

  • It is the pre-defined method available in Thread class.
  • It must be overridden into user defined Thread class.
  • It is used to place the thread logic that will execute parallel and independent from thread space.

 

Can we call run() method directly?

  • Yes, but the logic executes from the current thread space.

 

Why we need to call start() method to start a thread?

  • start() method provides pre-defined logic to allocate the thread space from which run() method logic executes independently.

 

What is the use of sleep() method?

  • sleep() method is used to cease(stops) thread execution for the specified number of milliseconds.

 

What is the difference between sleep() and join() methods?

  • sleep() method stops the thread for specified number seconds.
  • join() method stops the thread until joined thread execution completes.

 

What is synchronization?

  • Allowing threads sequentially when multiple threads are trying to access a resource.

 

When we use try with multiple except blocks?

  • A transaction(task) is a collection of steps and in chance of getting more than exception in that transaction.

 

When constructor gets execute?

  • Constructor executes when we create object for a class.

 

What is the use of id() method?

  • id() method is pre-defined and it returns object id of specified object address.

 

What is the need of arguments constructor in a class?

  • Arguments constructor is used to initialize object level variable in object creation process.

 

How to define private variables in python?

  • Defining a variable preceded by double underscore(_ _) is called Private variable.

 

Where to access private members of python class?

  • We can access private variables only with in the class in which they have defined.

What is protected variable in python and how to define it?

  • Defining a variable preceded by single underscore(_) is called protected variable. We can access these variables only in Parent-Child relation.

 

Why we use constructor?

  • A special method used to initialize(assign values to) object level variables in object creation process.

 What is class level variable?

  • Defining a variable inside the class and outside to all the method of class. We need to access class level variables using identity of class.

What is object level variable?

  • Defining a variable using constructor to store the information inside object is called object level variable. We can access these variables only by using object address.

 

What is class level method?

  • Defining a method inside the class without ‘self’ argument. It is called common functionality of all the objects in the application.

 

What is object level method?

  • Defining a method using ‘self’ keyword as first argument is called Object level method. It is called specific functionality of Object in python application.

 

How to access class level members in python?

  • We can access class level members such as variables and methods using Identity of class.

How to access object level members in python programming?

  • Object level members can be accessed in 2 ways.
  • Using ‘self variable’ from object level area.
  • Using ‘Object reference variable’ from class level area.

 

Can we define methods as private in python?

  • Yes, but we can access only with in the class. We can define both class level and object level methods as private.

 

How to access Parent class members from Child class?

  • By creating object for Child class, we can access Parent class functionality using child object address.

What is the use of compile() function in ‘re’ module?

  • compile() function is used to create pattern object with specified string and it can be used everywhere in regular expressions.

 

What ‘r’ represents in Regular expression?

  • ‘r’ represents raw string in Regular expression specification.

 

What is module in Python?

  • Module is a Python source file with .py extensions. Module is either collection of functions(in procedure oriented programming) or collection of classes(in Object oriented programming).

 

What is the need of modularity program?

  • Modularity is the concept of dividing the application into parts by which we can develop the code easily by sharing among multiple programmers and Debugging become easy.

How to connect modules in python?

  • ‘import’ keyword is used to connect modules in Python programming. We can also use ‘from’ keyword along with ‘import’ to connect the modules and classes.

 

What is the need of import keyword?

  • We can connect the classes of different modules or Functions in python application.

 

Describe ‘from’ keyword in python?

  • ‘from’ keyword is used to access specific set of classes from a module or specific set of modules from a package in python application.

 

How to access duplicate classes from different modules?

  • By placing the classes into different packages(folders), we can create duplicate classes in Python programming.

 

Can we access duplicate functions from different modules?

  • Yes we can access by specifying the function name along with module name.

 

What is package in python?

  • A folder or directory which contains set of modules or set of classes.

 

What is character system?

  • Representation of all the symbols of a language using constant integer values is called Character system.

Differentiate ASCII and UNICODE?

  • ASCII stands for American Standard Code for Information Interchange. UNICODE is Universal Code. These are the examples of Character systems.

What is immutable object in python?

  • Immutable object state cannot be modified once it has been created.

 

What are the examples of Immutable objects?

  • Immutable objects are String, Integer, Float, Tuple and so on.
  • Only List is Mutable object in Python programming.

 

What is the use of chr() function in Python?

  • chr() function is used to convert an integer into character.

 

How to find the length of String in Python?

  • len() function is used to find the length of the string. It is not belongs to string class.

 

How to access String elements?

  • We can access the elements of String either by Indexing or Slicing.

 

How to process sub strings in python?

  • Using the concept of slicing or by using pre-defined functionality of string object.

What is indexing and slicing?

  • Indexing is used to access an element from the collection objects.
  • Slicing is used to access a set of elements from the collection object such as String, List, Tuple..

 

What is the use of isalnum() method of String object?

  • A pre-defined function of String class and is used to check whether the string contains only alphabets and Numbers. It returns a boolean value.

 

What are the boolean methods of String object?

  • isalpha() , isalnum() , isnumeric() and so on.

 

What is String pool area?

  • A memory area that stores String(Immutable) objects.

 Can we store duplicate objects in String pool area?

  • Duplicate objects are not allowed in String pool area. If we try to create a duplicate object, existing object address will be shared.

What is List in python?

  • An ordered collection object that can able to store the duplicate elements as well as heterogeneous elements.

Differentiate List and Set?

  • List is ordered where as Set in un-ordered. List allow duplicates and Set allow unique elements. List allow indexing and Set doesn’t allow.

 

What is the main difference between List and Tuple?

  • List is Mutable object but Tuple is Immutable object.

 

How dictionary store the elements?

  • Dictionary collection object store the element using keys. Keys must be unique. If we try to store the elements using duplicate keys, existing elements will be replaced.

Describe the functions to perform mathematical set operations?

  • union() returns all the elements from specified sets.
  • intersection() returns common elements from specified sets.
  • symmetric() returns distinct elements from specified sets.

 

Can we store duplicates inside List?

  • Yes, a List object allows duplicate elements.

 

Is Tuple an ordered collection object?

  • Yes tuple is an ordered collection object.

 

Describe pop() function in List?

  • pop() function either deletes the top(last) element of the List or deletes the specified index element from the List.

 

What is the use of extend() function in List?

  • extend() function is used to add more than one element(another collection) to collection object.

What is the difference between pop() and remove() function?

  • pop() function removes specified index element.
  • remove() function removes specified element.

 

What is the difference between clear() and del in Collections?

  • clear() function is used to delete all the elements from collection. ‘del’ keyword is used to remove the collection object variable completely.

Can we access elements using negative indexing and slicing?

  • Yes allowed, we can use both indexing and negative indexing to access the elements of collection object.