Skip to main content

Posts

Showing posts with the label Java Tricky

Java Tricky Interview Questions Part - 2

Java Tricky Questions 1. How can we print an Array in Java? We can print an array by using methods of Arrays class. We can either use Arrays.toString() method or we can use Arrays.deepToString() method. Since array doesn't implement toString() method by itself, just passing an array to System.out.println() will not print its contents. But we can use Arrays.toString() to print each element of an array. 2. Is it ok to use random numbers in the implementation of hashcode() method in Java? No. The hashcode of an object should be always same. If you use random number in hashcode() method, then you may get a different value of hashcode for same object. This will break the hashcode contract. 3. Between two types of dependency injections, constructor injection and setter dependency injection, which one is better? Constructor injection guarantees that a class will be initialized with all its dependencies during creation. But setter injection provides flexibility to...