Java is an extensive language and is also a highly sought after one. Many candidates face some simple but tough questions with respect to Java interviews. This section covers many of the possible Questions asked by interviewers as well as Answers for them.
Q. What is transient variable?
A. Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.
Q. What do you understand by Synchronization?
A. Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multi threaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption.
E.g. Synchronizing a function:
public synchronized void Method1 () {
// Appropriate method-related code.}
E.g. Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
// Synchronized code here.}}
Q. What is the Collection API?
A. The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.
Example of classes: HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
Example of interfaces: Collection, Set, List and Map.
Q. Is Iterator a Class or Interface?
A. Iterator is an interface which is used to step through the elements of a Collection.
Q. What are the similarities/difference between an Abstract class and Interface?
A. Differences: Interfaces provide a form of multiple inheritance. A given class can implement multiple interfaces but can only extend one abstract class.Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
Similarities: Neither Abstract classes or Interface can be instantiated.All abstract methods declared within them must be implemented by the first concrete class that implements / inherits them.
Q. What is an abstract Class?
A. It is a class that cannot be instantiated. It is defined using the keyword abstract. It must be extended in order to be used. There must be at least one abstract method in this class.
Q. What are the three mail principals of OOP? Explain them.
A. Inheritance, Polymorphism and Encapsulation
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
Inheritance is the process by which one object acquires the properties of another object.
The meaning of Polymorphism is something like one name many forms. Polymorphism enables one entity to be used as general category for different types of actions. The specific action is determined by the exact nature of the situation. The concept of polymorphism can be explained as "one interface, multiple methods".
Q. Explain garbage collection.
A. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program cannot directly free the object from memory, instead it is the job of the garbage collector to automatically free the objects that are no longer referenced by a program.
Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists.
You can explicitly call the System.gc() method but there is no guarantee that the garbage collector will execute.
Q. Does Java use "Pass by value" or "Pass by reference”?
A. Java uses "Pass by value". For primitive variables it just passes the value. For objects it passes the value of the reference.
int x =10;
int y=x;
y=20;
System.out.println(x); x--> 10
String a="aaa";
String b=a;
b="bbb";
System.out.println(a); a--> bbb
Integer q=10;
Integer r=q;
r=20;
System.out.println(q); q--> 10 (Wrappers are treated as primitives)
ColumnItem aaa= new ColumnItem ("aaa");
ColumnItem bbb= new ColumnItem("ccc");
bbb=aaa;
bbb.name="bbb";
System.out.println(aaa.name); aaa.name --> bbb
No comments:
Post a Comment