Java
Interview
Questions
www.JavaInterview.in
1
Following
are
the
important
topics
that
are
important
from
interview
perspective
for
core
java.
We
cover
these
in
great
detail
in
this
guide.
CORE
JAVA
...........................................................................................................................................................
2
WHY
IS
JAVA
SO
POPULAR?
.............................................................................................................................................
2
WHAT
IS
PLATFORM
INDEPENDENCE?
...............................................................................................................................
2
WHAT
ARE
THE
IMPORTANT
DIFFERENCES
BETWEEN
C++
AND
JAVA?
.......................................................................................
3
WHAT
ARE
WRAPPER
CLASSES?
........................................................................................................................................
3
WHAT
ARE
THE
DIFFERENT
UTILITY
METHODS
PRESENT
IN
WRAPPER
CLASSES?
...........................................................................
4
WHAT
IS
AUTO
BOXING?
................................................................................................................................................
5
ARE
ALL
STRINGS
IMMUTABLE?
.......................................................................................................................................
6
WHERE
ARE
STRING
LITERALS
STORED
IN
MEMORY?
..............................................................................................................
6
CAN
YOU
GIVE
EXAMPLES
OF
DIFFERENT
UTILITY
METHODS
IN
STRING
CLASS?
............................................................................
6
EXPLAIN
ABOUT
TOSTRING
METHOD
IN
JAVA?
....................................................................................................................
7
WHAT
IS
THE
USE
OF
EQUALS
METHOD
IN
JAVA?
..................................................................................................................
8
WHAT
ARE
THE
IMPORTANT
THINGS
TO
CONSIDER
WHEN
IMPLEMENTING
EQUALS
METHOD?
........................................................
9
WHAT
IS
THE
HASHCODE
METHOD
USED
FOR
IN
JAVA?
.........................................................................................................
9
WHAT
IS
CASTING?
......................................................................................................................................................
10
WHAT
IS
IMPLICIT
CASTING?
.........................................................................................................................................
10
WHAT
IS
EXPLICIT
CASTING?
.........................................................................................................................................
10
HOW
ARE
VARIABLES
INITIALIALIZED
IN
JAVA?
...................................................................................................................
10
WHAT
IS
A
NESTED
IF
ELSE?
CAN
YOU
EXPLAIN
WITH
AN
EXAMPLE?
.......................................................................................
11
ARRAYS
..............................................................................................................................................................
12
HOW
DO
YOU
DECLARE
AND
CREATE
AN
ARRAY?
................................................................................................................
12
CAN
THE
SIZE
OF
AN
ARRAY
BE
CHANGED
DYNAMICALLY?
.....................................................................................................
12
2
Java
Interview
Questions
www.JavaInterview.in
CAN
YOU
CREATE
AN
ARRAY
WITHOUT
DEFINING
SIZE
OF
AN
ARRAY?
......................................................................................
12
WHAT
ARE
THE
DEFAULT
VALUES
IN
AN
ARRAY?
.................................................................................................................
12
HOW
DO
YOU
LOOP
AROUND
AN
ARRAY
USING
ENHANCED
FOR
LOOP?
...................................................................................
13
HOW
DO
YOU
PRINT
THE
CONTENT
OF
AN
ARRAY?
..............................................................................................................
13
HOW
DO
YOU
COMPARE
TWO
ARRAYS?
...........................................................................................................................
13
HOW
DO
YOU
SORT
AN
ARRAY?
......................................................................................................................................
14
ENUM
................................................................................................................................................................
14
WHAT
IS
AN
ENUM?
....................................................................................................................................................
14
HOW
DO
YOU
CREATE
AN
ENUM
FROM
A
STRING
VALUE?
....................................................................................................
14
WHAT
IS
AN
ENUM
ORDINAL?
.......................................................................................................................................
14
HOW
DO
YOU
COMPARE
TWO
ENUMS?
...........................................................................................................................
14
CAN
YOU
USE
A
SWITCH
STATEMENT
AROUND
AN
ENUM?
...................................................................................................
14
Core
Java
Following
videos
cover
these
topics
in
great
detail.
In
addition
to
following
this
guide,
we
recommend
that
you
watch
the
videos
as
well.
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0.
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Why
is
Java
so
Popular?
Two
main
reasons
for
popularity
of
Java
are
1. Platform
Independence
2. Object
Oriented
Language
We
will
look
at
these
in
detail
in
later
sections.
What
is
Platform
Independence?
This
video(https://www.youtube.com/watch?v=lLgcgvIHyAw)
explains
Platform
Independence
in
great
detail.
Refer
to
it
for
more
detailed
answer.
Platform
Independence
is
also
called
build
once,
run
anywhere.
Java
is
one
of
the
most
popular
platform
independent
languages.
Once
we
compile
a
java
program
and
build
a
jar,
we
can
run
the
jar
(compiled
java
program)
in
any
Operating
System
-
where
a
JVM
is
installed.
Java
achieves
Platform
Independence
in
a
beautiful
way.
On
compiling
a
java
file
the
output
is
a
class
file
-
which
contains
an
internal
java
representation
called
bytecode.
JVM
converts
bytecode
to
executable
instructions.
The
executable
instructions
are
different
in
different
operating
systems.
So,
there
are
different
JVM's
for
different
operating
systems.
A
JVM
for
windows
is
different
from
a
JVM
for
mac.
However,
both
the
JVM's
understand
the
bytecode
and
convert
it
to
the
executable
code
for
the
respective
operating
system.
Java
Interview
Questions
www.JavaInterview.in
3
What
are
the
important
differences
between
C++
and
Java?
1. Java
is
platform
independent.
C++
is
not
platform
independent.
2. Java
is
a
pure
Object
Oriented
Language
(except
for
primitive
variables).
In
C++,
one
can
write
structural
programs
without
using
objects.
3. C++
has
pointers
(access
to
internal
memory).
Java
has
no
concept
called
pointers.
4. In
C++,
programmer
has
to
handle
memory
management.
A
programmer
has
to
write
code
to
remove
an
object
from
memory.
In
Java,
JVM
takes
care
of
removing
objects
from
memory
using
a
process
called
Garbage
Collection.
5. C++
supports
Multiple
Inheritance.
Java
does
not
support
Multiple
Inheritance.
Java
Interview
Questions
www.JavaInterview.in
At
http://www.JavaInterview.in,
we
want
you
to
clear
java
interview
with
ease.
So,
in
addition
to
focussing
on
Core
and
Advanced
Java
we
also
focus
on
topics
like
Code
Reviews,
Performance,
Design
Patterns,
Spring
and
Struts.
We
have
created
more
than
20
videos
to
help
you
understand
these
topics
and
become
an
expert
at
them.
Visit
our
website
http://www.JavaInterview.in
for
complete
list
of
videos.
Other
than
the
videos,
we
answer
the
top
200
frequently
asked
interview
questions
on
our
website.
With
more
900K
video
views
(Apr
2015),
we
are
the
most
popular
channel
on
Java
Interview
Questions
on
YouTube.
Register
here
for
more
updates
:
https://feedburner.google.com/fb/a/mailverify?uri=RithusTutorials
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
What
are
wrapper
classes?
This video(https://www.youtube.com/watch?v=YQbZRw2yIBk) covers the topic in great detail.
A brief description is provided below.
A primitive wrapper class in the Java programming language is one of eight classes provided in the
java.lang package to provide object methods for the eight primitive types. All of the primitive wrapper
classes in Java are immutable.
4
Java
Interview
Questions
www.JavaInterview.in
Wrapper: Boolean,Byte,Character,Double,Float,Integer,Long,Short
Primitive: boolean,byte,char
,double, float, int , long,short
Wrapper classes are final and immutable. Examples of creating wrapper classes are listed below.
Integer
number
=
new
Integer(55);//int
Integer
number2
=
new
Integer("55");//String
Float
number3
=
new
Float(55.0);//double
argument
Float
number4
=
new
Float(55.0f);//float
argument
Float
number5
=
new
Float("55.0f");//String
Character
c1
=
new
Character('C');//Only
char
constructor
//Character
c2
=
new
Character(124);//COMPILER
ERROR
Boolean
b
=
new
Boolean(true);
//"true"
"True"
"tRUe"
-
all
String
Values
give
True
//Anything
else
gives
false
Boolean
b1
=
new
Boolean("true");//value
stored
-
true
Boolean
b2
=
new
Boolean("True");//value
stored
-
true
Boolean
b3
=
new
Boolean("False");//value
stored
-
false
Boolean
b4
=
new
Boolean("SomeString");//value
stored
-
false
b
=
false;
What
are
the
different
utility
methods
present
in
wrapper
classes?
A
number
of
utility
methods
are
defined
in
wrapper
classes
to
create
and
convert
them
from
primitives.
valueOf
Methods
Provide another way of creating a Wrapper Object
Integer
seven
=
Integer.valueOf("111",
2);//binary
111
is
converted
to
7
Integer
hundred
=
Integer.valueOf("100");//100
is
stored
in
variable
xxxValue
methods
xxxValue methods help in creating primitives
Integer
integer
=
Integer.valueOf(57);
int
primitive
=
seven.intValue();//57
float
primitiveFloat
=
seven.floatValue();//57.0f
Float
floatWrapper
=
Float.valueOf(57.0f);
int
floatToInt
=
floatWrapper.intValue();//57
float
floatToFloat
=
floatWrapper.floatValue();//57.0f
parseXxx
methods
parseXxx methods are similar to valueOf but they return primitive values
Java
Interview
Questions
www.JavaInterview.in
5
int
sevenPrimitive
=
Integer.parseInt("111",
2);//binary
111
is
converted
to
7
int
hundredPrimitive
=
Integer.parseInt("100");//100
is
stored
in
variable
static
toString
method
Look
at
the
example
of
the
toString
static
method
below.
Integer
wrapperEight
=
new
Integer(8);
System.out.println(Integer.
toString(wrapperEight));//String
Output:
8
Overloaded
static
toString
method
2nd
parameter:
radix
System.out.println(Integer
.toString(wrapperEight,
2));//String
Output:
1000
static
toXxxString
methods.
Xxx
can
be
Hex,Binary,Octal
System.out.println(Integer
.toHexString(wrapperEight));//String
Output:8
System.out.println(Integer
.toBinaryString(wrapperEight));//String
Output:1000
System.out.println(Integer
.toOctalString(wrapperEight));//String
Output:10
What
is
Auto
Boxing?
Autoboxing
is
the
automatic
conversion
that
the
Java
compiler
makes
between
the
primitive
types
and
their
corresponding
object
wrapper
classes.
For
example,
converting
an
int
to
an
Integer,
a
double
to
a
Double,
and
so
on.
If
the
conversion
goes
the
other
way,
this
is
called
unboxing.
Integer
ten
=
new
Integer(10);
ten++;//allowed.
Java
does
had
work
behind
the
screen
for
us
Boxing
and
new
instances
Auto
Boxing
helps
in
saving
memory
by
reusing
already
created
Wrapper
objects.
However
wrapper
classes
created
using
new
are
not
reused.
Two
wrapper
objects
created
using
new
are
not
same
object.
Integer
nineA
=
new
Integer(9);
Integer
nineB
=
new
Integer(9);
System.out.println(nineA
==
nineB);//false
System.out.println(nineA.equals(nineB));//true
Two
wrapper
objects
created
using
boxing
are
same
object.
Integer
nineC
=
9;
6
Java
Interview
Questions
www.JavaInterview.in
Integer
nineD
=
9;
System.out.println(nineC
==
nineD);//true
System.out.println(nineC.equals(nineD));//true
Are
all
Strings
immutable?
This
video
(https://www.youtube.com/watch?v=wh6L8zO_Hr4)
covers
all
the
topics
related
to
Strings
in
great
detail.
Refer
to
it
for
more
details.
Value
of
a
String
Object
once
created
cannot
be
modified.
Any
modification
on
a
String
object
creates
a
new
String
object.
String
str3
=
"value1";
str3.concat("value2");
System.out.println(str3);
//value1
Note
that
the
value
of
str3
is
not
modified
in
the
above
example.
The
result
should
be
assigned
to
a
new
reference
variable
(or
same
variable
can
be
reused).
String
concat
=
str3.concat("value2");
System.out.println(concat);
//value1value2
Where
are
string
literals
stored
in
memory?
All
strings
literals
are
stored
in
"String
constant
pool".
If
compiler
finds
a
String
literal,it
checks
if
it
exists
in
the
pool.
If
it
exists,
it
is
reused.
Following
statement
creates
1
string
object
(created
on
the
pool)
and
1
reference
variable.
String
str1
=
"value";
However,
if
new
operator
is
used
to
create
string
object,
the
new
object
is
created
on
the
heap.
Following
piece
of
code
create
2
objects.
//1.
String
Literal
"value"
-
created
in
the
"String
constant
pool"
//2.
String
Object
-
created
on
the
heap
String
str2
=
new
String("value");
Can
you
give
examples
of
different
utility
methods
in
String
class?
String
class
defines
a
number
of
methods
to
get
information
about
the
string
content.
String
str
=
"abcdefghijk";
Get
information
from
String
Following
methods
help
to
get
information
from
a
String.
//char
charAt(int
paramInt)
System.out.println(str.charAt(2));
//prints
a
char
-
c
System.out.println("ABCDEFGH".length());//8
System.out.println("abcdefghij".toString());
//abcdefghij
System.out.println("ABC".equalsIgnoreCase("abc"));//true
//Get
All
characters
from
index
paramInt
Java
Interview
Questions
www.JavaInterview.in
7
//String
substring(int
paramInt)
System.out.println("abcdefghij".substring(3));
//cdefghij
//All
characters
from
index
3
to
6
System.out.println("abcdefghij".substring(3,7));
//defg
Explain
about
toString
method
in
Java?
This video (https://www.youtube.com/watch?v=k02nM5ukV7w) covers toString in great detail.
toString method is used to print the content of an Object. If the toString method is not overridden in a
class, the default toString method from Object class is invoked. This would print some hashcode as
shown in the example below. However, if toString method is overridden, the content returned by the
toString method is printed.
Consider the class given below:
class
Animal
{
public
Animal(String
name,
String
type)
{
this.name
=
name;
this.type
=
type;
}
String
name;
String
type;
}
Run this piece of code:
Animal
animal
=
new
Animal("Tommy",
"Dog");
System.out.println(animal);//com.rithus.Animal@f7e6a96
Output does NOT show the content of animal (what name? and what type?). To show the content of the
animal object, we can override the default implementation of toString method provided by Object class.
Adding
toString
to
Animal
class
class
Animal
{
public
Animal(String
name,
String
type)
{
this.name
=
name;
this.type
=
type;
}
String
name;
String
type;
public
String
toString()
{
return
"Animal
[name="
+
name
+
",
type="
+
type
+
"]";
}
}
8
Java
Interview
Questions
www.JavaInterview.in
Run this piece of code:
Animal
animal
=
new
Animal("Tommy","Dog");
System.out.println(animal);//Animal
[name=Tommy,
type=Dog]
Output now shows the content of the animal object.
What
is
the
use
of
equals
method
in
Java?
Equals method is used when we compare two objects. Default implementation of equals method is
defined in Object class. The implementation is similar to == operator. Two object references are equal
only if they are pointing to the same object.
We need to override equals method, if we would want to compare the contents of an object.
Consider the example Client class provided below.
class
Client
{
private
int
id;
public
Client(int
id)
{
this.id
=
id;
}
}
== comparison operator checks if the object references are pointing to the same object. It does NOT look
at the content of the object.
Client
client1
=
new
Client(25);
Client
client2
=
new
Client(25);
Client
client3
=
client1;
//client1
and
client2
are
pointing
to
different
client
objects.
System.out.println(client1
==
client2);//false
//client3
and
client1
refer
to
the
same
client
objects.
System.out.println(client1
==
client3);//true
//similar
output
to
==
System.out.println(client1.equals(client2));//false
System.out.println(client1.equals(client3));//true
We can override the equals method in the Client class to check the content of the objects. Consider the
example below: The implementation of equals method checks if the id's of both objects are equal. If so, it
returns true. Note that this is a basic implementation of equals and more needs to be done to make it foolproof.
class
Client
{
private
int
id;
public
Client(int
id)
{
this.id
=
id;
}
@Override
Java
Interview
Questions
www.JavaInterview.in
9
public
boolean
equals(Object
obj)
{
Client
other
=
(Client)
obj;
if
(id
!=
other.id)
return
false;
return
true;
}
}
Consider running the code below:
Client
client1
=
new
Client(25);
Client
client2
=
new
Client(25);
Client
client3
=
client1;
//both
id's
are
25
System.out.println(client1.equals(client2));//true
//both
id's
are
25
System.out.println(client1.equals(client3));//true
Above code compares the values (id's) of the objects.
What
are
the
important
things
to
consider
when
implementing
equals
method?
Any equals implementation should satisfy these properties:
1. Reflexive. For any reference value x, x.equals(x) returns true.
2. Symmetric. For any reference values x and y, x.equals(y) should return true if and only if
y.equals(x) returns true.
3. Transitive. For any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns
true, then x.equals(z) must return true.
4. Consistent. For any reference values x and y, multiple invocations of x.equals(y) consistently
return true or consistently return false, if no information used in equals is modified.
5. For any non-null reference value x, x.equals(null) should return false.
Let's now provide an implementation of equals which satisfy these properties:
//Client class
@Override
public
boolean
equals(Object
obj)
{
if
(this
==
obj)
return
true;
if
(obj
==
null)
return
false;
if
(getClass()
!=
obj.getClass())
return
false;
Client
other
=
(Client)
obj;
if
(id
!=
other.id)
return
false;
return
true;
}
What
is
the
hashCode
method
used
for
in
Java?
HashCode's are used in hashing to decide which group (or bucket) an object should be placed into. A
group of object's might share the same hashcode.
10
Java
Interview
Questions
www.JavaInterview.in
The implementation of hash code decides effectiveness of Hashing. A good hashing function evenly
distributes object's into different groups (or buckets).
A good hashCode
method
should
have
the
following
properties
If obj1.equals(obj2) is true, then obj1.hashCode() should be equal to obj2.hashCode()
obj.hashCode() should return the same value when run multiple times, if values of obj used in
equals() have not changed.
If obj1.equals(obj2) is false, it is NOT required that obj1.hashCode() is not equal to
obj2.hashCode(). Two unequal objects MIGHT have the same hashCode.
A sample hashcode implementation of Client class which meets above constraints is given below:
//Client class
@Override
public
int
hashCode()
{
final
int
prime
=
31;
int
result
=
1;
result
=
prime
*
result
+
id;
return
result;
}
What
is
casting?
Casting is used when we want to convert on data type to another.
There are two types of Casting
Implicit Casting
Explicit Casting
What
is
Implicit
Casting?
Implicit Casting is done by the compiler. Good examples of implicit casting are all the automatic widening
conversions i.e. storing smaller values in larger variable types.
int
value
=
100;
long
number
=
value;
//Implicit
Casting
float
f
=
100;
//Implicit
Casting
What
is
Explicit
Casting?
Explicit Casting is done through code. Good examples of explicit casting are the narrowing conversions.
Storing larger values into smaller variable types;
long
number1
=
25678;
int
number2
=
(int)number1;//Explicit
Casting
//int
x
=
35.35;//COMPILER
ERROR
int
x
=
(int)35.35;//Explicit
Casting
Explicit casting would cause truncation of value if the value stored is greater than the size of the variable.
int
bigValue
=
280;
byte
small
=
(byte)
bigValue;
System.out.println(small);//output
24.
Only
8
bits
remain.
How
are
variables
initialialized
in
Java?
Member
and
Static
variables
are
alway
initialized
with
default
values.
Default
values
for
numeric
types
is
0,
floating
point
types
is
0.0,
boolean
is
false,
char
is
'\u0000'
and
object
reference
variable
is
null.
Local/block
variables
are
NOT
initialized
by
compiler.
Java
Interview
Questions
www.JavaInterview.in
1
1
If
local
variables
are
used
before
initialization,
it
would
result
in
a
compilation
error.
package
com.rithus.variables;
public
class
VariableInitialization
{
public
static
void
main(String[]
args)
{
Player
player
=
new
Player();
//score
is
an
int
member
variable
-
default
0
System.out.println(player.score);//0
-
RULE1
//name
is
a
member
reference
variable
-
default
null
System.out.println(player.name);//null
-
RULE1
int
local;
//not
initialized
//System.out.println(local);//COMPILER
ERROR!
RULE3
String
value1;//not
initialized
//System.out.println(value1);//COMPILER
ERROR!
RULE3
String
value2
=
null;//initialized
System.out.println(value2);//null
-
NO
PROBLEM.
}
}
What
is
a
nested
if
else?
Can
you
explain
with
an
example?
Look
at
the
example
below.
The
code
in
first
if
condition
which
is
true
is
executed.
If
none
of
the
if
conditions
are
true,
then
code
in
else
is
executed.
int
z
=
15;
if(z==10){
System.out.println("Z
is
10");//NOT
executed
}
else
if(z==12){
System.out.println("Z
is
12");//NOT
executed
}
else
if(z==15){
System.out.println("Z
is
15");//executed.
Rest
of
the
if
else
are
skipped.
}
else
{
System.out.println("Z
is
Something
Else.");//NOT
executed
}
z
=
18;
if(z==10){
System.out.println("Z
is
10");//NOT
executed
}
else
if(z==12){
System.out.println("Z
is
12");//NOT
executed
}
else
if(z==15){
System.out.println("Z
is
15");//NOT
executed
}
else
{
System.out.println("Z
is
Something
Else.");//executed
}
12
Java
Interview
Questions
www.JavaInterview.in
Arrays
Refer
to
this
video(https://www.youtube.com/watch?v=8bVysCXT-io)
for
exhaustive
coverage
of
all
the
interview
questions
about
arrays.
How
do
you
declare
and
create
an
array?
Lets first discuss about how to declare an array. All
below
ways
are
legal.
However,
using
the
third
format
of
declaration
is
recommended.
int
marks[];
//Not
Readable
int[]
runs;
//Not
Readable
int[]
temperatures;//Recommended
Declaration
of
an
Array
should
not
include
size.
//int
values[5];//Compilation
Error!
Declaring
2D
Array
Examples:
int[][]
matrix1;
//Recommended
int[]
matrix2[];
//Legal
but
not
readable.
Avoid.
Lets
now
look
at
how
to
create
an
array
(define
a
size
and
allocate
memory).
marks
=
new
int[5];
//
5
is
size
of
array
Declaring
and
creating
an
array
in
same
line.
int
marks2[]
=
new
int[5];
Can
the
size
of
an
array
be
changed
dynamically?
Once
An
Array
is
created,
its
size
cannot
be
changed.
Can
you
create
an
array
without
defining
size
of
an
array?
Size
of
an
array
is
mandatory
to
create
an
array.
//marks
=
new
int[];//COMPILER
ERROR
What
are
the
default
values
in
an
array?
New
Arrays
are
always
initialized
with
default
values.
int
marks2[]
=
new
int[5];
System.out.println(marks2[0]);//0
Default
Values
byte,short,int,long
0
float,double
0.0
boolean
false
Java
Interview
Questions
www.JavaInterview.in
1
3
object
null
How
do
you
loop
around
an
array
using
enhanced
for
loop?
Name
of
the
variable
is
mark
and
the
array
we
want
to
loop
around
is
marks.
for
(int
mark:
marks)
{
System.out.println(mark);
}
How
do
you
print
the
content
of
an
array?
Lets
look
at
different
methods
in
java
to
print
the
content
of
an
array.
Printing
a
1D
Array
int
marks5[]
=
{
25,
30,
50,
10,
5
};
System.out.println(marks5);
//[I@6db3f829
System.out.println(
Arrays.toString(marks5));//[25,
30,
50,
10,
5]
Printing
a
2D
Array
int[][]
matrix3
=
{
{
1,
2,
3
},
{
4,
5,
6
}
};
System.out.println(matrix3);
//[[I@1d5a0305
System.out.println(
Arrays.toString(matrix3));
//[[I@6db3f829,
[I@42698403]
System.out.println(
Arrays.deepToString(matrix3));
//[[1,
2,
3],
[4,
5,
6]]
matrix3[0]
is
a
1D
Array
System.out.println(matrix3[0]);//[I@86c347
System.out.println(
Arrays.toString(matrix3[0]));//[1,
2,
3]
How
do
you
compare
two
arrays?
Arrays
can
be
compared
using
static
method
equals
defined
in
Arrays
class.
Two
arrays
are
equal
only
if
they
have
the
same
numbers
in
all
positions
and
have
the
same
size.
int[]
numbers1
=
{
1,
2,
3
};
int[]
numbers2
=
{
4,
5,
6
};
System.out.println(Arrays
.equals(numbers1,
numbers2));
//false
int[]
numbers3
=
{
1,
2,
3
};
System.out.println(Arrays
.equals(numbers1,
numbers3));
//true
14
Java
Interview
Questions
www.JavaInterview.in
How
do
you
sort
an
array?
Arrays
can
be
sorted
using
static
utility
method
sort
defined
in
Arrays
class.
int
rollNos[]
=
{
12,
5,
7,
9
};
Arrays.sort(rollNos);
System.out.println(
Arrays.toString(rollNos));//[5,
7,
9,
12]
Enum
Refer
to
this
video
(https://www.youtube.com/watch?v=64Y7EP8-Ark)
for
exhaustive
coverage
of
all
the
interview
questions
about
Enum.
What
is
an
Enum?
Enum allows specifying a list of values for a Type. Consider the example below. It declares an enum
Season with 4 possible values.
enum
Season
{
WINTER,
SPRING,
SUMMER,
FALL
};
How
do
you
create
an
enum
from
a
String
value?
Function valueOf(String) is used to convert a string to enum.
//Converting
String
to
Enum
Season
season
=
Season.valueOf("FALL");
Function name() is used to find String value of an enum.
//Converting
Enum
to
String
System.out.println(season.name());//FALL
What
is
an
Enum
Ordinal?
Java assigns default ordinals to an enum in order. However, it is not recommended to use ordinals to
perform logic.
//Default
ordinals
of
enum
//
By
default
java
assigns
ordinals
in
order
System.out.println(Season.WINTER.ordinal());//0
System.out.println(Season.SPRING.ordinal());//1
System.out.println(Season.SUMMER.ordinal());//2
System.out.println(Season.FALL.ordinal());//3
How
do
you
compare
two
Enums?
Values of Enums can be compared using == or the equals function.
//Comparing
two
Enums
Season
season1
=
Season.FALL;
Season
season2
=
Season.FALL;
System.out.println(season1
==
season2);//true
System.out.println(season1.equals(season2));//true
Can
you
use
a
Switch
Statement
around
an
Enum?
Example
below
shows
how
we
can
use
a
switch
around
an
enum.
Java
Interview
Questions
www.JavaInterview.in
1
5
//Using
switch
statement
on
an
enum
public
int
getExpectedMaxTemperature()
{
switch
(this)
{
case
WINTER:
return
5;
case
SPRING:
case
FALL:
return
10;
case
SUMMER:
return
20;
}
return
-1;//
Dummy
since
Java
does
not
recognize
this
is
possible
}
Videos
We
have
created
more
than
20
videos
to
help
you
understand
these
topics
and
become
an
expert
at
them.
You
can
watch
these
videos
for
free
on
YouTube.
Visit
our
website
http://www.JavaInterview.in
for
complete
list
of
videos.
We
answer
the
top
200
frequently
asked
interview
questions
on
the
website.
Register
here
for
more
updates
:
https://feedburner.google.com/fb/a/mailverify?uri=RithusTutorials
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Java
Interview
:
A
Guide
for
Experienced:
https://www.youtube.com/watch?v=0xcgzUdTO5M
Collections
Interview
Questions
1:
https://www.youtube.com/watch?v=GnR4hCvEIJQ
Collections
Interview
Questions
2:
https://www.youtube.com/watch?v=6dKGpOKAQqs
Collections
Interview
Questions
3:
https://www.youtube.com/watch?v=_JTIYhnLemA
Collections
Interview
Questions
4:
https://www.youtube.com/watch?v=ZNhT_Z8_q9s
Collections
Interview
Questions
5:
https://www.youtube.com/watch?v=W5c8uXi4qTw