Part 1
Core java interview questions for 3 years experience Covered in this post:
Will the code compile If the parent
class method is not throwing any exception but the child class method
is throwing a checked exception?
-
No Class won’t be compiled. We will get a compile-time
error.
-
We can see the below example.
package simplifiedjava.crackedInterview; public class Parent {
public
void
check(String relation) {
System.out.println("Parent Class check() method.");
}
} package simplifiedjava.crackedInterview; import java.io.FileNotFoundException; public class Child extends Parent{
public
void
check(String relation)throws
FileNotFoundException {
System.out.println("Child Class check() method.");
}
}
Output : Compile time error:
Exception FileNotFoundException is not compatible with throws
clause in Parent.check(String) |
Will the parent class constructor be
invoked while you have invoked the child class constructor without a
super keyword?
- Yes. If you create an object of child class and call child class constructor. Child class constructor internally calls parent class constructor implicitly.
package simplifiedjava.crackedInterview; public class OverrideOverloadedMethodDemo {
public static void
main(String[] args) {
Child c
= new
Child();
} } package simplifiedjava.crackedInterview; public class Parent {
public
Parent() {
System.out.println("Parent Class Constructor");
} } package simplifiedjava.crackedInterview; public class Child extends Parent{
public
Child() {
System.out.println("Child Class Constructor...");
} }
output :
Parent Class Constructor
Child Class Constructor... |
Can I declare the constructor as an
abstract?
- Constructors cannot be abstract.
- Abstract keyword can be applied to class and method, not constructor or blocks.
Can we override or overload the
constructor?
- We cannot override the constructor but we can overload the
constructor.
- Constructor overriding is not possible because the constructor must have
the same name of the class and doesn’t have any return type. But for
overriding signature must be same. So ultimately overriding is not
possible.
- Constructor overloading is possible because the constructor name is the
same as class name and type of parameter and no of parameters are
different so constructor overloading is allowed.
What is exception propagation?
- An Exception is thrown from the current method and if it is not handled
then it drops to the previous method. If that exception is not handled in
the previous method as well then it drops to the previous method and so
on. If you never handle that exception then that exception reached to main
method or base method which is called exception propagation.
- Please refer to the below diagram of exception propagation.
Current Method |
Exception occurred in this function and it is not
handled. |
m4() |
Exception not handled in current method so dropped in method
m4(). |
m3() |
Exception not handled in m4() method so dropped in method
m3(). |
m2() |
Exception not handled in m3() method so dropped in method
m2(). |
m1() |
Exception not handled in m2() method so dropped in method
m1(). |
main() or base() |
Exception not handled in m1() so dropped in main() method and the
program terminates abruptly. |
Can we write a try-catch block inside
a catch block? What is the purpose to write try block inside catch
block?
- Yes. We can write a try-catch block inside the catch block.
- Generally, we are required to try/catch block inside catch block to close
the connections or close the statements or flush the data from statements
and connections.
What is the abstract method defined
in predicate functional interface?
- Predicate has a defined test() method which is only one abstract
method.
- Test method accepts only one parameter to test it.
- Please refer to the below syntax for Predicate.
@FunctionalInterface
public interface
Predicate<T> {
boolean
test(T t);
} |
What is the difference between Normal
class and Inner class?
|
Normal Class / Outer Class |
Inner Class |
1. |
An outer class can be instantiated without instantiating an inner
class. |
Non-static Inner class cannot be instantiated without
instantiating an outer class. |
2. |
The outer class does not totally depend on the inner class. |
An inner class totally depends on the outer class. |
3. |
An outer class cannot be private. |
An inner class can be private. |
4. |
An outer class cannot be static. |
An inner class can be static. |
5. |
An outer class cannot be protected. |
An inner class can be protected. |
What is the difference between
HashSet and TreeSet?
|
HashSet |
TreeSet |
1. |
The data Structure of HashSet is HashTable. |
Data Structure of TreeSet is a Balanced Tree. |
2. |
Heterogeneous (Different types of objects) objects are
allowed. |
Heterogeneous (Different types of objects) objects are not
allowed. |
3. |
HashSet doesn’t maintain any sorting order. |
TreeSet maintains natural sorting order. |
What is the difference between Stack
Memory and Heap Memory?
|
Stack Memory |
Heap Memory |
1. |
Stack memory is mainly used for storing the order of method
execution and local variables. |
Heap memory is mainly used for storing actual objects created
with a new keyword. |
2. |
Stack memory works with the LIFO mechanism. |
There is no mechanism to allocate and deallocate the memory in
heap. |
3. |
Stack variables are visible only to the owner thread. |
Objects are visible to all threads. |
4. |
We can increase stack memory size by using –XSS parameter. |
We can modify the size of heap memory by using Xms and –Xmx. |
5. |
If the stack gets full and still trying to add more elements in
it then it will throw StackOverFlowError. |
If JVM gets full then it will throw OutOfMemoryError. |
- 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:
1. Can you declare constructor static, final, abstract and synchronized.
2. How does object become an eligible for garbage collection.
3. How thread communicate each other.
4. Can you tell me what are the Serializable and Externalizable?
5. Can we declare outer class is private.
6. What is Inner Class.
7. What is ArrayList and what are the characteristics of ArrayList.
8. What is the abstract method defined in Function functional interface.
9. What is the return type of apply method in Function functional interface.
10. What is Bi-Predicate functional 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