Interview Questions 1 - 10  «Prev Next»

Java Variables and Scope Questions

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

    Answer:
    In Java, a variable that is declared as `final` cannot have its value changed once it is assigned. The `final` keyword ensures that the variable is immutable in terms of its reference or value assignment.
    Key Points:
    1. Primitive Types:
    • If a `final` variable is a primitive type (like `int`, `double`, `char`, etc.), you cannot change its value after it has been initialized.

    Example:
     final int x = 5;
     x = 10;  // This will cause a compilation error.
     

    2. Reference Types (Objects):
    • If a `final` variable is a reference to an object, you cannot reassign the reference to point to a different object. However, you can still modify the internal state (fields) of the object that the reference points to, as long as the fields themselves are not `final`.

    Example:
    final MyClass obj = new MyClass();
    obj = new MyClass();  // This will cause a compilation error.
    
    obj.value = 10;  // This is allowed, assuming 'value' is not final.
    

    Summary:
    • For Primitives: A `final` primitive variable's value cannot be changed after it is assigned.
    • For Objects: A `final` reference variable cannot be reassigned to a different object, but the object's internal state can be modified if its fields are not `final`.

  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. What is the difference between
    1. pass by reference with objects and
    2. pass by value with primities
    when passing parameters into functions in Object-Oriented Programming (OOP)?

    Answer:
    The concepts of "pass by reference" with objects and "pass by value" with primitives using Java are fundamental to developers.
    1. Pass by Reference with Objects:
    In Java, all object references are passed by value. However, this can be confusing because it often feels like "pass by reference." When you pass an object to a method, you are passing the reference (a pointer to the memory location of the object) by value. This means the method receives a copy of the reference, not the object itself, but it can still modify the object through this reference.
    Pass By Reference means you are passing the address itself rather than passing the value.

    Example:
    class MyClass {
     int value;
     MyClass(int value) {
      this.value = value;
     }
    }
    
    public class Main {
     public static void modifyObject(MyClass obj) {
      obj.value = 10;
     }
    
     public static void main(String[] args) {
      MyClass myObj = new MyClass(5);
      modifyObject(myObj);
      System.out.println(myObj.value);  // Output: 10
     }
    }
    

    In this example, `myObj` is passed to the `modifyObject` method. The method modifies `myObj.value` to 10. Because the reference to `myObj` was passed (even though it’s a copy of the reference), the method modifies the original object, and the change is reflected in the output.
    2. Pass by Value with Primitives:
    In Java, primitive data types (like `int`, `double`, `char`, etc.) are always passed by value. This means that when a primitive is passed to a method, a copy of the value is passed, and any modification inside the method does not affect the original value. Pass by Value means passing a copy of the value to be passed.
    Example:
    public class Main {
     public static void modifyValue(int x) {
      x = 10;
     }
     public static void main(String[] args) {
      int num = 5;
      modifyValue(num);
      System.out.println(num);  // Output: 5
     }
    }
    

    In this example, `num` is passed to the `modifyValue` method. The method attempts to change `x` to 10, but since `x` is a copy of `num`, this change does not affect the original `num`. The output remains 5.
    Key Points:
    • Objects: Java passes a copy of the object reference to methods, which allows the method to modify the object itself (as shown in the first example). This behavior often feels like "pass by reference" but is technically "pass by value of the reference."
    • Primitives: Java passes a copy of the primitive value to methods, so changes inside the method do not affect the original value (as shown in the second example).

    This distinction is critical in understanding how data is handled and modified when passed to methods in Java.



  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