Wednesday 31 October 2012

What are different types of inner classes? Nested -level classes, Member classes, Local classes, Anonymous classes. Explain them




There are 4 types of inner classes - Member inner class, Local inner class, Static inner class and Anonymous inner class
1. Member inner class – A member of a class(enclosing class).
2. Local inner class – An inner class that is defined within a block.
3. Static inner class – Like static members, this class itself is static.
4. Anonymous inner class – A class without a name and implements exactly only one interface or exactly extends one abstract class.

The enclosing class can not have the accessibility to the inner class. To do so, the outer class must create an object of its nested class except for static inner class. The member classes can access all of its enclosing class’s members, because inner class is like a member of a class.
Anonymous inner class is used when an object creation, one time single method invocation to be done and releasing the object at once. This is particular in event driven programming.
The nested classes are implemented in handling AWT, Swing and Applet programming. An anonymous inner class is invoked when an action event is to be performed.

What is a “dirty read”?


In typical database transactions, one transaction reads changes the value while the other reads the value before committing or rolling back by the first transaction. This reading process is called as ‘dirty read’. Because there is always a chance that the first transaction might rollback the change which causes the second transaction reads an invalid value. 

What are the main advantages of ORM like hibernate?


Hibernate implements extremely high-concurrency architecture with no resource-contention issues. This architecture scales extremely well as concurrency increases in a cluster or on a single machine.
Other performance related optimizations that hibernate performs are:
  • Caching objects
  • Executing SQL statements later, when needed
  • Never updating unmodified objects
  • Efficient Collection Handling
  • Rolling two updates into one
  • Updating only the modified columns
  • Outer join fetching
  • Lazy collection initialization
  • Lazy object initialization

Explain the role of Session interface in Hibernate.


  • In hibernate, the Session interface wraps a JDBC connection, holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier and is a factory for Transaction
  • Session session = sessionFactory.openSession();
  • The Session interface is the primary interface used by Hibernate applications.
  • It is a single-threaded, short-lived object representing a conversation between the application and the persistent store.
  • It allows you to create query objects to retrieve persistent objects.

What are Collection types in Hibernate?


  • ArrayType,
    Constructor: ArrayType(String role, String propertyRef, Class elementClass, boolean isEmbeddedInXML)
  • BagType,
    Constructor: BagType(String role, String propertyRef, boolean isEmbeddedInXML)
  • CustomCollectionType, A custom type for mapping user-written classes that implement PersistentCollection
    Constructor: CustomCollectionType(Class userTypeClass, String role, String foreignKeyPropertyName, boolean isEmbeddedInXML)
  • IdentifierBagType,
    Constructor: IdentifierBagType(String role, String propertyRef, boolean isEmbeddedInXML)
  • ListType,
    Constructor: ListType(String role, String propertyRef, boolean isEmbeddedInXML)
  • MapType,
    Constructor: MapType(String role, String propertyRef, boolean isEmbeddedInXML)
  • SetType
    Constructor: SetType(String role, String propertyRef, boolean isEmbeddedInXML)

What is the difference between merge and update?


update () : When the session does not contain an persistent instance with the same identifier, and if it is sure use update for the data persistence in hibernate..........

What is Hibernate proxy ?

Mapping of classes can be made into a proxy instead of a table. A proxy is returned when actually a load is called on a session. The proxy contains actual method to load the data. The proxy is created by default by Hibernate, for mapping a class to a file. The code to invoke Jdbc is contained in this class.

Wednesday 8 August 2012

Write a prgram to how to print " X " In java ?????????


class Demo{
public static void main(String [] args)
{
 char prnt = 'X';
int i, j, s, nos = 0;  //nos controls the spacing.
//1st triangle
for (i = 9; i >= 1; i = i - 2) {
for (s = nos; s >= 1; s--) {  //Spacing control
System.out.print("  ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i) { //This hollows the triangle
System.out.print(prnt);
} else {
System.out.print("  ");
}
}
System.out.print("\n");
nos++;  // In the upper triangle the space increments by 1.
}
/* Since both the triangles have the same peak point, while printing the second triangle we'll ignore its peak point. Thus i starts from 3 and the nos from 3.*/
nos = 3;
for (i = 3; i <= 9; i = i + 2) {
for (s = nos; s >= 1; s--) {
System.out.print("  ");
}
for (j = 1; j <= i; j++) {
if (j == 1 || j == i) {
System.out.print( prnt);
} else {
System.out.print("  ");
}
}
System.out.print("\n");
nos--; //The spaces are in a decrementing order.
}

}
}
OUT PUT :


   X                            X 
     X                       X
        X                X 
           X         X
                 X
             X      X
        X               X
    X                       X
X                               X

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