Tuesday 15 January 2013

Core Java Interview Questions


36. What is synchronized means in thread?
A method or a block of code can be synchronized. Only one thread is allowed to access the method.

37. What is the difference in synchronizing the static or non static method?
When a sync static method is called the lock is made on the class but if the sync non static method is called then lock is made on the object.

38. Difference between sleep and yield?
The Sleep() method makes a thread to go to sleep mode for a pre defined time and the lock is not released.
The yield() releases the lock on the object and the other thread allowed the lock the object.

39. Difference between notify and notifyall?
notify( ) calls the first thread that called wait( ) on the object whereas
notifyAll( ) calls all the threads that called wait( ) on the object. Object is called based on priority.

40. Can we synchronize a class or variable?
No

                                                          Previous

Core Java Interview Questions


31. What is OutOfMemoryError in java? How to handle it?
This error occurs when JVM is not able to allocate memory for an object.
Temporary Solution:
 Increase the heap size by using the following command.
 -Xms1024m -Xmx1024m
Permanent Solution:
 Find the memory leaks in the code and ensure the memory is used effectively.

32. What is Final, Finally and Finalize?
Final is a keyword. Variable declared final cannot be modified. Method declared final cannot be overridden. Class declared final cannot be inherited.
Finally is a block. Finally is executed always after the catch block.
Finalize method is used for garbage collection by java.

33. Explain Thread?
Threads are used when multiple task need to be performed on the single program.

34. What are different ways of creating thread?
• Extend thread class and override run() method
• Implement runnable interface

35. What are the different states in thread’s lifecycle?
• New
• Runnable
• Running
• Waiting
• Blocked
• Sleeping
• Dead

                                                               Previous - Next

Sunday 13 January 2013

Core Java Interview Questions



21. Can we declare a main method as final?
Yes.

22. Can we declare an interface as final?
No.

23. Can we have an abstract class with no abstract methods in it?
Yes .

24. Can we have private and protected modifiers for variables in interfaces?
No. All variables in interface are public, Static, Final by default.

25. Is null a keyword?
No.

26. Can we override an overloaded method?
Yes.

27. What is runtime polymorphism?
It is the process in which the overridden method is decided at runtime.

28. What is abstraction in java?
It is hiding the implementation and showing only the functionality. Best example is the interface.

29. What is an abstract class?
A class declared as abstract is called asbtract class. It cannot be instantiated.

30. Can we declare a method abstract as well as final?
No. Abstract method need to be overridden but if you declare it final then we cannot override it.

                                                                 Previous - Next

Core Java Interview Questions



11. What is Inheritance?
One class acquiring the properties and behavior from another class is called inheritance.

12. Which is the super class of all classes?
Object class

13. Does java support pointers? Why?
No. Pointers refer to memory address. They are complex and not safe.

14. Can Main method be overloaded?
Yes. Main method can be overloaded.

15. What is method overloading in java?
Having methods of same name but with different parameters in a class is called method overloading.

16. What is method overriding in java?
Having methods of same name and same parameter but in different class is called as method overriding.

17. Can we override static methods?
It is not advisable to override static method but still it can be over rided. If they are over rided , the class will compile and run but the results might not be as expected because the static method is more bound to the class and not the class’s object.

18. What is a Final Class?
A class declared final cannot be inherited.

19. What is a Final Method?
A method declared final cannot be overridden.

20. What is a Final Variable?
A class declared final cannot be inherited.

                                                                    Previous - Next

Core Java Interview Questions



1. Is java platformindependent? Explain.
Java is platform independent. we can write and compile the java code in one platform (like  Windows) to generate the class files and can execute the class in any other supported platform (like Linux,Solaris,etc). Java is called “Write once and run anyware”.

2. How a class file can be run different platforms?
The JVM takes care of running the class files. There are unique JVM for all known platforms. So depending on the platforms the JVM differs.

3. What is the initial value of instance variable in java?
null

4. What is a constructor?
It is just like a method which is used to initialize variables or perform some operation when an object is created for the class.

5. Can a constructor be final?
No

6. What is a Static variable?
Static variable gets only one memory when the class is first loaded. Any changes made to static variable are reflected in all the objects of the class.

7. What is a Static method?
Static method can be invoked without even creating the object for the class.
It belong to the class rather than its object. Static method can access only other static method.

8. Why main method is static?
Main method has to be called first in java. If main is not static then JVM has to create an object and then call the method.

9. What static block?
Static blocks are executed only once,  when the class is loaded for the first time.

10. What is THIS in java?
It refers to current object.

                                                                           Next

Using Xpath with camel


Lets see how to extract data from XML files using Xpath in camel.

Create a java project in eclipse.
Create a folder by name testFileFolder and place the below given file.
The xml file that we will be using it, lets call it demo.xml


<emp>
 <name>admin</name>
 <phone>1234567890</phone>
 <address>address 1</address>
 <address>address 2</address>
</emp>

We will be extracting the address data alone from the xml.

Next create a java file by name MyService.java as given below,


package com.techexample.processor;


import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class MyService implements Processor {
 @Override
 public void process(Exchange exchange) throws Exception {
  String data=(String)exchange.getIn().getBody();
  System.out.println(data);
 }
}


Now the main part, we are going to create the camel context file.
Create a file with name camelContext.xml and place it inside a folder say Resource

The file context is,

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
 xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring
         http://camel.apache.org/schema/spring/camel-spring.xsd">
 <bean id="splitdata" class="com.techexample.processor.MyService" />
 <camel:camelContext id="readfile" trace="true">
  <camel:route id="CsvSplit" trace="true">
   <camel:from uri="file:testFileFolder" />
   <camel:split parallelProcessing="false">
    <camel:xpath>/emp/address/text()</camel:xpath>
    <camel:convertBodyTo type="java.lang.String"/>
    <camel:to uri="bean:splitdata" />
   </camel:split>
  </camel:route>
 </camel:camelContext>
</beans>

Now, we have to write a java file to bring up the camel context file.

Click this link for the java file, MyClient.java


Steps to run the camel project

Go to project properties, under java buildpath -> source , add the resource and testfileFolder.
Now right click in our MyClient.java file and click runAs -> java application


OUTPUT:


address 1
address 2

Friday 11 January 2013

Bring up the camel context


Create a java file by name MyClient.java

package com.techexample.processor;

import org.apache.camel.spring.Main;

public class MyClient {
 public static void main(String[] args) throws Exception {
  Main main = new Main();
  main.setApplicationContextUri("camelContext.xml");
  main.enableHangupSupport();
  main.run();
 }
}

Split Multiple rows using camel



Lets see how to read a file with multiple rows using CAMEL.

Create a java project in eclipse.
Create a folder by name testFileFolder and place the below given file.
The file that we are going to read, lets say demo.txt

data in file,

line one data
line two data
line three data
line four data
line five data
line six data

Next create a java file by name SplitData.java as given below,

package com.techexample.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class SplitData implements Processor {
public static int count = 0;
@Override
public void process(Exchange exchange) throws Exception {
String data=(String)exchange.getIn().getBody();
count++;
System.out.println("Line "+count+" info is: "+data);
System.out.println("=============================");
}
}


Now the main part, we are going to create the camel context file.
Create a file with name camelContext.xml and place it inside a folder say Resource

The file context is,

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring
         http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="splitdata" class="com.techexample.processor.SplitData" />
<camel:camelContext id="readfile" trace="true">
<camel:route id="CsvSplit" trace="true">
<camel:from uri="file:testFileFolder?noop=true;" />
<camel:split>
<camel:tokenize token="\n" />
<camel:to uri="bean:splitdata" />
</camel:split>
</camel:route>
</camel:camelContext>
</beans>

Now, we have to write a java file to bring up the camel context file.


Click this link for the java file, MyClient.java


Steps to run the camel project

Go to project properties, under java buildpath -> source , add the resource and testfileFolder.
Now right click in our MyClient.java file and click runAs -> java application


OUTPUT:


Line 1 info is: line one data

=============================
Line 2 info is: line two data

=============================
Line 3 info is: line three data

=============================
Line 4 info is: line four data

=============================
Line 5 info is: line five data

=============================
Line 6 info is: line six data
=============================


File Reading


Lets see how to read a file with multiple rows using CAMEL.

Create a java project in eclipse.
Create a folder by name testFileFolder and place the below given file.
The file that we are going to read, lets say demo.txt

Data in file, "This is my test data".

Next create a java file by name PrintData.java as given below,

package com.techexample.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class PrintData implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
String data=(String)exchange.getIn().getBody();
System.out.println(data);
System.out.println("=============================");
}
}



Now the main part, we are going to create the camel context file.
Create a file with name camelContext.xml and place it inside a folder say Resource

The file context is,

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
         http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring 
         http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="printdata" class="com.techexample.processor.PrintData" />
<camel:camelContext id="readfile" trace="true">
<camel:route id="CsvSplit" trace="true">
<camel:from uri="file:testFileFolder?noop=true;" />
<camel:split>
<camel:tokenize token="\n" />
<camel:to uri="bean:printdata" />
</camel:split>
</camel:route>
</camel:camelContext>
</beans>


Now, we have to write a java file to bring up the camel context file.

Click this link for the java file, MyClient.java


Steps to run the camel project

Go to project properties, under java buildpath -> source , add the resource and testfileFolder.
Now right click in our MyClient.java file and click runAs -> java application


OUTPUT:

This is line one

=============================
This is line two

=============================
This is line three

=============================
This is line four

=============================
This is line five

=============================
This is line six
=============================