Core Java Interview Questions and Answers
1.Whta is Final Variable?
If you make any variable as final, you cannot change the value of final variable.
2.What is final Method?
Final method can’t be overridden.
3.What is Final class ?
Final Class Can’t be inherited.
4.What is Constructor ?
Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.
5.What is the puspose of default constructor?
The Default Constructor provide the default values to the objects.The java compiler creates a default constructor only if there is no constructor in the class.
6.Can you make a constructor final?
No,Constructor can’t be final.
7.What is static variable ?
* Static variable is used to refer the common property of all objects (that is not unique for each object)
* Static variable gets memory only once in class area at the time of class loading.
8.What is Static Method
* A ststic method belongs to the class rather than object of a class.
* A static method can be invoked without the need for creating an instance of a class.
* static method can access static data member and can change the value of it.
9. Why main method is static?
Because object is not required to call static method if It were non static method, jvm creats object first then call main() method that will lead to the problem of extra memory allocation.
10.What is static block?
* Is used to initialize the static data member.
* It is excuted before main method at the time of classloading.
11. What is difference between static (Class)method and instance method?
*A method i.e. declared as static is known as static method.
*Object is not required to call static method.
* Non-static (Instance) members cannot be accessed in static context(Static block and static nested class) directly.
· A method i.e. not declared as static is know as instance method.
· Object is required to call instance methods.
· Static and non-static variable both can be accessed in instance methods.
12.Why multiple inheritance is not supported in java?
To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.
13. What is method overloading?
If a class have multiple methods by same name but different parameters, it is known as method overloading. It increases the readability of the program.
14.Can we overload main() method?
Yes , ofcourse! You can have many main() methods in a class by overloading the main method.
15. What is method overriding?
* If a subclass provides a specifiv implementation of a method that is already provided by its parent class,it is known as method overloading.
* It is used for runtime polymorphism and to provide the specific implementation of the method.
16.Can we override static method?
No, you can’t override the static method because they are the part of class not oject.
17. Why we cannot override static method?
It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.
18. Difference between method Overloading and Overriding?
*.Method overloading increases the readability of the program.
*.Method overloading is occurs within the class.
*In this case, parameter must be different.
* Method overriding provides the specific implementation of the method that is already provided by its super class.
* Method overriding occurs in two classes that have IS-A relationship.
* In this case, parameter must be same.
19. What is polymorphism?
* Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather that at compile-time.
*In this process, an overridden method is called through the reference variable of a superclass.
*The determination of the method to be called is based on the object being referres to by the reference variable.
20.What is Abstraction?
*Abstraction is a process of hiding the implementation details and showing only functionality to the user.
* Abstraction lets you focus on what the object does instead of how it does it.
21. What is abstract class ?
*A class that is declared as abstract is known as abstract class.
* It needs to be extended and its method implemented.
* It cannot be instantiated.
22.can there be any abstract method without abstract class?
No, if there is any abstract method in a class, that class must be abstract.
23. Can you use abstract and final both with a method?
No Because abstract method needs to be overridden whereas you can’t override final method.
24.What is Interface?
*Interface is a blueprint of a class that have static constants and abstract methods.
* it can be used to achive fully abstraction and multiple inheritance.
25. Can you declare an interface method static ?
No, because methods of an interface is abstract bydefault, and static and abstract keywords can’t be used together.
26.What is different between abstract class and interface?
*An abstract class can have method body(non-abstract methods).
*An abstract class can have instance variables.
*An abstract class can have constructor.
*An abstract class can have static methods.
*You can extends one abstract class.
*Interface have only abstract methods.
*An interface cannot have instance variable.
*Interface cannot have constructor.
*Interface cannot have static methods.
*You can implement multiple interfaces.
27.What is package?
*A package is a group of similar type of class interfaces and subpackages.
*It provides access protection and removes naming collision.
28. What is Exception Handling?
*Exception handling is a mechanism to handle errors.
*It is mainly used to handle checked exception.
29.What is difference between checked exception and Unchecked Exception?
*The classes that extend throwable class except RuntimeException and Error are Known as checked excetion e.g.IOExcetion,SQLExcetion etc. Checked exception are checked at complie-time.
The classes that extend RuntimeExcetion are known as unchecked exception
e.g. ArithmeticException,NullpointerException etc.
*Unchecked exception are not checked at complie-time.
30.What is Difference Between throw and throws?
*throw is used to expilicity throw an exception.
*checked exception can not be propagated with throw only.
* throw is followed by an instance.
*throw is used within the method.
*you cannot throw multiple exception.
*throws is used to declare an exception
*checked exception can be propagated with throws.
* throws is followed by lcass.
*throws is used with the method signature.
*You can declare multiple exception e.g.
public void method() throws IOException,SQLException.
31.What is the meaning of immutable in terms of String?
*The simple meaning of immutable is unmodifiable or unchangeable.
*Once string object has been created, its values can’t be changed.
32.basic difference between string and string buffer object?
*String is an immutable object.
*String is a mutable object.
33.Difference between StringBuffer and StringBuilder?
String Buffer is synchronized whereas StringBuilder is not synchronized.
34.What is multithreading?
Multithreading is a process of executing multiple threads simultaneously.
Its main advantage is:
*Threads share the same address space.
*Thread is lightweight.
*Cost of communication between process is low.
35.What is thread?
*A thread is a lightweight subprocess.It is a separate path of execution .
*It is called separate path of execution because each thread runs in a separate stack frame.
36.What is synchronization?
Synchronization is the capability of control the access of multiple threads to any shared resource .
It is used
1.To prevent thread interference.
2.To prevent consistency problem.
37.What is the purpose of synchronized block?
1.Synchronized block is used to lock an object for any shared resource.
2.Scope of synchronized block is smaller than the method.
38.What is Garbage Collection?
*Garbage collection is a process of reclaiming the runtime unused objects.
*It is performed for memory management.
39.What is the purpose of finalize() method?
*finalize() method is invoked just before the object is garbage collected.
* It is used to send request to JVM to perform garbage collection.
40.Different between final,finally and finalize?
Final: final is a keyword , final can be variable , method or class. You can,t change the value of final variable, can’t override final method, can’t inherit final class.
Finally : finalyy block is used in exception handling. Finally block is always executed.
Finalize(): finalize() method is used in garbage collection. finalize() method is invoked just before the oject is garbage collected. The finalize() method can be used to perform any cleanup processing.
41.What is serialization ?
Serialization is a process of writing the state of an object into a byte stream.
42.What is difference between ArrayList and Vector?
*ArrayList is not synchronized.
2.ArrayList is not a legacy class.
3.ArrayList increases its size by 50% of the array size.
*Vector is synchronized
*Vector is a legacy class.
3.Vector increases its size by doubling the array size
43.Different Between HashMap and Hashtable?
*HashMap is not synchronized
*HashMap can contain one null key and multiple null values.
*Hashtable is synchronized.
*hashtable cannot contain any null key nor value.
44.Different Between Iterator and Enumeration?
*Iterator can traverse legacy and non-legacy elements.
*Iterator is fail-fast
*Iterator is slower than Enumeration
*Enumeration can traverse only legacy elements.
*Enumeration is not fail-fast.
*Enumeration is faster than Iterator.
45.What is singleton class?
Singleton class means that any given time only one instance of the class is present,in one JVM.