Wednesday 9 November 2016

Data types and Basic Java Interview Questions


1) What is the right data type to represent a price in Java? (answer)
BigDecimal if memory is not a concern and Performance is not critical, otherwise double with predefined precision.


2) How do you convert bytes to String? (answer)
you can convert bytes to the string using string constructor which accepts
byte[], just make sure that right character encoding otherwise platform's default character encoding will be used which may or may not be same.


3) How do you convert bytes to long in Java? (answer)
This questions if for you to answer :-)


4) Can we cast an int value into byte variable? what will happen if the value of
int is larger than byte?
Yes, we can cast but int is 32 bit long in java while byte is 8 bit long in java so when you cast an int to byte higher 24 bits are lost and a byte can only hold a value from -128 to 128.


5) There are two classes B extends A and C extends B, Can we cast B into C e.g. C = (C) B; (answer)


6) Which class contains clone method? Cloneable or Object? (answer)
java.lang.Cloneable is marker interface and doesn't contain any method clone method is defined in the object class. It is also knowing that clone() is a native method means it's implemented in C or C++ or any other native language.


7) Is ++ operator is thread-safe in Java? (answer)
 No it's not a thread safe operator because its involve multiple instructions like reading a value, incriminating it and storing it back into memory which can be overlapped between multiple threads.


8) Difference between a = a + b and a += b ? (answer)
The += operator implicitly cast the result of addition into the type of variable used to hold the result. When you add two integral variable e.g. variable of type byte, short, or int then they are first promoted to int and them addition happens. If result of addition is more than maximum value of a then a + b will give compile time error but a += b will be ok as shown below


byte a = 127;
byte b = 127;
b = a + b; // error : cannot convert from int to byte
b += a; // ok


9) Can I store a double value in a long variable without casting? (answer)
No, you cannot store a double value into a long variable without casting because the range of double is more  that long and you we need to type cast. It's not dificult to answer this question but many develoepr get it wrong due to confusion on which one is bigger between double and long in Java.


10) What will this return 3*0.1 == 0.3? true or false? (answer)
This is one of the really tricky questions. Out of 100, only 5 developers answered this question and only of them have explained the concept correctly. The short answer is false because some floating point numbers can not be represented exactly.


11) Which one will take more memory, an int or Integer? (answer)
An Integer object will take more memory an Integer is the an object and it  store meta data overhead about the object and int is primitive type so its takes less space.


12) Why is String Immutable in Java? (answer)
One of my favorite Java interview question. The String is Immutable in java because java designer thought that string will be heavily used and making it immutable allow some optimization easy sharing same String object between multiple clients. See the link for the more detailed answer. This is a great question for Java programmers with less experience as it gives them food for thought, to think about how things works in Java, what Jave designers might have thought when they created String class etc.

13) Can we use String in the switch case? (answer)
Yes from Java 7 onward we can use String in switch case but it is just syntactic sugar. Internally string hash code is used for the switch. See the detaiedl answer for more explanation and discussion.

14) What is constructor chaining in Java? (answer)
When you call one constructor from other than it's known as constructor chaining in Java. This happens when you have multiple, overloaded constructor in the class.

Share This

0 comments: