Monday, November 17, 2014

Interview Questions


  • 8 balls , 7 correct 1 heavy. What will be  the minimum value of iterations to find faulty ball ??
  • 8 balls , 7 correct 1 faulty [ don't know light or heavy ] . What will be  the minimum value of iterations to find faulty ball ??
  • In an array of integer, find pair of 2 numbers such that the sum is equal to k.
  • What is time complexity in Map for get and put operations ?
  • How to make class immutable ? 
  • What are patterns you know ?
  • What is singleton pattern ?
  • How mysql saves data ?
  • What are the optimization techniques in mysql ?
  • What are cons in using hibernate ?
  • How do you implement caching in hibernate ?
  • What is difference in GET and POST ?
  • How can you index a column in table  ?
  • What is generics ? 
  • What did you used to implement Web-services  ?
  • How will you find kth minimum number in array of integers ? 

Thursday, June 5, 2014

Java Programs For Various Series

Fibonacci Series :- 

Code :- 


import java.util.Scanner;


public class Fibonacci {


public static void main(String arg[]){

Scanner in = new Scanner(System.in);
int n = in.nextInt();

for (int i = 0; i <= n; i++) {
           System.out.print(fibonacci(i) + " ");
}
 
in.close();
}


public static int fibonacci(int n) {

      if (n == 0) {
          return 0;
      } else if (n == 1) {
          return 1;
      } else {
          return fibonacci(n - 1) + fibonacci(n - 2);
      }
  }
}



Output:-

10

0 1 1 2 3 5 8 13 21 34 55




Saturday, March 1, 2014

On The Cloud [AWS]

I was assigned to setting up new server on AWS and i was pretty much excited while i started working on it. Being a java guy, i am having no knowledge of working with servers, but let see:-

Setting up ISPConfig 3:-

Just followed the instruction given on this article. And yeah ISPConfig is installed.

Successfully generated SPF

Now i am stuck with DKIM, i have to set my mail server and i used Postfix for it, but my ClaimAV is real pain in the arse. I have created DKIM key and all other settings but now mail delivery is stopped. :'(

Dammn itt. I had no SWAP on AWS that is why it was failing.
I found out via:-
free -m

I added swap space by help of this link :-

SWAPFILE=/mnt/swapfile.swap
dd if=/dev/zero of=$SWAPFILE bs=1M count=512
mkswap $SWAPFILE
swapon $SWAPFILE


and voila its working......  ^_^

Sunday, September 22, 2013

Java inheritance concept

Interviewers loves to trap candidates in inheritance and overridding concepts. So here is the solution. Just copy the code below and you will understand all about it as soon as you run this code. Let me know in case of any confusion.  ;)

P.S.:- Be careful for the "**".




public class InheritanceConcept {

public InheritanceConcept() {
System.out.println("Inside Polymorphism Constructor");
}

public static void main(String args[]) {

Parent p = new Parent();
Child c = new Child();

Parent pCast = new Child();
// Child cCast = (Child) new Parent(); //Class Cast Exception **Downcasting

p.ParentsMethod(); //inside ParentsMethod
p.OverriddenMethod(); //inside Parent's OverriddenMethod

c.ParentsMethod(); //inside ParentsMethod
c.ChildsMethod(); //inside ChildsMethod
c.OverriddenMethod(); //inside Child's OverriddenMethod

pCast.ParentsMethod(); //inside ParentsMethod
pCast.OverriddenMethod(); //inside Child's OverriddenMethod **Runtime Polymorphism/Dynamic Binding

}
}

class Parent{

Parent(){
System.out.println("Inside Parent's Constructor");
}

void OverriddenMethod(){
System.out.println("inside Parent's OverriddenMethod");
}

void ParentsMethod(){
System.out.println("inside ParentsMethod");
}
}

class Child extends Parent{

Child(){
System.out.println("Inside Child's Constructor");
}

void OverriddenMethod(){
System.out.println("inside Child's OverriddenMethod");
}

void ChildsMethod(){
System.out.println("inside ChildsMethod");
}
}

Tuesday, June 26, 2012

Comparison BetweenTwo Dates


Monday, June 11, 2012

Select Option Ops

<html>
<body>

<select id="slct" onchange="slct();">
  <option value="1">Volvo</option>
  <option value="2">Saab</option>
  <option value="3">Mercedes</option>
  <option value="4">Audi</option>
</select>

<script>
function slct(){

    var selObj = document.getElementById('slct');
    var selIndex = selObj.selectedIndex;

    alert(selIndex);
    alert(selObj.options[selIndex].value);
    alert(selObj.options[selIndex].text);
}
</script>


</body>
</html>

Tuesday, May 22, 2012

Codelets

#Convert String to Date in Java


java.util.Date = java.text.DateFormat.getDateInstance().parse(date String);
or


SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" );
Date date = format.parse( myString );

Saturday, March 3, 2012

Read File In JAVA


import java.io.*;

class ReadFile{

    public static void main(String args[]) {
        try {

            FileInputStream fstream = new FileInputStream("c:/yash.txt");

            DataInputStream disobj = new DataInputStream(fstream);

            BufferedReader br = new BufferedReader(new InputStreamReader(disobj));

            String frst = br.readLine();
            String scnd = br.readLine();
            if (frst != null) {
                System.out.println(frst);
                System.out.println(scnd);
            }
            else{
                System.out.println("empty");
            }
            disobj.close();

        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

    }
}

Sunday, February 19, 2012

In The Spring 3.0

Introduction-


To be very honest i am not some die hard geek coder or some sort of JAVA Guru writing some Bible. I am just a beginner who just started learning to work in JAVA but i am trying to note all the points i am covering while learning Spring 3.0. Just hoping, may be it will also help you. Have fun. :-)

Tuesday, December 13, 2011

Generate random string of certain length

function genRandomString()

{
$length = 10;
$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$string = null;
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
return $string;
}



$x=genRandomString();
echo substr($x,0,6);

?>

Monday, December 5, 2011

XML Parser in JSP

/**
*
* @author yashx1@gmail.com
*/

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.InputStream;
import java.net.URL;
import com.ahoy.ubid.utils.ParseUtil;

public class XmlParserHelp {

public static void main(String argv[]) {
try {
ParseUtil p = new ParseUtil();
URL xmlURL = new URL("http://www.newsletter.uahoy.com:8081/WeBid/api.auctions.php");
InputStream xml = xmlURL.openStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(xml);
xml.close();
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("result");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("auctionTitle : " + p.getTagValue("auctionTitle", eElement));

//System.out.println("auctioneerId : " + p.getTagValue("auctioneerId", eElement));
}
}
} catch (Exception e) {
System.out.println(e);
}
}

}

Tuesday, November 22, 2011

Validation For Checking If User Is Above 18 Years Of Age

This code block returns if user is above 18 years of age. it also have checks for leap year and all. Although it is written in php but logic can be used in other platforms also.


//for calculating birthdate.
$birth_day = $_GET["day"];
$birth_month = $_GET["month"];
$birth_year = $_GET["year"];
$DATE = $birth_year . $birth_month . $birth_day;
function CheckAge($day, $month, $year)
{
$QCPASS=0;
if((empty($day))&&(empty($month))&&(empty($year))){
echo "atleast put the date, man!! :@";
$QCPASS=1;
}

elseif((!empty($day))&&(!empty($month))&&(!empty($year))){
if($day<1||$day>31){
echo "day is not valid. R u nuts ?? ";
$QCPASS=1;
}
elseif(($month<1||$month>12)&&$month!=2){
echo "month is not valid. may god help u.....";
$QCPASS=1;
}
elseif($month==2){
if($year%4==0&&$day>29)
{
echo "only till 29 in feb";
$QCPASS=1;
}
elseif($year%4!=0&&$day>28){
echo "only till 28 not a leap year bro...";
$QCPASS=1;
}
}
elseif($month==4||$month==6||$month==9||$month==11){
if($day>30){
echo "some months have 30 days only. sorry i cant change my calendar for u.";
$QCPASS=1;
}
}
if($QCPASS==0){

{
$NOW_year = gmdate('Y');
$NOW_month = gmdate('m');
$NOW_day = gmdate('d');

if (($NOW_year - $year) > 18){
echo "fine,u r above 18. do watever u lyk dude.....";
}
elseif ((($NOW_year - $year) == 18) && ($NOW_month > $month)){
echo "fine,u r above 18. do watever u lyk dude.....";
}
elseif ((($NOW_year - $year) == 18) && ($NOW_month == $month) && ($NOW_day >= $day)){
echo "fine,u r above 18. do watever u lyk dude.....";
}
else{
echo "mummy se bolo complan pilayein"; //if err exist
}
}
}
}
else if((!empty($day))||(!empty($month))||(!empty($year))){
echo "LOL......u missed some fields above. :D ";
}


}

CheckAge($birth_day, $birth_month, $birth_year);

?>

Generating MD5 Encrypted Password For Storing In Database


This code block generates hash code for an input such as password. But this must be kept in mind that this uses MD5 encryption method,which is only one way encryption method. Means if u want to decrypt the stored password in database it is not feasible. So you can also use reset passord method for setting new password:-

[This code is written in php,butt this can be used in any other desired language using the same function.]


function get_hash(){
$string = '0123456789abcdefghijklmnopqrstuvyxz';
$hash = '';
for ($i = 0; $i < 5; $i++){
$rand = rand(0, (34 - $i));
$hash .= $string[$rand];
$string = str_replace($string[$rand], '', $string);
}
return $hash;
}

Validation For e-mail Id

This code generates the valid output for an email. It is written for php but can be used in any language :-

function getmailvalid(){
$x=$_GET['email'];
$atpos=strpos($x,"@");
$dotpos=strripos($x,".");

if ($atpos<1 || $dotpos<$atpos+2 || $dotpos+2>=strlen($x)){
return 1;
}
else{
return 0;
}
}
$valideid=getmailvalid();