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 type of parameter passing does Java support?

java supporting two types of parameter passing
1.pass by value
2.pass by refernce

Objects are passed by value or by reference?

1- Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object .
2- Every thing in java is "pass by value" or "call by value" only.

How can one prove that the array is not null but empty using one line of code?


    Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.

What is a local, member and a class variable?

Variables declared within a method are “local” variables.
Variables declared within the class i.e not within any methods are “member” variables (global variables).
Variables declared within the class i.e not within any methods and are defined as “static” are class variables

Monday 9 July 2012

What is the need for Hibernate xml mapping file? .

. hbm.xml file is required for configuring the Hibernate configuration for the app. Configuration parameters like SQL Dialect to be used are defined here. SQL dialect differs for every DB.

What are the Core interfaces are of Hibernate framework?

Core Interfaces are- SessionFactory, Session, Query, Transaction.

What is the general flow of Hibernate communication with RDBMS?

For any communication with DB, hibernate converts each HQL query to the equivalent SQL query with the configuration based in hbm.xml file to map the database. All HQL queries are parsed and converted to SQL using JDBC and then sent to the DB.

What is Hibernate Query Language (HQL)?

HQL is nothing but object oriented query language which is very similar to SQL. HQL is used for communication with DB. It supports almost all the features of native SQL query

What is the diffence between Constructor and Method ?


constructor  :-
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.

method :-
 A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 

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...