Questions

Un article de Sometimes Kitties Think Too.

SCJP


Random Questions

****************
 **************
  ************
   **********
    ********
     ******
      ****
       **


1. Can "protected" be used to modify a class?

       No, protected can only be used for constructors, inner classes, methods and field variables

2. What's the difference between public and protected

       Protected - had default access, meaning anything in the package can access it as well as classes that extend the class in which the (method/variable,etc) are declared (even if it's outside of the package).
       Public doesn't have the last restriction, meaning anything can call it.


3. What is Covariant?

       Only allowed since 1.5
       A return type that is the subclass of the return type of the method with the same signature in
       the superclass.  You can override a superclass method and still change the return type...in 1.5


4. What is a RuntimeException? How is different from other exceptions?

       http://www.javaworld.com/javaworld/javaqa/2002-02/01-qa-0208-exceptional.html
       RuntimeException extends from RuntimeException or Error.  Some examples: ArrayIndexOutofBounds, NullExceptionException
               + A caller to a method that throws a RE doesn't need to catch it
               + a method signature doesn't need to declare the exception
       RE are in contrast to checked exceptions.  All classes in the Exception hierarchy other than those in the RuntimeException sub-hierarchy are checkedc exceptions.  Checked exceptions must either catch or throw.
       e.g.
       public class ExceptionalClass {
       public void method1() throws CheckedException {
               // ... some code
               throw new CheckedException( "something bad happened" );
       }
           public void method2( String arg ) {
               if( arg == null ) {
                   throw new NullPointerException( "arg passed to method2 is null" );
       }
       }
       public void method3() throws CheckedException {
           method1();
       }
       }


       --> method2() throws an RE


5. How can enums be accessed?

       (1) if declared outside a class: <enumName>.<constant>
       (2) if encapsulated in enclosing class:  <enclosingClassName>.<enumName>.<constant>
       + enum is implemented as a class type with restricted values.
       + enums can be declared
               1) outside classes (same access modifiers as a non-inner class: public, default) in same class file
               2) in their own class file
               3) Enclosed in another class
       + remember, SEMI-COLON in declaration is OPTIONAL
       + enum values are considered "constants" (static and final)
       + can contain constructors, methods, variables and constant specific class body

6. What does "static methods cannot be overridden, only re-defined" mean?

       Instance methods (non-static) in base classes can be overridden in derived classes.
       This means that if the run-time type of an object is the derived class, then even if the object is being referred to with a base class reference type, the overidden method will be called.
       If there is a static method in the derived class that has the same signature as one in the base class, then the type of the reference you use to refer to the object determines which version of the method is called.

7. Do reference variables ever live on the heap?

       Yes, if they're contained in an object

8. How many objects are eligible for collection per class?

       Depends on how many instance variables are defined, (each of these adds one more to the total)

9. Do shorts need to be cast?

       They are automatically widened (implicitly cast) when assigned to ints.  However, a short
       that takes an int needs to downcast the arg (narrow).

10. What's the difference between explicit and implicit casting?

       Explicity casting syntax: ( <Class> ) arg.  E.g.  (short) 7
       Implicit casting is when arguments are automatically widened by the compiler.


11. Are ALL local variables final?

       - No.  Anonymous inner classes can only access final variables from outside class.

12. When should exceptions be declared in the class declaration as opposed to the method declaration?

       - A class cannot declare that it throws an exception

13. Can constructors throw exceptions?

       - Yes.

14. What is the relationship of final variables in cast statements?

       Case statements's switch labels can only be finals or constants

15. Remember, it is illegal to have more that one case statement using the same value.

16. What is the difference between overriding and overloading? When do which methods get called?

       Overriding occurs when a subclass takes the same parameters and has the same (or covariant) return type as the super class.
       Overloading requires that at least the parameters be different than the super's method.
       for non-static methods, the method executed will be dictated by the object type.
       for static methods, the method executed is determined by the reference type.  (See No.6)


17. When to '==' work for primatives?

       + '==' is the only way to compare primatives
       + For objects '==' compares reference values
       + for Wraper objects, equality is true if the auto-boxed primatives are equal within the range
               of the literal pool


18) Does the primative array get reassigned? how does it grow

       Primative arrays cannot grow. But the reference can be re-assigned to a larger array.

19) What needs to implement Comparable and what needs to implement Comparator?

       If a Collection is to be sorted, it can implement either the Comparable or Comparator interface.
       The following methods need to be overridden in each case:
       Comparable --> compareTo(T o)     void sort(List<T> list) where <T extends Comparable<? super T>>
       Comparator --> compare(T o1, T o2)  sort( E e, Comparator c)
       Comparator interface is used more for 3rd party classes.  Can produce many sorted sequences.
       Comparable is used more by Wrapper classes.


20) Why does method(ArrayList<? super Cat> error out when supplied with Mammal (which is higher than Cat)?

       <? super E> means that only objects of equal or lesser inheritance can be added

21) Why does compiler handle int[] and int... the same in method signature?

22) Remember that anonymous classes SUBCLASS original class and so inherit methods!

23) Why would a method innner class be abstract?

24) What are the default arguments for wrapper class constructors?

25) Queues and polling and concurrent modification

26) Can you add IS-A elements to a Collection?

27) How does the printf() and format() methods work?

       for %b - uses String.valueOf(boolean)