Java Interview Questions For 5 Years
Java Interview Questions For 5 Years
Java Interview Questions For 5 Years
years Experience
© Copyright by Interviewbit
Contents
Resources
22. Conclusion
Introduction:
If you have 5+ years of experience in the field of so ware development and more
specifically in Java, it is very much necessary to know all the concepts of Java in
depth. The interviewer can ask you the trickiest of questions that might confuse you
and even rattle your foundation of what you know about Java. You need to be well-
prepared to know the concepts in depth and prove why you are a developer with 5+
years of experience. In this section, we will see what are the most commonly asked
Core Java Interview questions and how you can answer them. We will cover a wide
range of important Java topics that are the applications of variables, collections,
polymorphism, strings, datatypes, and threads.
Transient provides
Volatile ensures that the JVM does
flexibility and control
not re-order the variables and
over the attributes of
ensures that the synchronization
objects from being
issues are avoided.
serialized.
Transient variables
are initialized with
Volatile variables do not have any default value
default values. corresponding to the
data type at the time
of deserialization.
Transient variables
cant be used along
with static keywords
because the static
Volatile variables can be used along variables are class-
with the static keyword. level variables and
not related to the
individual instances.
This matters during
serialization.
Synchronization Vector is
synchronized
and thread-safe
by default. This
means that the ArrayList is
internal state of neither thread-
the Vector is safe nor
not synchronized.
compromised
even if multiple
threads are
operating on it.
Speed ArrayList is
Since Vector is faster than
synchronized, it Vector because
works relatively there is no
slower than overhead of
ArrayList. maintaining
synchronization.
Since Collection is an interface dealing with the data type of iterable objects, the
interface extends the Iterable interface as shown below:
Since Collections is a java class, it extends the Object class as shown below:
The 3 dots feature was started in Java 5 and the feature is known as varargs (variable
arguments). This simply means that the method can receive one or more/multiple
String arguments as shown below:
someMethod("Java", "Interview");
someMethod("Java", "Interview", "Questions");
someMethod(new String[]{"Java", "Interview", "Questions"});
These received arguments can be used as an array and can be accessed by iterating
through loops as shown below:
But the disadvantage of this way is that the initialization cannot be done lazily and
the getInstance() method is called even before any client can call.
Using synchronized keyword: We can make use of the synchronized keyword
upon the getInstance method as shown below:
In this method, we can achieve lazy initialization, and also since we use
synchronized keywords, the object initialization is also thread-safe.
The only problem is that since the whole method is synchronized, the
performance is impacted in the presence of multiple threads.
}
}
return instance;
}
}
The output of this code is “String method Invoked”. We know that null is a value that
can be assigned to any kind of object reference type in Java. It is not an object in
Java. Secondly, the Java compiler chooses the method with the most specific
parameters in method overloading. this means that since the String class is more
specific, the method with String input parameter is called.
wait() should be called in loop construct always because whenever a thread gets
resources to execute again, it is always better to check the condition before
execution. The standard way of wait and notify usage is as shown below:
synchronized (resource) {
while (wait condition)
resource.wait(); // release the resource lock and reacquire it post wait
// operations to perform
}
This expectation mismatch is due to the error that occurs while rounding float-point
numbers and the fact that in Java, only the floating-point numbers that are powers
of 2 are represented accurately by the binary representation. The rest of the numbers
should be rounded to accommodate the limited bits as required.
We should know that in Java, for the type Double, the MIN_VALUE and MAX_VALUE
are positive numbers. The Double.MIN_VALUE has the value of {"detectHand":false}
which is a positive number but the least of all values belonging to the Double class.
Hence the program results in printing 0.0 as obviously the Double.MIN_VALUE >
0
This means that if the overridden method is throwing IOException, then the
overriding child class method can only throw IOException or its sub-classes. This
overriding method can not throw a higher Exception than the original or overridden
method.
Line 1 results in false, whereas line 2 results in true. Let us see why.
In Java, if the references point to different objects and have the same content, they
are not equal in terms of using double equals. By this logic, the statement
num3==num4 should have resulted in false. But, the Integer.java class in Java has
a private inner class called IntegerCache.java which helps to cache the Integer
objects ranging from -128 to 127 . All the numbers lying between this range are
cached internally by the Integer class. While defining an object as:
This means that if the value lies in the range -128 to 127, then it returns the Integer
instance from the cache, else it creates a new instance. This means that the num3
and num4 point to the same object in the IntegerCache and thereby the comparison
results in true.
class X {
static int i = 1111;
static{
i = i-- - --i; //L1
}
{
i = i++ + ++i; //L2
}
}
class Y extends X{
static{
i = --i - i--; //L3
}
{
i = ++i + i++; //L4
}
}
i = i-- - --i;
i = 1111 - 1109 = 2
L2 ->
i = i++ + ++i;
i = 0 + 2 = 2
L3 ->
i = --i - i--;
i = 1 - 1 = 0
L4 ->
i = ++i + i++;
i = 3 + 3 = 6 = y.i
L5 ->
y.i = i from class Y which is 6.
Hence the output is 6.
The output of the code is “Compile Error” This is because the code is not able to find
any constructor that matches InterviewbitTest().
We should note that the variables declared as final only takes care of not re-assigning
the variable to a different value. The individual properties of an object can still be
changed.
Resources
22. Conclusion
In this section, we have seen that a lot of questions have been asked on
polymorphism (Method overloading & overriding), inheritance, collections, strings,
and threads. One has to know the concepts and the working of each of these in-depth
to ensure you ace the Java Interview.
Css Interview Questions Laravel Interview Questions Asp Net Interview Questions