Java Questions 11 - 20  «Prev Next»

Java Stubs and Skeletons (Interview Questions)

  1. What does a stub class consist of?

    Answer:
    Stubs and Skeletons are used in the context of CORBA and RMI.

  2. In Java, what are the advantages of using references as attributes?

    Answer:
    Using references as attributes in Java offers several advantages:
    1. Memory Efficiency:
      • Sharing Objects: Multiple objects can share the same instance of another object, reducing memory usage. For example, if you have many employee objects, they might all reference the same department object.
        Department marketing = new Department("Marketing");
        Employee emp1 = new Employee("John", marketing);
        Employee emp2 = new Employee("Jane", marketing);
                

        Here, both emp1 and emp2 reference the same marketing department, avoiding unnecessary duplication.
    2. Flexibility:
      • Polymorphism: References allow for polymorphism where a variable can hold objects of the declared type or any of its subclasses. This enables dynamic behavior based on the actual object type at runtime:
        List<String> list = new ArrayList<>(); // ArrayList implements List
                
      • Late Binding: The actual method called can be determined at runtime, allowing for more flexible and extensible code.
    3. Ease of Modification:
      • Changing References: It's straightforward to change what a reference points to without affecting other parts of the program. This can be useful for scenarios like updating a user's current session or changing a view in an MVC pattern.
        UserSession currentSession = userSessions.get("user123");
        currentSession = new UserSession("newSession");
                

    4. Encapsulation:
      • Object Relationships: References help in maintaining complex relationships between objects, enhancing the design of object-oriented systems. Encapsulation ensures that one object can control access to its internal state through methods, even when accessed via reference.
    5. Nullability:
      • Optional Values: References can be set to null, which can signify the absence of an object. This can be utilized to represent optional attributes where not every instance needs that attribute:
        class Person {
            private String middleName; // Can be null if the person has no middle name
        }
                
    6. Garbage Collection:
      • Automatic Memory Management: Java's garbage collector can reclaim memory when there are no more references to an object, simplifying memory management for developers.
    7. Interface-Based Programming:
      • Decoupling: Using interfaces as types for references allows for a design where concrete implementations can be swapped without changing the client code, promoting loose coupling.
        PaymentGateway gateway = new StripeGateway();
        // Later, can switch to:
        gateway = new PaypalGateway();
                

    However, these benefits come with responsibilities like managing reference integrity, handling null references, and ensuring thread safety when multiple threads access shared objects. Proper use of references can lead to more maintainable, efficient, and scalable Java applications.

    We avoid data redundancy and the potential loss of data integrity.


  3. What is another advantage of using object references?

    Answer:
    Storing a reference of an object only requires 4 bytes, instead of the number of bytes of storage the referenced object as a whole occupies in memory.

  4. What are 3 distinguishing features of an object oriented language?

    Answer:
    1. User defined (reference) types,
    2. Inheritance,
    3. Polymorphism

  5. What does a class define?

    Answer:
    a) The data that an object will encapsulate, known as the object's attributes b)The behaviors/services that an object will be able to perform, known as methods



  6. How many constructors can you call with the "this" keyword?

    Answer:
    One. Within the constructor, "this" is a reference to the current object, the object whose method or constructor is being invoked.


  7. What is the inverse relationship between static and non-static methods in Java?
    Answer:
    You cannot call non-static methods from inside static methods. You can call static methods for the class itself.


  8. What does the static method allow?

    Answer:
    Putting a static method inside a class allows it access to other static methods.


  9. Describe synchronization with respect to multithreading?

    Answer:
    With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.


  10. What are 2 different ways of using threads?

    Answer:
    The thread could be implemented by using the "runnable interface" or by inheriting from the Thread class. The former option (implementing the runnable interface) is more advantageous, since you can only achieve multiple inheritance through the use of interfaces.

SEMrush Software