J2EEOnline J2EE   





Java Certification Questions 1-10  «Prev  Next»

Java Questions 1

  1. What is a downcast in Java?
    Answer:
    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?
    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?
    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.