Java Viva Questions & Answers – All 27 Programs
Q1 — Perfect Number + Multiplication Table
• What is a perfect number? → Number equal to sum of its divisors.
• Why loop till √n? → Efficient divisor check.
• Why start sum at 1? → 1 divides all numbers.
• Use of ternary operator? → For compact conditional output.
Q2 — Harshad (Niven) Number
• What is a Harshad number? → Divisible by sum of its digits.
• Why use % and / ? → To extract and remove digits.
• Why check sum!=0? → Avoid divide-by-zero.
Q3 — Car Class
• What is a constructor? → Used to initialize objects.
• What is [Link] used for? → Prevents negative speed.
• What is encapsulation? → Bundling data and methods.
Q4 — String Compression
• Why use StringBuilder? → For efficient string handling.
• What is output of aaabbcccc? → a3b2c4.
• Complexity? → O(n).
Q5 — Student Attendance using Vector
• What is Vector? → Resizable array (synchronized).
• Difference between Vector and ArrayList? → Vector is synchronized.
• How to iterate Vector? → Using loop or iterator.
Q6 — Car Mileage Array
• Why use double array? → To store decimal mileage values.
• How to find average? → Sum/5.
• Use of [Link]()? → To print array easily.
Q7 — Student Registration
• How many constructors? → Three – default, parameterized, copy.
• What is copy constructor? → Copies data from another object.
Q8 — BankAccount
• What is constructor chaining? → Calling one constructor from another.
• Rule for this()? → Must be first line in constructor.
Q9 — Vehicle Inheritance
• What does extends mean? → Used for inheritance.
• Use of super()? → Calls parent constructor.
Q10 — HotelRoom
• Purpose of switch? → Selects room price by type.
• Default price? → ■2000.
• What is copy constructor? → Copies object data.
Q11 — Device Interface
• What is an interface? → Collection of abstract methods.
• Can class implement multiple interfaces? → Yes.
• Difference class vs interface? → Class has code; interface has declarations.
Q12 — Car Rental (Abstract)
• What is abstract class? → Cannot be instantiated.
• What must subclasses do? → Implement abstract methods.
Q13 — Smartphone Interfaces
• Can Java inherit multiple classes? → No.
• Can it implement multiple interfaces? → Yes.
• What does extends do? → Inherits parent class.
Q14 — Payroll System
• What is polymorphism? → Same method, different behavior.
• What is abstract method? → Must be implemented by subclass.
Q15 — Multiple Inheritance via Interface
• Why no multiple class inheritance? → Avoids diamond problem.
• How achieved? → By multiple interfaces.
Q16 — Area Calculator
• What is method overloading? → Same name, different parameters.
• Constant for circle area? → [Link].
Q17 — Phone Call Override
• What is overriding? → Subclass redefines parent method.
• What is overloading? → Same name, diff params.
Q18 — Employee Salary
• Use of super? → Calls parent method.
• Why override? → To change or extend parent behavior.
Q19 — University Student
• Why cast to double? → To get fractional average.
• Use of ternary operator? → To print pass/fail quickly.
Q20 — Library Book
• What is boolean flag? → Tracks availability.
• What does issueBook() return? → True if issued successfully.
Q21 — Calculator with Exception
• What is try-catch-finally? → Exception handling blocks.
• What is ArithmeticException? → Division by zero error.
• What is finally used for? → Runs always.
Q22 — User-Defined Exception
• How to create custom exception? → Extend Exception class.
• Checked or unchecked? → Checked exception.
Q23 — Bank Minimum Balance
• Why use custom exception? → For business rule violation.
• Condition to throw? → If balance < ■1000.
Q24 — Threads using Runnable
• Why use Runnable? → Allows extending other classes.
• start() vs run()? → start creates thread; run executes logic.
• How to pause? → [Link]().
Q25 — Buffered File I/O
• Why use Buffered streams? → Faster I/O performance.
• getBytes()? → Converts String to byte array.
• Must handle which exception? → IOException.
Q26 — Swing Shopping Page
• Which package for Swing? → [Link].
• addActionListener purpose? → Handles button clicks.
• AWT vs Swing? → Swing is lightweight and flexible.
Q27 — AWT Calculator
• What is ActionListener? → Handles action events.
• What if input invalid? → Throws NumberFormatException.
• Why dispose()? → Closes window properly.