Java Questions 51 - 100  «Prev Next»

Java File Input/Output (Questions)

  1. What is a "static" method in Java?

    Answer:
    Static methods use no instance variables of any object of the class they are defined in. Static methods typically take all the data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class.

  2. What type of streams are System.in, System.out, and System.err?

    Answer:
    These streams are byte streams.

  3. What is a nested class?

    Answer:
    The Java programming language allows you to define a class within another class. Such a class is called a nested class.
    Class OuterClass{
    	class NestedClass{
    	}
    }
    

  4. Which classes does the java.io.RandomAccessFIle implement?

    Answer:
    The java.io.RandomAccessFile class implements both the DataInput and DataOuput interface and therefore can be used for both reading and writing.

  5. Describe the "Singleton Design Pattern"?

    Answer:
    The Singleton is used to ensure that only one object of a particular class is instantiated. The Singleton pattern is used in single-treaded applications. The Singleton pattern works by having a special method that is used to instantiate the desired object. More on the Singleton Design Pattern can be found here here

  6. When should you use an interface and when should use an abstract class?

    Answer:
    1) Interfaces provide a form of multiple inheritance, because you can implement multiple interfaces. A class can extend only one other class, even if that class has only abstract methods.
    2) An abstract class can have a partial implementation, protected parts, static methods. Whereas interfaces are limited to public constant and public methods with no implementation.

  7. What is a class variable in Java?

    Answer:
    A class variable is any field declared with the static modifier; this tells the compiler there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated.

  8. Why do we use wildcards with Java Generics?

    Answer:
    To make our APIs more flexible.

  9. What are explicit type parameters in Java Generics?

    Answer:
    We could have written the signature for this method another way, without using wildcards at all:
    class Collections {
       public static <T, S extends T>
              void copy(List<T> dest, List<S> src) {
       ...
    }
    

    This is fine, but while the first type parameter is used both in the type of dst and in the bound of the second type parameter, S, S itself is only used once, in the type of src, nothing else depends on it. This is a sign that we can replace S with a wildcard. Using wildcards is clearer and more concise than declaring explicit type parameters, and should therefore be preferred whenever possible. Wildcards also have the advantage that they can be used outside of method signatures, as the types of fields, local variables and arrays. Here is an example.

  10. How many types does the Collection type contain?

    Answer:
    One that is e. Most of the containers in Java have a fixed number of type parameters.
    The Map has 2 types, the 1) key type and the 2) value type.