Card Set Information

2013-08-20 18:48:16

Show Answers:

  1. 1. What access control levels do classes have and what does class access mean?
    Default or public. A can instantiate, extend, access members of B
  2. 2. What implicit access modifiers do interfaces have?
    Implicit abstract class, implicit public static final instance vars, implicit public abstract methods
  3. 3. How many class declaration per file?
    1 public, >1 default
  4. 4. What access controls levels do members have and what does member access mean?
    Public, protected, default, private. A's method can access B's members, subclass can inherit parent member
  5. 5. Can subclass see parent's private members?
    No, private members not inherited. Defining same method name is not overriding.
  6. 6. Default vs protected members
    Protected: same package can access by ref (ref.member) or by inheritance (this.parentMember), diff package access by inheritance not ref, same package cannot access by ref an already inherited.
  7. Default: same package can access by inherit or ref
  8. 7. What modifiers can be applied to local vars inside methods?
    Never access modifiers. Only "final"
  9. 8. What rule relates abstract classes to their methods and implementations?
    Abstract class can have 0 or more abstract methods
  10. 1 or more abstract methods always require class to me abstract
  11. First concrete class must implement all outstanding abstract methods
  12. 9. Can abstract classes be instantiate?
  13. 10. Do I need to import from same package?
  14. 11. Abstract final
    • Abstract static
    • Abstract private
    • All illegal
  15. 12. How can synchronized by used?
    Only on methods or code blocks, can combine with any access modifiers or final
  16. 13. What is native modifier and how is it used?
    Only on methods, implemented in platform depended code, semicolon to omit
  17. 14. What is strictfp modifier and how is it used?
    Only on classes and methods
  18. 15. What are the rules for var args?
    • Last in parameters
    • Only one per method
  19. 16. What are the rules for constructors?
    • No return type
    • Any access modifier
    • Can't be static, final, abstract
  20. 17. What are numerical primitives' ranges in bits?
    Byte 8, short 16, int 32, long 64, float 32, double 64
  21. 18. What is char primitive type?
    16 bit Unicode. Can assign to number type bigger than short.
  22. 19. What modifiers can be used on instance variables?
    3 access, final, transient, volatile
  23. NOT abstract, synchronized, strictfp, native, static
  24. 20. Do local or instance variables have default values upon declaration but before instantiation?
    • Instance have default
    • Local doesn't
  25. 21. What is shadowing and what use case does it have?
    Local var name same as instance var name.
  26. Commonly used by method parameters
  27. 22. Explain arrays in java
    Array is always object on heap. Can only hold same or subclasses of same types. Can't expand/contract dynamically (use java collections). Declare, construct, initialize. JVM doesn't allocate space until initialization.
  28. 23. What does final modifier mean for class, method, variable?
    • Can't be subclassed
    • Can't be overridden
    • Can't have new value/reference
  29. 24. Explain transient, volatile
    • Skip serialization
    • Private copy for each thread
  30. Both modifiers can only be used on instance vars
  31. 25. Explain enum declaration rules
    Declared at file level can only have default or public access
  32. Declare inside class. Semicolon optional if no more enum declaration code follows.
  33. Cannot declare inside methods
  34. 26. What are constant specific class body?
    Specific constant overrides enum method.
  35. 27. What are java rules for extending and implementing interfaces?
    Interface can extend multiple interfaces. Concrete class that implements interface must implement all in inheritance tree. Abstract class doesn't have to. Interface can extend multiple interfaces.
  36. 28. Do static methods have direct access to non static vars or methods?
    No, need to initialize a reference to the object first. Otherwise compile error. Static methods don't know about the current instance.
  37. 29. What does MyEnums.values() return?
    An array of MyEnum's values
  38. 30. What is autoboxing?
  39. 31. What methods do all java classes inherit?
    Object's equals(), clone(), notify(), wait()... Etc
  40. 32. What method can you call and what exception can be thrown using a polymorphic supertype reference?
    Compiler can invoke method of declared reference, not specific method in actual object.
  41. Compiler allows checked exceptions thrown if declared by supertype.
  42. 33. Explain IS-A vs HAS-A
    Extend, implement, passes typeof test, polymorphic
  43. Has reference to another object
  44. 34. What can ref var reference if declared as class type vs as interface type?
    Class type can ref all subclasses
  45. Interface type can ref any class that implements the interface
  46. 35. How does compiler vs JVM differ in what they know about polymorphic method invocations?
    Compiler knows inheritance so can invoke method available as per ref var.
  47. JVM knows real object and invokes overridden method down inheritance tree.
  48. Polymorphic method invocation only applies to instance methods, not static methods or vars.
  49. 36. What are overriding rules of subclass?
    • Same argument list as superclass.
    • Same or subtype return type as superclass.
    • Access level can be same or less restrictive than superclass.
    • Same package can override non-private non-final non-static.
    • Different package can override non-final non-static public or protected.
    • Overriding method can throw any unchecked runtime exceptions regardless if declared by overridden.
    • Overriding method cannot throw checked exceptions broader than exceptions declared in overridden.
    • Overriding method can throw narrower fewer exceptions than overridden.
  50. 37. What are overloading rules?
    • Overloaded methods must change arg list.
    • Overloaded methods can change return type.
    • Overloaded methods can change access modifier.
    • Overloaded methods can declare new or broader checked exceptions.
    • Method can be overloaded in same or different class (note the inherited methods first).
  51. 38. Explain overridden vs overloaded invocations at compile and run time.
    • Overridden determined at runtime based on object type.
    • Overloaded determined at compile time based on arg ref type.
  52. 39. Explain what is allowed in down casting and up casting.
    Down casting must pass instanceof otherwise compiles fine but throws ClassCastException.
  53. Upcasting is implicitly always legal.
  54. Compiler only knows about inheritance, so will throw inconvertible type error if not same inheritance casting.
  55. 40. Is A implements X and B extends A then is B also X?
    Yes even without B specifically saying it implements X. If A is concrete class then B doesn't need to actually implement anything in X.
  56. 41. What are rules for concrete class implementing interface?
    • Provide concrete implementations of all methods in interface.
    • Follow rules for legal overrides.
    • Declare no checked exceptions other than those declared by interface method or subclasses of those declared.
  57. 42. Can implementing class be abstract?
    Yes, no need to provide concrete implementations for all methods.
  58. 43. Explain interface extend or implement interfaces?
    Class can implement multiple interface. Interface can extend multiple interfaces but not implement interfaces.
  59. 44. How would you write a class that extends a class and implements an interface?
    Extend always comes before implements.
  60. 45. What are covariant return types?
    Returning subtype. Java 1.4 flags will complain about incompatible return types but allowed in 1.5+
  61. 46. What are 6 rules for returning value?
    • Can return null for object return type.
    • Can return array.
    • Can return implicitly casted primitives.
    • Can return explicitly casted object.
    • Can return implicitly casted objects.
    • Can just "return" early out of void functions only.
  62. 47. Can char be implicitly casted into int?
  63. 48. What are rules of constructor?
    Any access modifier.
  64. No return type.
  65. If no constructor defined, compiler generates default no-arg constructor matching class access modifier.
  66. If you define any constructor, compiler won't generate default.
  67. Compiler auto inserts super() in first line of all constructors.
  68. Cannot call any instance method or access instance var until super constructor completes.
  69. Only static vars and methods can be accessed as part to super() or this() call.
  70. Can only directly call constructor within another constructor, never from within a method.
  71. 49. How can private constructor allow an instance of the the class to be used?
    Class must provide static method or var that allows access to an instance created from within the class.
  72. 50. Can method have same name as constructors?
  73. 51. Do abstract classes or interfaces have default constructors?
    Abstract yes, called when concrete instantiated. Interface no.
  74. 52. Is interface part of objects inheritance tree?
  75. 53. Will compiler insert default super() if superclass only has overloaded constructor with args?
    No need to manually insert with args matching superclass
  76. 54. Are constructors inherited and do subclass default constructor depend on superclass definition of constructor?
    No, constructors can't be overridden only instance methods can be.
  77. 55. How does compiler insert this() and super() into constructors?
    Cannot have both. Won't insert super() if this() exists. Never inserts this(). First line must be super() or this() in all constructors.
  78. 56. Do static class var need to be initialized?
    No they get default values like instance vars.
  79. 57. Do static methods have direct access to static vars and methods in the same class?
    Yes, ClassName.staticMethod or this.staticVar is implicit. Can access static vars and methods directly.
  80. Alternatively, can also create ref to new instance of object and .staticMethod
  81. 58. Since static methods don't need an instance of the class, can they exist and be invoked from abstract class?
  82. 59. Can static methods be overridden?
    No never. Polymorphism wont work on static redefinitions that are not overrides.
  83. 60. What is OO design for coupling and cohesion and what are their goals?
    • Loose coupling, high cohesion.
    • Easy to create/resuse, easy to maintain, easy to enhance.
  84. 61. Is B extends A, can we cast
  85. B b = (B) new A()
    No never! A IS NOT a B, A is not instance of B!
  86. 62. If B extends A, and they have same instance var declaration and same instance method declaration, what is called by
    • A var = new B();
    • var.instanceVar
    • var.instanceMethod
    • Calls A's instance var and B's instance method.
  87. 63. Are primitive types also instanceof Object?
    Yes an int can be boxed in an Integer object and "widened" into an Object.
  88. 64. If more than one method signature matches what you are trying to call, what is chosen first by compiler?
    • 1. Perfect match
    • 2. Smallest number of args that are wider than params (implicit upcast for both primitive or objects)
    • 3. Smallest number of args that are implicitly autoboxed into params
    • 4. Var args last resort chosen last
  89. 65. What goes on the stack and what goes on the heap?
    Instance var and objects on heap. Local vars and methods go on stack.
  90. 66. What happens to stack when a method completes execution?
    Method and its local vars removed from stack, but object those local vars refer to may still be on heap.
  91. 67. How many ways to represent an int primitive literal?
    Octal: prefixed by 0, up to 21 digits excluding leading 0.
  92. Hex: 0-f case insensitive, prefixed by 0x, up to 16 digits excluding leading 0X
  93. Decimal: System.out.println prints in decimal
  94. 68. How many ways to represent long primitive literal?
    Same oct hex dec suffixes by l/L
  95. 69. How many ways to represent floating point primitive literal?
    Same dec/oct/hex dot dec/oct/hex
  96. Default is 64bit double primitive type. Suffix d/D optional.
  97. float declarations require mandatory f/F suffix, otherwise loss of precision compile error.
  98. 70. What are possible values of boolean and char primitive literals?
    boolean: true/false, 0/1 in ifs will throw compile error
  99. char: single character or unicode (prefix with \u) or escaped code with single quotes, positive 16-bit (0-65535) number, out of range needs a cast otherwise compile error
  100. 71. What does expression if int or smaller produce?
    Always an int type. Will require explicit cast if to be assigned to be smaller-than-int primitive type, otherwise compile error.
  101. 72. What are primitive type are all integer literals by default? What primitive type are all floating point literals by default?
  102. double
  103. 73. When does compiler automagically implicitly narrow cast?
    byte b = 27; ?????
  104. Usually widening cast is implicit and narrowing cast must be explicit.
  105. 74. What happens to bits during explicit narrowing cast?
    Int drops everything after decimal point. Drops bits of left. Leading 1 makes it negative.
  106. 75. How does compound assignment operators affect casting?
    Puts implicit cast +=, /= etc
  107. 76. What's inside variable?
    Bit holders, primitive holds value, ref var holds memory address.
  108. 77. How many different scopes and who has longest/shortest? What happens when try to access out of scope?
    Static var longest, instance var, local var, block var shortest.
  109. Compile error: cannot find symbol
  110. 78. What are default values of instance vars?
    • Object = null
    • byte, short, int, long = 0
    • float, double = 0
    • boolean = false
    • char = '\u0000'
  111. 79. Do arrays have default values? If so what type of var must they be?
    Since all arrays are object, instance var default to null upon declaration, local var defaults to nothing. In bltj cases, upon initialization, all elements get default values.
  112. 80. When will compilation fail due to uninitialized local var
    Using it uninitialized cuz no default values (both local primitives and local ref vars will not have default values).
  113. Using it when initialization happens in conditional block!!!
  114. 81. Explain difference between string object ref var vs regular object ref var?
    String pool, immutable, creates new string or finds matching in pool when trying to change it
  115. 82. Does java pass by reference?
    No, pass by copy of variable value running on single VM
  116. 83. If code tries to access out of bound index in array, where does it fail?
    Runtime ArrayIndexOutOfBoundsExceotion
  117. 84. What is an anonymous array?
    Just in time, must not specify size, no need to assign to ref var.
  118. new int[ ] {1,2,3}
  119. 85. What are legal array element assignments or initializations?
    Subclass of object type declared by array reference.
  120. Instance of any class that implements interface type declared by array ref.
  121. Primitives that can be promoted/casted/widened implicitly to declared array type.
  122. 86. What can array ref var assignment refer to?
    For primitives, can refer to another array of same primitive type and dimension, any size.
  123. For objects, can refer to another array of same object types or subclass of object, or refer to any array of any class that implements given index.
  124. 87. What are init blocks and what sequence of execution do they appear wrt constructor and methods?
    • Init blocks execute in order
    • Static init blocks first
    • Instance init blocks run after super()
  125. 88. What objects are immutable?
    Strings. Wrapper objects. These classes are marker final.
  126. 89. How do you convert wrapper to primitive?
    primitive xxxValue()
  127. 90. How do you convert strings to primitive?
    static primitive parseXxx(String) throws NFE
  128. 91. How do you convert string to wrapper?
    • static Wrapper valueOf(String) throws NFE
    • static Wrapper valueOf(String, int radix) throws NFE //only Long and Integer
    • new Wrapper(String) //except new Character(char)
  129. 92. How do you convert primitive to wrapper?
    new Wrapper(primitive)
  130. 93. How do you convert wrapper to string?
    • String toString()
    • static String toString(primitive)
    • static String toString(primitive, int radix) //only for Long and Integer classes
  131. 94. What is difference between equals() and ==
    == means same reference OR same primitive value for 2 Booleans, Bytes, Character (\u0000 to\u007f) Short and Integer (-128 to 127)
  132. Wrapper's equals() means same type and same value.
  133. 95. What can compiler achieve box-then-widen or widen-then-box?
    Can box then widen. Knows what to box into first. Widen follows inheritance for objects. Throws compile error if widen then box.
  134. 96. Can you widen a wrapper type?
    No fails instanceof test. They are peers.
  135. 97. What is finalize() method?
    Every object inherit it from Object class. Runs once and only once before GC but GC not always guaranteed to run. Explicitly calling or overriding finalize may intelligiblize GC for some objects.
  136. 98. When are objects eligible for GC?
    Inaccessible from any live thread.
  137. 99. Do static or class variables get default values?