Interview Questions

Utils

Java - Encryption Decryption Example

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java IP Address Generator

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java Square Root

This code sample contains methods to find a square root of both perfect and non perfect squares using java.

public class SquareRoot
{
        public static void main(String[] args)
        {
                SquareRoot sR = new SquareRoot();
                System.out.println("Square Root of 100 -> " + sR.getPerfectSquareRoot(100));
                System.out.println("Square Root of 51 -> " + sR.getSquareRoot(51, 0.025f));
        }

        public float getPerfectSquareRoot(int num)
        {
                return getSquareRoot(num, 0.0f);
        }

        public float getSquareRoot(int num, float errorFactor)
        {
                float r = 0;

                float x = num / 2;

Java Prime Number

This code sample contains methods to generate Prime numbers in a given range using Java.

import java.util.ArrayList;
import java.util.List;

public class PrimeNumber
{
        public static void main(String[] args)
        {
                PrimeNumber pN = new PrimeNumber();
                System.out.println("Is 73 a prime number -> " + pN.isPrime(73));
                System.out.println("List of prime numbers up to 200 -> " + pN.getListOfPrimes(200));
        }

        public boolean isPrime(int n)
        {
        int k = (int) Math.sqrt(n);
                for (int i = 2; i <= k; i++)
                {
                        if (n % i == 0)
                                return false;
                }

Java Password Validation

The following code sample is used to perform simple password validation in java.

class PasswordValidator
{
        public PasswordValidator()
        {
                super();
        }

        public static void main(String[] args)
        {
                PasswordValidator passwordValidator = new PasswordValidator();
               
                String userName = "techdive";
                String passWord = "java2011";
                System.out.println("Input : UserName "+userName+" PassWord -> "+passWord);
               
                passwordValidator.passwordValidation(userName,passWord);
                System.out.println();
                passWord = "Java2011*";

Java - Reflection

Reflection is used to examine the run time behavior of Java applications running in the Java Virtual machine.

Using reflection, instances can be created using the fully qualified names of the class.

Note: Reflection is a more powerful and advanced feature. It should not be used extensively unless the developer has a strong knowledge in Java.

In the below example, let us see how to create an object for the class and invoke its methods at run time using reflection.

Example:

Java - IP address (IPv4) format check

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java - Password Generator

The following class is used for generating passwords containing 'n' characters.

PwdGenerator.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

Java - Random Number Generator

The following class contains utility methods to generate random numbers. The code is self explanatory with necessary comments.

RandomNumberGenerator.java

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

Concurrent Modification Exception

java.util.ConcurrentModificationException

The above exception is thrown by java runtime when a collection is modified by a thread, at the same time when another thread is iterating over it in a non-synchronous environment. Let's see an example for the same.

ConModTest.java

package in.techdive.java;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

public class ConModTest
{
        public ConModTest()
        {
        }

        public static void main(String[] args)
        {