Interview Questions 1 - 10  «Prev Next»

Java Object Equality and Autoboxing Questions

  1. What does it mean in Java that 2 objects are equal?

    Answer:

    In Java, when two objects are considered "equal", it means that they have the same state and values. In other words, they are identical in terms of their attributes and properties. By default, Java uses the == operator to check if two objects are identical, which means it checks if they are the same object in memory (same reference). However, this is not always the desired behavior, especially when working with custom classes. To check if two objects are equal in terms of their state and values, you need to override the equals() method in your class. This method should compare the relevant attributes and properties of the objects and return true if they are equal, and false otherwise. For example, consider a Person class with name and age attributes. You might override the equals() method like this:
    public class Person {
     private String name;
     private int age;
    
     @Override
     public boolean equals(Object o) {
        if (o instanceof Person) {
            Person other = (Person) o;
            return name.equals(other.name) && age == other.age;
        }
        return false;
     }
    }
    
    Now, when you compare two Person objects using the equals() method, it will check if they have the same name and age values, rather than just checking if they are the same object in memory. Note that it's also important to override the hashCode() method when overriding equals(), to ensure that equal objects have the same hash code.


  2. What is the difference between String and StringBuffer?

    Answer:
    Since String objects are immutable, you can alias to a particular String as many times as you want. The operator "+" has been overloaded for String objects.

  3. Give an example of polymorphism?

    Answer:
    If mammal has the method speak(), then you can pass a human object to the mammal method, because human inherits from mammal. Overriding methods must have the
    1. same name
    2. parameter list and type
    3. return the same type

  4. What is encapsulation?

    Answer:
    Access Control is often referred to as implementation hiding. Wrapping data and methods within classes while hiding the implementation is often called encapsulation.

  5. What is the purpose of an abstract class?

    Answer:
    The purpose of the abstract class is to extend the abstract class through inheritance.

  6. What is true about all methods in an interface?

    Answer:
    They are all abstract.

  7. When a class implements an interface, what does the keyword implements signal?

    Answer:
    The keyword implements signals that the class provides method definitions for at least some of the methods in the interface.


  8. What does autoboxing do in Java 2, v5.0?

    Answer:
    Autoboxing simplifies the interplay between primitive types and objects.
    class Wrap{
     public static void main(String args[]){
      Integer iOb = new Integer(100);
    	int i = iOb.intValue();
    	System.out.println( i + " " + iOb);
     }
    }
    

    The process of encapsulating a value within an object is called boxing.The following line boxes the value 100 into an Integer.
     
    Integer iOb = new Integer(100);
    

    The process of extracting a value from a type wrapper is called "unboxing". The following line unboxes the value in iOb with this statement.
    int i = iOb.intvalue();
    



  9. How has metadata evolved between Java SE 7 and Java SE 17?

    Answer:
    The evolution of metadata handling in Java from Java SE 7 to Java SE 17 reflects broader trends in the language's development towards more robust, efficient, and clear code practices. Here’s a detailed look at the key changes and enhancements:
    Java SE 7
    Java SE 7 did not introduce any major changes specifically related to metadata, which typically refers to annotations and the reflection API in Java. However, the groundwork provided by previous Java versions continued to be essential. The most commonly used metadata-related features were:
    • Annotations: Introduced before Java SE 7, annotations allow metadata to be included directly in the code. Common annotations include `@Override`, `@Deprecated`, and `@SuppressWarnings`.
    • Reflection API: This API allows Java code to examine or "reflect" upon Java classes, interfaces, constructors, methods, and fields at runtime.

    Enhancements in Java SE 8
    Java SE 8, released in March 2014, introduced several enhancements that, while not directly modifying the metadata system, impacted how metadata could be used:
    • Type Annotations: Extended the use of annotations to new targets. For the first time, developers could annotate any use of a type (not just declarations), which improved the capability of the language to support more robust type checking at compile-time.
    • Repeated Annotations: Provided the ability to apply the same annotation to a declaration or type use multiple times, enhancing the expressiveness of the language.
    Java SE 9 to Java SE 17
    From Java SE 9 onwards, including Java SE 17, there were no major changes directly to how metadata works in Java, but several features indirectly influenced how metadata could be utilized:
    • Module System (Java SE 9): Introduced in Java SE 9, the module system added a new level of structure to Java applications. It includes the ability to specify which packages are accessible to other modules, which can be considered as a form of metadata at the module level.
    • Records (Java SE 14 to Java SE 17): Introduced as a preview in Java SE 14 and finalized in Java SE 16, records are a special kind of class in Java. They are transparent carriers for immutable data, automatically implementing data handling methods like `equals()`, `hashCode()`, and `toString()`. While not a direct change to metadata, records reduce the need for boilerplate code which often involves annotations.
    • Sealed Classes (Java SE 17): Allows developers to restrict which other classes or interfaces may extend or implement them. This feature, finalized in Java SE 17, is akin to a form of compile-time metadata, providing clearer and more controlled hierarchies.

    These enhancements, particularly type annotations and the introduction of new features like modules, records, and sealed classes, show how Java's approach to metadata has evolved to support better type safety, more expressive code, and clearer program architectures. These changes make Java code easier to maintain and more robust against common programming errors.
    For Java SE 5, Metadata enabled you to embed supplemental information into a source file. This information is called an annotation and leaves the semantics of a program unchanged. However, this information can be used by various tools during both development and deployment.

  10. What is true about the superclass Object?

    Answer:
    An object instantiated from the Object class can reference any object type.
    Example:
    public class ObjectTest {
     public static void main(String[] args) {
      Object obj1 = new Object();
      String s1 = new String("Test String");
      obj1 = s1;
      System.out.println(obj1.toString());
     }
     public String toString() {
      return "";
     }
    }