Core Java Telephonic Round QuestionAnswer
Core Java Telephonic Round QuestionAnswer
10) How to Swap two numbers without using temp variable? (solution)
This question is ages old. I have first seen this question way back in 2005 but I
am sure its even older than that. Good thing about this problem is that except XOR
trick all solution has some flaws, which is used to test whether candidate really
knows his stuff or not. Check out the solution for all three possible solution and
drawback of each.
12) Write Java program to reverse String without using API? (solution)
One more question to test problem solving skill of candidate. You wouldn't expect
these kind of question in telephonic round of Java interview but these questions
have now become norms. All interviewer is looking it for logic, you don't need to
write the code but you should be able to think of solution.
19) Can we have return statement in finally clause? What will happen? (detailed
answer)
Yes, you can use return statement in finally block, but it will not prevent finally
block from being executed. BTW, if you also used return statement in try block then
return value from finally block with override whatever is returned from try block.
21) Difference between private, public, package and protected in Java? (detailed
answer)
All four are access modifier in Java but only private, public and protected are
modifier keyword. There is no keyword for package access, its default in Java.
Which means if you don't specify any access modifier than by default that will be
accessible inside the same package. Private variables are only accessible in the
class they are declared, protected are accessible inside all classes in same
package but only on sub class outside package and public variables e.g. method,
class or variables are accessible anywhere. This is highest level of access
modifier and provide lowest form of encapsulation.
23) Write a Program to find maximum and minimum number in array? (solution)
This is another coding question test problem solving ability of candidate. Be ready
for couple of follow up as well depending upon how you answer this question.
Simplest way which comes in mind is to sort the array and then pick the top and
bottom element. For a better answer see the solution.
27) What is difference between calling start() and run() method of Thread?
(detailed answer)
You might have heard this question before, if calling start() method calls the
run() method eventually then why not just call the run() method? Well the
difference is, start method also starts a new thread. If you call the run method
directly then it will run on same thread not on different thread, which is what
original intention would be.
29) How to find middle element of linked list in one pass? (solution)
Another simple problem solving question for warm up. In a singly linked list you
can only traverse in one direction and if you don't know the size then you cannot
write a loop to exactly find out middle element, that is the crux of the problem.
One solution is by using two pointers, fast and slow. Slower pointer moves 1 step
when faster pointer move to 2 steps, causing slow to point to middle when fast is
pointing to end of the list i.e. null. Check out solution for Java code sample.
30) What is equlas() and hashCode() contract in Java? Where does it used? (detailed
answer)
One of the must ask question in Java telephonic interview. If a guy doesn't know
about equals() and hashCode() then he is probably not worth pursuing further
because its the core of the Java fundamentals. The key point of contract is that if
two objects are equal by equals() method then they must have same hashcode, but
unequal object can also have same hashcode, which is the cause of collision on hash
table based collection e.g HashMap. When you override equals() you must remember to
override hashCode() method to keep the contract valid.
31) Why wait and notify methods are declared in Object class? (detailed answer)
This question is more to find out how much experience you really have and what is
your thinking towards Java API and its design decision. Similar question is why
String is immutable in Java? Well, true answer can only be given by Java designers
but you can reason something. For example, wait and notify methods are associated
with locks which is owned by object not thread, and that's why it make sense to
keep those method on java.lang.Object class. See the detailed answer for more
discussion and reasoning.
34) What is difference between Iterator and Enumeration in Java? (detailed answer)
Main difference is that Iterator was introduced in place of Enumeration. It also
allows you to remove elements from collection while traversing which was not
possible with Enumeration. The methods of Iterator e.g. hasNext() and next() are
also more concise then corresponding methods in Enumeration e.g. hasMoreElements().
You should always use Iterator in your Java code as Enumeration may get deprecated
and removed in future Java release.
36) Difference between static and dynamic binding in Java? (detailed answer)
This is usually asked as follow-up of previous question, static binding is related
to overloaded method and dynamic binding is related to overridden method. Method
like private, final and static are resolved using static binding at compile time
but virtual methods which can be overridden are resolved using dynamic binding at
runtime.
40) What is difference between Checked and Unchecked Exception in Java? (detailed
answer)
Checked exception ensures that handling of exception is provided and its verified
by compiler also, while for throwing unchecked exception no special provision is
needed e.g. throws clause. A method can throw unchecked exception without any throw
clause