Interview Questions


Java - One or More Arguments - VarArgs

Consider a method in java, which should accept one or more arguments of same type. There are two ways to implement it. Have a look at the following code.

class Test
        public static void main(String[] args) throws Exception
                Test test = new Test();
                test.Process(new String[]{"str1","str2","str3"});
        public void ProcessVarArgs(String st1,String...sts)
                for(String s:sts)
        public void Process(String[] sts) throws Exception

Java - Empty Collections

When a collection is returned from a method, and if the collection to be returned is null , then it is always better to return an empty collection.
There are methods available in Collections class to return empty collection with respect to Map, List and Set.

public class Matrix<T>
    public static void main(String[] input1)
        //Write code here
        Matrix<String> mt = new Matrix<String>();
        Collection<String> lstStr = mt.processColl(null);
    public Collection<T> processColl(Collection<T> coll)
        if(null == coll)

Java String Template Example

import java.nio.file.Files;
import java.nio.file.Paths;

import org.stringtemplate.v4.ST;

public class StringTemplateHelper {

        public static void main(String[] args) throws IOException,
                        URISyntaxException {
                // TODO Auto-generated method stub
                StringTemplateHelper stHelper = new StringTemplateHelper();
                ST emailTestTemplate = new ST(new String(Files.readAllBytes(Paths
                                                "/email/email-Test.html").toURI()))), '$', '$');

Java email with Attachment

import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class AttachmentMailer
    private String host    = "";

Spring - Java Object Clone

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

* This class provides methods to copy properties from on object to another
public class JavaObjectClone {

        public static <T> T deepCopy(T object,Class<T> aClass) throws ClassNotFoundException, IOException{
                ByteArrayOutputStream bOut = new ByteArrayOutputStream();

Java - Number To Word Converter

package com;

import java.text.DecimalFormat;

public class NumberToWord {

        private static final String[] tensWords = { "", " ten", " twenty",
                        " thirty", " forty", " fifty", " sixty", " seventy", " eighty",
                        " ninety" };

        private static final String[] words = { "", " one", " two", " three",
                        " four", " five", " six", " seven", " eight", " nine", " ten",
                        " eleven", " twelve", " thirteen", " fourteen", " fifteen",

SOFEA - Service Oriented Front End Architecture

In most of the web applications developed in the world, some of the server side UI frameworks such as spring, struts2,wicket etc., are used. These framework use the MVC pattern for developing UI. The major issue here is most of the UI processing tasks (business logic + view generation(html/css)) are rendered at the server side itself. This makes the
client side(browser) very thin & dumb.

Java 7 New features

The following are some of the new feautres in Java 7 (SE 7) Programming language

1.) Strings can be used in Switch Statements, for example

         public static void main(String[] args) {
                String str = args[0];
                case "one":
                        System.out.println("The String represents number 1");
                case "two":
                        System.out.println("The String represents number 2");
                                System.out.println("Not a valid string");


2.) Underscore can be used in numberic values

Java - Custom Annotation

The following code sample illustrates how to create a custom annotation in Java. This example annotation is used to calculate the time taken to execute a method. The code is self explanatory.

/* Start of Annotation */
package com;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value = { ElementType.METHOD })
public @interface CalcTimeTaken {

/* End of Annotation */

package com;

Java - EMI Calculator

The following code can be used to calculate EMI (Equated Monthly Installment), by providing
the following parameters
Principal, Rate of Interest and Period(in months).

package com.tdd;

public class EMCalc {

        public static void main(String[] args) {
                EMCalc eCalc = new EMCalc();
                eCalc.calcEmiAllMonths(1000, 10, 12);

        public Double calcEmi(double p, double r, double n) {
                double R = (r / 12) / 100;
                double e = (p * R * (Math.pow((1 + R), n)) / ((Math.pow((1 + R), n)) - 1));

                return e;