0% found this document useful (0 votes)
20 views

Python Viva Theory Answers

The document provides a comprehensive overview of Python concepts including control flow, functions, file handling, object-oriented programming, and libraries. Key topics include the differences between loops, recursion, exception handling, classes, and the use of libraries like NumPy and Pandas. It serves as a guide for understanding essential Python programming principles and practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python Viva Theory Answers

The document provides a comprehensive overview of Python concepts including control flow, functions, file handling, object-oriented programming, and libraries. Key topics include the differences between loops, recursion, exception handling, classes, and the use of libraries like NumPy and Pandas. It serves as a guide for understanding essential Python programming principles and practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Viva Theory Answers (Control Flow to Libraries)

2. Control Flow and Functions

Q: Difference between if, elif, and else statements

A: The 'if' statement checks the first condition. If it's false, 'elif' checks the next condition. 'else' runs when

none of the above conditions are true.

Q: Difference between for and while loops

A: 'for' loop is used for iterating over a sequence (like a list, tuple). 'while' loop runs as long as a condition is

true.

Q: What is an infinite loop? How to prevent it?

A: An infinite loop runs forever unless stopped. To prevent it, ensure loop conditions eventually become false.

Q: Recursion with example

A: Recursion is when a function calls itself. Example: factorial(n) = n * factorial(n-1).

Q: Functions, default arguments, and parameters

A: Functions are blocks of reusable code. Default arguments take a value if none is provided. Parameters are

inputs to functions.

Q: Local vs Global variables

A: Local variables are declared inside functions and not accessible outside. Global variables are accessible

everywhere.

Q: Return statement

A: The 'return' statement ends a function and optionally returns a value.

3. File Handling, Packaging, and Debugging

Q: Open and read a file

A: Use open('filename', 'r') to read a file. Then use read() or readline() to get its content.

Q: Modes: r, w, a

A: 'r' = read, 'w' = write (overwrite), 'a' = append (add to end).

Q: Exception handling

A: Use try-except blocks to handle errors and avoid crashes.

Q: Types of errors

A: SyntaxError, TypeError, ValueError, etc.

Q: Try-except-else-finally
A: 'try' runs code, 'except' handles errors, 'else' runs if no error, 'finally' runs no matter what.

Q: Python packages and modules

A: Packages are directories with __init__.py. Modules are .py files. Both help organize code.

Q: Creating a package

A: Make a directory, add __init__.py and other .py files. Import using 'import package.module'.

4. Object-Oriented Programming (OOP) in Python

Q: Classes and objects

A: A class is a blueprint; an object is an instance of a class.

Q: Encapsulation and access modifiers

A: Encapsulation hides data. Use '_' for protected, '__' for private, nothing for public.

Q: Inheritance

A: Inheritance lets one class inherit from another. Example: class Child(Parent):

Q: Types of inheritance

A: Single, Multiple, Multilevel, Hierarchical, Hybrid.

Q: Polymorphism

A: Same method name, different behaviors. Example: len() works on list and string.

Q: Overloading vs Overriding

A: Overloading = same function name with different params (not native in Python). Overriding = subclass

changes parent method.

Q: Class vs instance variables

A: Class variables are shared, instance variables are unique to each object.

Q: Constructor and methods

A: __init__() is a constructor. Methods are functions in classes.

Q: self keyword

A: 'self' refers to the instance calling the method.

6. Python Libraries

Q: NumPy

A: NumPy is a library for numerical computations and handling arrays.

Q: Create NumPy array

A: Use numpy.array([1,2,3]) to create an array.

Q: Advantages of Pandas
A: Pandas offers data structures and tools for data manipulation and analysis.

Q: Matplotlib

A: Matplotlib is used to create static, animated, and interactive visualizations.

Q: List vs NumPy array

A: NumPy arrays are faster and consume less memory than Python lists.

You might also like