Basic Java Interview Questions and Answers covered in this
post:
Which is the top most Parent class in
Java? What the methods of Object Class?
- Java.lang.Object is the parent class of java hierarchy.
- Following are the defined methods in Object class.
1. public final Class getClass() : returns the Class
class object.
2. public String toString(): Convert object to
String.
3. public boolean equals(): Comparing this object to another
object.
4. public int hashCode(): return the hash code of
object.
5. protected Object clone(): return the duplicate
object.
6. public final void wait(): put the thread into waiting
area.
7. public final void notify(): notify the waiting or
sleeping thread.
8. public final void notifyAll(): notify all the waiting and sleeping thread.
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 null keyword in java?
- No. null is not a keyword in java. Null is like literals like
true and false in java.
What is Actual Parameter and what is Formal Parameter?
- Actual Parameters: If we pass the parameters while
calling a function are called Actual Parameter.
- Formal Parameter: If we are catching parameters in
function are called formal parameters.
package simplifiedjava.crackedInterview; public class ActualAndFormalParameters {
public static void
main(String[] args) {
int a
= 10;
int b
= 20;
addition(a,b); // a and b are Actual Parameter }
public static void
addition(int num1, int num2) {
// num1 and num2 are formal parameters
}
} |
What is the difference between Local
variable and Instance variable?
|
Local Variable |
Instance Variable |
1. |
Local variable declares inside the method, block and
constructor. |
Instance variable declared outside the method, block and
constructor but inside the class. |
2. |
We have to manually initialize the local variable before use. |
When instance variable gets created default value assigned to that
variable. |
3. |
Scope of local variable up to method execution. |
Instance variable scope is wider than the local variable. |
What is the difference between Instance
variable and Class Variable?
|
Instance Variable |
Class Variable |
1. |
The instance variable belongs to the object. |
The Class variable belongs to the class. |
2. |
Every object has its copy of the instance variable. |
The class variable is a common variable among all objects. |
3. |
Instance variable declared with data type only. |
A class variable is declared with data type and static keyword. |
4. |
Instance variable access with reference variable. |
Class variable access with the class name. |
What are the uses of this keyword?
Where it is applicable and what are the rules to use this
keywords?
- this keyword is used to refer to the current object.
- this keyword can be used to the invoked current class
constructor.
- this keyword can be used to the invoked current method.
- this can be used to return the current class object.
- this can be used to pass as an argument in method as well as
constructor.
What are the uses of the super keyword?
Where it is applicable and what are the rules to use this
keywords?
- The super keyword can be used to refer parent class instance
variable.
- Super keyword can be used to invoke the parent class
method.
- Super keyword can be used to invoke parent class
constructor.
What are the access modifiers and their
scope?
- There are four access modifiers which are as follows.
1. Default
2. Private
3. Protected
4. Public
- Scope:
Access Modifier |
Within Class |
Within Package |
Subclass in another package |
Outside package |
Default |
Y |
Y |
N |
N |
Private |
Y |
N |
N |
N |
Protected |
Y |
Y |
Y |
N |
Public |
Y |
Y |
Y |
Y |
Can we use static members inside the
non-static area?
- Yes. We can use static members inside the non-static
area.
- We can observe this in the following demo.
package simplifiedjava.crackedInterview;
public class
Parent { static int a = 10;
public static void
main(String[] args) {
new
Parent().check(); }
public void
check(){
System.out.println("Parent Class check() method = "+ a);
}
}
Output : Parent Class check() method = 10 |
- 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:
21. What is static import.
22. where the static variables are stored in java.
23. What is diamond problem in Java. Why java doesn't support multiple inheritance.
24. What is marker interface? Can you tell me the example of marker interface?
25. What are the wrapper classes in java. Why do we require wrapper classes.
26. What is variable shadowing?
27. What is static method shadowing?
28. What is object cloning.
29. What is covarient return type.
30.What is the purpose of final keyword where it can be applicable.
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