Interview Questions 1 - 10  «Prev Next»

Java Variables and Scope Questions

  1. (J2SE) Can the value of a final variable in Java change?

    Answer:
    No, not after it has been initialized.

  2. What are the types of predefined variable scopes in JSP 3.1?

    Answer:
    In JSP (JavaServer Pages) 3.1, which is part of the Jakarta EE platform, variable scopes define the lifespan and accessibility of attributes (variables) across different components of a web application. JSP 3.1 continues to support four predefined variable scopes, each serving distinct purposes and contexts within a JSP-based application. These scopes are:
    1. Page Scope:
      • Lifespan: Variables in page scope are accessible only within the same page where they were defined. Their lifespan is limited to the current request for that particular JSP page.
      • Usage: Page scope is suitable for temporary variables that are not needed beyond the context of the current page processing. It is the default scope for JSP page attributes.
    2. Request Scope:
      • Lifespan: Variables in request scope are available to the JSP pages, servlets, or other components processing the same client request. They live throughout the lifecycle of an HTTP request and are discarded after the response is sent back to the client.
      • Usage: Request scope is ideal for passing information between components serving the same request, such as forwarding data from a servlet to a JSP page for rendering.
    3. Session Scope:
      • Lifespan: Variables in session scope are accessible across multiple requests from the same client within a single session. These variables are preserved until the user's session is invalidated or times out.
      • Usage: Session scope is used for storing user-specific information, such as login credentials, preferences, or shopping cart contents, that should persist across multiple page requests and visits
    4. Application Scope:
      • Lifespan: Variables in application scope are available to all components of the application across all sessions and requests. They are initialized when the application starts and destroyed when the application is shut down.
      • Usage: Application scope is suitable for storing global information that needs to be shared across all users and components of the application, such as configuration settings or application-wide data.

    Understanding the distinction between these scopes is crucial for effective state management in JSP-based applications. Choosing the appropriate scope for variables ensures efficient resource utilization, maintains data integrity, and enhances the user experience by providing a coherent context as users interact with the application.


  3. What is an object?

    Answer:
    The individual entities are objects. For example, the class House would describe a collection of entities that each have a number of bedrooms, a number of bathrooms, a kind of roof.

  4. What is a class?

    Answer:
    A class is a general description of a group of entities that all have the same characteristics, that is, they can all perform the same kinds of actions, and the same pieces of information are meaningful for all of them.

  5. (J2SE) What is the difference between
    1. pass by reference and
    2. pass by value?


    Answer:
    Pass By Reference means you are passing the address itself rather than passing the value.
    Pass by Value means passing a copy of the value to be passed. In Java objects are passed by reference and primitives are passed by value.

  6. What are two static methods of the Thread class?

    Answer:
    Sleep and yield are 2 static methods of the Thread class.

  7. What are the three thread states in which a thread cannot run?

    Answer:
    1. blocked
    2. sleeping
    3. waiting

    notify(): One object waits on another object to notify it.

  8. Under what conditions is variable considered constant?

    Answer:
    A variable is considered constant when it is marked 1) static and 2) final.

  9. A class that implements an interface must specify which type of access for the method of the interface?

    Answer:
    If you have a method named void m1(); inside of an interface IAnything, and have a class "ConcreteClass" that implements that interface, ConcreteClass must change the access of the method to "public".

  10. What is LookupDispatch Action in Struts?

    Answer:
    The LookupDispatch Action dispatches a request based on a reverse lookup of a request parameter value in the resource bundle.

SEMrush Software