Tuesday, May 27, 2008

Java Interview Questions - 3

Here is the collection of some of frequently asked questions on Core Java interview.
These questions are collected from various sources such as forums, discussion rooms etc where people posted questions that were asked to them on java interviews.
P.S : If you need anymore info or think answers are incorrect please add comments on it and i will try to fix them

1)Can I get Session object from servlet or jsp to simple java class?
Yes. In your simple java class, have a instance variable of type Session/HttpSession and getter and setter method for it. From a servlet, set the HttpSession object for this class.

2)What is a singleton class?
Singleton classes are those which are designed such that we can create only one instanceof the object and provide a method to access this instance of the class. Creation of multiple instances is avoided by making all its constructors as private.

3)What happened if i clone a singleton object?
We cannot clone singleton class. Since the constructor is private, the clone methodof the object class is not visible.Further you can opt for overriding clone method in your singleton class and throw suitable exception by yourself ( CloneNotSupported exception)

4)What is the difference between sendRedirect and forward in servlet ?
sendRedirect is done at the server side without any knowledge of the calling client.There will be no change in the url or any changes for the clients point of view.
forward sends the new url to the client and the client goes to that url for further action.There will be visible change in the url in the browser

5)Can I call a static variable with object?
Yes, But not a good practice. Method call will be based on the type of the Objectnot on the instance of the object.

6)What are the collection framework hibernate supports?
Bag,Set,List,Map and Array

7) Explain System.out.println()
System.out.println()
System is a class in java.lang package.
out is a final static member of type PrintStream(java.io) in System class.(System class imports java.io.* for us so we need
not bother to import java.io package.)
and
println is the method provided by PrintStream class.

8) What is the difference between sleep(1000), suspend() and wait(1000) ?
Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the
sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.

object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original,
waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.

9) JVM and Platform Independence OR Java as Compiled and Interpreted
Java is called Platform independent language because a program which is written in Java can run on any machine independent of which platform it is running on ( Windows/Linux/Solaris..).
Here JVM (Java Virtual Machine) plays an important role in it.
All java files will be converted into .class files which contains byte code which is not platform dependent. Then comes the JVM which interprets the byte code into platform/machine dependent code. That is why java is also called compiled and interpreted language.
Even though the JVM is different for different platform, the key advantage here is, we do not need to compile the java application to make it run on different platform it was originally created.

10)How many objects are created in the below code
String s= new String("abc");
Assuming that there is no String "abc" inside String constant pool,
2 objects are created.
One in Heap memory and the other in the String Constant Pool.

Tuesday, May 20, 2008

Static variables and methods

‘static’ is a modifier in java which can be used for methods and instance variables and a block of code outside any methods inside a class.
We can use static as a modifier to a class or a method when the member’s behaviour doesn’t depend on the instance of the class so that there will be no need to create an instance of that class to access the static members.

Variables and members marked as static belong to a class rather than any particular instance of the class. So, if you modify a variable in one instance of the class, it will affect all future use of the variable in any other instance of the class (unless the class itself is reloaded).

Static methods can’t access non-static variables of a class.

Static methods cannot be overridden.

Static modifier can only be applied to methods, instance variables and top level inner classes.

Declaration and Access control – 2

Some Quick Reference points about Declaration and Access control for Java Job Interviews
1. A method marked as ‘private’ can’t be overridden. This does not mean that we cannot have a method with same name and signature in the subclass.

2. For a sub class outside the package, the protected member can be accessed only through inheritance. i.e the protected members cannot be accessed by its sub classed outside the package using the reference of a super class.

3. We cannot apply any access modifiers to local variables. The only modifier that can be applied to local variable is ‘final’.

4. ‘ synchronized’ modifier can only be applied to methods or a block of code inside a method.

5. ‘synchronized’ modifier indicates that the methods or block of code to make sure that only one thread accesses the code at a time.

6. ‘native’ modifier indicates that the method is implemented in a platform dependent language such as C.

7. ‘native’ can be applied only to methods.

8. ‘strictfp’ modifier can be applied to classes and methods.

9. ‘strcitfp’ modifier forces floating point literals to adhere to the IEE754 standars.

10. ‘transient’ and ‘volatile’ modifier can be applied only to instance variables.

11. ‘transient’ variables are ignored during serialization of the objects.

12. The volatile modifier tells the JVM that a thread accessing the variable must always reconcile its own private copy of the variable with the master copy in memory.

Monday, May 12, 2008

The ‘abstract’ keyword of Java Programming Language

One of the frequently asked questions in Java interviews are about abstract classes and abstract methods.

‘abstract’ is a non-access modifier that can be applied to classes and methods.

This modifier cannot be applied to instance variables and local variables.

If a class is marked as abstract, it cannot be instantiated. i.e we cannot create objects of abstract class. An abstract class must be sub classed (extended) and the first concrete ( non-abstract) class extending an abstract class must implement all the abstract methods of the super class (if not implemented in any of the super classes.

If there is any method in an abstract class marked as abstract, then the class has to be marked as abstract. But we can have an abstract class without any abstract methods.

An abstract keyword has a significant role in java polymorphism and inheritance concepts.

Abstract methods ends with a semicolon – instead of curly braces.

We cannot mark a class both as abstract and final.

We cannot mark a method as both abstract and synchronized.

We cannot mark a method as both abstract and native.

We cannot mark a method as both abstract and strictfp.

We cannot mark a method as both abstract and private.

All abstract methods must be implemented in first non-concrete subclass unless it is implemented in any of the super class which are also abstract.

Tuesday, May 6, 2008

final - modifier of Java

Some important facts about final keyword of Java Language

final is a keyword in java which is used as a modifier for a classes, methods, instance variables and local variables.

This is one of the non-access modifier.

When a class is marked as 'final', it can never be extended. i.e the final classes cannot be sub classed. We rarely mark a class as final because this stops us from using java inheritance - which is one of the key pillars of Java Technology. A class is marked as final so that no one can override the methods of that class - As a security measure. Imagine the consequences of badly overriding methods of java.lang.String class (which is a final class).

A class can never be marked as both 'final' and 'abstract', both of which are contradictory to each other.

When a method is marked as final, it can never be overridden, even though the class itself is non-final.

When an instance variable is marked as final, once it is given a value, it can never change. If we assign a reference variable to an object, then we can never reassign a new object to that reference variable. Although we can change the properties of that object. All final variable must be initialized by a value explicitly before each constructors finishes running. Else the compiler will throw an exception.

final is the only modifier that can be applied for a method local variable. It should be given a value during variable declaration.

Thursday, May 1, 2008

Declaration and Access Control - Quick Reference

Java Job Interview Questions - Quick Reference Points / Points to Remember




Declaration and Access Control - Quick Reference

Ø There can be only one public class per a source code file.

Ø The name of the source code file must be same as the public class in that file.

Ø A class file should first have a package name (if any) , then import statements (if any) and class declaration.

Ø A class can be marked either as public or with no access modifier, which specifies package (default) access.

Ø A class can be marked as strictfp, final or abstract.

Ø A class can not be declared as private or protected.

Ø A class cannot be marked as both abstract and final.

Ø A final class cannot be sub classed ( extended)

Ø An abstract class must be sub classed.

Ø An abstract class cannot be instantiated i.e it is not possible to create an object of an abstract class.

Ø Marking a class as final is to avoid overriding of its method in subclasses.

Ø An abstract class is used to define a generic behaviour of an object.

Ø If any method of a class is abstract, whole class must be marked as abstract.

Ø A class can be abstract even if there are no abstract methods.

Ø Abstract methods ends with a semicolon.

Ø Abstract classes let us to use polymorphism.

Ø Methods and instance variables of a class is collectively known as members of a class.

Ø Members of a class can have four different access levels – public, protected, default and
private.

Ø public – Access Anywhere

Ø protected – Access in the same package and in subclasses.

Ø default (no access modifier) – Access inside same package.

Ø private – Access inside same class.

Ø A member marked as private cannot be accessed outside the same class.