Java, J2EE & SOA Certification Training
- 37k Enrolled Learners
- Live Class
Java is today the most trusted language across the global developer community. With nearly all of the Fortune 1000 companies running applications on the Java Virtual Machine (JVM), Java has become a legendary language. Even while new languages and technology closely related to Java are popping up every now and then, they leverage capabilities of the JVM in some way.
It is almost certain that Java will never go out of fashion. A career in Java will open up multiple opportunities in a wide range of job roles. We at Edureka have curated a list of definitive Java questions that will help you breeze through your interview. In case you have attended Java interviews yourself or have any questions that you want us to answer for you, do feel free to add them as comments below.
|Abstraction||Abstraction is hiding the implementation details & only providing the functionality to the user|
|Encapsulation||Encapsulation is a mechanism of wrapping up the data and code together as a single unit|
|Inheritance||Inheritance is a process where one class acquires the properties of another|
|Polymorphism||Polymorphism is the ability of a variable, function or object to take multiple forms|
Immutable objects are those whose state cannot be changed once they are created. Any modification will result in a new object e.g. String, Integer, and other wrapper class.
The purpose of garbage collection is to identify and discard the objects that are no longer needed by the application to facilitate the resources to be reclaimed and reused.
Polymorphism is briefly described as “one interface, many implementations”. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism:
Compile time polymorphism is method overloading. Runtime time polymorphism is done using inheritance and interface.
Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters. On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.
You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it’s not accessible there. What you can do is create another private method with the same name in the child class.
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of the parent object. The idea behind inheritance in Java is that you can create new classes building upon existing classes. When you inherit from an existing class, you can reuse methods and fields of parent class, and you can also add new methods and fields.
Inheritance represents the IS-A relationship, also known as parent-child relationship.
Inheritance is used for:
The super keyword in Java is a reference variable that is used to refer the immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created i.e. referred by super reference variable.
Java super Keyword is used to refer:
Constructor in Java is a special type of method that is used to initialize the object. It is invoked at the time of object creation. It constructs the values i.e. provides data for the object and that is why it is known as constructor. Rules for creating Java constructor:
Types of Java constructors:
A constructor that has no parameter is known as default constructor.
Syntax of default constructor:
A class consists of Local Variable, Instance Variables and Class Variables.
The local variables are not initialized to any default value; neither primitives nor object references.
PATH is an environment variable used by the operating system to locate the executables. This is the reason we need to add the directory location in the PATH variable when we install Java or want any executable to be found by OS.
Classpath is specific to Java and used by Java executables to locate class files. We can provide the classpath location while running a Java application and it can be a directory, ZIP file or JAR file.
The static keyword denotes that a member variable or method can be accessed, without requiring an instantiation of the class to which it belongs. You cannot override static methods in Java, because method overriding is based upon dynamic binding at runtime and static methods are statically binded at compile time. A static method is not associated with any instance of a class, so the concept is not applicable.
Major difference between Heap and Stack memory are:
There are three ways of creating a Thread:
1) A class may extend the Thread class
2) A class may implement the Runnable interface
3) An application can use the Executor framework, in order to create a thread pool.
The Runnable interface is preferred, as it does not require an object to inherit the Thread class.
Synchronization refers to multi-threading. A synchronized block of code can be executed by only one thread at a time. As Java supports execution of multiple threads, two or more threads may access the same fields or objects. Synchronization is a process which keeps all concurrent threads in execution to be in sync. Synchronization avoids memory consistence errors caused due to inconsistent view of shared memory. When a method is declared as synchronized the thread holds the monitor for that method’s object. If another thread is executing the synchronized method the thread is blocked until that thread releases the monitor.
The ways of achieving thread safety in Java are:
Synchronized keyword can be applied to static/non-static methods or a block of code. Only one thread at a time can access synchronized methods and if there are multiple threads trying to access the same method then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object and thus prevents race condition.
A HashMap in Java stores key-value pairs. The HashMap requires a hash function and uses hashCode and equals methods in order to put and retrieve elements to and from the collection. When the put method is invoked, the HashMap calculates the hash value of the key and stores the pair in the appropriate index inside the collection. If the key exists then its value is updated with the new value. Some important characteristics of a HashMap are its capacity, its load factor and the threshold resizing.
String is immutable and final in Java, so a new String is created whenever we do String manipulation. As String manipulations are resource consuming, Java provides two utility classes: StringBuffer and StringBuilder.
Got a question for us? Please mention it in the comments section and we will get back to you.