Interview Questions 1 - 10  «Prev Next»

Java SE 25: Static Methods, Volatile, and Primitive Types

Review maps and arrays, volatile visibility, instance access from static methods, floating-point widths, char values, and this versus super.

  1. How do Map and HashMap differ?

    Answer: Map is an interface describing key/value associations; HashMap is a hash-based implementation. Declare a variable as Map when its contract is sufficient, and select HashMap, LinkedHashMap, TreeMap, or another implementation according to required behavior. Map cannot be instantiated directly.

  2. What are two important facts about Java arrays?

    Answer: An array is an object with a fixed length, and it has a component type that determines which values its elements can hold. Components can be primitive values or references; arrays of arrays can have rows of different lengths.

    Arrays have default-initialized elements and runtime type checks. Avoid treating a physical memory-placement description as a substitute for these language guarantees; JVM optimizations can change how a program is implemented.

  3. What does volatile provide?

    Answer: For a volatile field, a write happens-before a subsequent read of that field, providing visibility and ordering guarantees. Volatile reads and writes are atomic, but a compound operation such as count++ is a read-modify-write sequence and is not made atomic by volatile.

    Use synchronization, an atomic class, or another suitable protocol when an invariant spans several actions or fields. A volatile reference does not make every field of its referenced object volatile.

  4. How do static and instance methods differ?

    Answer: An instance method has a receiver and can operate on its state through this. A static method has no implicit receiver and is associated with the declaring type. Overridden instance methods dispatch by runtime object type; static methods are hidden rather than overridden.

  5. How many bits do float and double use?

    Answer: Float has the IEEE 754 binary32 value format and Double has binary64; the primitive types are float and double. Their widths are 32 and 64 bits, with 24 and 53 bits of significand precision respectively for normal values.

    Binary floating point cannot exactly represent many decimal fractions, including 0.1. Width does not mean a fixed number of accurate decimal digits for every operation; rounding and magnitude matter.

  6. What does programming to an interface mean?

    Answer: Clients depend on a behavioral contract rather than one implementation's internals. For example, a service accepting Map can work with different map implementations if they satisfy its required ordering, null, concurrency, and mutability assumptions.

    An interface type alone does not guarantee interchangeability. Document the behavior the client relies on, or use a narrower abstraction when those requirements are not shared by all implementations.

  7. Which values can a char hold?

    Answer: A char is an unsigned 16-bit integral value representing a UTF-16 code unit, from 0 through 65535. A character literal such as 'A' or a representable integer constant such as 65 can initialize it. Decimal, hexadecimal, octal, and binary literals are numeric notations, not separate accepted data types.

    A general int variable requires an explicit narrowing cast to char. Some Unicode characters require a surrogate pair, so one char does not always represent a complete Unicode character.

  8. Can a static method call an instance method?

    Answer: Yes, through an explicit object reference. It cannot use an implicit this because no receiver exists for the static method. Creating an object or receiving one as a parameter supplies the receiver.

    public class StaticReceiver {
        private final String name;
        StaticReceiver(String name) { this.name = name; }
        String greeting() { return "Hello " + name; }
        static String greet(StaticReceiver receiver) {
            return receiver.greeting();
        }
        public static void main(String[] args) {
            System.out.println(greet(new StaticReceiver("Java")));
            char letter = 0x41;
            System.out.println(letter);
        }
    }
    
  9. What can a static method access?

    Answer: It can directly access static members permitted by access rules, use its parameters and locals, and access instance members through an appropriate receiver. Access control still applies, including private access within the declaring class. Calling an instance method on a null receiver throws NullPointerException.

  10. What are this and super used for?

    Answer: This denotes the current instance and can distinguish its fields from shadowing parameters. Super selects accessible superclass behavior or fields relative to that instance; it is not a separate parent object. Constructor invocations this(...) and super(...) delegate initialization.

    Neither provides an implicit instance inside a static method. Java 25's early-construction rules also restrict instance access before superclass construction.

References: Java SE 25 Language Specification, Map, Float, Double, Character, AtomicInteger.