Tuesday, 24 July 2012

examples of tricky and confusing java questions which asked in written test of java job interview ?(Right solution)

class A
{
public void m1(String str)
{
System.out.println("I am from String agrument method");
}

public void m1(Object obj)
{
System.out.println("I am from Object argument method");
}
public static void main(String args[])
{
A aobj=new A();
aobj.m1(null);
}
}


Solution : -
     "I am from String agrument method"

Why is java called "purely" object oriented language ?

Solution : -
               Java is NOT a purely Object Oriented Language because given following step below : -
             
      1. Not all pre-defined types in Java (primitives) are objects.
                   2. Static variables can be used without creating an instance of an object.
                   3. Overriding a static method / variable isn’t possible.
                   4. Operations on objects should only be done through methods
                       exposed by the object which   isn’t entirely true in Java .
                      Example : - “Foo” + “Bar”

Wednesday, 11 July 2012

/* Program to change an integer to words - NUMTOWD.C */


# include <stdio.h>
# include <conio.h>
void main()
{
long n, a[10], i, c = 0 ;
clrscr() ;
printf("Enter a number : ") ;
scanf("%ld", &n) ;
while(n > 0)
{
a[c] = n % 10 ;
n = n / 10 ;
c++ ;
}
printf("\n") ;
for(i = c - 1 ; i >= 0 ; i--)
{
switch(a[i])
{
case 0 :
printf("ZERO ") ;
break ;
case 1 :
printf("ONE ") ;
break ;
case 2 :
printf("TWO ") ;
break ;
case 3 :
printf("THREE ") ;
break ;
case 4 :
printf("FOUR ") ;
break ;
case 5 :
printf("FIVE ") ;
break ;
case 6 :
printf("SIX ") ;
break ;
case 7 :
printf("SEVEN ") ;
break ;
case 8 :
printf("EIGHT ") ;
break ;
B.Bhuvaneswaran A.45
case 9 :
printf("NINE ") ;
break ;
}
}
getch() ;
}
RUN 1 :
~~~~~~~
Enter a number : 225589
TWO TWO FIVE FIVE EIGHT NINE

Tuesday, 10 July 2012

How would you detect and minimise memory leaks in Java?

In Java memory leaks are caused by poor program design where object references are long lived and the garbage
collector is unable to reclaim those objects.
Detecting memory leaks:
􀂃 Use tools like JProbe, OptimizeIt etc to detect memory leaks.
􀂃 Use operating system process monitors like task manager on NT systems, ps, vmstat, iostat, netstat etc on
UNIX systems.
􀂃 Write your own utility class with the help of totalMemory() and freeMemory() methods in the Java Runtime
class. Place these calls in your code strategically for pre and post memory recording where you suspect to be
causing memory leaks. An even better approach than a utility class is using dynamic proxies (Refer Q11 in
How would you go about section…) or Aspect Oriented Programming (AOP) for pre and post memory
recording where you have the control of activating memory measurement only when needed. (Refer Q3 – Q5
in Emerging Technologies/Frameworks section).
Minimising memory leaks:
In Java, typically memory leak occurs when an object of a longer lifecycle has a reference to objects of a short life cycle.
This prevents the objects with short life cycle being garbage collected. The developer must remember to remove the references
to the short-lived objects from the long-lived objects. Objects with the same life cycle do not cause any issues because the
garbage collector is smart enough to deal with the circular references (Refer Q33 in Java section).
􀂃 Design applications with an object’s life cycle in mind, instead of relying on the clever features of the JVM.
Letting go of the object’s reference in one’s own class as soon as possible can mitigate memory problems.
Example: myRef = null;
􀂃 Unreachable collection objects can magnify a memory leak problem. In Java it is easy to let go of an entire
collection by setting the root of the collection to null. The garbage collector will reclaim all the objects (unless
some objects are needed elsewhere).
􀂃 Use weak references (Refer Q32 in Java section) if you are the only one using it. The WeakHashMap is a
combination of HashMap and WeakReference. This class can be used for programming problems where you
need to have a HashMap of information, but you would like that information to be garbage collected if you are
the only one referencing it.

what is the benefits of private constructor?

with the help of private constructor you can add a constraint in your class that nobody can create the object of your class outside the class. coz constructor is visible only inside the class.

can we make a private constructor ?? if yes so tell me how ??

yes
we can.
ex
class A{
private A()
{}
}

what is the main difference between final and abstract ??/

abstract is applied for only methods and class.
The abstract class must have subclass and the abstract method must need to implement in subclass.if we apply the final at class level the class does not have subclass and final method does not override. So the abstract and final is invalid combination

What is Map Collection?

A map collection refers to a set of maps that are compiled and organized for a specific purpose, such as research, education, or preservatio...