Monday, 18 January 2016

Strings are immutable

Que: Write a program that proves Strings are immutable.
Ans: Though there is no way to find if Strings are immutable through coding, but the code below is an attempt to show if Strings are mutable.Remember the only way to say if Strings are immutable is to see the API.
 public class ImmutableTest {
        public static void main(String args[]){
            String initial = "ABCDEFG";
            String after = initial.replace('A', 'Z');
    System.out.println("Immutable Demo \n");
            System.out.println("initial = " + initial);
            System.out.println("after= " + after);
        }
    }


OUTPUT :



Fizz Buzz Problem

@: FizzBuzz problem :@
Write a Java program that prints the numbers from 1 to 50. But for 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"
This is also one of the classical programming questions, which is asked on any Java programming or technical interviews. Here is a sample Java program to solve FizzBuzz problem :
  public class FizzBuzzTest{
        public static void main(String args[]){
            for(int i = 1; i <= 50;i++) {
            if(i % (3*5) == 0) System.out.println( i + " FizzBuzz");
            else if(i % 5 == 0) System.out.println( i + " Buzz");
            else if(i % 3 == 0) System.out.println( i + " Fizz");
           // else System.out.println(i);
            }
        }
    }



OUTPUT :