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

Python Viva Questions and Answers

The document provides answers to various questions about Python programming, covering topics such as variables, data types, operators, functions, loops, and file handling. It explains concepts like dynamic typing, exception handling, and the differences between data structures such as lists, tuples, and dictionaries. Additionally, it discusses regular expressions and data formats like CSV and JSON.

Uploaded by

Anish
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)
10 views5 pages

Python Viva Questions and Answers

The document provides answers to various questions about Python programming, covering topics such as variables, data types, operators, functions, loops, and file handling. It explains concepts like dynamic typing, exception handling, and the differences between data structures such as lists, tuples, and dictionaries. Additionally, it discusses regular expressions and data formats like CSV and JSON.

Uploaded by

Anish
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

Here are the answers to the Viva Questions listed in your document.

+1

1. What is Python and why is it considered an interpreted language? Python is a


high-level, general-purpose programming language. It is considered an interpreted language
because the code is executed line-by-line by an interpreter at runtime, rather than being
compiled into machine code beforehand.

2. What are variables and how are they created in Python? Variables are named
references to memory locations used to store data. In Python, variables are created the
moment you assign a value to them using the assignment operator (=).

3. Explain dynamic typing in Python. Dynamic typing means that the data type of a variable
is determined at runtime, not in advance. You do not need to declare the type (like int or float)
explicitly, and a variable can hold different types of data at different times during execution.

4. What is the difference between / and // operators? The / operator performs standard
division and returns a float (decimal value). The // operator performs floor division, returning
the largest integer less than or equal to the result (rounding down).
+1

5. Why is type conversion needed when using the input() function? The input() function
always returns data as a string. Type conversion (casting) is needed (e.g., using int() or float())
to convert that string into a number so mathematical operations can be performed on it.
+2

6. What happens when division by zero occurs in Python? Python raises a runtime error
called ZeroDivisionError. If not handled using exception handling, this error will crash the
program.

7. Explain the concept of expressions in Python. An expression is a combination of values,


variables, operators, and function calls that the interpreter evaluates to produce a single value
(e.g., a + b or len(str) > 5).

8. What are literals and identifiers?


●​ Literals: Fixed values directly written into the code (e.g., 10, "Hello", 3.14).
●​ Identifiers: Names given to entities like variables, functions, and classes to identify
them.​

9. What are conditional statements in Python? Conditional statements are control


structures that allow a program to execute certain blocks of code only if specific conditions
are true (e.g., if, elif, else).
+1
10. Explain the working of if–elif–else. The program first evaluates the if condition. If true, it
executes that block. If false, it checks the elif (else if) conditions sequentially. If all conditions
are false, the else block is executed.

11. What are logical operators? Give examples.


Logical operators are used to combine conditional statements. Examples include:
●​ and: Returns True if both statements are true.
●​ or: Returns True if one of the statements is true.
●​ not: Reverses the result (returns False if the result is true).​
+1​

12. What is exception handling? Exception handling is a mechanism to respond to runtime


errors (exceptions) gracefully so that the program continues to run or terminates cleanly
without crashing.
+2

13. Why is try–except used in Python programs? The try block is used to wrap code that
might throw an error (like user input or file operations), and the except block catches specific
errors to prevent the program from crashing.
+2

14. What is a ValueError? A ValueError is a specific exception raised when a function


receives an argument of the correct type but an inappropriate value (e.g., trying to convert
the string "abc" to an integer).
+1

15. How does exception handling improve program robustness? It prevents unexpected
crashes when errors occur (like invalid input or missing files) and allows the program to
provide user-friendly error messages or alternative solutions.
+3

16. What is a function in Python? A function is a block of organized, reusable code that is
used to perform a single, related action. It runs only when it is called.
+1

17. Difference between built-in and user-defined functions.


●​ Built-in functions: Pre-defined in Python (e.g., print(), len(), input()).
●​ User-defined functions: Created by the programmer using the def keyword to perform
specific tasks.​
+1​

18. What is a fruitful function? A fruitful function is a function that returns a value (result)
back to the caller using the return statement.
19. What is a void function? A void function performs an action (like printing to the screen)
but does not return a value (it implicitly returns None).

20. What are parameters and arguments?


●​ Parameters: The variables listed inside the parentheses in the function definition.
●​ Arguments: The actual values sent to the function when it is called.​

21. Explain local and global variables.


●​ Local variables: Declared inside a function and can only be accessed within that
function.
●​ Global variables: Declared outside any function and can be accessed throughout the
program.​

22. Why are functions important for code reuse? Functions allow you to write code once
and call it multiple times throughout the program. This reduces redundancy, makes the code
shorter, and makes it easier to maintain.

23. What is a loop in Python? A loop is a control structure that repeats a block of code
multiple times until a specified condition is met.

24. Difference between for loop and while loop.


●​ For loop: Used for iterating over a sequence (like a list, tuple, string, or range) a specific
number of times.
●​ While loop: Repeats as long as a specified condition is true.​

25. What is the purpose of the range() function? The range() function generates a
sequence of numbers, which is commonly used in for loops to control the number of
iterations.

26. Explain break and continue statements.


●​ Break: Terminates the loop immediately and moves control to the next statement after
the loop.
●​ Continue: Skips the rest of the code inside the current iteration and jumps to the next
iteration of the loop.​

27. What is an infinite loop and how can it be avoided? An infinite loop occurs when the
loop's condition never becomes false. It can be avoided by ensuring the loop variable is
updated correctly so the condition eventually fails.

28. What is a string and why is it immutable? A string is a sequence of characters enclosed
in quotes. It is immutable because once created, its content cannot be changed (you cannot
modify a character at a specific index; you must create a new string).
+1

29. What is slicing? Give an example. Slicing is a technique to extract a specific part of a
sequence (string, list, tuple). For example, if s = "Python", s[0:2] returns "Py".
+1

30. Difference between list and tuple.


●​ List: Mutable (can be changed), defined with square brackets [].
●​ Tuple: Immutable (cannot be changed), defined with parentheses ().​
+1​

31. Why are dictionaries called key–value data structures? They store data in pairs where
a unique "key" maps to a specific "value". You access the value by referencing its key rather
than an index.

32. How is word frequency counted using dictionaries? You iterate through a list of words.
For each word, you check if it is already a key in the dictionary. If yes, you increment its value;
if no, you add it as a new key with a value of 1.
+1

33. Why are tuples used for sorting dictionary data? Dictionaries are inherently unordered
(in older Python versions) or sorted by insertion order. To sort by value or key, dictionary items
are often converted into a list of tuples (key, value) which can then be sorted using standard
functions.

34. What is file handling in Python? File handling refers to the ability to create, read,
update, and delete files on the system using Python's built-in functions like open(), read(),
and write().
+1

35. Difference between read mode and write mode in files.


●​ Read mode ('r'): Opens a file for reading. Raises an error if the file does not exist.
●​ Write mode ('w'): Opens a file for writing. Creates a new file if it doesn't exist or
truncates (clears) the file if it does.​

36. What is a regular expression? A regular expression (regex) is a sequence of characters


that forms a search pattern, used for string matching and manipulation (e.g., finding emails or
phone numbers).
+2

37. What is the use of [Link]()? The [Link]() function is used to replace occurrences of a
regex pattern in a string with a specified replacement string (used for cleaning or masking
data).
+2

38. Difference between [Link]() and [Link]().


●​ [Link](): Returns a list of all matches of a pattern in the string.
●​ [Link](): Replaces the matches with a new string and returns the modified string.​
+2​

39. What is CSV and where is it used? CSV (Comma Separated Values) is a simple file
format used to store tabular data, such as a spreadsheet or database. It is widely used for
data exchange between different programs.
+1

40. What is JSON and why is it popular for data exchange? JSON (JavaScript Object
Notation) is a lightweight data-interchange format that is easy for humans to read and write
and easy for machines to parse. It is popular because it is language-independent and works
well for web data transfer.

You might also like