Showing posts with label Java Program. Show all posts
Showing posts with label Java Program. Show all posts

Saturday, 1 February 2014

Interview question code


Interview question code

Question :- wap to print the numbers from 1 to 50. But in multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"



solution:-

         public class FizzBTest
{
 //Create main function
    public static void main(String args[])
  {
           for(int i = 1; i <= 50; i++) 
          {
            if(i % (3*5) == 0) System.out.println("FizzBuzz");
            else if(i % 5 == 0) System.out.println("Buzz");
            else if(i % 3 == 0) System.out.println("Fizz");
            else System.out.println(i);
         } 
   } 

}


Posted By:M@ddy Th@kur 
On:Shareyourconscience

Sunday, 12 January 2014

Java problem to delete all duplicate digits from an input number?

Java problem to delete all duplicate digits from an input number?

Question:Write a program in java to delete all duplicate digits from an input number?

 Eg: if input is 112354 then output will be 12354 

Answer

import java.util.*;
public class duplicate {

public static void main(String args[]) 
{ long n;
int i=0,l=0,k=0;

Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
n=sc.nextLong();
String s=Long.toString(n);
char c[]=new char[s.length()];
ArrayList cc=new ArrayList();
String temp = Long.toString(n);
int[] arr = new int[temp.length()];
for (int m = 0; m< temp.length(); m++)
{
arr[m] = temp.charAt(m) - '0';
}
for(i=0;i<s.length();i++)
{
for(int j=0;j<s.length();j++)
{

if(arr[i]==arr[j]&&i!=j)
{
l=1;
}

}
if(l==0)
{
cc.add(arr[i]);
k++;
}
l=0;
}
Iterator itr=cc.iterator();
while(itr.hasNext())
{
Object ans=itr.next();
System.out.print(ans);

}
}
}


Posted By:Tanuj Kumar 
On:Shareyourconscience

Monday, 30 December 2013

Most frequently asked interview question on String in java

Most frequently asked interview question on String in java

 Here we describe difference between (==) and .equals() :-

Both of them very much differ in their significance and working as equals() method is present in the java.lang.Object class and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the '==' operator is expected to check the actual object instances are same or not.
Let us see an example we have two String objects and they are being pointed by two different reference variables as a1 and a2.

 a1 = new String("xyz");
 a2 = new String("xyz");
Now, if you use the "equals()" method to check for their equivalence as

 if(a1.equals(a2))
      System.out.println("a1.equals(a2) is TRUE");
 else
      System.out.println("a1.equals(a2) is FALSE");
You will get the output as TRUE as the 'equals()' method check for the content equivality.
Lets check the '==' operator..

if(s1==s2)
     System.out.printlln("a1==a2 is TRUE");
   else
     System.out.println("a1==a2 is FALSE");
Now you will get the FALSE as output because both a1 and a2 are pointing to two different objects even though both of them share the same string content. It is because of 'new String()' everytime a new object is created.
Try running the program without 'new String' and You will get TRUE for both the tests. 

   String a1 = "xyz";
   String s2 = "xyz";

                                                         

Posted By:M@ndeep R@w@t
On:Shareyourconscience