Java Questions 21 - 30  «Prev  Next»

Java Questions 25

  1. What happens when you declare a variable as transient?

    Answer:
    A variable marked transient tells the JVM to ignore the variable when you attempt to serialize the object.

  2. What is the purpose of serialization?

    Answer:
    Serialization lets you save an object by writing its state to a special type of I/O Stream.
    FileOutputStream fileOutStream = new FileOutputStream(file);
    ObjectOutputStream output = new ObjectOutputStream(fileOutStream);
    

  3. To which types of variables can volatile be applied to ?

    Answer:
    The volatile keyword can only be applied to instance variables. The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own 1) private copy of the variable with the 2) master copy in memory.

  4. What is true about a static member in Java?

    Answer:
    All static members exist before you ever make a new instance of a class, and there will be only one copy of a static member regardless of the number of instances of that class.

  5. Where is an enum declaration prohibited?

    Answer:
    Enums can be declared as their own separate class and they must not be declared within a method. As of 5.0, Java lets you restrict a variable to having one of only a few pre-defined values, in other words, one value from an enumerated list. (The items in the enumerated list are called, surprisingly, enums.)

  6. How would you write a simple enum for the colors RED, BLUE, and GREEN?

    Answer:
    public enum Color{
     RED, BLUE, GREEN;
    }
    

  7. What is the constant type of an enum?

    Answer:
    An enum constant's type is the enum type.

  8. What is an enum in Java?

    Answer:
    A Java Enum is a special Java type used to define collections of constants. A Java enum type is a special kind of Java class and an enum can contain constants, methods.
    The most important thing to remember is that "enums" are not Strings or ints.

  9. What does the enum method values() return?

    Answer:
    Every enum has a static method called "values()", that returns an array of the enum's values in the order they are declared.

  10. Can you invoke an enum constructor directly?

    Answer:
    You can NEVER invoke an enum constructor directly. The enum constructor is invoked automatically, with the arguments you define after the constant value.