Java Viva Qa
Java Viva Qa
3. String Operations
Q1: What is the difference between String.length() and String.length?
A: String.length() is a method for String objects while .length is a property for arrays. They both
return the length/size.
Q2: Can you use Math.max() to find the maximum of three numbers? How?
A: Yes, by nesting: Math.max(a, Math.max(b, c)).
Q5: What data type would you use for numbers with decimal points?
A: For decimal numbers, we should use float or double data types.
Q5: How would you handle very large numbers that might exceed integer range?
A: We could use long instead of int, or BigInteger class for arbitrarily large numbers.
6. Constructors
Q1: What is the purpose of a constructor in Java?
A: Constructors initialize newly created objects and set initial values for object attributes.
7. Array of Objects
Q1: How is an array of objects different from an array of primitive types?
A: An array of objects contains references to objects, while primitive arrays contain actual
values.
Q5: What is the difference between length and size() for arrays and collections?
A: Arrays use .length (a property), while collections like ArrayList use .size() (a method).
8. Single Inheritance
Q1: What is inheritance in Java?
A: Inheritance is a mechanism where a child class acquires properties and behaviors of a parent
class.
Q4: What happens if both catch and finally blocks throw exceptions?
A: The exception from the finally block is thrown, and the original exception is suppressed.
Q2: What are some common approaches to check for palindrome numbers?
A: Reverse the number and compare, or compare digits from both ends.
Q5: Can you convert the number to a string to check for palindrome?
A: Yes, but it requires extra space for the string and is generally less efficient.
Q2: What data type are command line arguments received as?
A: They are received as Strings and need to be converted to numeric types.
Q4: What is the maximum factorial that can be calculated using int/long?
A: 12! for int (overflow at 13!), 20! for long (overflow at 21!).
Q5: How would you handle the case when the lower limit is less than 2?
A: Either start from 2 or skip numbers less than 2 as they can't be prime.