Skip to main content

Posts

Showing posts with the label core java

Mixed Java Tricky Interview Questions

Tricky Mixed Interview Questions 1. What are Wrapper classes in Java? Java has concept of Wrapper classes to allow primitive types to be accessed as objects. Primitive types like boolean, int, double, float etc. have corresponding Wrappers classes – Boolean, Integer, Double, Float etc. Many of these Wrapper classes are in java.lang package. Java 5.0 has launched the concept of Autoboxing and Unboxing in Java for Wrapper classes. E.g. public class WrapperTest{ public static void main(String args[]){ //Converting int into Integer int count=50; Integer i=Integer.valueOf(count); //converting int into Integer Integer j=a; //autoboxing, now compiler will write Integer.valueOf(count) internally System.out.println(count+" "+i+" "+j); } } 2. What is the purpose of native method in Java? The native keyword is used for applying to a method to indicate that the method is implemented in native code using JNI(Java Native Interface). The...

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...

Java Tricky Interview Questions

Java Tricky Questions 1. Is there any difference between a = a + b and a += b expressions? When we add two integral variables e.g. variables of type byte, short, or int in Java, then they are first promoted to int type, and then addition happens. The += operator implicitly casts the result of addition into the type of variable used to hold the result. What happens when you put return statement or System.exit () on try or catch block? Will finally block execute? It is a popular tricky Java interview question. Most of the programmers think that no matter what the finally block will always execute. This question challenges that concept by putting a return statement in the try or catch block or calling System.exit() from try or catch block. You can answer by saying that finally block executes even if we put a return statement in the try block or catch block. But finally block does not execute if you call System.exit() from try or catch block. 2. What does the expre...