LATEST UPDATES

Tuesday 12 April 2016

Java questions - part 10

1. How many objects are created in the following piece of code?
My class C1, C2, C3;
C1 = new Myclass ();
C2 = new Myclass ();

Exp:           Only 2 objects are created, C1 and C3. The reference C2 is only declared and not initialized.

2. Can a public class 'Myclass' be defined in a source file named 'Yourclass.java'?

Exp:           The source file name, if it contains a public class, must be the same as the public name itself with a  .java extension.

3. Can main() method be declared final?

Exp:           Yes, the main() method can be declared final, in addition to being public static.

4. What is HashMap and Map?

Exp:           Map is an interface and Hashmap is the class that implements Map.

5. Difference between HashMap and HashTable?

Exp:           The HashMap class is roughly equivalent to HashTable, except that it is unsynchronised and permits nulls. (HashMap allows null values as key and value whereas HashTable doesnot allow).
          HashMap does not guarantee that the order of the Map will remain constant over time. HashMap is unsynchronised and HashTable is synchronised.

6. Difference between Vector and ArrayList?

Exp:           Vector is synchronised whereas ArrayList is not.

7. Difference between Swing and Awt?

Exp:           Awt are heavy-weight components. Swings are light-weight components. Hence Swing works faster than Awt.

8. What will be the default values of all the elements of an array defined as an instance variable?

Exp:           If the array is an array of primitive types, then all the elements of the array will be initialised to the default value corresponding to that primitive type.           Example: All the elements of an array of Int will be initialised to 0 (zero), while that of booleab type will be initialised to false. Whereas if the array is an array of reference (of any type), all the elements will be initialised to null. 

Designed By Blogger Templates