Java Questions 51 - 100  «Prev Next»

Java SE 25 Interview Questions: Static Methods, Nested Classes, I/O, Interfaces, and Generics

This Java SE 25 interview review covers several core language and API topics: static methods, standard I/O streams, nested classes, RandomAccessFile, the Singleton design pattern, interfaces, abstract classes, class variables, and generic wildcards. These topics are frequently tested because they reveal whether a developer understands the difference between class-level behavior, object-level behavior, stream-based input/output, and type-safe API design.

  1. What is a static method in Java?
    Answer:
    A static method belongs to the class rather than to an individual object. It can be called by using the class name, and it cannot directly access instance variables or instance methods unless it has an object reference. Static methods are commonly used for utility behavior, factory methods, and operations that do not depend on per-object state.
    public class MathDemo {
        public static int square(int value) {
            return value * value;
        }
    
        public static void main(String[] args) {
            int result = MathDemo.square(5);
            System.out.println(result);
        }
    }
  2. What type of streams are System.in, System.out, and System.err?
    Answer:
    System.in is an InputStream, which is a byte input stream. System.out and System.err are PrintStream objects, which write output to byte-oriented streams while providing convenient methods such as print, println, and printf.
    System.out.println("Standard output");
    System.err.println("Error output");
  3. What is a nested class?
    Answer:
    A nested class is a class declared inside another class. Java supports static nested classes and non-static nested classes. A non-static nested class is also called an inner class, and it is associated with an instance of the enclosing class.
    public class OuterClass {
        private String message = "Outer instance state";
    
        static class StaticNestedClass {
            void print() {
                System.out.println("Static nested class");
            }
        }
    
        class InnerClass {
            void print() {
                System.out.println(message);
            }
        }
    }
  4. Which interfaces does java.io.RandomAccessFile implement?
    Answer:
    In Java SE 25, java.io.RandomAccessFile implements DataInput, DataOutput, and Closeable. It can read and write primitive Java values and strings at specific positions in a file by using a file pointer.
    import java.io.RandomAccessFile;
    
    public class RandomAccessFileDemo {
        public static void main(String[] args) throws Exception {
            try (RandomAccessFile file = new RandomAccessFile("example.dat", "rw")) {
                file.writeInt(25);
                file.seek(0);
                int value = file.readInt();
                System.out.println(value);
            }
        }
    }
  5. What is the Singleton design pattern?
    Answer:
    The Singleton design pattern restricts a class so that only one instance of the class is created. In modern Java, the simplest safe Singleton implementation is often an enum Singleton because it is concise, thread-safe, and resistant to common serialization issues.
    public enum ApplicationConfig {
        INSTANCE;
    
        public String getEnvironmentName() {
            return "production";
        }
    }

    The Singleton pattern should be used carefully. It can be useful for shared configuration or stateless services, but overuse can introduce hidden global state and make testing more difficult.

    More on the Singleton Design Pattern can be found at gofpattern.com.

  6. When should you use an interface, and when should you use an abstract class?
    Answer:
    Use an interface when you want to define a capability or contract that unrelated classes can implement. A class can implement multiple interfaces, so interfaces are the preferred choice for modeling roles such as Comparable, Runnable, AutoCloseable, or application-specific service contracts.

    Use an abstract class when related classes should share common state, constructors, protected helper methods, or partial implementation. A class can extend only one class, so an abstract class is best when the shared inheritance relationship is central to the design.
    interface ReportExporter {
        void export(String fileName);
    }
    
    abstract class BaseReport {
        private final String title;
    
        protected BaseReport(String title) {
            this.title = title;
        }
    
        public String title() {
            return title;
        }
    
        public abstract void print();
    }
  7. What is a class variable in Java?
    Answer:
    A class variable is a field declared with the static modifier. There is one shared copy of the variable associated with the class, regardless of how many objects are created from that class.
    public class Counter {
        private static int objectCount = 0;
    
        public Counter() {
            objectCount++;
        }
    
        public static int getObjectCount() {
            return objectCount;
        }
    }
  8. Why do we use wildcards with Java generics?
    Answer:
    Wildcards make generic APIs more flexible. They allow a method, field, or variable to accept a family of related generic types instead of one exact type. The wildcard ? extends T is commonly used when a method reads values from a producer, and ? super T is commonly used when a method writes values to a consumer.
    import java.util.List;
    
    public class WildcardDemo {
        public static double sumNumbers(List<? extends Number> numbers) {
            double total = 0.0;
    
            for (Number number : numbers) {
                total += number.doubleValue();
            }
    
            return total;
        }
    }
  9. What are explicit type parameters in Java generics?
    Answer:
    Explicit type parameters declare one or more named type variables for a class, interface, method, or constructor. They are useful when a type variable must be referenced more than once in the declaration. If a type variable is used only once, a wildcard may be clearer.
    import java.util.List;
    
    public class CopyDemo {
        public static <T> void copyTo(List<? super T> destination,
                                      List<? extends T> source) {
            for (T item : source) {
                destination.add(item);
            }
        }
    }

    This version uses wildcards to express the producer-consumer relationship: the source produces values of type T, and the destination consumes values of type T.

  10. How many type parameters does Collection have?
    Answer:
    The Collection interface has one type parameter: Collection<E>, where E represents the element type. By contrast, Map<K,V> has two type parameters: K for the key type and V for the value type.
    import java.util.Collection;
    import java.util.List;
    import java.util.Map;
    
    Collection<String> names = List.of("Ada", "Grace", "James");
    Map<Integer, String> users = Map.of(1, "Ada", 2, "Grace");

These questions remain useful for Java SE 25 because they connect foundational language rules with practical API usage. A modern Java developer should understand how static members differ from instance members, how nested classes are organized, how byte streams and file APIs work, and how generics improve type safety without sacrificing API flexibility.


SEMrush Software