Archive

Posts Tagged ‘Java interview Questions’

Interview Questions : Java Fundamentals

June 30th, 2009

1. Give a few reasons for using Java?

a.  Java is a fun language. Let’s look at some of the reasons:

  • Built-in support for multi-threading, socket communication, and memory management (automatic garbage collection).
  • Object Oriented (OO).
  • Better portability than other languages across operating systems.
  • Supports Web based applications (Applet, Servlet, and JSP), distributed applications (sockets, RMI. EJB etc) and network protocols (HTTP, JRMP etc) with the help of extensive standardised APIs (Application Program Interfaces).

2. What is the main difference between the Java platform and the other software platforms?

a.  Java platform is a software-only platform, which runs on top of other hardware-based platforms like UNIX, NT etc. ava Virtual Machine (JVM) – ‘JVM’ is a software that can be ported onto various hardware platforms.  Byte codes are the machine language of the JVM.

3. What is the difference between C++ and Java?

a.  Both C++ and Java use similar syntax and are Object Oriented, but:

  • Java does not support pointers. Pointers are inherently tricky to use and troublesome.
  • Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.
  • Java does not support destructors but rather adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method.
  • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework (Collection Framework)
  • All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions.
  • C++ requires explicit memory management, while Java includes automatic garbage collection.

4. What are the advantages of Object Oriented Programming Languages (OOPL)?

a. The Object Oriented Programming Languages directly represent the real life objects like Car, Jeep, Account, Customer etc. The features of the OO programming languages like polymorphism, inheritance and encapsulation make it powerful.

[Tip: remember pie which, stands for Polymorphism, Inheritance and Encapsulation are the 3 pillars of OOPL]

5. What is the difference between aggregation and composition?

a.  Aggregation is an association in which one class belongs to a collection. This is a part of a whole relationship where a part can exist without a whole.
For example a line item is a whole and product is a part. If a line item is deleted then corresponding product need not be deleted. So aggregation has a weaker relationship.

Composition is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted.
For example An order is a whole and line items are parts. If an order deleted then all corresponding line items for that order should be deleted. So composition has a stronger relationship.

6. How does the Object Oriented approach improve software development?

a. The key benefits are:

  • Re-use of previous work: using implementation inheritance and object composition.
  • Real mapping to the problem domain: Objects map to real world and represent vehicles, customers, products etc: with encapsulation.
  • Modular Architecture: Objects, systems, frameworks etc are the building blocks of larger systems.

The increased quality and reduced development time are the by-products of the key benefits discussed above. If 90% of the new application consists of proven existing components then only the remaining 10% of the code
have to be tested from scratch.

7. Why there are some interfaces with no defined methods (i.e. marker interfaces) in Java?

a. The interfaces with no defined methods act like markers. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. Example Serializable, Cloneable etc

8. When is a method said to be overloaded and when is a method said to be overridden?
a. Method Overloading :

  • Overloading deals with multiple methods in the same class with the same name but different method signatures.

class MyClass {
public void initParams(int param) {…}
public void initParams(int param1, long param2)
{ … }
}

  • Both the above methods have the same method names but different method signatures, which mean the methods are overloaded.
  • Overloading lets you define the same operation in different ways for different data.

Method Overriding:

  • Overriding deals with two methods, one in the parent class and the other one in the child class and has the same name and signatures.

class BaseClass{
public void initParams(int param) {…}
}
class MyClass extends BaseClass {
public void initParams(int param) { …}
}

  • Both the above methods have the same method names and the signatures but the method in the subclass MyClass overrides the method in the superclass BaseClass.
  • Overriding lets you define the same operation in different ways for different object types.

9. What is the main difference between an ArrayList and a Vector? What is the main difference between Hashmap and Hashtable?

a. Vector/Hashtable: Original classes before the introduction of Collections API. Vector & Hashtable are synchronized. Any method that touches their contents is thread-safe.
ArrayList/Hashmap: So if you don’t need a thread safe collection, use the ArrayList or Hashmap. Why pay the price of synchronization unnecessarily at the expense of performance degradation.

10. So which is better?

a. As a general rule, prefer ArrayList/Hashmap to Vector/Hashtable. If your application is a multithreaded application and at least one of the threads either adds or deletes an entry into the collection then use new Java collection API‘s external synchronization facility as shown below to temporarily synchronize your collections as needed:

Map myMap = Collections.synchronizedMap (myMap);
List myList = Collections.synchronizedList (myList);

Java arrays are even faster than using an ArrayList/Vector and perhaps therefore may be preferable. ArrayList/Vector internally uses an array with some convenient methods like add(..), remove(…) etc.

S.Chandru Java , , , , ,

Interview Questions : OOPs and Core Java

April 16th, 2009

OOPS and CORE JAVA

1. What is JVM (Java Virtual Machine)?

a. JVM is software implementation which stands on top of the hardware platform and OS.  It provides abstraction compiled java program and the hardware and OS. The Java virtual machine executes instructions that a Java compiler generates. All the Java programs are compiled into bytecodes(.class files). JVM  interpret Java bytecodes into instructions understandable by the particular processor.

2. What is OOPs?

a. Object-Oriented Programming (OOP) is a programming language model orgnized around the “Objects” rather than the “actions” and data rather than the logic. Programming techniques may include data abstraction, encapsulation, inheritance, information hiding, modularity and polymorphism.

3. What is Class?

a. Class is a template that describes the kinds of state and behavior that object of its type supports.

4. What is Object?

a. Object is run time entity and will have its own state and access to all of its behaviors defined by its class. At run time when JVM encounters the “new” keyword , it will use appropriate class create a object which is instance its class.

5. What is Interface?

a. Interface is 100% abstract super class that defines the methods a subclass must support, but not how they supported. A powerful companion to inheritance is the use of Interfaces.

6. What is abstract class?

a. Abstract class can never be instantiated, its sole purpose is to be extended(to subclassed). It is incomplete class.

7. What is the purpose of abstract class?

a. Consider a abstract class represent Person, it has three methods “numOfEyes”, “numOfLegs” and “sex”. Now conceptually all humans have to two legs and two eyes, but sex is something which can vary from person to person. So write implementaton for methods “numOfEyes” and “numOfLegs” and leave the method “sex” implemetation to subclass. Now class Male and class Female extends Person, implementation for method “sex” of Female is implemented in class Female and of Male is implemented in class Male.

8. What are the abstract methods?

a. Abstract methods do not have implementation. Abstrct methods should be implemeted in the subclass which inherits them. The class inherits abstract class should implement all the abstract methods or else java compiler will throw an error. Abstract class can have abstract methods. Abstract methods defined using “abstract” keyword.

9. What is state (instance variables) in Java?

a. State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object’s instance variables make up the object’s state.

10. What is Behavior (methods) in Java?

a. Behavior (methods) When a programmer creates a class, she creates methods for that class. Methods are where the class’ logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

11. What is relation between Classes and Objects?

a. They look very much same but are not same. Class is a definition, while object is instance of the class created. Class is blue print or prototype while objects are actual existing in real world. For example  CAR is class and Mustang is object of the class.

12. What is the difference between “Abstract” classes and “Interfaces”?

a. Abstract classes can only be extended by any class while Interface can not be, it has to be, implemented. Interfaces can not implement methods, whereas an abstract class can have implementation. Class can implement any number of interfaces but only one super class.

13. What is difference between Static and Non-Static fields of class?

a. Static fields are fields per class whereas Non-Static fields are fields per object of the class. Non-Static values are called as instance variables. Each object of the class has its own copy of Non-Static instance variables. So when a new object is created of the same class it will have completely its own copy of instance variable. Where in Static values have only one copy of instance variables and will be shared among all the objects of the class.

14. What is use of “instanceof”  keyword?

a. “instanceof” keyword is used to check what is the type of the object.

15. How do refer to a current instance of object?

a. You can refer the current instance of object using this” keyword.

S.Chandru Java ,