Software Engineering - Python Interview Qs Flashcards

1
Q

What is the difference between a list and a tuple in Python?

A

Lists are mutable (can be modified) and defined with square brackets [], while tuples are immutable (cannot be modified) and defined with parentheses ().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of the self keyword in Python?

A

self is a reference to the current instance of a class, used to access its attributes and methods. It is the first parameter of instance methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between == and is in Python?

A

== compares the values of two objects, while is compares their identity (memory address).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the concept of list comprehension in Python.

A

List comprehension is a concise way to create lists based on existing lists or other iterable objects, using a single line of code with a for loop and optional if condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are generators in Python?

A

Generators are functions that return an iterator object, allowing you to iterate over a sequence of values without storing them all in memory at once. They use the yield keyword instead of return.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the Global Interpreter Lock (GIL) in Python?

A

The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes simultaneously. It can limit the performance of CPU-bound multithreaded programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are decorators in Python? Provide an example.

A

Decorators are a way to modify or enhance the behavior of functions or classes without directly changing their source code, using the @decorator_name syntax. Example: @staticmethod, @classmethod, or custom decorators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between shallow copy and deep copy in Python?

A

Shallow copy creates a new object but references the original object’s nested objects, while deep copy creates a completely independent copy of the object and all its nested objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you handle exceptions in Python?

A

Exceptions are handled using try/except/finally blocks. The try block contains the code that may raise an exception, the except block handles specific exceptions, and the finally block executes regardless of whether an exception occurred.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of the if name == ‘main’: idiom in Python?

A

This idiom is used to determine whether a Python script is being run directly (as the main program) or being imported as a module. Code inside the if block will only execute when the script is run directly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are lambda functions in Python? Provide an example.

A

Lambda functions are small, anonymous functions defined with the lambda keyword, often used for short, one-line operations. Example: lambda x: x ** 2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between a module and a package in Python?

A

A module is a single Python file containing definitions and statements, while a package is a directory of Python modules with a special init.py file, used to organize related modules hierarchically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain the concept of duck typing in Python.

A

Duck typing is a programming style where the suitability of an object is determined by the presence of certain methods and properties, rather than its actual type. It emphasizes the behavior of objects over their explicit class or type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the differences between Python 2 and Python 3?

A

Python 3 introduced changes in print syntax (print() function), integer division (/ returns float), Unicode support (all strings are Unicode by default), and some renamed or removed functions. Python 2 is no longer maintained as of January 2020.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you create a virtual environment in Python?

A

Virtual environments can be created using the venv module in Python 3, or the virtualenv package in Python 2. Example: python3 -m venv myenv.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the init.py file in Python packages?

A

The init.py file is used to initialize a Python package and can contain package-level imports, variables, or functions. Its presence also marks the directory as a Python package.

17
Q

What are the key features of Python?

A

Easy to learn and read, dynamically typed, interpreted, multi-paradigm (supports object-oriented, procedural, and functional programming), and has a large standard library and third-party ecosystem.

18
Q

How do you implement object-oriented programming (OOP) in Python?

A

OOP in Python is implemented using classes, which are defined with the class keyword. Objects are instances of classes, created using the class constructor. Python supports inheritance, polymorphism, and encapsulation.

19
Q

What is the difference between a class method and a static method in Python?

A

A class method receives the class as its first argument (cls) and can access or modify class state, while a static method does not receive any implicit first argument and cannot access or modify class state. Class methods are defined with the @classmethod decorator, and static methods with the @staticmethod decorator.

20
Q

How do you optimize the performance of Python code?

A

Some techniques to optimize Python code include: using built-in functions and libraries, optimizing loops, using list comprehensions or generators, caching results (e.g., with @lru_cache decorator), using efficient data structures, and profiling code to identify bottlenecks.