Java Questions

  1. (Java): What is ClassLoader in Java?
    In Java this is a class which is used to load class files in Java. Java code is compiled into class file by javac compiler and JVM executes Java program, by executing byte codes written in class file.
  2. (Java): How does Java achieve platform independence?
    Java program runs on JVM rather on any particular platform or machine. When you compile a Java program it creates .class file which is collection of byte code, these byte code are not machine instruction instead they are instruction which Java virtual machine can understand. Since every Java program runs on Java virtual machine, the same byte code can be run on any platform.
  3. (Java): Java Virtual Machine or (JVM)
    This allows Java programs to run on any device or operating system (known as the "Write once, run anywhere" principle), and to manage and optimize program memory.
  4. (Java): How does the Java byte code interact with the Operating System?
    The bytecode instructions are passed to the execution engine. JVM then interacts with the underlying OS with the help of native code that is bound to a particular JVM implementation of a specific platform.
  5. (Java):Describe the differences between ArrayList and HashSet in Java?
    The main difference between ArrayList and HashSet is that one is a List implementation while other is a Set implementation.

    • ArrayList implements List interface while HashSet implements Set interface in Java.
    • ArrayList allows duplicates while HashSet doesn't allow duplicates.
    • ArrayList is an ordered collection and maintains insertion order of elements while HashSet is an unordered collection and doesn't maintain any order.
    • ArrayList is backed by an Array while HashSet is backed by an HashMap instance.
    • ArrayList is that its index based you can retrieve object by calling get(index) or remove objects by calling remove(index) while HashSet is completely object based. HashSet also doesn't provide get() method.
  6. (Java):How do you create a thread-safe Singleton in Java?
    To accomplish this the programmer can use an Enum implementation or using a static field to initialize the  Singleton instance.
  7. (Java):When do you need to use  the volatile variable in Java?
    3 scenarios:

    1) Any variable which is shared between multiple threads should be made volatile, in order to ensure that all thread must see the latest value of the volatile variable.

    2) A signal to compiler and JIT to ensure that compiler does not change ordering or volatile variable and moves them out of synchronized context.

    3) You want to save the cost of synchronization as volatile variables are less expensive than synchronization.
  8. (Java):Describe when to use a transient variable in Java?
    When implementing Serializable you have a member field that you DON'T want serialized.
  9. (Java):What is a transient variable in Java?
    In Java this is a variable whose value is not serialized during Serialization and which is initialized by its default value during de-serialization.

    • Example:
    • Image Upload 1
  10. (Java):When do we use the Serializable interface in Java?
    Use this when we want to convert an object into a sequence of bytes. This is useful when we want to save an object in a file or when we want to send an object through some streams or over the network via sockets which  can only  be accomplished via serialization.
  11. (Java):Describe when to use the keyword: implements.
    Use this to implement an interface in Java.
  12. (Java):Describe when to use the keyword: extends.
    This is mainly used to extend a class i.e. to create a subclass in Java. Also you can use to extend an interface.
  13. (Java): Describe the difference between Serializable and Externalizable in Java?
    (1) Serializable is a marker interface i.e. does not contain any method but Externalizable interface contains two methods writeExternal() and readExternal().

    (2) The  responsibility of Serialization. when a class implements Serializable interface,  the default Serialization process gets kicked off and that takes responsibility of serializing super class state. In other words: it passes the responsibility of serialization to JVM and it’s default algorithm.When any class in Java implement then it is your responsibility to implement Serialization process i.e. preserving all important information.

    (3)  Performance. You can not do much to improve performance of default serialization process except reducing number of fields to be serialized by using transient and static keyword but with Externalizable interface you have full control over Serialization process.

    (4)Maintenance. When your Java class implements Serializable interface its tied with default representation which is fragile and easily breakable.. if structure of class changes e.g. adding or removing field. By using interface you can create your own custom binary format for your object.
  14. (Java): What is the scope of the private access keyword?
    The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
  15. (Java): What is the scope of the public access keyword?
    The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
  16. (Java): What is the scope of the protected access keyword?
    The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
  17. (Java): What is the scope of the default or blank access specifier?
    The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
Card Set
Java Questions
Basic interview questions for Java from