Java Questions 1-10  «Prev  Next»

Cohesion and Coupling used in Software Development

  1. What is a downcast in Java?

    Answer:
    In Java, downcasting refers to the process of converting a reference of a parent class type into a reference of a child class type. This operation is performed explicitly by the programmer and is used when it's necessary to access specific properties or methods of a child class that aren't available in the parent class reference. When a type is downcast, the following occurs:
    1. Explicit Cast Required: Downcasting must be explicitly performed by the programmer using the cast operator. This is because downcasting is inherently unsafe, the compiler cannot guarantee at compile time that the object referenced by the parent class type can be successfully treated as an instance of the child class.
    2. Runtime Type Checking: At runtime, the Java Virtual Machine (JVM) checks the actual type of the object to ensure that it is compatible with the target type of the downcast. If the object being downcast is not an instance of the target type or one of its subclasses, a `ClassCastException` is thrown.
    3. Access to Child-Specific Members: After a successful downcast, the reference can be used to access fields, methods, and properties specific to the child class that weren't accessible through the parent class reference. This enables the invocation of more specific behavior or the manipulation of additional state held in the child class.
    4. No Impact on the Object: The downcasting operation does not modify the object itself. The object remains an instance of its original class; only the type of the reference used to access the object changes.
    Consider the following example:
    class Parent { }
    class Child extends Parent {
        void childMethod() { }
    }
    
    Parent parentReference = new Child();
    Child childReference = (Child) parentReference; // Downcasting
    childReference.childMethod(); // Now accessible
    

    In this example, `parentReference` is of type `Parent` but actually references an object of type `Child`. To call `childMethod()`, which is not available in the `Parent` class, downcasting is necessary. The reference `parentReference` is explicitly cast to `Child`, and the downcasted reference `childReference` is then used to invoke `childMethod()`.
    Downcasting is a powerful feature, but it must be used with caution due to the risk of `ClassCastException`. It is generally advisable to use the `instanceof` operator to check the type of the object before performing a downcast to ensure type safety.
    This occurs when you cast the reference down the inheritance tree to a more specific class.

  2. What is the difference between cohesion and coupling?

    Answer:
    Cohesion of a single module or component is the degree to which its responsibilities form a meaningful unit; higher cohesion is better.
    Coupling between modules or components is their degree of mutual interdependence; lower coupling is better.
    1. Size: number of connections between routines.
    2. Intimacy: the directness of the connection between routines.
    3. Visibility: the prominence of the connection between routines.
    4. Flexibility: the ease of changing the connections between routines.

  3. How often will the finalize() method get called?

    Answer:
    For any given object, the finalize() method will be called only once (at most) by the garbage collector.

  4. What type of code should appear in the "finally" clause?

    Answer:
    The code placed in the finally block is to release any resource allocated in the try clause.

  5. What is the purpose of assertions?

    Answer:

    Assertions let you test your assumptions during development, but the assertion code basically evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove.

  6. What does the following command line invocation do?

    Answer:
    Run the program at the command prompt using the following command
    java -D<name>=<value> class 
    

    On a Window's machine, enter the following command at the DOS prompt.
    C:\Java>java -DcmdProp=cmdVal PropertiesTest
     
    import java.io.FileInputStream;
    import java.util.Properties;;
    public class PropertiesTest {
     public static void main(String[] args) throws Exception{
      FileInputStream propFile = new FileInputStream ("myProperties.txt");
      Properties p = new Properties (System.getProperties());
      p.load(propFile);
      System.setProperties(p);
      System.getProperties().list(System.out);		 
     }
    }
    


    Answer:
    Additional output is displayed besides what is displayed below.
    -- listing properties --
    java.runtime.name=Java(TM) SE Runtime Environment
    sun.boot.library.path=C:\Program Files (x86)\Java\jdk1.7.0\...
    java.vm.version=21.0-b17
    java.vm.vendor=Oracle Corporation
    java.vendor.url=http://java.oracle.com/
    path.separator=;
    java.vm.name=Java HotSpot(TM) 64-Bit Server VM
    file.encoding.pkg=sun.io
    user.script=
    user.country=US
    

  7. What does the -d command - line argument instruct the compiler to do?

    Answer:
    The -d option lets you tell the compiler in which directory to put the .class file(s) it generates. (d is for destination).

  8. What are the 2 ways to create a thread?

    Answer:
    You can extend the Thread class or implement the Runnable interface.

  9. What are the 5 thread states that a thread can be in?

    Answer:
    1. New
    2. Runnable
    3. Running
    4. Waiting/blocked/sleeping
    5. Dead

  10. What priority does a thread get assigned?

    Answer:
    A thread receives a default priority that is the priority of the thread of execution that creates it.

SEMrush Software