Q. What do you need to do to run a class with a main() method in a package?
Example: Say, you have a class named “Pet” in a project folder “c:\myProject” and package named
com.xyz.client, will you be able to compile and run it as it is?
package com.xyz.client;
public class Pet {
public static void main(String[] args) {
System.out.println("I am found in the classpath");
}
}
To run �� c:\myProject> java com.xyz.client.Pet
The answer is no and you will get the following exception: “Exception in thread "main" java.lang.-
NoClassDefFoundError: com/xyz/client/Pet”. You need to set the classpath. How can you do that? One of the
following ways:
1. Set the operating system CLASSPATH environment variable to have the project folder “c:\myProject”.
2. Set the operating system CLASSPATH environment variable to have a jar file “c:/myProject/client.jar”, which has the Pet.class file in it.
3. Run it with –cp or –classpath option as shown below:
c:\>java –cp c:/myProject com.xyz.client.Pet
OR
c:\>java -classpath c:/myProject/client.jar com.xyz.client.Pet
Important: Two objects loaded by different class loaders are never equal even if they carry the same values, which mean a class is uniquely identified in the context of the associated class loader. This applies to singletons too, where each class loader will have its own singleton.
Q : 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]
Q : 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.
Q : is the difference between composition and aggregation?
A : The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.
Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom. Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.
Q. Which one to favor, composition or inheritance?
The guide is that inheritance should be only used when subclass ‘is a’ superclass.
�� Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if the superclass is modified.
�� Do not use inheritance just to get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism then use interface inheritance with composition, which gives you code reuse .
Q : What do you mean by polymorphism, inheritance, encapsulation, and dynamic binding?
A : Polymorphism – means the ability of a single variable of a given type to be used to reference objects of different types, and automatically call the method that is specific to the type of object the variable references. In a nutshell, polymorphism is a bottom-up method call. The benefit of polymorphism is that it is very easy to add new classes of derived objects without breaking the calling code uses the polymorphic classes or interfaces. When you send a message to an object even though you don’t know what specific type it is, and the right thing happens, that’s called polymorphism. The process used by object-oriented programming languages to implement polymorphism is called dynamic binding.
Inheritance – is the inclusion of behavior (i.e. methods) and state (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. The key benefit of Inheritance is that it provides the formal mechanism for code reuse. Any shared piece of business logic can be moved from the derived class into the base class as part of refactoring process to improve maintainability of your code by avoiding code duplication. The existing class is called the
superclass and the derived class is called the subclass. Inheritance can also be defined as the process whereby one object acquires characteristics from one or more other objects the same way children acquire characteristics from their parents. There are two types of inheritances:
1. Implementation inheritance (aka class inheritance): You can extend an application’s functionality by reusing functionality in the parent class by inheriting all or some of the operations already implemented. In Java, you can only inherit from one superclass. Implementation inheritance promotes reusability but improper use of class inheritance can
cause programming nightmares by breaking encapsulation and making future changes a problem. With implementation inheritance, the subclass becomes tightly coupled with the superclass. This will make the design fragile because if you want to change the superclass, you must know all the details of the subclasses to avoid breaking them. So when using implementation inheritance, make sure that the subclasses depend only on the behavior of the superclass, not on the actual implementation. For example in the above diagram, the subclasses should only be concerned about the behavior known as area() but not how it is implemented.
2. Interface inheritance (aka type inheritance): This is also known as subtyping. Interfaces provide a mechanism for specifying a relationship between otherwise unrelated classes, typically by specifying a set of common methods each implementing class must contain. Interface inheritance promotes the design concept of program to interfaces not to implementations. This also reduces the coupling or implementation dependencies between systems. In Java, you can
implement any number of interfaces. This is more flexible than implementation inheritance because it won’t lock you into specific implementations which make subclasses difficult to maintain. So care should be taken not to break the implementing classes by modifying the interfaces.
Q.What is garbage collection? What is the process that is responsible for doing that in java?
Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process
Q. What kind of thread is the Garbage collector thread?
It is a daemon thread.
Q. What is a daemon thread?
These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.
Q. How will you invoke any external process in Java?
Runtime.getRuntime().exec(….)
Q. What is the finalize method do?
Before the invalid objects get garbage collected, the JVM give the user a chance to clean up some resources before it got garbage collected.
Q. What is mutable object and immutable object?
If a object value is changeable then we can call it as Mutable object. (Ex., StringBuffer, …) If you are not allowed to change the value of an object, it is immutable object. (Ex., String, Integer, Float, …)
Q. What is the basic difference between string and stringbuffer object?
String is an immutable object. StringBuffer is a mutable object.
Q. What is the purpose of Void class?
The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the primitive Java type void.
Q. What is reflection?
Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions.
Q. What is the base class for Error and Exception?
Throwable
Q. What is the byte range?
-128 to 127
Q. What is the implementation of destroy method in java.. is it native or java code?
This method is not implemented.
Q. What is a package?
To group set of classes into a single unit is known as packaging. Packages provides wide namespace ability.
Q. What are the approaches that you will follow for making a program very efficient?
By avoiding too much of static methods avoiding the excessive and unnecessary use of synchronized methods Selection of related classes based on the application (meaning synchronized classes for multiuser and non-synchronized classes for single user) Usage of appropriate design patterns Using cache methodologies for remote invocations Avoiding creation of variables within a loop and lot more.
Q. What is a DatabaseMetaData?
Comprehensive information about the database as a whole.
Q. What is Locale?
A Locale object represents a specific geographical, political, or cultural region
Q. How will you load a specific locale?
Using ResourceBundle.getBundle(…);
Q. What is JIT and its use?
Really, just a very fast compiler… In this incarnation, pretty much a one-pass compiler — no offline computations. So you can’t look at the whole method, rank the expressions according to which ones are re-used the most, and then generate code. In theory terms, it’s an on-line problem.
Q. Is JVM a compiler or an interpreter?
Interpreter
Q. When you think about optimization, what is the best way to findout the time/memory consuming process?
Using profiler
Q. What is the purpose of assert keyword used in JDK1.4.x?
In order to validate certain expressions. It effectively replaces the if block and automatically throws the AssertionError on failure. This keyword should be used for the critical arguments. Meaning, without that the method does nothing.
Q. How will you get the platform dependent values like line separator, path separator, etc., ?
Using Sytem.getProperty(…) (line.separator, path.separator, …)
Q. What is skeleton and stub? what is the purpose of those?
Stub is a client side representation of the server, which takes care of communicating with the remote server. Skeleton is the server side representation. But that is no more in use… it is deprecated long before in JDK.
Q. What is the final keyword denotes?
final keyword denotes that it is the final implementation for that method or variable or class. You can’t override that method/variable/class any more.
Q. What is the significance of ListIterator?
You can iterate back and forth.
Q. What is the major difference between LinkedList and ArrayList?
LinkedList are meant for sequential accessing. ArrayList are meant for random accessing.
Q. What is nested class?
If all the methods of a inner class is static then it is a nested class.
Q. What is inner class?
If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class.
Q. What is composition?
Holding the reference of the other class within some other class is known as composition.
Q. What is aggregation?
It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation.
Q. What are the methods in Object?
clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString
Q. Can you instantiate the Math class?
You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public.
Q. What is singleton?
It is one of the design pattern. This falls in the creational pattern of the design pattern. There will be only one instance for that entire JVM. You can achieve this by having the private constructor in the class. For eg.,
public class Singleton {
private static final Singleton s = new Singleton();
private Singleton() { }
public static Singleton getInstance() { return s; }
// all non static methods … }
Java Interview Questions
Monday, July 20, 2009
|
digg it
Leave your comment
Posted by Sitesh Gupta
No Responded To This Post
Leave A Reply