Python_Viva_Answers (1)
Python_Viva_Answers (1)
Answers
1. Question 1
To reverse a number, we extract digits using modulus (%) and reconstruct the reverse by
multiplying the previous result by 10 and adding the digit. We use integer division (//) to
remove the last digit.
2. Question 2
This program calculates areas of circle, rectangle, and triangle using a menu-driven
approach. It uses separate functions and applies respective formulas: Circle - πr², Rectangle
- l×b, Triangle - ½×b×h.
3. Question 3
The program calculates gross salary by taking basic salary and adding DA (70%), TA (30%),
and HRA (10%). The total is computed and displayed as gross salary.
4. Question 4
It demonstrates basic arithmetic operations like addition (+), subtraction (-), multiplication
(*), division (/), and modulus (%) on two input numbers.
5. Question 5
Inheritance lets a child class use methods from a parent class. The program shows single
inheritance with a Dog class inheriting from Animal and overriding its speak method.
6. Question 6
Exception handling is used to handle runtime errors using try, except, and finally blocks.
This helps to prevent program crashes and handle errors gracefully.
7. Question 7
To find the sum of natural numbers, the program adds numbers from 1 to n using a loop or
the formula n(n+1)/2.
8. Question 8
This program checks if a number is even or odd using the modulus operator. It may also
include loops for repeated checks.
9. Question 9
Factorial is the product of all numbers from 1 to n. The program uses a loop or recursion to
compute it.
10. Question 10
This program converts units like Rupees to Dollars, Celsius to Fahrenheit, etc., using
conversion formulas and user input.
11. Question 11
Simple Interest is calculated using the formula (P×R×T)/100. The program takes principal,
rate, and time as inputs and computes the interest.
12. Question 12
To swap two variables, we use a temporary variable or Python’s tuple swapping (a, b = b, a)
without a third variable.
13. Question 13
Triangle patterns are printed using nested loops, where the outer loop controls rows and
inner loop prints stars or numbers.
14. Question 14
This program checks the type of character using conditions: isdigit(), islower(), isupper(),
or else it’s a special character.
15. Question 15
It prints the multiplication table of a number using loops from 1 to 10 or more based on
user input.
16. Question 16
Fibonacci sequence is generated using a while loop starting from 0 and 1, and adding the
previous two terms to get the next.
17. Question 17
A number is prime if it’s greater than 1 and divisible only by 1 and itself. The program
checks divisibility up to square root of the number.
18. Question 18
This is a simple calculator using functions for each operation. It takes user input and
performs addition, subtraction, multiplication, or division.
19. Question 19
Leap year check: A year is leap if divisible by 4, not divisible by 100 unless divisible by 400.
The program uses conditional logic.
20. Question 20
Quadratic equations are solved using the quadratic formula: (-b±√(b²-4ac))/2a. The
program checks the discriminant to decide real or complex roots.
21. Question 21
To find the largest among three numbers, we compare them using if-elif-else conditions.
22. Question 22
Armstrong number: A number equal to the sum of its digits raised to the power of number
of digits. The program checks this property.
23. Question 23
Palindrome check involves reversing the number and comparing it with the original. If they
match, it's a palindrome.
24. Question 24
LCM is found using the formula: LCM(a,b) = (a×b)//GCD(a,b). The program uses a loop or
math.gcd to compute it.
25. Question 25
To find factors of a number, we check every number from 1 to n to see if it divides n without
remainder.