Tuesday, 24 April 2012

JAVA interview questions 2

Q11.What is singleton class? where is it used? 
Singleton is a design pattern meant to provide one and only one instance of an object. Other 
objects can get a reference to this instance through a static method (class constructor is kept 
private). Why do we need one? Sometimes it is necessary, and often sufficient, to create a single instance of a given class. This has advantages in memory management, and for Java, in garbage collection. Moreover, restricting the number of instances may be necessary or desirable for 
technological or business reasons--for example, we may only want a single instance of a pool of database connections. 

Q12.What is a compilation unit? 
The smallest unit of source code that can be compiled, i.e. a .java file. 

Q13.Is strings a wrapper class? 
String is a class, but not a wrapper class. Wrapper classes like (Integer) exist for each primitive type. They can be used to convert a primitive data value into an object, and vice versa. 

Q14.Why java does not have multiple inheritances? 
The Java design team strove to make Java: 
• Simple, object oriented, and familiar 
• Robust and secure 
• Architecture neutral and portable 
• High performance 
• Interpreted, threaded, and dynamic 
The reasons for omitting multiple inheritance from the Java language mostly stem from the 
"simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a 
language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple). 
In the designers' opinion, multiple inheritance causes more problems and confusion than it 
solves. So they cut multiple inheritance from the language (just as they cut operator 
overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache. 

Q15.Why java is not a 100% oops? 
Many people say this because Java uses primitive types such as int, char, double. But then all the rest are objects. Confusing question. 

Q16.What is a resource bundle? 
In its simplest form, a resource bundle is represented by a text file containing keys and a text value for each key. 
* Core Java 
* Servlets & JSP 
* Struts 
* EJB 
* J2ME 

Q17.What is transient variable? 
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. 

Q18.What is Collection API? 
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. 

Q19.Is Iterator a Class or Interface? What is its use? 
Iterator is an interface which is used to step through the elements of a Collection. 

Q20.What is similarities/difference between an Abstract class and Interface? 
Differences are as follows: 
• Interfaces provide a form of multiple inheritance. A class can extend only one other class. 
• Interfaces are limited to public methods and constants with no implementation. 
Abstract classes can have a partial implementation, protected parts, static methods,etc. 
• A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. 
• Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast. 
• Neither Abstract classes or Interface can be instantiated. 

No comments:

Post a Comment