Core Java Interview Questions - 2021 - Sample
Core Java Interview Questions - 2021 - Sample
CORE JAVA
INTERVIEW QUESTIONS
YOU’LL MOST LIKELY BE ASKED
367
Interview Questions
CORE JAVA
INTERVIEW QUESTIONS
YOU’LL MOST LIKELY BE ASKED
Vibrant Publishers books are available at special quantity discount for sales
promotions, or for use in corporate training programs. For more information
please write to [email protected]
– David Han
Ph.D.Associate Professor of Management Science and Statistics ,
University of Texas at San Antonio
– Yogesh Jadhav
Professor & Training and Placement Officer, Sinhgad Institutes
What experts say about this book!
– Shashikanth K
Placement Coordinator & Associate Professor at CSE Department
I really liked the way each and every part of Java is touched in
the book. Plus, the in-detail explanation along with snippet code
examples makes it more clear and easy to understand.
03. Streams.......................................................................................169
04. Method References...............................................................177
05. DateTime API..........................................................................183
06. Static and Default Interface Methods............................191
07. Optionals....................................................................................195
08. Collection Improvements...................................................201
09. Miscellaneous..........................................................................207
Index.................................................................305
Dear Reader,
Thank you for purchasing Core Java Interview Questions You’ll Most Likely Be Asked.
We are committed to publishing books that are content–rich, concise and approachable
enabling more readers to read and make the fullest use of them. We hope this book provides
the most enriching learning experience as you prepare for your interview.
Thanks again for your purchase. Good luck with your interview!
facebook.com/vibrantpublishers
SECTION
01
CORE JAVA
JAVA 8
JAVA 9
HUMAN RESOURCE
OOPs Concepts
CHAPTER
01
001. Explain method overloading and method overriding.
Answer:
Method overloading occurs when there are two or more
methods in a class with the same name but with different
number/type of arguments. The following code demonstrates
this:
return a+a;
}
}
www.vibrantpublishers.com
OOPs Concepts 9
www.vibrantpublishers.com
10 Core Java Interview Questions You’ll Most Likely Be Asked
www.vibrantpublishers.com
12 Core Java Interview Questions You’ll Most Likely Be Asked
Answer:
The above compiles fine. It uses method overloading. Camera
is a sub–class of Electronics and both classes have a method
called displayPrice. However, the displayPrice method
in both classes differ in the type of argument.
class Laptop {
public void displayPixel (int width, int height) { };
//Line 1
public void displayPixel (float width, float height)
{}; //Line 2
}
www.vibrantpublishers.com
OOPs Concepts 13
CHAPTER
02
011. What are the possible ways of declaring the Java main
method?
Answer:
Following are the possible ways of declaring the Java main
method.
a. public static void main (String argument [])
b. static public void main (String argument [])
So, while declaring the Java main method, the order of the
public, static, void keyword does not matter, they can be
written in any order.
16 Core Java Interview Questions You’ll Most Likely Be Asked
www.vibrantpublishers.com
Java Basics 17
015. What are the modifiers that can be specified with a method
declaration?
Answer:
The following keywords can be specified with a method
declaration:
a. public
b. private
www.vibrantpublishers.com
18 Core Java Interview Questions You’ll Most Likely Be Asked
c. protected
The keywords public, private and protected are access
specifiers. They specify from where the method can be
accessed.
d. static: used to indicate that the method is a class level
method
e. final: used to indicate that the method cannot be overridden
f. abstract: used to specify that a sub–class should override
the method
g. native: used to specify that the method is implemented in
another language like C++
h. synchronized: used to indicate that the method can be
accessed by only one thread at a time
www.vibrantpublishers.com
Java Basics 19
/*
This for loop repeats a block of code 10 times
*/
for (int i=0; i < 10; i++) {
}
www.vibrantpublishers.com
20 Core Java Interview Questions You’ll Most Likely Be Asked
/**
* Method that does something
* @param a
*/
public void myMethod (int a) {
}
www.vibrantpublishers.com
Java Basics 21
020. Explain with a code sample how you will read a value
entered by a user.
Answer:
Java provides a class called java.util.Scanner. This can
be used to read an input from a user. The following code
demonstrates this:
System.out.println(“Enter a Number:”);
Scanner scanner = new Scanner (System.in);
int num = scanner.nextInt();
System.out.println(“The entered number is “:num);
scanner.close();
Enter a Number:
10
The entered number is 10
www.vibrantpublishers.com
This page is intentionally left blank
Data Types,
CHAPTER
Variables and
Arrays
03
021. What are the possible ways of declaring an array of short
data type?
Answer:
You can declare an array of short data type in any of the
following ways:
a. short a1[];
b. short [] a2;
c. short b1[] [];
d. short [] [] b2;
e. short [] c1 = {2,3,4};
f. short c2[] = {2,3,4};
Approaches a and b declare a single dimensional short array.
Approaches c and d declare a two–dimensional short array.
Approaches e and f declare a single dimensional short array
and initialize it. In all the approaches, the square brackets
24 Core Java Interview Questions You’ll Most Likely Be Asked
can be placed either after the variable name or after the short
keyword.
int i = 30;
byte bValue = (byte)i;
So, you need to specify the “byte” keyword before the int
variable. Yes, it is necessary to cast an int variable to byte
explicitly, otherwise a compilation error will occur as below:
www.vibrantpublishers.com
Data Types, Variables and Arrays 25
024. Write a code snippet that demonstrate how you can assign a
float value to a short variable.
Answer:
The following code demonstrates converting a float value to a
short variable:
025. What is the default value for int, double, float, boolean,
long and char data types?
Answer:
The default values for the data types mentioned above are as
follows:
int = 0
double = 0.0d
float = 0.0f
boolean = false
long = 0L
char = ‘u0000’
www.vibrantpublishers.com
26 Core Java Interview Questions You’ll Most Likely Be Asked
Answer:
Line 1 compiles fine as it creates a two–dimensional array. Line
2 compiles fine as it creates a single dimensional array. Line 3
compiles fine as it declares and initializes an int variable.
Since a two– dimensional array is an array of arrays, Line 4 also
compiles fine. It assigns a single dimensional array to one of
the dimensions in the two–dimensional array. Line 5 causes a
compilation error because we cannot assign an int variable to
an int array variable.
Answer:
The code compiles fine, there is no issue at all. The code first
creates a Car class. It creates a car object called myCar. Finally,
it creates a Car array called myCars which is initialized with
three values. The Car constructor is directly invoked during
array initialization.
www.vibrantpublishers.com
Data Types, Variables and Arrays 27
028. You are given an assignment to create a game of Tic Tac Toe
using a multi–dimensional array. Which of the following is
the correct way to initialize the array?
a. int ttt = new int[3][3];
b. int[]ttt = new int[3][3];
c. int[][]ttt = new int[3][3];
d. int ttt [3][3] = new int[][];
Answer:
(c) Answer c is correct. The correct syntax for declaring a
multi–dimensional array is
www.vibrantpublishers.com