Question: What is the purpose of the toString() method in Java?
|
Answer: The toString() method is used to return a string representation of an object.
It is implicitly called when an object is concatenated with a string or when it is passed to System.out.println().
|
Question: What is the Math class in Java, and what are some commonly used methods of this class?
|
Answer: The Math class in Java is a utility class provided in the java.lang package that
contains methods for performing mathematical operations. Some commonly used methods of the Math class include:
• max(x,y): Returns the highest value of x and y
• min(x,y): Returns the lowest value of x and y
• sqrt(x): Returns the square root of x
• cbrt(x): Returns the cube root of x
• abs(x): Returns the absolute (positive) value of x
• random(): Returns a random number between 0.0 (inclusive), and 1.0 (exclusive)
• floor(x): Returns the value of x rounded down to its nearest integer
• round(x): Returns the value of x rounded to its nearest integer
• ceil(x): Returns the value of x rounded up to its nearest integer
• pow(x, y): Returns the value of x to the power of y
All Math methods are declared as static.
|
Question: What is the purpose of the 'default' keyword in Java interfaces?
|
Answer: The 'default' keyword in Java interfaces is used to define default implementations of methods.
It allows interfaces to evolve over time by adding new methods without breaking existing implementations.
|
Question: What is the purpose of the autoboxing and unboxing feature in Java?
|
Answer: In Java autoboxing is the automatic conversion of primitive data types to their corresponding wrapper objects.
And unboxing is the automatic conversion of wrapper objects to their corresponding primitive data types.
|
Question: What is the hashCode() method in Java?
|
Answer: The hashCode() method in Java is a method of the Object class that returns the hash code value for an object.
It is used in hash-based data structures such as HashMap to determine the bucket location for storing objects.
|
Question: Can you explain the concept of method overriding in Java?
|
Answer: You can find below the concept:
• Method overriding in Java is the ability of a subclass to provide a specific implementation of a method that is already defined in its superclass.
• The overridden method must have the same name, return type, and parameters as the method in the superclass.
One generally will override a method when there is a class hierarchy, and where there is a sensible default behaviour
which many of the subclasses will want to use, but when some subclasses need a different behaviour.
|