Java Questions 1-10  «Prev  Next»

Java Questions 1

  1. What is a downcast in Java?

    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?

    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?

    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?

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

  5. What is the purpose of assertions?

    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?
    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.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());

    Additional output is displayed besides what is displayed below.
    -- listing properties -- SE Runtime Environment
    sun.boot.library.path=C:\Program Files (x86)\Java\jdk1.7.0\...
    java.vm.vendor=Oracle Corporation
    path.separator=; HotSpot(TM) 64-Bit Server VM

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

    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?

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

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

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

  10. What priority does a thread get assigned?

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