Interview Q&A

Tricky Python Interview Questions and Answers.

Interview questions play a pivotal role in gauging a candidate's mastery of the Python programming language, as well as in evaluating their prowess in coding and problem-solving.

Interview QnA

Below are several challenging Python interview questions designed to aid in your technical interview preparation:

  1. 1 • What is Python?

Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.

  1. 2 • What are the key features of Python?

Some key features of Python include its simplicity, readability, dynamic typing, automatic memory management (garbage collection), extensive standard library, and support for multiple programming paradigms.

  1. 3 • Difference between list and tuple in Python.

Lists are mutable, meaning their elements can be changed after creation, while tuples are immutable, meaning their elements cannot be changed after creation. Lists are defined using square brackets '[]', while tuples are defined using parentheses '()'.

  1. 4 • Find the output of the following code:

sorted_data = lambda lst: sorted(set(lst))[-2]
list = [3, 5, 2, 8, 5, 9, 1]

Output: 8

  1. 5 • What is a decorator in Python?

A decorator is a function that takes another function as input and extends or modifies its behavior without modifying its code directly and decorators are typically used to add functionality to existing functions or methods.

  1. 6 • Difference between '==' and 'is' operators.

The '==' operator compares the values of two objects, while the 'is' operator compares the memory addresses of two objects to check if they are the same object.

  1. 7 • What is 'staticmethod' and 'classmethod' in Python?

The 'staticmethod' is used to define a static method within a class that does not require access to the class or instance, while 'classmethod' is used to define a method that operates on the class itself rather than on instances of the class, and it receives the class as its first argument conventionally named 'cls'.

  1. 8 • What are '__str__' and '__repr__' in Python?

'__str__' is used to return a human-readable string representation of an object and is called by the 'str()' function or 'print()' function.

'__repr__' is used to return an unambiguous string representation of an object and is called by the 'repr()' function or when an object is evaluated in the REPL (Read-Eval-Print Loop).

  1. 9 • What are local and nonlocal variables in Python?

Local variables are declared within a function and are only accessible within that function, while Nonlocal variables are used in nested functions and are variables in the outer function's scope that are accessible within the inner function.

  1. 10 • What will be the output of the following code?

words = lambda sentence: " ".join(sentence.split()[::-1])
my_words = "S D R O W"
words(my_words)

Output: W O R D S

  1. 11 • Calculate the square of each element in a list:

squared = lambda lst: [x**2 for x in lst]

The 'squared' is a lambda function that takes a list ('lst') as input, after that, It utilizes a list comprehension to iterate through each element ('x') in the input list and square it ('x**2').

  1. 12 • Find the output of the following Python code:

a = 5
b = 10
a, b = b, a

Output: a = 10 and b = 5

  1. 13 • Use of the '*args' and **kwargsi' in Python.

'*args' and '**kwargs' are special syntax in Python function definitions that allow functions to accept a variable number of positional arguments and keyword arguments, respectively.

  1. 14 • Difference between Python 2 and Python 3?

Python 2 has reached its end-of-life and is no longer maintained, while Python 3 is the current version, with improvements such as better Unicode support, syntax enhancements, and other language updates.

  1. 15 • Python is an interpreter or compiler language?

Python is an interpreted, high-level programming language, means that Python code is executed line by line, without the need for compilation into machine code beforehand.

  1. 16 • Explain the use of '__init__' in Python classes.

'__init__' is a special method in Python classes used for initializing new objects. It is called automatically when a new instance of the class is created and can be used to set initial values for object attributes.

  1. 17 • Explain the concept of inheritance in Python.

Inheritance is a mechanism in object-oriented programming that allows a class (subclass) to inherit attributes and methods from another class (superclass). This promotes code reusability and allows for the creation of a hierarchy of classes with shared behavior and characteristics.

  1. 18 • Write a code to check if a number is a power of two:

def is_power_of_two(n):
⠀⠀return n != 0 and (n & (n - 1)) == 0

The function returns 'True' if 'n' is a power of two, and 'False' otherwise.

  1. 19 • What are the different data types in Python?

Python supports various data types, including integers, floats, strings, booleans, lists, tuples, dictionaries, and sets.

  1. 20 • What is PEP 8?

PEP 8 is the Python Enhancement Proposal that provides guidelines for writing Python code to improve its readability and maintainability. It covers aspects like naming conventions, indentation, whitespace usage, imports, etc.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.