Java interview questions

Java quiz questions

  • 1.

    What is the applet class loader, and what does it provide ?

    Answer:

    When an applet is loaded over the internet, the applet is loaded by the applet classloader. The class loader enforces the Java name space hierarchy. Also, the class loader guarantees that a unique namespace exists for classes that come from the local file system, and that a unique namespace exists for each network source. When a browser loads an applet over the net, that applet’s classes are placed in a private namespace associated with the applet’s origin. Then, those classes loaded by the class loader are passed through the verifier.The verifier checks that the class file conforms to the Java language specification . Among other things, the verifier ensures that there are no stack overflows or underflows and that the parameters to all bytecode instructions are correct.

    View
  • 2.

    What is the difference between applets loaded over the internet and applets loaded via the file system ?

    Answer:

    Regarding the case where an applet is loaded over the internet, the applet is loaded by the applet classloader and is subject to the restrictions enforced by the applet security manager. Regarding the case where an applet is loaded from the client’s local disk, the applet is loaded by the file system loader. Applets loaded via the file system are allowed to read files, write files and to load libraries on the client. Also, applets loaded via the file system are allowed to execute processes and finally, applets loaded via the file system are not passed through the byte code verifier.

    View
  • 3.

    What are untrusted applets ?

    Answer:

    Untrusted applets are those Java applets that cannot access or execute local system files. By default, all downloaded applets are considered as untrusted.

    View
  • 4.

    What are the restrictions imposed on Java applets ?

    Answer:

    Mostly due to security reasons, the following restrictions are imposed on Java applets:

    • An applet cannot load libraries or define native methods.
    • An applet cannot ordinarily read or write files on the execution host.
    • An applet cannot read certain system properties.
    • An applet cannot make network connections except to the host that it came from.
    • An applet cannot start any program on the host that’s executing it.
    View
  • 5.

    What is the difference between an Applet and a Java Application ?

    Answer:

    Applets are executed within a java enabled browser, but a Java application is a standalone Java program that can be executed outside of a browser. However, they both require the existence of a Java Virtual Machine (JVM). Furthermore, a Java application requires a main method with a specific signature, in order to start its execution. Java applets don’t need such a method to start their execution. Finally, Java applets typically use a restrictive security policy, while Java applications usually use more relaxed security policies.

    View
  • 6.

    What happens when an applet is loaded ?

    Answer:

    First of all, an instance of the applet’s controlling class is created. Then, the applet initializes itself and finally, it starts running.

    View
  • 7.

    Explain the life cycle of an Applet.

    Answer:

    An applet may undergo the following states:

    • Init: An applet is initialized each time is loaded.
    • Start: Begin the execution of an applet.
    • Stop: Stop the execution of an applet.
    • Destroy: Perform a final cleanup, before unloading the applet.
    View
  • 8.

    What is an Applet ?

    Answer:

    A java applet is program that can be included in a HTML page and be executed in a java enabled client browser. Applets are used for creating dynamic and interactive web applications.

    View
  • 9.

    How does finally block differ from finalize() method ?

    Answer:

    A finally block will be executed whether or not an exception is thrown and is used to release those resources held by the application. Finalize is a protected method of the Object class, which is called by the Java Virtual Machine (JVM) just before an object is garbage collected.

    View
  • 10.

    What will happen to the Exception object after exception handling ?

    Answer:

    The Exception object will be garbage collected in the next garbage collection.

    View
  • 11.

    What is the importance of finally block in exception handling ?

    Answer:

    A finally block will always be executed, whether or not an exception is actually thrown. Even in the case where the catch statement is missing and an exception is thrown, the finally block will still be executed. Last thing to mention is that the finally block is used to release resources like I/O buffers, database connections, etc.

    View
  • 12.

    What is the difference between throw and throws ?

    Answer:

    The throw keyword is used to explicitly raise a exception within the program. On the contrary, the throws clause is used to indicate those exceptions that are not handled by a method. Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible exceptions. Finally, multiple exceptions are separated by a comma.

    View
  • 13.

    What is the difference between Exception and Error in java ?

    Answer:

    Exception and Error classes are both subclasses of the Throwable class. The Exception class is used for exceptional conditions that a user’s program should catch. The Error class defines exceptions that are not excepted to be caught by the user program.

    View
  • 14.

    What are the two types of Exceptions in Java ? Which are the differences between them ?

    Answer:

    Java has two types of exceptions: checked exceptions and unchecked exceptions. Unchecked exceptions do not need to be declared in a method or a constructor’s throws clause, if they can be thrown by the execution of the method or the constructor, and propagate outside the method or constructor boundary. On the other hand, checked exceptions must be declared in a method or a constructor’s throws clause. See here for tips on Java exception handling.

    View
  • 15.

    Does Garbage collection occur in permanent generation space in JVM ?

    Answer:

    Garbage Collection does occur in PermGen space and if PermGen space is full or cross a threshold, it can trigger a full garbage collection. If you look carefully at the output of the garbage collector, you will find that PermGen space is also garbage collected. This is the reason why correct sizing of PermGen space is important to avoid frequent full garbage collections. Also check our article Java 8: PermGen to Metaspace.

    View
  • 16.

    When does an Object becomes eligible for Garbage collection in Java ?

    Answer:

    A Java object is subject to garbage collection when it becomes unreachable to the program in which it is currently used.

    View
  • 17.

    What is the difference between Serial and Throughput Garbage collector ?

    Answer:

    The throughput garbage collector uses a parallel version of the young generation collector and is meant to be used with applications that have medium to large data sets. On the other hand, the serial collector is usually adequate for most small applications (those requiring heaps of up to approximately 100MB on modern processors).

    View
  • 18.

    What is structure of Java Heap ? What is Perm Gen space in Heap ?

    Answer:

    The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. Heap memory consists of live and dead objects. Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects are those which will never be accessible by the application, but have not been collected by the garbage collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.

    View
  • 19.

    If an object reference is set to null, will the Garbage Collector immediately free the memory held by that object ?

    Answer:

    No, the object will be available for garbage collection in the next cycle of the garbage collector.

    View
  • 20.

    When is the finalize() called ? What is the purpose of finalization ?

    Answer:

    The finalize method is called by the garbage collector, just before releasing the object’s memory. It is normally advised to release resources held by the object inside the finalize method.

    View

© 2017 QuizBucket.org