1. Following are the three top-level declarations for Java source file. -package declaration -import declarations -class / Interface definitions None of these elements are mandatory but must be in the order given above, if present. 2. In Java, a valid declaration of the main() method must be public and static, having void as the return type and take a single array of String objects as arguments. The order of static and public is not relevant. Also declaring method as final does not affect the method's ability to use as main() method. 3. It is ok to have no public class in a java source file as long as there is no attempt to execute the class file. i.e there is no call to the main() method. 4. A compile-time error occurs if an attempt is made to create an instance of an abstract class. 5. If a class that is not declared abstract contains an abstract method, then a compile-time error occurs. 6. An abstract class can define non-abstract methods. 7. Any class that is a subclass of abstract class must implement the abstract methods of the superclass or else declare them as abstract itself. Such a subclass then cannot be instantiated. 8. A subclass can be declared abstract regardless of whether superclass is declared abstract. 9. It is a compile-time error for a static method to be declared abstract because, when you declare a method as abstract, you agree to implement that method in the subclasses by overriding it. On the contrary static methods cannot be overridden (they can only be hidden in the sub classes). Thus static and abstract combination leads to a potentially contradictory situation and hence the compiler tags this as an error. By the same token, static methods cannot be defined in abstract class. 10. Method overloading allows a method with the same name but different parameter types and parameter order (together called method signature) to have different implementations. 11. Changing just the return type or the exceptions thrown, in the implementation is not enough to overload a method and is flagged as compile time error. 12. A constructor in Java is declared like a method, except that the name is identical to the class name and it does not have any return type. 13. If you don't specify an initial value to instance and static variables, a default value will be assigned automatically when the object is created. Fields that are of numeric type are initialized with zero, fields of type char will be initialized with 'u\000', and fields that store class references or references to arrays will be initialized with null. Local variables remain uninitialized unless explicitly initialized. 14. Constructors are not inherited in the subclasses. 15. Constructors can be overloaded. 16. If you do not provide a class with at least one constructor definition, java will provide a default "no-args" constructor for the class. 17. In Java , when an object is created, initialization is done in this order: (1) Set fields to default initial values (0, false, null) (2) Call the constructor for the object (but don't execute the body of the constructor yet) (3) Invoke the superclass's constructor (4) Initialize fields using initializers and initialization blocks (5) Execute the body of the constructor 18. Constructors can be declared public, protected or private but not abstract, static, synchronized, final or native 19. this() and super() calls can only be used in a constructor definition and must appear as a first statement. Remember that super() cannot be followed by this() and vice versa, which means they cannot be combined in the same constructor definition. 20. If first statement of your constructor is not one of the following super(), super(args), this() or this(args) then super() call is supplied by java compiler. 21. Comments in Java do not nest. 22. A compile-time error occurs if a class has the same simple name as any of its enclosing classes or interfaces. 23. A top level class cannot be private or protected. 24. The visibility of the class is not limited by the visibility of its members. i.e A class will all the members declared private can still be declared public. 25. A compile-time error occurs if the same modifier appears more than once in a class declaration. 26. It is a compile-time error if a class depends on itself. i.e 27. class Point extends ColoredPoint { int x, y; } class ColoredPoint extends Point { int color; } causes a compile-time error. 28. It is a compile time error when you attempt to make a static reference to a non-static method. Most common mistake happens in main() method. 29. A compile-time error occurs if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses. 30. A compile-time error occurs if a class is declared both final and abstract, because the implementation of such a class could never be completed 31. Because a final class never has any subclasses, the methods of a final class are never overridden. 32. A final variable need not be initialized at its declaration, but it must be initialized once before it is used. These variables are also known as blank final variables. 33. Java does not guarantee that any particular object will be garbage collected, but make promise to call the finalize on an object before reclaiming the memory it occupies. 34. There is no guarantee what so ever as to when the finalize method is called. 35. There is no specific order in which the finalize method will be called. 36. Overriding finalize method in your class does not prioritize your object for garbage collection over any other object which do not have finalize method overridden in it's class. 37. Any exception thrown by the finalize method causes the finalization of that object to be stopped, but is not notified to the application and that object is still considered as finalized. 38. finalize can run only once on the object in it's entire life cycle. 39. finalize method defined in Object class, as such does nothing. In order to get some meaningful thing done , you will have to override this method in the subclass. 40. Unlike constructors, which are chained from the subclass to superclass, finalizers are not implicitly chained. It is considered a good programming practice if you call the superclass finalizer like super.finalize() in your subclass. 41. By default garbage collector will not execute the finalizers of any objects left on heap when the application exists. 42. The first character of an identifier cannot be a digit. 43. Identifiers can be of any length in Java. 44. Static inner class (also called as nested class) behave much like top-level class (package level class) except that they are defined within the scope of another class, called the enclosing class. 45. Static inner classes can access only static members of the enclosing class. 46. Non-static inner classes are defined within the scope of an enclosing class. In other words inner classes need an instance of the enclosing class when they are created. EnclosingClass.new syntax is used to create an instance of inner class. To refer to the instance of the enclosing class from within an inner class you use the EnclosingClass.this syntax. 47. Inner classes have full access to the members of the enclosing class, so they can access private members as well as public and protected . 48. Non-static Inner classes may not declare static members, unless they are compile-time constant fields (i.e static final variables) 49. Local inner classes, like local variables, can not be declared public, protected, private, or static 50. Local inner classes can not have static members and they can not have the same name as the enclosing class in which they are defined. 51. Local and anonymous inner classes can access only final local variables. 52. Anonymous inner classes can not have a constructor. 53. Packages in Java are groups of related classes. These are similar to libraries in many computer languages. For example, you can imagine a package called Commute, which would have numerous classes defined in it such as Car, Boat, Airplane, Train, AmphibiousCar and so on. Applications that deal with items of this sort can benefit from importing the imaginary Commute package. 54. A compile-time error occurs if the same interface is mentioned two or more times in a single implements clause. This is true even if the interface is named in different ways; for example, the code: class Redundant implements java.lang.Cloneable, Cloneable { int x; } results in a compile-time error because the names java.lang.Cloneable and Cloneable refer to the same interface. 55. A class may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any of the fields by its simple name results in a compile-time error In the example from JLS: interface Colors { int WHITE = 0, BLACK = 1; } interface Separates { int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3; } class Test implements Colors, Separates { public static void main(String[] args) { System.out.println(BLACK); } } The name BLACK in the method main is ambiguous, because class Test has two members named BLACK, one inherited from Colors and one from Separates. 56. Interfaces are always implicitly static so they are never considered to be inner. 57. All methods in an interface are automatically public. However while implementing the interface, you must declare the method as public. Otherwise compiler will assume that the method as default (package) visibility and will complain that you try to supply a weaker access privilege. 58. A compile-time error occurs if an interface has the same simple name as any of its enclosing classes or interfaces. 59. While every class is an extension of class Object, there is no single interface of which all interfaces are extensions. 60. Keywords are reserved words, and is a compile time error if they are used as identifiers in the program. 61. The switch statement transfers control to one of several statements depending on the value of an expression. The type of the expression must be char, byte, short, or int, or a compile-time error occurs. 62. No two of the case constant expressions associated with a switch statement may have the same value. 63. At most one default label may be associated with the same switch statement. 64. If no case matches and there is no default label, then no further action is taken and the switch statement completes normally. 65. break keyword is not mandatory in switch statement. 66. Java permits nesting of switch statements. The inner switch statement in this case can redefine case labels but cannot redeclare variables. 67. A static field, sometimes called a class variable, is incarnated when the class is initialized. 68. If a field is declared static , there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. 69. A static method is always invoked without reference to a particular object. An attempt to reference the current object using the keyword this or the keyword super in the body of a static method results in a compile-time error. 70. Static methods cannot be overridden in the subclass, but by declaring static method in a subclass with the same signature as the static method in superclass will hide the implementation of static method in the superclass . 71. A native method is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN, or assembly language. 72. The body of a native method is given as a semicolon only, indicating that the implementation is omitted native MethodA(); and not native MethodA() {}; 73. A compile-time error occurs if a native method is declared abstract 74. All instances of Wrapper classes are immutable. 75. Wrapper classes are declared final and cannot have subclasses. They override equals() method . The static valueOf(String) methods in primitive wrapper classes return wrapper objects and not primitives. 76. Java supports four levels of accessibility (visibility). In order of most accessible to least accessible, they are: 1. public : entire world has access to it. 2. protected : accessible in this package and also in subclass in other package. Remember that non subclasses in other package do not have access to protected members. 3. default : package access. 4. private : accessible only in the class. 77. A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array. e.g int[] intArray; or int intArray[]; 78. You can create an array object with new or an explicit initializer: intArray = new int[10]; intArray = new int[] { 1,2,3,6,8 }; int[] intArray = { 1,2,3 ); 79. Note that you can't create an array object with following declaration int intArray[30]; 80. In Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NULL character). 81. In Java, Exceptions identify errors that arise in your program. Exceptions are objects of subclasses of the class Throwable. 82. All subclasses of java.lang.Error class and RuntimeException class are called "Unchecked Exceptions". These are not required to be caught in your code. "Unchecked Exceptions" are usually fatal or irrecoverable. All other exceptions must be accounted for in your code and are termed as "Checked Exceptions" and the compiler will flag error if such errors are not caught. Java uses try-catch-finally construct to deal with exceptions, both "checked" and "unchecked". Even though it is not recommended to catch "unchecked" exceptions, the compiler will not object if you throw or catch them. Remember that the order of try-catch-finally must be maintained. 83. try block without atleast one catch block or finally block after it gives compile error. 84. It is a compile time error if in your code catch block for a superclass exception, shadows a catch block for subclass exception e.g catch (Exception e) { //... } catch(ArithmeticException er) { //... } 85. If a method throws exceptions that aren't caught, and aren't represented by subclasses of the class Error, or by subclasses of the class RuntimeException, then you must identify the exception classes in a throws clause in the method definition or must be dealt within calling method. 86. String objects are immutable. Following example will illustrate the concept. class MyClass { public static void main (String args[]) { String str1 = new String("JAVA"); str1.concat("CODE"); System.out.println(str1); } } 87. Above code will print "JAVA" and not "JAVA CODE" because String object (str1 in our example) cannot be changed once created. In other words the String object is immutable. This is a very important Certification Objective. 88. String class overrides equals() method defined in Object class. So that two instances of String class, having same contents in them are equal. 89. The placement, sizing and resizing of components is taken care by layout managers in Java. There are five main layout managers, namely FlowLayout, GridLayout, BorderLayout, CardLayout, GridBagLayout. All of these implement the LayoutManager interface. 90. One event source (typically components) can have multiple event listeners registered on it. Conversely, a single listener can register with multiple event sources. 91. There are six collection interfaces which are at the root of Collection Framework. Following is their association. Collection-----+ Set-----------+ SortedSet I I--------------+ List
Map--------+ SortedMap Set is unique and do not allow duplicate objects to be stored. Elements stored in the Set are unordered and unsorted. HashSet class directly implements Set interface
1 Comments:
1. Following are the three top-level declarations for Java source file.
-package declaration
-import declarations
-class / Interface definitions
None of these elements are mandatory but must be in the order given above, if present.
2. In Java, a valid declaration of the main() method must be public and static, having void as the return type and take a single array of String objects as arguments. The order of static and public is not relevant. Also declaring method as final does not affect the method's ability to use as main() method.
3. It is ok to have no public class in a java source file as long as there is no attempt to execute the class file. i.e there is no call to the main() method.
4. A compile-time error occurs if an attempt is made to create an instance of an abstract class.
5. If a class that is not declared abstract contains an abstract method, then a compile-time error occurs.
6. An abstract class can define non-abstract methods.
7. Any class that is a subclass of abstract class must implement the abstract methods of the superclass or else declare them as abstract itself. Such a subclass then cannot be instantiated.
8. A subclass can be declared abstract regardless of whether superclass is declared abstract.
9. It is a compile-time error for a static method to be declared abstract because, when you declare a method as abstract, you agree to implement that method in the subclasses by overriding it. On the contrary static methods cannot be overridden (they can only be hidden in the sub classes). Thus static and abstract combination leads to a potentially contradictory situation and hence the compiler tags this as an error. By the same token, static methods cannot be defined in abstract class.
10. Method overloading allows a method with the same name but different parameter types and parameter order (together called method signature) to have different implementations.
11. Changing just the return type or the exceptions thrown, in the implementation is not enough to overload a method and is flagged as compile time error.
12. A constructor in Java is declared like a method, except that the name is identical to the class name and it does not have any return type.
13. If you don't specify an initial value to instance and static variables, a default value will be assigned automatically when the object is created. Fields that are of numeric type are initialized with zero, fields of type char will be initialized with 'u\000', and fields that store class references or references to arrays will be initialized with null. Local variables remain uninitialized unless explicitly initialized.
14. Constructors are not inherited in the subclasses.
15. Constructors can be overloaded.
16. If you do not provide a class with at least one constructor definition, java will provide a default "no-args" constructor for the class.
17. In Java , when an object is created, initialization is done in this order:
(1) Set fields to default initial values (0, false, null)
(2) Call the constructor for the object (but don't execute the body of the constructor yet)
(3) Invoke the superclass's constructor
(4) Initialize fields using initializers and initialization blocks
(5) Execute the body of the constructor
18. Constructors can be declared public, protected or private but not abstract, static, synchronized, final or native
19. this() and super() calls can only be used in a constructor definition and must appear as a first statement. Remember that super() cannot be followed by this() and vice versa, which means they cannot be combined in the same constructor definition.
20. If first statement of your constructor is not one of the following super(), super(args), this() or this(args) then super() call is supplied by java compiler.
21. Comments in Java do not nest.
22. A compile-time error occurs if a class has the same simple name as any of its enclosing classes or interfaces.
23. A top level class cannot be private or protected.
24. The visibility of the class is not limited by the visibility of its members. i.e A class will all the members declared private can still be declared public.
25. A compile-time error occurs if the same modifier appears more than once in a class declaration.
26. It is a compile-time error if a class depends on itself. i.e
27. class Point extends ColoredPoint { int x, y; }
class ColoredPoint extends Point { int color; }
causes a compile-time error.
28. It is a compile time error when you attempt to make a static reference to a non-static method. Most common mistake happens in main() method.
29. A compile-time error occurs if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses.
30. A compile-time error occurs if a class is declared both final and abstract, because the implementation of such a class could never be completed
31. Because a final class never has any subclasses, the methods of a final class are never overridden.
32. A final variable need not be initialized at its declaration, but it must be initialized once before it is used. These variables are also known as blank final variables.
33. Java does not guarantee that any particular object will be garbage collected, but make promise to call the finalize on an object before reclaiming the memory it occupies.
34. There is no guarantee what so ever as to when the finalize method is called.
35. There is no specific order in which the finalize method will be called.
36. Overriding finalize method in your class does not prioritize your object for garbage collection over any other object which do not have finalize method overridden in it's class.
37. Any exception thrown by the finalize method causes the finalization
of that object to be stopped, but is not notified to the application and that object is still considered as finalized.
38. finalize can run only once on the object in it's entire life cycle.
39. finalize method defined in Object class, as such does nothing. In order to get some meaningful thing done , you will have to override this method in the subclass.
40. Unlike constructors, which are chained from the subclass to superclass, finalizers are not implicitly chained. It is considered a good programming practice if you call the superclass finalizer like super.finalize() in your subclass.
41. By default garbage collector will not execute the finalizers of any objects left on heap when the application exists.
42. The first character of an identifier cannot be a digit.
43. Identifiers can be of any length in Java.
44. Static inner class (also called as nested class) behave much like top-level class (package level class) except that they are defined within the scope of another class, called the enclosing class.
45. Static inner classes can access only static members of the enclosing class.
46. Non-static inner classes are defined within the scope of an enclosing class. In other words inner classes need an instance of the enclosing class when they are created. EnclosingClass.new syntax is used to create an instance of inner class. To refer to the instance of the
enclosing class from within an inner class you use the EnclosingClass.this syntax.
47. Inner classes have full access to the members of the enclosing class, so they can access private members as well as public and protected .
48. Non-static Inner classes may not declare static members, unless they are compile-time constant fields (i.e static final variables)
49. Local inner classes, like local variables, can not be declared public, protected, private, or static
50. Local inner classes can not have static members and they can not have the same name as the enclosing class in which they are defined.
51. Local and anonymous inner classes can access only final local variables.
52. Anonymous inner classes can not have a constructor.
53. Packages in Java are groups of related classes. These are similar to libraries in many computer languages. For example, you can imagine a package called Commute, which would have numerous classes defined in it such as Car, Boat, Airplane, Train, AmphibiousCar and so on. Applications that deal with items of this sort can benefit from importing the imaginary Commute package.
54. A compile-time error occurs if the same interface is mentioned two or more times in a single implements clause. This is true even if the interface is named in different ways; for example, the code:
class Redundant implements java.lang.Cloneable,
Cloneable
{
int x;
}
results in a compile-time error because the names java.lang.Cloneable and Cloneable refer to the same interface.
55. A class may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any of the fields by its simple name results in a compile-time error
In the example from JLS:
interface Colors
{
int WHITE = 0, BLACK = 1;
}
interface Separates
{
int CYAN = 0, MAGENTA = 1, YELLOW = 2,
BLACK = 3;
}
class Test implements Colors, Separates
{
public static void main(String[] args)
{
System.out.println(BLACK);
}
}
The name BLACK in the method main is ambiguous, because class Test has two members named BLACK, one inherited from Colors and one from Separates.
56. Interfaces are always implicitly static so they are never considered to be inner.
57. All methods in an interface are automatically public. However while
implementing the interface, you must declare the method as public. Otherwise compiler will assume that the method as default (package) visibility and will complain that you try to supply a weaker access privilege.
58. A compile-time error occurs if an interface has the same simple name as any of its enclosing classes or interfaces.
59. While every class is an extension of class Object, there is no single interface of which all interfaces are extensions.
60. Keywords are reserved words, and is a compile time error if they are used as identifiers in the program.
61. The switch statement transfers control to one of several statements depending on the value of an expression. The type of the expression must be char, byte, short, or int, or a compile-time error occurs.
62. No two of the case constant expressions associated with a switch statement may have the same value.
63. At most one default label may be associated with the same switch statement.
64. If no case matches and there is no default label, then no further action is taken and the switch statement completes normally.
65. break keyword is not mandatory in switch statement.
66. Java permits nesting of switch statements. The inner switch statement in this case can redefine case labels but cannot redeclare variables.
67. A static field, sometimes called a class variable, is incarnated when the class is initialized.
68. If a field is declared static , there exists exactly one incarnation of the
field, no matter how many instances (possibly zero) of the class may
eventually be created.
69. A static method is always invoked without reference to a particular object. An attempt to reference the current object using the keyword this or the keyword super in the body of a static method results in a compile-time error.
70. Static methods cannot be overridden in the subclass, but by declaring static method in a subclass with the same signature as the static method in superclass will hide the implementation of static method in the superclass .
71. A native method is implemented in platform-dependent code, typically written in another programming language such as C, C++, FORTRAN,
or assembly language.
72. The body of a native method is given as a semicolon only,
indicating that the implementation is omitted
native MethodA(); and not
native MethodA() {};
73. A compile-time error occurs if a native method is declared abstract
74. All instances of Wrapper classes are immutable.
75. Wrapper classes are declared final and cannot have subclasses. They override equals() method . The static valueOf(String) methods in primitive wrapper classes return wrapper objects and not primitives.
76. Java supports four levels of accessibility (visibility). In order of most accessible to least accessible, they are:
1. public : entire world has access to it.
2. protected : accessible in this package and also in subclass in other package. Remember that non subclasses in other package do not have access to protected members.
3. default : package access.
4. private : accessible only in the class.
77. A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array. e.g
int[] intArray; or int intArray[];
78. You can create an array object with new or an explicit initializer:
intArray = new int[10];
intArray = new int[] { 1,2,3,6,8 };
int[] intArray = { 1,2,3 );
79. Note that you can't create an array object with following declaration
int intArray[30];
80. In Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NULL character).
81. In Java, Exceptions identify errors that arise in your program. Exceptions are objects of subclasses of the class Throwable.
82. All subclasses of java.lang.Error class and RuntimeException class are called "Unchecked Exceptions". These are not required to be caught in your code. "Unchecked Exceptions" are usually fatal or irrecoverable. All other exceptions must be accounted for in your code and are termed as "Checked Exceptions" and the compiler will flag error if such errors are not caught. Java uses try-catch-finally construct to deal with exceptions, both "checked" and "unchecked". Even though it is not recommended to catch "unchecked" exceptions, the compiler will not object if you throw or catch them. Remember that the order of try-catch-finally must be maintained.
83. try block without atleast one catch block or finally block after it gives
compile error.
84. It is a compile time error if in your code catch block for a superclass
exception, shadows a catch block for subclass exception
e.g
catch (Exception e)
{
//...
}
catch(ArithmeticException er)
{
//...
}
85. If a method throws exceptions that aren't caught, and aren't represented by subclasses of the class Error, or by subclasses of the class RuntimeException, then you must identify the exception classes in a throws clause in the method definition or must be dealt within calling method.
86. String objects are immutable. Following example will illustrate the concept.
class MyClass
{
public static void main (String args[])
{
String str1 = new String("JAVA");
str1.concat("CODE");
System.out.println(str1);
}
}
87. Above code will print "JAVA" and not "JAVA CODE" because String object (str1 in our example) cannot be changed once created. In other words the String object is immutable. This is a very important Certification Objective.
88. String class overrides equals() method defined in Object class. So that two instances of String class, having same contents in them are equal.
89. The placement, sizing and resizing of components is taken care by layout managers in Java. There are five main layout managers, namely FlowLayout, GridLayout, BorderLayout, CardLayout, GridBagLayout.
All of these implement the LayoutManager interface.
90. One event source (typically components) can have multiple event listeners registered on it. Conversely, a single listener can register with multiple event sources.
91. There are six collection interfaces which are at the root of Collection
Framework. Following is their association.
Collection-----+ Set-----------+ SortedSet
I
I--------------+ List
Map--------+ SortedMap
Set is unique and do not allow duplicate objects to be stored. Elements
stored in the Set are unordered and unsorted. HashSet class directly
implements Set interface
11:49 PM
Post a Comment
<< Home