Subscribe to Java/SCJP        RSS Feed

SCJP Notes - Part 5

Icon Leave Comment
1. When a primitive is passed to a method, it is passed by value. When an object is passed to a method, it is passed by reference.

2. InputStream/OutputStream class hierarchy allows for bytes to be read and written from files. Reader/Writer class hierarchy allows for characters to be read and written from files.

3. I/O filter reads from one stream and writes to another stream after altering the data.

4. ObjectInputStream allows reading of objects from streams.

5. read() returns –1 and readLine() returns null upon reaching the end of file.

6. Externalizable interface allows classes to control the way in which their objects are serialized. Externalizable extends Serializable.

7. When a file object is created, no file/directory is created in the local file system.

8. To create a reader object from InputStream object, the InputStreamReader class is used.

9. The labels must adhere to the rules for a valid variable name and should also adhere to java naming convention.

10. If labeled continue is used and label doesn’t refer to a loop, compile time error is flashed.

11. The exception handlers for most specific exceptions must be placed above those for more general exceptions.

12. When an object of type Exception or of type Throwable is thrown, it must be caught. These objects are checked exceptions.

13. Each method must either catch or list each unhandled checked exception. This is referred to as Java’s catch or specify requirement.

14. Objects of RuntimeException, Error and their subtypes are unchecked exceptions and unchecked exceptions may not be specified or handled.

15. The overridden method can throw exception types that are subclasses of those thrown by the base method. Also, the overridden method may throw the same exceptions as the overridden method or no exceptions at all.

16. If you prefer to have the stack trace indicate that the catch clause from which the object was re-thrown is the original location of exception, use the fillInStackTrace() method. This hides the location where the exception object was created.

17. System.runFinalizers() asks that the finalizers be executed but that memory not necessarily be freed.

18. The mark-sweep algorithm gets its name from the two passes it takes over memory. It takes the first pass to mark those objects that are no longer used and a second pass removes (sweeps) those objects from memory.

19. The total time that a garbage collection system takes to manage memory is more than it takes to manage memory by hand. This is because hand-written memory management will rarely wait until memory is exhausted. It is the action of deleting whole bunch of objects at once that causes the system to pause.

20. Runtime class is a singleton class and is used to communicate with the virtual machine.

21. The finalize() method returns void and takes no arguments. Objects that have their finalizers run but not their memory deallocated are known as phantoms.

22. In Java, two types of programs that can be created are Applications and applets.

23. Java was developed by James Gosling.

24. The problem of name collision is common when using multiple inheritance. This occurs when same member is inherited from a number of parents and which to choose becomes a problem.

25. System.arraycopy(Object src, int srcIndex, Object dest, int destIndex, int length) should be used to efficiently copy the arrays.

26. Java does not support multidimensional arrays. But it supports arrays of arrays.

27. char data[] = {‘a’,’b’,’c’}; String str = new String(data); is valid

28. String concatenation is implemented through StringBuffer class and its append method.

29. trim() method removes white spaces from both ends.

30. When there is an overlapping of characteristics between an office room and building, the relationship is has a and not is a.

31. An overridden method and the method in superclass may declare their methods synchronized independently of each other.

32. Return types, visibility, thrown exceptions and keywords play no part in determining valid overloaded methods.

33. Java always chooses overloaded methods with the closest matching parameter list.

34. It is the method calls themselves that determine overloaded method ambiguity.

35. The parseXxX() methods are also static like valueOf() methods but the diference is that former returns a primitive value and latter returns a wrapper object.

Double d = null;
		System.out.println((d)); will print null and

		Double d = null;
		System.out.println(Double.toString(d)); will throw null pointer exception.

37. The wrapper classes override the toString() method.

38. A value should not be compared directly with Float.NaN or Double.NaN but instead the isNaN method be used.298. If two objects are equal according to equals() method then invoking hashcode() on the objects should return the same int value.

39. The equals() methods in wrapper classes check for type and the wrapped value of the objects in question.

40. The six core interfaces of collections framework are 1) collection 2) map 3) set 4) list 5) sortedmap 6) sortedset

41. ArrayList and Vector used array in their internal representation and LinkedList uses doubly linked list.

42. The advantage of ArrayList over Array is that it can dynamically change its size. But the advantage is that searching a particular element becomes time consuming job.

43. The major difference between Vector and ArrayList classes is that the methods of Vector class are synchronized and hence suits the applications requiring thread safety. Due to synchronized methods, operations on Vectors are more time consuming than ArrayList.

44. The linkedList improves the insertion and deletion in the middle but doesn’t provide the constant time for accessing an element at particular location.

45. HashSet stores unique elements only and uses hashtable data structure for storing the elements which uses the hashcode function. The order is not guaranteed.

46. LinkedHashSet extends HashSet and uses doubly LinkedList internally to maintain order of elements.

47. A TreeSet maintains the natural order of elements and it implements SortedSet interface to maintain uniqueness and also uses an instance of TreeMap to use the tree data structure. Even if an element is tried to be inserted twice, it will be inserted only once. The natural order is determined by using the Comparable or Comparator interfaces implemented by the elements of collection.

48. The comparator interface has int compare(Object obj1,Object obj2) and Comparable interface has int compareTo(Object o) which can be overridden to provide user defined comparison.

49. HashMap is an unordered Map and uses the hashtable data structure.

50. Hashtable class’s name is Hashtable and not HashTable. Hashtable’s methods are synchronized and it doen’t allow nulls as key or as values.

51. A TreeMap implements SortedMap interface and uses the tree data structure and hence maintains the natural order of elements. Like TreeSet, TreeMap also maintains the natural order. null can’t be used as key in TreeMap.

52. LinkesHashMap extends HashMap and maintains the order by using linkedlist. The ordering can be discarded by using the constructor of LinkedHashMap class. It maintains insertion as well as access order.

53. The size() method returns the number of elements in the collection. Only in case of hashtable based data structure is the capacity increased beforehand.

54. The collection implementation providing a one argument constructor accepting Collection as it argument is only a convention.

55. The implementation which want some method to optional simply throw UnsupportedOperationException in that method.

56. Is a object of HashMap an instanceOf Collection? No because Map interface doesn’t extend Collection interface. Is a object of HashSet an instance of Collection? Yes Indirect superclass/superinterfaces also result in true for instanceOf operator.

57. All kinds of Arrays are objects so a test on instanceOf operator with the Object class and an Array always returns true.

58. Collections class provides sorting on the lists via Collections.sort(List lst) method.
Searching can be done on a list by using the method public static int binarySearch(List list, Object key)

59. Collections class also provides thread safe collections and immutable collections which when tried to be modified, throw UnsupportedOperationException.

60. The Arrays class also supplies utility methods like sort, binarySearch, asList. The asList() of Arrays class and toArray() of Collection interface are complementary to each other.

61. If there are no more elements present in the collection then NoSuchElementException is thrown. If remove() is called before next() even once then IllegalStateException is thrown.

62. The contains(Object o) method internally uses the equals() method. The equals() method if overridden should accept only objects of type Object.

63. The equals() method should be reflexive, symmetric, transitive and consistent. The equals() and hashCode() are bind by a contract.

64. Though hashCode() method in Object class generates unique hashcode for all objects but it is not sufficient as it can make out the equal objects which is used in case of collections using the hashtable. The hashtable based implementation is used so that the accessing any element takes constant time which is made possible by assigning unique hashcode to each object and same hashcode to equal objects. (If two equal objects have different hashcodes, the set collection may store duplicate elements

0 Comments On This Entry


June 2018

24 252627282930

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)