Friday 1 June 2012

Interview Question on core java for Freshers


1.what is difference between #include and import statement

Ans:#include makes the compiler to go the c/c++ library and copy the code in our program from header file. So program size will be increased and to compile that code waste of time. And memory will be wasted
Import statement makes the jvm to go the java standard library, execute the code there and it brings back only the results into our program. Here no code is copied and hence no waste of memory.
So import is an more efficient than #include

2. what is difference between print() and println() in java

Ans: these both methods displays in results In console. But print() method displays the result and then retains the cursor in the same in cursor line. In println() the cursor will go into nextline after printling the code.

3. Can we call main() method from anther class

Ans: yes we can call.
Ex:class a
{
Public static void main(String arg[])
{
System.out.println(“hai..”);
}
}
Class b
{
Public static void main(String arg[])
{
a.main(arg);
}
}

4.What is Jagged Arrays in java

Ans: a jagged array is an array that contains a group of arrays within it. A jagged array can store arrays of any size. Jagged arrays are also called “irregular multidimensional arrays”
Ex:int x[][]=new int[2][];
X[0]=new int[3];  //memory for first array.
X[1]=new int[2];//memory for 2nd array.
X[0]--->x[0][0],x[0][1],x[0][2]
X[1]--->x[1][0],x[1][1],x[1][2]
Program
class class3
{
      public static void main(String arg[])
      {
            int x[][]=new int[2][];
            x[0]=new int[3];
            x[1]=new int[2];
            //assign values for first array
            int s=10;
            for(int i=0;i<3;i++)
            {
                  x[0][i]=s;
                  s=s+10;                
            }
            //assign values for second array
             s=100;
            for(int i=0;i<2;i++)
            {
                  x[1][i]=s;
                  s=s+100;               
            }
            //display first array
            for(int i=0;i<3;i++)
            {
                  System.out.println(x[0][i]);
            }    
            //similarly display second array also    
      }
}

5.What if the main method is declared as private?
The program compiles properly but at runtime it will give "Main method not public." message.


6.Expain the reason for each keyword of public static void main(String args[])?
public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.
static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.
void: main does not return anything so the return type must be void
The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.

7.What are the differences between == and .equals() ?

The == operator compares two objects to determine if they are the same object in memory i.e. present in the same memory location. It is possible for two String objects to have the same value, but located in different areas of memory.
== compares references while .equals compares contents. The method public boolean equals(Object obj) is provided by the Object class and can be overridden. The default implementation returns true only if the object is compared with itself, which is equivalent to the equality operator == being used to compare aliases to the object. String, BitSet, Date, and File override the equals() method. For two String objects, value equality means that they contain the same character sequence. For the Wrapper classes, value equality means that the primitive values are equal.
 public class EqualsTest {
              
               public static void main(String[] args) {

                               String s1 = "abc";
                               String s2 = s1;
                               String s5 = "abc";
                               String s3 = new String("abc");
                               String s4 = new String("abc");
                               System.out.println("== comparison : " + (s1 == s5));
                               System.out.println("== comparison : " + (s1 == s2));
                               System.out.println("Using equals method : " + s1.equals(s2));
                               System.out.println("== comparison : " + s3 == s4);
                               System.out.println("Using equals method : " + s3.equals(s4));
               }
}


8.What is final, finalize() and finally?

final - declare constant
finally - handles exception
finalize - helps in garbage collection
Variables defined in an interface are implicitly final. A final class can't be extended i.e., final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations, and makes thread safety a little easier to achieve. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant). finalize() method is used just before an object is destroyed and garbage collected. finally, a key word used in exception handling and will be executed whether or not an exception is thrown. For example, closing of open connections is done in the finally method.

9.What is the GregorianCalendar class?
The GregorianCalendar provides support for traditional Western calendars.
GregorianCalendar is a concrete subclass of Calendar and provides the standard calendar system used by most of the world.

10.Why there are no global variables in Java?
Global variables are globally accessible. Java does not support globally accessible variables due to following reasons: The global variables breaks the referential transparency
Global variables creates collisions in namespace

11.What is the SimpleTimeZone class?
The SimpleTimeZone class provides support for a Gregorian calendar

12.How does Java handle integer overflows and underflows?
It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.










No comments:

Post a Comment