Java Interview Questions and Answers covered in this post:
What is the role of break and continue keywords in java?
-
break: break
keyword can be used to jump out of the loop.
- continue: continue keyword can
be used to break one iteration for a specific condition and continue with
the next iteration.
- We will look at examples for a break and continue.
Break Example: package simplifiedjava.crackedInterview; public class BreakStatementExample { public static void main(String[] args) {
for(int
i=0; i<10; i++) {
System.out.println("Value of "+ i);
if(i
== 5) {
System.out.println("Before Break");
break;
}
}
System.out.println("outside loop");
} }
OUTPUT :
Value of 0
Value of 1
Value of 2
Value of 3
Value of 4
Value of 5
Before Break
outside loop
Continue Example: package simplifiedjava.crackedInterview; public class ContinueStatementExample { public static void main(String[] args) {
for(int
i=0; i<10; i++) {
System.out.println("Value of "+ i);
if(i
== 5) {
System.out.println("Before continue = "+ (i
+ i));
continue;
}
}
System.out.println("outside loop");
}
}
OUTPUT:
Value of 0
Value of 1
Value of 2
Value of 3
Value of 4
Value of 5
Before continue = 10
Value of 6
Value of 7
Value of 8
Value of 9
outside loop |
Is Java Pass by value or Pass by reference. How will you prove java is
call by value?
- Java is Call by Value not a Pass by reference.
-
We will illustrate with the examples below.
package simplifiedjava.crackedInterview; public class Addition {
int num1
= 100; int num2 = 0;
public static void
main(String[] args) {
Addition add = new
Addition();
System.out.println("Before Adding Numbers : "+ add.num1);
add.addNumber(add.num1);
System.out.println("After Adding Numbers : "+ add.num1); }
public void
addNumber(int num2) {
num2 = num2
+ 100;
} }
OUTPUT:
Before Adding Numbers : 100 After Adding Numbers : 100 Explaination : In the above program, we have passed integer numbers and trying to add 100. But the change remained in addNumber() function not in main().
package simplifiedjava.crackedInterview; public class Addition { int num1 = 100;
public static void
main(String[] args) { Addition add = new Addition();
System.out.println("Before Addition " + add.num1);
add.addNumberWithObject(add);
System.out.println("After Addition " + add.num1); }
public void
addNumberWithObject(Addition addCopy) {
addCopy.num1
= 200;
} } OUTPUT :
Before Addition 100 After Addition 200
Explanation: We have passed the reference variable as a value so
the value of num1 got changed. |
Can you write a code to customize Employee Sorting order by Salary or Designation?
package simplifiedjava.crackedInterview; import java.util.Comparator; public class Employee {
private
String empName;
private
String dept;
private
Double salary; private String designation;
public
Employee(String empName, String dept, Double salary, String designation) {
super();
this.empName = empName;
this.dept = dept;
this.salary = salary;
this.designation = designation;
}
public
Employee(String empName) {
this.empName = empName;
}
public
String getEmpName() {
return empName;
}
public
String getDept() {
return dept;
}
public void
setDept(String dept) {
this.dept = dept;
}
public
Double getSalary() {
return salary;
}
public void
setSalary(Double salary) {
this.salary = salary;
}
public
String getDesignation() {
return designation;
}
public void
setDesignation(String designation) {
this.designation = designation;
}
public void
setEmpName(String empName) {
this.empName = empName;
}
@Override
public
String toString() {
return "Employee [empName=" + empName + ", dept=" + dept + ", salary=" + salary + ", designation=" + designation
+ "]";
} } package simplifiedjava.crackedInterview; import java.util.Comparator; public class SortEmployeeBySalary implements Comparator<Employee>{
public int
compare(Employee e1, Employee e2) {
return e1.getSalary().compareTo(e2.getSalary());
} } package simplifiedjava.crackedInterview;
import
java.util.ArrayList; import java.util.List; public class ComparableAndComparatorDemo { public static void main(String[] args) {
List<Employee> empList = new
ArrayList<Employee>();
empList.add(new
Employee("Yogesh","IT",100000.00,"System Analyst"));
empList.add(new
Employee("Arpita","Mangement",1200000.00,"Trustee"));
empList.add(new
Employee("Shweta","DevOps",45000.00,"Jenkin Engineer"));
empList.add(new
Employee("Shruti","IT",65000.00,"DB Admin"));
empList.add(new
Employee("Priyanka","IT",35000.00,"Test Engineer")); System.out.println("=================== Employee Before Sorting ====================="); System.out.println(empList);
System.out.println("=================== Employee After Sorting
=====================");
empList.sort(new
SortEmployeeBySalary());
System.out.println(empList);
} }
Output:
=================== Employee Before Sorting
=====================
[Employee [empName=Yogesh, dept=IT, salary=100000.0,
designation=System Analyst], Employee [empName=Arpita,
dept=Mangement, salary=1200000.0, designation=Trustee], Employee
[empName=Shweta, dept=DevOps, salary=45000.0, designation=Jenkin
Engineer], Employee [empName=Shruti, dept=IT, salary=65000.0,
designation=DB Admin], Employee [empName=Priyanka, dept=IT,
salary=35000.0, designation=Test Engineer]]
=================== Employee After Sorting
=====================
[Employee [empName=Priyanka, dept=IT, salary=35000.0,
designation=Test Engineer], Employee [empName=Shweta, dept=DevOps,
salary=45000.0, designation=Jenkin Engineer], Employee
[empName=Shruti, dept=IT, salary=65000.0, designation=DB Admin],
Employee [empName=Yogesh, dept=IT, salary=100000.0,
designation=System Analyst], Employee [empName=Arpita,
dept=Mangement, salary=1200000.0, designation=Trustee]]
|
List out some inbuilt methods of String class?
- String of inbuilt method of String class.
1. length()
2. substring()
3. contains()
4. equals()
5. concat()
6. replace()
7. isEmpty()
8. intern()
9. indexOf()
10. trim()
11. toUpperCase()
12. toLowerCase()
What is the difference between ClassNotFoundException and NoClassDefFoundError?
|
ClassNotFoundException |
NoClassDefFoundError |
1. |
ClassNotFoundException is checked exception. |
NoClassDefFoundError is an unchecked Exception. |
2. |
It is a child class of Exception class. |
It is a child class of Error class. |
3. |
This exception occurs when JVM is trying to load a class that is
not present in the classpath at runtime. |
This error occurs when JVM is trying to load a class that is not
present in classpath at run time but was present at compile
time. |
4. |
It occurs when the classpath is not updated with the required jar
files. |
It occurs when the required class definition is missing at
runtime. |
5. |
This exception thrown by Class.forName() or loadClass(). |
This error is thrown by Java Runtime System. |
1.
What is the difference between final, finally and finalize?
|
Final |
Finally |
Finalize |
1. |
Final is a keyword. |
Finally is block. |
Finalize() is a method. |
2. |
Final keyword is applicable to class, method and variable. |
Finally block is applicable in exception handling with try or try
catch block. |
Finalize() method can be used in garbage collection. |
3. |
Final class cannot be sub-classed.
Final method cannot be overridden.
Final variable is not modifiable. |
Every time finally block will be executed exception is thrown or
not. An Exception is caught or not. If you execute System.exit(0)
then only the finally block won’t be executed. |
Finalize() method can run to release the resources acquired by
objects. After running finalize() method unreachable objects will be
eligible for garbage collection. |
What is the daemon thread?
- Daemon thread is a very lightweight thread that is running in the
background.
- You may invoke the setDaemon() method to convert normal thread to daemon
thread.
- isDaemon() method can be used to check whether the particular thread is
daemon or not.
- When all non-daemon thread completes then the daemon thread terminates
automatically.
- For example, the Garbage Collector thread is a daemon thread.
What is the difference between Runnable and Callable?
|
Runnable |
Callable |
1. |
The runnable interface has the run() method which needs to override
an implemented class. |
The callable interface has a call() method which needs to override
an implemented class. |
2. |
Run() method cannot return a value. |
Call() method can return a value. |
3. |
Runnable cannot throw checked Exception. |
Callable can throw checked exception. |
4. |
Runnable cannot be used in Executor Framework. |
Callable can use in Executor Framework. |
5. |
public interface Runnable {
void run();
} |
public interface
Callable<V> {
V
call() throws Exception;
} |
Which scenario are the best example to implement of LinkedList and
ArrayList?
- LinkedList: If you want to
perform addition and deletion kind of operation in between the list then we
should go for LinkedList.
- ArrayList: If you want to
perform a searching, sorting kind of operation then we should go for
ArrayList.
What is SortedMap? How objects will be stored in SortedMap
bydefault?
-
SortedMap is a child interface of Map interface.
- The map is ordered according to the natural sorting order of its keys but
not the values.
- All keys inserted into a sorted map must implement the Comparable interface
(or be accepted by the specified comparator).
- TreeMap is the implementation class of the SortedMap interface.
- Please refer to the below example. The reference is SortedMap and its
implementation is TreeMap.
package simplifiedjava.crackedInterview;
import
java.util.SortedMap; import java.util.TreeMap; public class SortedMapDemo {
public
static
void
main(String[] args) {
SortedMap<Integer, String>
sMap
= new
TreeMap<Integer, String>();
sMap.put(1000, "Thousand");
sMap.put(1, "One");
sMap.put(10000, "Ten Thousand");
sMap.put(100, "Hundred");
sMap.put(10, "Ten");
System.out.println(sMap);
}
}
Output:
{1=One, 10=Ten, 100=Hundred, 1000=Thousand, 10000=Ten
Thousand} |
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on some support
projects and then I moved to java projects. I had worked with many
multi-national companies like R-Systems, Tata Consultancy
Services, Cybage Softwares. Fortunately, TCS and Cybage
has given me an opportunity to take interviews for experienced
candidates. I have conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments