0% found this document useful (0 votes)
9 views5 pages

Python Control Flow Functions Interview Questions

The document is a compilation of interview questions and answers related to Python control structures, functions, and operators. It covers topics such as variable declaration, data types, operators, control structures, functions, and recursion. Each section provides concise explanations and examples to clarify key concepts in Python programming.

Uploaded by

Rinki Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Python Control Flow Functions Interview Questions

The document is a compilation of interview questions and answers related to Python control structures, functions, and operators. It covers topics such as variable declaration, data types, operators, control structures, functions, and recursion. Each section provides concise explanations and examples to clarify key concepts in Python programming.

Uploaded by

Rinki Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Control Structures, Functions,

and Operators – Interview Q&A


🔹 Variables and Data Types
1. Q: How do you declare a variable in Python?

Answer: You simply assign a value to a variable:


Example: x = 5

2. Q: What are global and local variables?

Answer: Local variables are declared inside functions and accessible only there.
Global variables are declared outside any function and accessible throughout the module.

3. Q: What is the difference between mutable and immutable types?

Answer: Mutable types (like list, dict) can be changed.


Immutable types (like int, str, tuple) cannot be changed after creation.

4. Q: What are the built-in data types in Python?

Answer: - Numeric: int, float, complex


- Sequence: list, tuple, range, str
- Set: set, frozenset
- Mapping: dict
- Boolean: bool
- Binary: bytes, bytearray

5. Q: How does Python handle type casting?

Answer: Python supports:


- Implicit casting: e.g., float + int → float
- Explicit casting: int('5'), float('3.2'), str(10)
🔹 Operators and Expressions
6. Q: What are the types of operators in Python?

Answer: - Arithmetic: +, -, *, /, %, //, **


- Comparison: ==, !=, >, <, >=, <=
- Logical: and, or, not
- Assignment: =, +=, -=
- Identity: is, is not
- Membership: in, not in
- Bitwise: &, |, ^, ~, <<, >>

7. Q: What is the difference between ‘==’ and ‘is’ operators?

Answer: ‘==’ checks if values are equal.


‘is’ checks if variables point to the same object in memory.

8. Q: What is the use of the ‘del’ keyword?

Answer: It deletes variables, objects, or elements from collections.


Example: del x or del list[0]

9. Q: Explain operator precedence in Python.

Answer: Operator precedence determines the order in which operators are evaluated.
Example: *, / have higher precedence than +, -.
Use parentheses to override it.

🔹 Control Structures
10. Q: Explain if-else conditions in Python.

Answer: Example:
if x > 0:
print('Positive')
elif x == 0:
print('Zero')
else:
print('Negative')
11. Q: How do you write loops in Python?

Answer: For loop:


for i in range(5): print(i)

While loop:
i=0
while i < 5:
print(i)
i += 1

12. Q: What is the difference between 'while' and 'for' loops?

Answer: - while: used when the number of iterations is unknown


- for: used when iterating over a sequence

13. Q: What are break, continue, and pass statements?

Answer: - break: exits loop early


- continue: skips to next iteration
- pass: does nothing (placeholder)

14. Q: How does Python handle switch-case like statements?

Answer: Python doesn't have switch-case. Use if-elif-else or match-case (Python 3.10+).

🔹 Functions
15. Q: What are functions in Python?

Answer: Functions are reusable blocks of code that perform a specific task.

16. Q: How do you define a function?

Answer: Use `def` keyword:


def greet(name):
print('Hello', name)
17. Q: What are *args and **kwargs?

Answer: - *args: for variable number of positional arguments


- **kwargs: for keyword arguments
Example:
def func(*args, **kwargs):
print(args, kwargs)

18. Q: Explain the difference between return and print.

Answer: - return sends value back to the caller


- print displays output to the console

19. Q: What is a lambda function?

Answer: A lambda is an anonymous function defined using the `lambda` keyword.


Example: square = lambda x: x*x

20. Q: What are Python decorators?

Answer: Decorators modify the behavior of a function without changing its code.
Example:
@decorator
def func(): pass

21. Q: What is recursion in Python? Provide an example.

Answer: Recursion is when a function calls itself.


Example:
def factorial(n):
return 1 if n==0 else n * factorial(n-1)

22. Q: How is argument passing done in Python? (Pass by value or reference)

Answer: Python uses pass-by-object-reference (or pass-by-assignment). Mutable objects


can be modified inside functions.
23. Q: What is a generator?

Answer: A generator yields values one at a time using `yield` and maintains state between
calls.
Example:
def gen():
yield 1
yield 2

24. Q: Explain the difference between generators and iterators.

Answer: - Generator: Automatically creates an iterator using a function and `yield`


- Iterator: Any object implementing __iter__() and __next__() methods

25. Q: What is the difference between local and nonlocal variables?

Answer: - Local: defined inside a function


- Nonlocal: used in nested functions to refer to variables in the outer function scope

You might also like