Java Questions 41 - 50  «Prev  Next»

Java Questions on Variables and Initialization

  1. Which fact holds true for local variables in Java?
    Answer:
    Local variables, including primitives, must always be initialized before you attempt to use them.

  2. Can a variable be initialized within an if-block?
    Answer:
    Yes it can, but it is better practice to initialize the variables outside of the conditional block.
    The following code will compile
    if (true) {
      int x = 5;
    }
    
    and the scope will be within the parentheses declared after the if-statement.

  3. Can locally declared references (objects within a method) check for null?
    Answer:
    Yes.
    public void methodTestOne() {
     Outer o1 =null;
     if ( o1 == null) {
      System.out.println("o1 is equal to null");
      }
      // get the hashCode of a variable that has been assigned Null
      try {
       o1.hashCode();
      }
      catch(NullPointerException e) {
       e.printStackTrace();
      }
    }
     

  4. Can you assign the "null" value to an object that has been declared locally.
    Answer:

    Yes . See the code listed above.

  5. Are instance variables given a default value of null?
    Answer:
    All member variables have to be loaded into the heap so they have to be initialized with default values when an instance of a class is created. In the case of local variables, they do not get loaded into the heap and are stored on the stack until they are used. Before Java SE 7, member variables needed to be explicitly initialized.

  6. What is a good practice for variable initialization within a method?
    Answer:
    If you declare an object reference within a method, assign the "null" value to it, until you know what the object's value should be.

  7. What are the default values given to array elements in Java?

    Answer:
    Array elements are given their default values (0, false, null, '\u0000') regardless of whether the array is declared as an 1) instance or 2) local variable.
    package com.java.arrays;
    
    public class InitArray {
    	static int [] nums = new int[2];
    	static boolean [] values = new boolean [2];
    	static Object [] objs = new Object[2];
    	static char [] chars = new char [2];
    
    	public static void main(String[] args) {
    		System.out.println(nums[0]);
    		System.out.println(values[0]);
    		System.out.println(objs[0]);
    		System.out.println(chars[0]);	
    	}
    }
    

  8. The contents of a reference variable are a bit pattern. [True or False]

    Answer:
    True.

  9. What does a reference variable contain when you assign another reference variable to it?
    Answer:
    If we assign an existing instance of an object to a new reference variable, then 2 reference variables will hold the same bit pattern.

  10. How do Strings differ from other objects in Java ?
    Answer:
    The rules for modifying Strings are the exception to modifying objects since both references will see the change. For any object type (with the exception of String) where 2 references refer to the same object, if either reference is used to modify the object, both references will see the change because there is still only a single object.
    A null value means the reference variable is not referring to any object on the heap.