Monday, March 4, 2013

Spring Interview questions.


Question1: What is IOC or inversion of control?
Answer: This Spring interview question is first step towards Spring framework and many interviewer starts Spring interview from this question. As the name implies Inversion of control means now we have inverted the control of creating the object from our own using new operator to container or framework. Now it’s the responsibility of container to create object as required. We maintain one xml file where we configure our components, services, all the classes and their property. We just need to mention which service is needed by which component and container will create the object for us. This concept is known as dependency injection because all object dependency (resources) is injected into it by framework.

Example:
  <bean id="createNewStock" class="springexample.stockMarket.CreateNewStockAccont">
        <property name="newBid"/>
  </bean>
In this example CreateNewStockAccont class contain getter and setter for newBid and container will instantiate newBid and set the value automatically when it is used. This whole process is also called wiring in Spring and by using annotation it can be done automatically by Spring, refereed as auto-wiring of bean in Spring.


Question 2: Explain Bean-LifeCycle.
Ans: Spring framework is based on IOC so we call it as IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java object (POJO).
Following steps explain their life cycle inside container.
1. Container will look the bean definition inside configuration file (e.g. bean.xml).
2 using reflection container will create the object and if any property is defined inside the bean definition then it will also be set.
3. If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
4. If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
5. If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called before the properties for the Bean are set.
6. If an init() method is specified for the bean, it will be called.
7. If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
8. If the Bean definition in the Configuration file contains a 'destroy-method' attribute, then the corresponding method definition in the Bean class will be called.

Question 3: what is Bean Factory, have you used XMLBeanFactory?
Ans: BeanFactory is factory Pattern which is based on IOC design principles.it is used to make a clear separation between application configuration and dependency from actual code.
XmlBeanFactory is one of the implementation of bean Factory which we have used in our project.
org.springframework.beans.factory.xml.XmlBeanFactory is used to create bean instance defined in our xml file.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
Or
ClassPathResource resorce = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(resorce);

Sunday, January 13, 2013

Java Script Interview Questions and Answer


What’s relationship between JavaScript and ECMAScript? - ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.
What are JavaScript types? - Number, String, Boolean, Function, Object, Null, Undefined.
How do you convert numbers between different bases in JavaScript? - Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);
What does isNaN function do? - Return true if the argument is not a number.
What is negative infinity? - It’s a number in JavaScript, derived by dividing negative number by zero.

What boolean operators does JavaScript support? - &&, || and !
What does "1"+2+4 evaluate to? - Since 1 is a string, everything is a string, so the result is 124.
How about 2+5+"8"? - Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 78 is the result.
What looping structures are there in JavaScript? - for, while, do-while loops, but no foreach.
How do you create a new object in JavaScript? - var obj = new Object(); or var obj = {};
How do you assign object properties? - obj["age"] = 17 or obj.age = 17.
What’s a way to append a value to an array? - arr[arr.length] = value;
What is this keyword? - It refers to the current object.

Saturday, December 22, 2012

What are the main classes which are used in struts application?


 This is another beginner’s level Struts interview question which is used to check how familiar candidate is with Struts framework and API. Main classes in Struts Framework are:

Action servlet: it’s a back-bone of web application it’s a controller class responsible for handling the entire request.
Action class: using Action classes all the business logic is developed us call model of the application also.
Action Form: it’s a java bean which represents our forms and associated with action mapping. And it also maintains the session state its object is automatically populated on the server side with data entered from a form on the client side.
Action Mapping: using this class we do the mapping between object and Action.
ActionForward: this class in Struts is used to forward the result from controller to destination.

Thursday, December 6, 2012

How to select past 7 days records from MySQL


SELECT * FROM tablename.user_stats where upadtedDate between '2012-11-18' and '2012-12-6' and loginId=2

SELECT * FROM tablename.user_stats where upadtedDate BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()


Friday, November 23, 2012

core java interview questions and answers for experienced

Can an inner class be built in an Interface?

• Yes,an inner class may be built an Interface
Example :

public interface xyz
{
static int p=0;
void m();
class c
{
c()
{
int q;
System.out.println("inside");
};
public static void main(String c[])
{
System.out.println("inside ");
}
}
}

Why is explicit object casting needed?

In order to assign a superclass object in a variable to a subclass,one needs to do explicit casting.

For example:

Person person;
Man man;
man = (Man)person;

An automatic casting takes place when we typecast a object in subclass as parent class object.

Define Externalizable.

• Externalizable is coined as an Interface
• It extends the Serializable Interface.
• It also sends data into the Streams.
• Externalizable sends data in a Compressed Format.
• Externalizable is having two methods,for e.g. writeExternal(ObjectOuput out) & readExternal(ObjectInput in)

How can a collection object be sorted?

• // Sort
Collections.sort(list);

• // Sort case-insensitive
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

• // SortReverse-order
Collections.sort(list, Collections.reverseOrder ());

• // Reverse-order sort case-insensitive
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(list);

Define local, member and a class variable.

• Within a method variables declared are called “local” variables.

• Variables declared in the class i.e not in any methods are “member” variables (global variables).

• Variables declared in the class i.e not in any methods and are called as “static” are class variables

Name the different identifier states of a Thread.

Different types of identifiers of a Thread are:

• R - Running or runnable thread
• S - Suspended thread
• CW - Thread waiting on a condition variable
• MW - Thread waiting on a monitor lock
• MS - Thread suspended waiting on a monitor lock

Define Vector class?

 Differentiate the Vector and ArrayList.

• Vector canbe said a legacy class which has been introduced to implement the List interface since Java 2 platform v1.2
• Vector is always synchronized but ArrayList is not.
• When Vector class is synchronized, if we will run in multithreading environment we've to use ArrayList with Collections.
• Vector has a default size i.e 10 while arrayList has no default size .
• ArraayList is not having any method returning Enumerations where as vector list is having.

Differentiate between Enumeration and Iterator interface

• In java.util package the Enumeration and Iterator are available.
• The Enumeration interface is replicated by the Iterator interface.
• In preference to Enumeration new implementations should consider using Iterator .

The difference of Iterators from enumerations are:

• Enumeration has 2 methods namely hasMoreElements() & nextElement() where the Iterator contained three methods namely hasNext(), next(),remove().
• An optional remove operation is added in Iterator,and has shorter method names. We Use remove() to delete the objects but the Enumeration interface does not support this feature.
• The legacy classes use Enumeration interface .Vector.elements() & Hashtable.elements() method results Enumeration. All Java Collections Framework classes returns iterator. java.util.Collection.iterator() method returning an instance of Iterator.

What are the alternatives to inheritance?

• Delegation is an alternative to inheritance.

• Delegation denotes that you include an instance of any class as an instance variable, and forward messages to the instance.

• It is safer than inheritance because it ceases you to think about forwarded message , because the instance is of a known class, rather than a new class, and because it doesn’t force you to accept all the methods of the super class: you can provide only the methods that really make sense.

• On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).

Number the bits, used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

• Unicode requires 16 bits

• ASCII require 7 bits. but it is usually represented as 8 bits.

• UTF-8 represents characters using 8, 16, and 18 bit patterns.

• UTF-16 uses 16-bit and larger bit patterns.

Define reflection

• Reflection allows program related access to information about the fields, methods and constructors of loaded classes

• It use reflected fields, methods, and constructors

• It helps to operate on their underlying counterparts on objects,

• It operates within security restrictions.

What is RMI and how it is useful?

• Remote method invocation is called RMI.

• One can work with remote object using RMI .

• It gives a impression that you are working with a object that resides within your own JVM though it is somewhere.

• The protocol used by RMI is RMI-IIOP

Define a Collection API.

• The set of classes and interfaces supporting the operation on collections of objects is the Collection API .

• Than the vectors, arrays, and hashtables if effectively replaces,these classes and interfaces are more flexible, more powerful, and more regular

• class examples: HashSet, TreeMap, ArrayList, LinkedList,HashMap and TreeMap.

• interface examples: Set,List ,Collection and Map.

How many forms of Polymorphism are there?

polymorphism exists in three different forms in Java:

• Method overloading
• Method overriding through inheritance
• Method overriding through the Java interface

Define the wrapper classes in Java and name a few.

• Wrapper class is wraps around the primitive data type.

• list of the primitive types and the corresponding wrapper classes:

Primitive Wrapper
boolean java.lang.Boolean
byte java.lang.Byte
char java.lang.Character
double java.lang.Double
float java.lang.Float
int java.lang.Integer
long java.lang.Long
short java.lang.Short
void java.lang.Void

Differentiate between JDK ,JRE & JVM

• JDK stands for Java Development Kit.
It is the most widely used Java Software Development Kit.

• JRE stands for Java Runtime Environment
It is an implementation of the Java Virtual Machine which executes Java programs

• JVM stands for Java Virtual Machine
It is an interpreter.

Why will you use Comparator and Comparable interfaces?

• java.util.Comparator

• java.util.Comparator compares some other class’s instances,

• java.lang.Comparable

• java.lang.Comparable compares another object with itself .

Differentiate between final, finally and finalize.

• The keyword is final.sss
It is used for declaring a constant
It prevents a class from producing subclasses.

• finally is a code.
It always executes when the try block is finished,
Unless System.exit() has been called.

• finalize() is a method,
Before discarding by the garbage collector it is invoked .

Differentiate JAR and WAR files

JAR files:

• JAR files is the acronym stands for Java ARchive fles.
• JAR files allow aggregating many files into one,
• JAR is usually used to hold Java classes in a library.

WAR files:

• WAR files is the acronym stands for Web ARchive fles.
• WAR stores XML, java classes, and JavaServer pages
• WAR is mainly used for Web Application purposes.`

In a Java , how can you send program messages on the system console, but error messages, to a file?

• The class System has a variable out that denotes the standard output,
• the standard error device represents the variable err .
• Naturally, they both point at the system console.

In this way, the standard output can be sent to the file:

Stream x = new Stream(new FileOutputStream("error.txt"));
System.setErr(x);
System.Out(x);