Part 7
Core java interview questions and answers for 3-5 years experience covered in this post:
Does the finally block get executed if
the try or catch block has a return statement? How the control flows in
this situation?
- Yes. finally{} block will be executed even though you have a return
statement in either try block or catch block.
- finally{} block will be executed prior to the return statement
execution.
- finally{} block will not be executed only in one condition. Once you invoke System.exit(0) your JVM will be crashed and the finally{} block will not be executed.
What is the difference between
Synchronization Method and Synchronized Block?
|
Synchronization Method |
Synchronized Block |
1. |
A method declared with a Synchronized keyword is called
Synchronized Method. |
Inside a method, if a chunk of code declared inside block with
Synchronized keyword is called a Synchronized block. |
2. |
Performance is low as compared to Block. |
Performance is high as compared to the method. |
If your child class is Serializable and
parent class is not and child class is inheriting some properties of
parent class, in that case can you serialize child class?
- Yes, In that case, we can serialize the child class object.
- But while serialization, JVM will ignore the original value of parent class
instance variables and save the default value to file.
- At the time of de-serialization, if any non-serializable superclass is
present then JVM will execute instance control flow in the superclass. To
execute instance control flow in a class, JVM will always invoke the
default(no-arg) constructor of that class. So every non-serializable
superclass must necessarily contain a default constructor, otherwise, we
will get InvalidClassException.
- Please refer to the below example.
package simplifiedjava.crackedInterview; public class Parent { public int i;
public
Parent() {
System.out.println("Parent Class no-arg constructor called."); }
public
Parent(int i) {
this.i = i; }
public void
check(){
System.out.println("Parent Class check() method");
} }
package
simplifiedjava.crackedInterview; import java.io.Serializable; public class Child extends Parent implements Serializable{ int j; public Child() {
}
public
Child(int i,int j) {
super(i);
this.j = j; }
public void
check(){
System.out.println("Addition in Child Class "+ (i + j));
} } package simplifiedjava.crackedInterview;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializationInHeritanceDemo { public static void main(String[] args) throws IOException, ClassNotFoundException {
Child c1 = new
Child(10,20);
System.out.println(c1.i +" and "+ c1.j);
FileOutputStream fos = new
FileOutputStream(new
File("abc.ser"));
ObjectOutputStream oos = new
ObjectOutputStream(fos);
oos.writeObject(c1);
FileInputStream fis = new
FileInputStream("abc.ser");
ObjectInputStream ois = new
ObjectInputStream(fis);
Child c2
=(Child) ois.readObject();
System.out.println(c2.i +" and "+ c2.j);
}
}
Output:
Values Before Serialization : 10 and 20
Parent Class no-arg constructor called.
Values After Deserialization : 0 and 20 |
Can we declare constructor inside
anonymous inner class?
- No, we cannot declare a constructor inside an anonymous inner
class.
- Anonymous inner class doesn’t have any name and the constructor name should
be the same as the class name. If the class name does not exist then how can
we create a constructor?
What is SortedSet? What is the purpose
of SortedSet?
- If we want to represent a group of individual objects
according to some sorting order without duplicate then we should go for
SortedSet.
- SortedSet interface is a child interface of the Set
interface.
- Null is not allowed in SortedSet implementation.
- All elements inserted into a sorted set must implement the
Comparable interface (or be accepted by the specified comparator).
- Please refer to the below example.
package simplifiedjava.crackedInterview;
import
java.util.SortedSet; import java.util.TreeSet; public class SortedSetDemo {
public static void
main(String[] args) {
SortedSet<Integer> set = new
TreeSet<Integer>();
set.add(50);
set.add(10);
set.add(30);
set.add(20);
set.add(40);
System.out.println(set);
}
}
Output: [10, 20, 30, 40, 50] |
What do you mean by Concurrency
Level?
- Concurrency divided map objects into 16 parts that smaller parts are known
as Concurrency level.
What is Generic Type Parameter?
- Type parameter is an identifier that specifies a generic type
name.
- Type parameter is also known as the Type variable.
- Type parameter can be used to declare the return type.
- Please refer to the syntax.
class
ArrayList<T>{
add(T t);
T get(int index); }
class
ArrayList<String>{
add(String s);
String get(int index);
} |
package
simplifiedjava.crackedInterview; public class GenericTemplate<T> { T obj;
public
GenericTemplate(T obj) {
this.obj = obj;
}
public
T getobj() {
return obj;
}
public void
show() {
System.out.println("Type of Object "+ obj.getClass().getName());
} } package simplifiedjava.crackedInterview; public class GenericDemo {
public static void
main(String[] args) {
GenericTemplate<String> str = new
GenericTemplate<String>("Yogesh");
str.show(); System.out.println(str.getobj());
GenericTemplate<Integer> numbers = new
GenericTemplate<Integer>(100);
numbers.show(); System.out.println(numbers.getobj());
GenericTemplate<Double> dblNum = new
GenericTemplate<Double>(25.50);
dblNum.show();
System.out.println(dblNum.getobj());
} }
Output:
Type of Object java.lang.String
Yogesh
Type of Object java.lang.Integer
100
Type of Object java.lang.Double 25.5 |
What is the return type of apply method
in Consumer functional interface?
- The return type of accept method is void.
- Accept() method only consumes the input and process it.
What is the difference between Abstract
class and Interface with Default methods?
|
Abstract class |
Interface with Default methods |
1. |
An abstract class can be declared with an abstract keyword. |
Interface with default method declared with interface keyword and
default keyword for method. |
2. |
An abstract class can have a constructor. |
An interface cannot have a constructor. Doesn’t matter interface
has a default method or not. |
3. |
An abstract class can have a static block or a non-static
block. |
Interface with the default method cannot have a static or
non-static block. |
4. |
Abstract class can’t refer to lambda expression. |
Interface with default methods refers to lambda expression. |
5. |
Inside the abstract class, we can override the objects class
method. |
We cannot override objects class method in interface with default
methods. |
6. |
An abstract class can be declared protected members. |
Interface with default methods cannot declare protected
members. |
What is the abstract method defined in
Function functional interface?
- Function interface has defined apply() as an abstract
method.
- Please refer to the below example for syntax.
@FunctionalInterface
public interface
Function<T, R> {
R apply(T t);
} |
- 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
Upcoming questions in next post:
71. What will happen If I directly override run method instead of start() method.
72. What is the difference between Regular Inner Class and Static Inner Class.
73. How to remove the duplicates from list object without stream API.
74. Can we use ConcurrentHashMap in place of Hashtable?
75. What is the difference between ConcurrentHashMap and SynchronizedHashMap.
76. How will you identify lambda expression.
77. What will happen if I declared multiple abstract methods in one interface.
78. How will you invoke static methods from class.
79. What is Bi-Function functional interface.
80. Wap to implement Bi-Function interface.
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