Interview Questions 1 - 10  «Prev Next»

Java Questions Component Model

  1. What is the Java component model?

    Answer:
    A component model allows you to reuse components that come from a third party without having to learn a new component model.

  2. How do you specify the directory of byte code class files?

    Answer:
    When you use the -d option during compilation you explicitly specify where the compiled byte code class files go.

  3. Why did Java 5 introduce the enum data type?

    Answer:
    Java 5 introduced the `enum` data type to address several challenges and provide benefits in software development:
    1. Improved Type Safety:
      • Prior to enums, developers often used static final fields (constants) to represent fixed sets of values. These constants were simply strings or integers, making them prone to errors at runtime. Accidental modification or typos could lead to unexpected behavior undetected until execution.
      • Enums, on the other hand, define a type that restricts the variable to its predefined set of values. This allows the compiler to detect and prevent invalid assignments during compilation, enhancing code reliability.
    2. Enhanced Readability and Maintainability:
      • Using strings or integers for constants could lead to unclear code, especially when representing complex choices. Enums provide a more meaningful and self-documenting way to represent these choices with named constants. This improves code readability and maintainability for both the developer writing the code and those maintaining it in the future.
    3. Additional Functionality:
      • Enums are not just simple collections of constants. They can have additional functionalities built-in, such as:
        • Methods: Developers can define custom methods specific to the enum values, allowing for additional logic to be associated with each value.
        • Constructors: Enums can have constructors, allowing for initialization and potentially state management within the enum itself.
        • Other features: Enums can be used in switch statements, iterated over, and used with other language features, further enhancing their usefulness.

    Overall, the introduction of enums in Java 5 aimed to improve code reliability, readability, maintainability, and expressiveness when dealing with fixed sets of values.
    To ensure that a programmer would have a predefined list of type safe constants.


  4. What are the only two boolean literal values in Java?

    Answer:
    true and false are the only 2 boolean literals.

  5. What are some examples of literals in Java?

    Answer:
    int daysInMay = 31;
    int daysInJune = 30; 
    char char_y = 'Y';
    

  6. Which classes implement the CharSequence interface?

    Answer:
    The CharSequence interface is implemented by the String class as well as the StringBuilder and StringBuffer classes.
    The CharSequence interface is a standard interface for querying the length of and extracting characters and subsequences from a readable sequence of characters.


  7. In Java adjectives ending in -able are interfaces Serializable, Comparable etc.
    Why is Throwable a class?

    Answer:
    1. Exceptions have state. In particular, message, cause, and stack trace.
    2. It is easier for the JVM to implement efficient catch blocks. Class hierarchy checks are cheaper than interface checks.
    3. I suspect it would make it harder on Java programmers if they have to order their catch blocks based on arbitrary interfaces rather than on class hierarchies.

  8. What are the direct known subclasses of ArrayList?

    Answer:
    Class ArrayList<E>
    
    java.lang.Object
      java.util.AbstractCollection<E>
          java.util.AbstractList<E>
              java.util.ArrayList<E>
    All Implemented Interfaces:
    Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
    
    Direct Known Subclasses: AttributeList, RoleList, RoleUnresolvedList


  9. What is the difference between byte and char data types in Java?
    Answer:
    While both `byte` and `char` are primitive data types in Java used to store numerical values, they have distinct purposes and characteristics:
    1. Purpose:
      • `byte`: Primarily used for storing "small whole numbers" (integers) that typically fall within a specific range and require minimal memory usage. This can be helpful for efficient storage or calculations involving small numerical values, especially in memory-constrained environments.
      • `char`: Specifically designed to represent "single characters" (letters, numbers, symbols) using the "Unicode" character encoding scheme. Unicode allows representing a vast range of characters from various languages and symbols, making it ideal for text processing or storing diverse character data.
    2. Size and Value Range:
      • `byte`: Occupies "1 byte (8 bits)" of memory and can store whole numbers ranging from "-128 to 127" due to its signed nature (can represent both positive and negative values).
      • `char`: Occupies "2 bytes (16 bits)" of memory and can hold values from "0 to 65,535" due to being unsigned (only represents non-negative values). This larger size allows it to accommodate the broader character set of Unicode.
    3. Interpretation:
      • `byte`: Primarily treated as a "numeric value" for calculations or comparisons. However, it can also be used to store raw "binary data" in specific scenarios.
      • `char`: Interpreted as a "character" and is primarily used for storing and manipulating textual data or working with individual characters.
    4. Examples:
      • `byte`: Storing small counts, representing levels in a game (1-5), or storing control codes for simple data transmission.
      • `char`: Storing individual characters in a string, representing user input like a letter grade (A-F), or storing symbols for mathematical operations (+, -, *, /).

    In summary, choose `byte` when dealing with small, signed integers that prioritize memory efficiency. Choose `char` when working with individual characters or textual data that requires broader character representation using Unicode."

  10. What is the difference between DOM and SAX parser in Java?

    Answer:
    In Java, Document Object Model (DOM) parser loads the whole XML into memory and creates a tree based on DOM model. This helps it in quickly locating the nodes, and making a change in the structure of XML. On the other hand, Simple API for XML (SAX) parser is an event based parser. It doesn't load the whole XML into memory. Due to this reason DOM is faster than SAX but require more memory and is not suitable to parse large XML files.


SEMrush Software