Java Questions 21 - 30  «Prev  Next»

Java Questions 24

  1. What is an array?
    Answer: Arrays are objects that store multipe variables of the same type.
  2. What types of data types can arrays hold?
    Answer:
    Arrays can hold 1) primitive or 2) object references.
  3. Where does an array maintain its objects?
    Answer:
    An array will always be an object on the heap
  4. How do you use an array in a program?
    Answer:
    You must declare a variable to reference the array, and you must specify the type of array the variable can reference.
    Here is the syntax for declaring an array variable:
     
    dataType[] arrayRefVar;   // preferred way.			or
    dataType arrayRefVar[];  //  works but not preferred way.
    
  5. How do you declare a Primitive int and an Object reference to Threads?
    Answer:
    int [] key;
    Declare an Array of Object References. 
    Threads[] threads; 		
    

  6. Is it legal to include the size of the array in your declaration?
    Answer:
    No. It is not legal to include the size of the array in your declaration. [P57 SCJP by K.S.]
    Arrays can hold primitives or objects, but the array itself is always an object.
    When you declare an array, the brackets can be to the left or right of the variable name.
    It is not legal to include the size of an array in the declaration.
    An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array.
    To create and use an array, you must follow three steps:
    1. Declaration
    2. Construction
    3. Initialization
    public class MainClass {
    
      public static void main(String[] argvs) {
        int[] ints;
        ints = new int[4];
    
        for (int i = 0; i < ints.length; i++) {
          ints[i] = i;
        }
    
      }
    }
    
    It is never legal to include the size of the array in your declaration.
    public class PrimaryClass{
        public static void main(String[] argv){
            int[5] scores;
        }
    }
    
  7. When does the JVM allocate space for the array object?
    Answer:
    The JVM does not allocate space until you instantiate the array object.
  8. What does it mean to have a final object reference variable?
    Answer:
    A reference variable marked final can not be reassigned to refer to a different object. [P57 SCJP by K.S.]
  9. Is there such a thing as a final object?
    Answer:
    There are no final objects, only final references. [P57 SCJP by K.S.]
  10. What are 3 facts about final?
    Answer:
    1. A final class cannot be subclassed.
    2. A final method cannot be overridden by a subclass.
    3. A final variable cannot be assigned a new value.