Java Questions 11 - 20  «Prev Next»

Lazy initialization (Interview Questions)

  1. What is "lazy initialization"?

    Answer:
    The value of an attribute is initialized (set) when it is first accessed. The advantage of this approach is you only incur the expense of obtaining the value when and if you need it.

  2. What is the difference between a framework and a library in java?

    Answer:
    Frameworks are created by overriding the commonly used functionality to make developers work easy.
    For example JSF is a framework of JSP and the common idea behind the JSF is same as JSP but JSF framework handle the request mapping on its own by the just making the servlet entry in web.xml. To use such frameworks you need to have executables to add dependency.
    The library provides a set of functions and cannot operate standalone. But the framework provides an environment of services and can operate standalone. Of course you must implement the business logic because a framework does not provide business logic.

  3. What is the difference between StandardOpenOption.SYNC and StandardOpenOption.DSYNC?

    Answer:
    SYNC requires that all data (file data and file metadata managed by the filesystem) get written out synchronously while DSYNC requires that only the file data get written out synchronously. Looking at modern filesystems using concepts like 1) copy-on-write, 2) shadow-copying, 3) versioning, and 4) checksuming. Potential for data loss is a more confusing answer to provide; the advantages of asynchronous file I/O is that the underlying filesystem or disk can actually batch or order writes to avoid random I/O and structure the writes in a more sequential manner. In short, the ordering looks like:
    1. (no option) - Fastest, potential for loss of file data and file meta from 1 or more pending writes that haven't been flushed yet.
    2. DSYNC - Slower, waits until file data is written and returns (let's file meta get saved later)
    3. SYNC - Slowest, waits until both the file data and file meta are written out and give the thumbs up before returning.


  4. What is the class hierarchy for NoSuchFileException

    Answer:
    java.nio.file
    Class NoSuchFileException
    
    java.lang.Object
    java.lang.Throwable
    java.lang.Exception
    java.io.IOException
    java.nio.file.FileSystemException
    java.nio.file.NoSuchFileException
    All Implemented Interfaces:
    Serializable
    

  5. What is the difference between @PathParam and @QueryParam in REST?

    Answer:
    Query parameters are added to the url after the ? mark, while a path parameter is part of the regular URL.
    @PathParam and @QueryParam are JAX-RS annotations.
    They are only available to JAX-RS resource handlers, which technically are not servlets but are very similar.
    In a JAX-RS resource handler, one defines the endpoints processed by the handler using @Path annotations. For example @Path(/foo/bar). It is common practice for a single resource handler to handle what I like to call an endpoint node;
    it handles multiple endpoints that vary in one path qualifier.
    @Path(/foo/bar/a) for one the method that handles 'a' 
    @Path(/foo/bar//b) for the method that handles 'b' 
    
    Often times, we like to consolidate so we might choose to have a single method handle both endpoints. We can use a @PathParam to handle this.
    @Path(/foo/bar/{type}) where type is the path parameter. 
    

    The handler method signature would add this to assign the type path parameter to a local variable that can be used in the method declaration:
    @PathParam("type") String reqestType 
    
    JAX-RS automatically assignes the 'type' value of the path from the request to the requestType variable.
    @QueryParameter is handled differently in that it is only specified as part of the method declaration and is not included in @Path. For example,
    @QueryParam("subtype") String subType 
    

    could be added to the parameter list of the handler method to which the subtype query parameter is applicable. JAX-RS automatically assignes the query parameter value from the request to the subType variable.



  6. Why should we use nested classes and what are the benefits?

    Answer:
    Nested classes are used mostly for brevity and there is no need for creating an additional file containing the class. In addition, in many cases nested classes are relatively small, and tightly coupled with the "outer" class, so having all the code in the same file helps readability. For example, when building a complex Calendar with one or more Special Events, someone would be inclined to nest Special Events within the Calendar object, effectively encapsulating it. To this extent, one could argue that nesting classes is the best way to organize a file system.

  7. What is the importance of an "abstract class" ?

    Answer:
    Abstract classes have flexible behavior so they can contain undefined and defined methods. We can reuse the functionality for the default implementation of functions and we can implement the undefined methods in the subclass when required. If we compare this construct with an interface, we can use an abstract class if we want to set the default behavior for a subclass and we can use an interface if we want different behaviors for different classes.

  8. How can I submit a jsp html form to a servlet using JavaSript?

    Answer:
    Assign a url pattern to your servlet, and directly use it in the action of your form tag . If you are using Javascript, you will have to use Ajax, that is XmlHttpRequest. Jquery offers a cleaner way to make http calls.
    In addition, Javascript provides the following syntax for submitting the form.
    document.getElementById("form-id").submit();
    
  9. How can I use an Ajax call that uses json to fetch old data?
    I am using Json to fetch data from the database using an Ajax call.
    But it is not fetching the correct data and is not entering the controller but fetching old values stored in the JSON object.
    Is there any way to clear the old data from the JSON object so that it can fetch new data?

    Answer:
    Are you setting cache:false in your AJAX options?
    If not and your server returns the proper cache headers, then your browser could be caching the response. If you are not sure just open the net panel in your browser's dev tools and look for your AJAX request. If you see a response code of 304 "Not Modified" it was served from the cache because your server sent back the headers on a previous request that told the browser to cache the response.
    It is worth mentioning that sometimes dev tools will show a 200 but say "from cache" instead of "OK".
    A 304 is actually still a request to your server but one that asks the question
    has this resource changed?

    It will send along the e-tag and last-modified time from a previous request. Your webserver will need to determine if the resource has changed and respond with a 304 to say "the version you have is good" or 200 with the latest content.
    If you see 200 "from cache" that usually means that no request was made to your server and the browser determined it was alright to use the cached response based on its caching heuristics.

  10. I have a challenge of converting String to Date in Multithreaded Java Program:
    The data type conversion is one of the most common tasks in programming and a good programmer must know how to convert one type to another. There are many times, when you will be required to convert a String to java.util.Date object.
    Answer:
    The fact that SimpleDateFormat is not thread safe, because of internal states while parsing is interesting, however the subsequent synchronized approach bottlenecks the application.
    A SimpleDateFormat per thread, or a SimpleDateFormat pool would be a much better choice if performance is an issue.

SEMrush Software