Java Viva Questions & Answers – Expanded
Edition (All 27 Programs)
Q1 — Perfect Number + Multiplication Table
• What is a perfect number? → Sum of its proper divisors equals the number.
• Example of perfect number? → 6 or 28.
• Why start sum from 1? → Because 1 divides every number.
• Why loop till √n? → To check divisors efficiently.
• What does % operator do? → Checks remainder to find divisors.
• Use of ternary operator? → Simplifies condition printing.
• Output for 6? → 6 is a Perfect Number.
Q2 — Harshad (Niven) Number
• What is a Harshad number? → Divisible by the sum of its digits.
• Another name? → Niven number.
• Why use %10 and /10? → To extract and remove digits.
• Why check sum!=0? → Avoid division by zero.
• Example of Harshad? → 18 (since 18%9==0).
• Return type of function? → Boolean.
• What is [Link]()? → Used for handling negative numbers.
Q3 — Car Class
• What is encapsulation? → Binding data with methods.
• What is constructor? → Initializes object variables.
• Purpose of accelerate()? → Increases speed.
• Purpose of brake()? → Reduces speed safely.
• Why use [Link]()? → Prevents speed going below 0.
• What is honk() method? → Displays horn message.
• Object example? → Car c = new Car('Toyota','Red');
Q4 — String Compression
• Purpose of program? → Compress repeating characters.
• Input example? → aaabbcccc → a3b2c4.
• Why use StringBuilder? → Efficient for string modification.
• What is charAt()? → Fetches character by index.
• What is loop complexity? → O(n).
• Why check null or empty string? → Avoid errors.
• Output for aabb? → a2b2.
Q5 — Student Attendance using Vector
• What is Vector? → Dynamic array with synchronization.
• Difference from ArrayList? → Vector is thread-safe.
• Method to add element? → add().
• Remove element? → remove().
• Check size? → size() method.
• Check if element exists? → contains().
• Display elements? → [Link](v).
Q6 — Car Mileage Array
• Why use array? → To store multiple values of same type.
• Type used? → double for decimal values.
• How many cars? → 5 cars.
• Find average formula? → Total/5.
• Find max/min? → Loop comparison.
• Display array? → [Link]().
• Scanner used for? → User input.
Q7 — Student Registration
• How many constructors? → Three types used.
• What is default constructor? → No parameters, default values.
• What is parameterized constructor? → Takes parameters to initialize.
• What is copy constructor? → Copies data from another object.
• Keyword used for copy? → this keyword.
• Output of [Link]()? → Shows same as s2 data.
• Type of class? → Nested static class.