O Level Python Viva Question and Answer M3-R5 Viva


O Level Python Viva Questions and Answers

1. What is Python?

Python is a high-level, general-purpose programming language known for its readability and ease of use.

2. What are the key features of Python?

  • Interpreted language: No compilation needed.
  • Dynamically typed: Data types are assigned at runtime.
  • Object-oriented: Supports classes and objects.
  • Extensive libraries: Rich ecosystem for various tasks.
  • Open-source and free to use.

3. What are the different data types in Python?

Integers (int), Floating-point numbers (float), Strings (str), Booleans (bool), Lists (list), Tuples (tuple), Dictionaries (dict), Sets (set).

4. Explain the difference between lists and tuples.

  • Lists are mutable (changeable), tuples are immutable (unchangeable).
  • Lists use square brackets ([]), tuples use parentheses ()

5. How do you take input from the user in Python?

Use the input() function to get user input as a string.

6. What are operators in Python?

Python provides various operators for arithmetic, comparison, logical, assignment, etc.

7. What is a loop in Python? Explain different loop types.

Loops allow repetitive execution of code blocks.

  • for loop iterates over sequences.
  • while loop continues execution based on a condition.

8. What are conditional statements in Python? Explain if, else, and elif.

Conditional statements control program flow based on conditions.

  • if executes code if a condition is True.
  • else executes code if the if condition is False.
  • elif provides additional conditions to check.

9. What are functions in Python? How do you define a function?

Functions are reusable blocks of code that perform specific tasks. Use the def keyword to define functions with optional parameters and return values.

10. What is debugging in Python?

Debugging involves identifying and fixing errors in your code. Python provides tools like print statements and debuggers.

11. Explain how to access elements in lists and strings.

Use indexing (starting from 0) and slicing (extracting sub-sequences) with square brackets.

12. How do you iterate through elements in a list?

Use a for loop to access each element in the list.

13. Explain methods for adding, removing, and searching elements in lists.

Use built-in functions like append(), remove(), and index() to modify and search lists.

14. What are dictionaries in Python? How do you access and modify key-value pairs?

Dictionaries are collections of key-value pairs, accessed using square brackets with the key. You can add, remove, and modify values using their keys.

15. Explain the difference between lists and sets.

Sets are unordered collections of unique elements. Lists allow duplicates, sets don't.

16. How do you convert data types in Python?

Use built-in functions like int(), float(), and str() to convert between data types.

17. What are modules and packages in Python?

Modules are Python files containing code that can be imported for reuse. Packages are collections of modules organized hierarchically.

18. How do you import modules in Python?

Use the import statement to import modules by name.

19. What is the Python interpreter?

The Python interpreter is a program that reads and executes Python code line by line.

20. What is the difference between an interpreter and a compiler?

Interpreters execute code line by line, while compilers translate the entire code into machine code before execution.

21. Explain the use of break and continue statements in loops.

break exits a loop prematurely. continue skips to the next iteration.

22. What are exceptions in Python? How do you handle them using try, except, and finally blocks?

Exceptions are events that interrupt program flow. Use try to execute code, except to handle specific exceptions, and finally to execute code regardless of exceptions.

23. How do you write comments in Python?

Use the # symbol to write single-line comments. Use triple quotes (""" or ''') for multi-line comments.

24. Explain different string manipulation methods in Python.

Python offers various methods for string formatting, concatenation, searching, and modification.

25. How do you format strings in Python? Explain f-strings.

Use string formatting methods like .format() or f-strings (introduced in Python 3.6) to insert variables within strings.

26. How do you open and close files in Python?

Use the open() function to open files with a specific mode (read, write, append). Remember to close files using close().

27. Explain how to read and write data to files in Python.

Use methods like read(), readline(), and write() to interact with file contents.

28. How do you take command-line arguments in Python?

Use the sys module to access command-line arguments passed when running the script.

29. What are functions with arguments and return values?

Functions can accept arguments (inputs) and return values (outputs) to perform specific tasks.

30. Explain the concept of scope in Python.

Scope defines the accessibility of variables within different parts of your code (local vs. global scope).

31. What is object-oriented programming (OOP) in Python?

OOP is a programming paradigm that organizes code using classes and objects.

32. Explain the concept of classes and objects in Python.

Classes are blueprints for creating objects. Objects are instances of a class with attributes (data) and methods (functions).

33. What are the pillars of OOP (encapsulation, inheritance, polymorphism)?

  • Encapsulation: Bundling data and methods within a class.
  • Inheritance: Creating new classes (subclasses) that inherit properties from existing classes (superclasses).
  • Polymorphism: Ability of objects of different classes to respond to the same method call differently.

34. What are modules and packages in Python? (continued)

Modules are Python files containing code. Packages are hierarchical collections of modules.

35. How do you create your own modules and packages in Python?

You can create Python files to define modules and organize them into directories to create packages.

36. What are some popular Python libraries and their uses?

NumPy (numerical computing), Pandas (data analysis), Matplotlib (data visualization), and many more for various tasks.

37. What are lambdas in Python? How do you use them?

Lambdas are anonymous functions defined using the lambda keyword, often for concise single-line operations.

38. Explain list comprehensions and their benefits.

List comprehensions are concise ways to create lists based on existing iterables in a single line.

39. What are generators in Python? How are they different from functions?

Generators are functions that return an iterator object, allowing for memory-efficient processing of large sequences.

40. Explain the concept of iterators in Python.

Iterators are objects that allow you to iterate through a sequence one element at a time.

41. What is the importance of testing in Python?

Testing helps ensure your code works as expected and catches errors before deployment.

42. What are some common testing techniques in Python?

Unit testing focuses on individual functions and their behavior. Integration testing verifies how different modules interact.

43. Explain the use of the assert statement for testing.

The assert statement allows you to verify conditions during testing and raise errors if they fail.

44. What are some debugging techniques in Python?

Use print statements strategically to trace code execution. Utilize debuggers to step through code line by line and inspect variables.

45. Explain common Python errors and how to identify them.

Syntax errors (incorrect syntax), runtime errors (errors occurring during execution), and logical errors (errors in program logic).

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

The GIL is a mechanism that allows only one thread to execute Python bytecode at a time, impacting performance in multithreaded scenarios.

47. What are docstrings in Python? How are they used?

Docstrings are multi-line strings placed at the beginning of functions, modules, or classes to document their purpose, usage, and parameters. They are accessible using the __doc__ attribute.

48. Explain how to handle dates and times in Python.

Python provides the datetime module for working with dates, times, and timedeltas. You can create date and time objects, perform calculations, and format them for output.

49. What are regular expressions in Python? How are they used for pattern matching?

Regular expressions are text patterns used for searching and manipulating strings. The re module provides functions for pattern matching and substitution.

50. Explain the concept of recursion in Python. When is it useful?

Recursion is a programming technique where a function calls itself. It's useful for problems that can be broken down into smaller, self-similar subproblems (e.g., factorial calculation, tree traversals).

आपको आर्टिकल कैसा लगा? अपनी राय अवश्य दें
Please don't Add spam links,
if you want backlinks from my blog contact me on