Python Interview Questions and Answers

Python Interview Questions and Answers

Python Interview Questions and Answers



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 shutdown(release) the resources properly after use in the application.

What is the main advantage of the except block?

  • It avoids abnormal termination of the 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 a user exception class?

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

What is the use of a raise keyword?

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

What is the use of try with multi except for 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 Multitasking?

  • Performing more than one task simultaneously in the application.

What is the advantage of Multitasking?

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

Differentiate Multi-tasking and Multithreading?

  • The task is a program or process
  • Thread is a subprogram or subprocess.
  • In multitasking, n tasks execute in n process spaces whereas in multithreading, n tasks execute in single process space.

How to create a thread?

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

What is the use of a run() method?

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

Can we call a run() method directly?

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

Why do we need to call the 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 do we use to 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 within the class in which they have defined.

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

  • Defining a variable preceded by a single underscore(_) is called a 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 a module in Python?

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

What is the need of the 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 the package in python?

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

What is the 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 examples of Character systems.

What is an immutable object in python?

  • An 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.
  • An only List is a 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 does not belong to the 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?

  • The list is ordered whereas Set in un-ordered. List allows duplicates and Set allow unique elements. List allow indexing and Set don’t allow.

What is the main difference between List and Tuple?

  • List is a Mutable object but Tuple is an Immutable object.

How the 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 the collection object.

Share this post