Most asked interview questions and answers for Java developers on Core Java
1. Which are OOPs concept in Java?
- Class
- Object
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Learn more about above concept with example :
2. What contain Real-world object?
Real-worls object contains State and Behavior.
3. What is Wrapper class in Java?
Wrapper classes used for converting Primitive Data Type to Objects.
Sometimes, you already seen terms like, Integer.parseInt() or Double.parseDouble(). So this are wrapper classes for converting our Primitive types to Object.
4. What is Autoboxing in Java?
The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, int to Integer, double to Double.
public class Demo {
public static void main(String[] args) {
int intPrimitive = 10;
char charPrimitive = '2';
// Done Autoboxing automatically from int to Integer
Integer intObject = intPrimitive;
System.out.println(intObject.getClass()+" : "+intObject);
System.out.println("int to Character : "+Character.getNumericValue(charPrimitive));
}
}
Output :
class java.lang.Integer : 10
int to Character : 2
5. What is Unboxing in Java?
Unboxing is the inverse of Autoboxing. It is the process of converting a Wrapper class object into its corresponding Primitive Datatype. For example, an Integer object would be converted to a primitive data type int.
public class Demo {
public static void main(String[] args) {
Integer wrapperInteger = 2;
//Done Unboxing, from Integer to int
int intPrimitive = wrapperInteger;
System.out.println(intPrimitive);
}
}
6. What is advantages of Wrapper classes in Java?
- The primary advantage of Wrapper Classes is that we need Wrapper objects to function with collections which is only possible with the help of Wrapper classes.
- Wrapper classes have objects we can store null as a value. We could not store null in variables of primitive datatype.
7. What is Constructor in Java?
A class contains constructors that are invoked to create objects from the class blueprint.
In simple term, Constructor is a block of code that initializes the newly created object.
Constructor declarations look like method declaration, except that they use the name of the class and have no return type.
8. List rules of Constructor.
- Constructor name should be same as Class name
- Constructor does not contains any return type.
9. What happens when we does not define any constructor in java?
A default (no-argument) constructor is automatically created if we does not define any constructor in Java.
10. When we will get "The constructor is undefined" Error in Java?
If we write any constructor other than no-argument constructor, then compiler does not provided default no-arg constructor. And when we try to compile that code it gives "The constructor is undefined" error.
Refer following code.
public class Demo {
private int intPrimitive;
Demo(int var1) {
this.intPrimitive = var1;
}
public static void main(String[] args) {
// The line below will cause a compile time error.
// Here we get "The constuctor Demo() is undefined" Error
Demo obj = new Demo();
System.out.println(obj.intPrimitive);
}
}
If we define following no-argument constructor in above code, then above error resolve.
public Demo() {
}
11. What if you implement only parameterized constructor in class?
It will throw a compilation error as we seen in above question. The reason is, the statement Demo obj = new Demo(); is invoking a default constructor which we don’t have in our above code.
12. Can we define more than 1 Constructor in Java?
Yes, we can define more than 1 Constructor in Java (with different arguments).
13. Can an abstract class have a constructor in Java?
Yes, an abstract class can have a constructor.
14. Can an Interface contains constructor in Java?
No, Interface does not contains constructor in Java
15. What is access specifier for Default constructor in Java?
Default constructor have Public access specifier.
16. Can we declare Private constructor in Java?
Yes, we can declare Private constructor in Java. And private constructor have scope only within class.
17. Can we overload Constructor? or Is Constructor overloading Possible in Java?
Yes, we overload constructor in Java. we can create multiple constructors with same name but with different parameters in class.
Learn more about Method Overloading in Java for understand Overloading concept.
18. Is Constructor overriding Possible in Java?
No, Constructor overriding is not possible in Java
Constructor looks like a method but name should be as class name and no return value.
Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding. Super class name and Sub class names are different.
Learn about Method overriding concept.
19. What is use of this keyword?
‘this’ is a reference variable that refers to the current object.
Most programmers use when there's an overlapping local variable with the same name. (for example, Setter methods).
20. What is super keyword?
The super keyword in Java is a reference variable which is used to refer immediate parent class object.
super() is added in each class constructor automatically by compiler if there is no super() or this().
We can use super keyword with following condition :-
- Variables - refer parent class instant variable
- Methods - refer parent class methods
- Constructors - refer parent class constructors
Learn deeply about super keyword in Java with Examples :
21. Can we use super() and this() both in constructor?
No, we can not. Because super() and this() must be at first line of constructor.
If we write both then, compiler gives following error.
Constructor call must be the first statement in a constructor
22. What is the output of following code :
class Parent {
void method() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
void method() {
System.out.println("Child class method");
}
}
class Demo {
public static void main(String[] args) {
Parent obj = new Child();
obj.method();
}
}
Output :
Child class method
Explanation :
the method of Parent class, method() is overriden in Child class. So it is concept of runtime polymorphism. In Demo class we are creating object of Child class with Parent reference. So At runtime Child class method is called.
23. What is the output of following code :
class Parent {
static void method() {
System.out.println("Parent class method");
}
}
class Child extends Parent {
static void method() {
System.out.println("Child class method");
}
}
class Demo {
public static void main(String[] args) {
Parent obj = new Child();
obj.method();
}
}
Output :
Parent class method
Explanation :
We are using static method and static method can not be overridden. So Parent class method is called.
Learn more about Overriding in Java :
24. Can we declare Constructor as final?
Short Answer :- No Java constructor can not be declared as final.
Long answer :- Constructors are not inherited, so can't be overridden and the final keyword restricts further modification so there is no point for restriction on constructor.
If we try to declare constructor as final, compiler gives error.
25. Can we declare interface as final?
Short Answer :- No, interface can not be declare as final in Java.
Long Answer :- Interface contains only abstract methods, So we need a class that implements interface and provides body to abstract methods. If we declare interface as final, we can not implements interface so interface can not be final in Java.
Learn more about Interface :
26. What is Garbage Collector in Java?
The garbage collector is a component of the Java virtual machine which is responsible for reclaiming memory from dead objects. It's one of the key components and allows an application developer to focus on application development rather than doing memory management.
27. How can we call Garbage Collector (GC) explicitly in Java?
System.gc()
OR
Runtime.getRuntime().gc()
28. What is static binding in Java?
The binding which resolves at compile time (by compiler) known as Static binding in Java. Static binding also known as Early binding.
Binding of all the static, private and final methods is done at compile-time.
Method Overloading is best example of static binding.
Method overloading is determined at compile time. The compiler decides, based on the compile time type of the parameters passed to a method call, which method having the given name should be invoked.
29. What is dynamic binding in Java?
When the binding can not done at compile time known as Dynamic binding.
Dynamic binding also known as late binding.
Method overriding is best example of dynamic binding.
Method overriding determined by the runtime type of an object. At runtime, the method that gets executed can be a method of some sub-class that wasn't even written when the code that makes the call was compiled.
30. What is instanceOf in Java?
The instanceOf is used for compare instance type in Java. It return true or false.
String str = "Java";
//returns true
System.out.println(str instanceof String);
31. Difference between == and equals() in Java?
- == (double equals to) used for reference comparison.
- equals() used for Value comparison.
Related Articles :
- Abstract class and Interface interview questions and answers in java
- Java interview questions and answers on Static keyword
- Object Oriented Programming (OOP) concept in Java with Examples
Comments
Post a Comment