Wednesday, June 13, 2018

Some Common Java Pattern Programs

package learn;

import java.util.Scanner;

public class Patterns {

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        //Taking rows value from the user
        System.out.println("How many rows you want in this pattern?");
        int r = sc.nextInt();
        System.out.println("Here is your pattern....!!!");
       
     /*1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5*/
     
        for (int i = 1; i <= r; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }
            System.out.println();
        }
            System.out.println("5---------------");
       
     /*1
        2 2
        3 3 3
        4 4 4 4
        5 5 5 5 5*/
       
        for(int i=1;i<=r;i++)
        {
        for(int j=1;j<=i;j++)
        {
        System.out.print(i+" ");
        }
        System.out.println();
        }
        System.out.println("---------------");
       
     /*1
        1 2
        1 2 3
        1 2 3 4
        1 2 3 4 5
        1 2 3 4 5 6
        1 2 3 4 5
        1 2 3 4
        1 2 3
        1 2
        1*/
       
        for(int i=1;i<=r;i++) {
        for(int j=1;j<=i;j++) {
        System.out.print(j+" ");
        }
        System.out.println();
        }
        for(int i=r-1;i>=0;i--) {
        for(int j=1;j<=i;j++) {
        System.out.print(j+" ");
        }
        System.out.println();
        }
        System.out.println("---------------");
       
     /*1 2 3 4 5
        1 2 3 4
        1 2 3
        1 2
        1 */
       
        for(int i=r;i>=1;i--) {
        for(int j=1;j<=i;j++) {
        System.out.print(j+" ");
        }
        System.out.println();
        }
        System.out.println("---------------");
       
     /*6 5 4 3 2 1
        5 4 3 2 1
        4 3 2 1
        3 2 1
        2 1
        1*/
       
        for (int i = r; i >= 1; i--)
        {
            for (int j = i; j >= 1; j--)
            {
                System.out.print(j+" ");
            }
           
            System.out.println();
        }
        System.out.println("---------------");
       
       /*1 2 3 4 5
  1 2 3 4
  1 2 3
  1 2
    1
  1 2
  1 2 3
  1 2 3 4
  1 2 3 4 5
     */
   
    //Printing upper half of the pattern
      for(int i=r;i>=1;i--) {
      for(int j=1;j<=i;j++) {
      System.out.print(j+" ");
      }
      System.out.println();
      }
    //Printing lower half of the pattern
      for(int i=2;i<=r;i++) {
      for(int j=1;j<=i;j++) {
      System.out.print(j+" ");
      }
      System.out.println();
      }
      System.out.println("---------------");
     
/      1
      2 2
    3 3 3
   4 4 4 4
         5 5 5 5 5 */
     
      int rc=1;
      for (int i = r; i > 0; i--)
        {
            //Printing i spaces at the beginning of each row
            for (int j = 1; j <= i; j++){
                System.out.print(" ");
            }
            //Printing 'rowCount' value 'rowCount' times at the end of each row
            for (int j = 1; j <= rc; j++){
                System.out.print(rc+" ");
            }
            System.out.println();
            //Incrementing the rowCount
            rc++;
        }
      System.out.println("---------------");
     
   /*1
      1 2 1
      1 2 3 2 1
      1 2 3 4 3 2 1
      1 2 3 4 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
      1 2 3 4 5 6 7 6 5 4 3 2 1*/
   
      for (int i = 1; i <= r; i++)
        {
            //Printing first half of the row
            for (int j = 1; j <= i; j++)
            {
                System.out.print(j+" ");
            }
            //Printing second half of the row
            for (int k = i-1; k >= 1; k--)
            {
                System.out.print(k+" ");
            }
            System.out.println();
        }
      System.out.println("---------------");
   
     /*1
        2 1
        3 2 1
        4 3 2 1
        5 4 3 2 1
        6 5 4 3 2 1
        7 6 5 4 3 2 1*/
        for (int i = 1; i <= r; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}

System.out.println();
}
        System.out.println("---------------");

     /*1 2 3
        4 5
        6
        9 8 7 */
        int h=9;
        for(int i=1;i<=9;i++){
        if(i<=4||i>6) {
        if(i==4) {
        System.out.println(i);
        }
       
        else if(i>6){
        System.out.print(h-- +" ");
        }
        else {
        System.out.print(i+" ");
        }
        }
        else if(i == 5 || i == 6){
        System.out.println(i);
        }
        }
        System.out.println("\n---------------");
   
   /* 1
        2
        1 2 3 2 1
        4
        5 */
       
        for(int i=1;i<=5;i++) {
//         System.out.println(i);
        if(i==3) {
        for(int j=1;j<=3;j++) {
        System.out.print(j+" ");
        if(j==3) {
        for(int k=2;k>0;k--) {
        System.out.print(k+" ");
        }
        }
        }
        System.out.println();
        }
        System.out.println(i);
        }
        System.out.println("---------------");
   
        /* 1 * * * *
    1 2 * * *
    1 2 3 * *
    1 2 3 4 *
    1 2 3 4 5   */
   
     for(int i=1;i<=r;i++)  {
        for(int j=1;j<=r;j++) {
        if(j<=i) {
        System.out.print(j+" ");
        }
        else {
        System.out.print("*"+" ");
        }
        }
        System.out.println();
     }
    System.out.println("---------------");
   
 /*1
    2 1
    3 2 1
    4 3 2 1
    5 4 3 2 1*/
   
    for (int i = 1; i <= r; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}

System.out.println();
}
    System.out.println("---------------");
   
   /*12345
  2345
    345
        45
                  5
        45
    345
  2345
      12345*/
   
    for (int i = 1; i <= r; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= r; j++) {
System.out.print(j);
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = r-1; i >= 1; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= r; j++) {
System.out.print(j);
}
System.out.println();
}
System.out.println("---------------");
  /* 1 2 3 4 5
       2 3 4 5
3 4 5
 4 5
  5
        4 5
      3 4 5
    2 3 4 5
  1 2 3 4 5 */   
   
for (int i = 1; i <= r; i++)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= r; j++) {
System.out.print(j+" ");
}
System.out.println();
}
//Printing lower half of the pattern
for (int i = r-1; i >= 1; i--)
{
//Printing i spaces at the beginning of each row
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
//Printing i to rows value at the end of each row
for (int j = i; j <= r; j++) {
System.out.print(j+" ");
}
System.out.println();
}
System.out.println("---------------");
   
     /*1
10
101
1010
10101*/

for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= i; j++)
{
if(j%2 == 0)
{
System.out.print(0);
}
else
{
System.out.print(1);
}
}

System.out.println();
}
System.out.println("---------------");

             /*11111
11122
11333
14444
55555*/

for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= r-i; j++)
{
System.out.print(1);
}

for (int j = 1; j <= i; j++)
{
System.out.print(i);
}

System.out.println();
}
System.out.println("---------------");
   
    /*10000
02000
00300
00040
00005*/

for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= r; j++)
{
if(i == j)
{
System.out.print(i);
}
else
{
System.out.print(0);
}
}

System.out.println();
}
System.out.println("---------------");

    /*1
2 6
3 7 10
4 8 11 13
5 9 12 14 15*/

for (int i = 1; i <= r; i++)
{
int num = i;

for (int j = 1; j <= i; j++)
{
System.out.print(num+" ");

num = num+r-j;
}

System.out.println();
}
System.out.println("---------------");
   
        //Close the resources
        sc.close();
    }
}

Monday, June 11, 2018

10 Java Interview Programmatic Questions And Answers

10 Java Interview Programmatic Questions And Answers

1) FizzBuzz Problem.
Write a program in java which prints the numbers from 1 to 100. But, multiples of 3 should be replaced with “Fizz”, multiples of 5 should be replaced with “Buzz” and multiples of both 3 and 5 should be replaced with “FizzBuzz”?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class FizzBuzzProblem
{
    public static void main(String args[])
    {
        for(int i = 1; i <= 100; 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);
              }
        }
    }
}
2) Write a Java program to find out the nearest number to 100 of the given two numbers?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class NearestNumber
{
    static int nearestTo100(int input1, int input2)
    {
        int diff1 = Math.abs(100 - input1);
        int diff2 = Math.abs(100 - input2);
        if(diff1 < diff2)
        {
            return input1;
        }
        else if (diff2 < diff1)
        {
            return input2;
        }
        else
        {
            return input1;
        }
    }
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number");
        int input1 = sc.nextInt();
        System.out.println("Enter Second Number");
        int input2 = sc.nextInt();
        System.out.println(nearestTo100(input1, input2));
    }
}
3) There are two numbers ‘a’ and ‘b’. Write a java program which should print ‘a’ if ‘a’ is bigger than ‘b’ by 2 or more or should print ‘b’ if ‘b’ is bigger than ‘a’ by 2 or more. Otherwise, it should print “INCONCLUSIVE”?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class BiggerNumber
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First number");
        int a = sc.nextInt();
        System.out.println("Enter Second Number");
        int b = sc.nextInt();
        if((a > b) && (a - b) >= 2)
        {
            System.out.println(a);
        }
        else if ((b > a) && (b - a) >=2)
        {
            System.out.println(b);
        }
        else
        {
            System.out.println("INCONCLUSIVE");
        }
    }
}
4) Given a string “JAVAJ2EE”, write java program to print this string in the below format?
J
J A
J A V
J A V A
J A V A J
J A V A J 2
J A V A J 2 E
J A V A J 2 E E

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class PromrammingExample
{
    public static void main(String[] args)
    {
        String s = "JAVAJ2EE";
        char[] c = s.toCharArray();
        for(int i = 0; i < c.length; i++)
        {
            for(int j = 0; j <= i; j++)
            {
                System.out.print(c[j]+" ");
            }
            System.out.println();
        }
    }
}
5) How do you prove that String s1 = new String(“ONE”) and String s2 = “ONE” are two different objects in the memory?
Ans : Use “==” operator to compare s1 and s2. It will return ‘false’ if they are two different objects in the memory.

1
2
3
4
5
6
7
8
9
10
11
public class ProgrammingExamples
{
    public static void main(String[] args)
    {
        String s1 = "ONE";
        String s2 = new String("ONE");
        System.out.println(s1 == s2);      //Output : false
    }
}
6) What will be the output of this program?

1
2
3
4
5
6
7
8
9
public class CodingExamples
{
    public static void main(String args[])
    {
        int i = (byte) + (char) - (int) + (long) - 1;
        System.out.println(i);
    }
}
Ans : 1
7) Will this program compiles successfully?

1
2
3
4
5
6
7
8
9
10
11
public class CodingExamples
{
    public static void main(String args[])
    {
        System.out.println(1);
         http://javaconceptoftheday.com/
        System.out.println(2);
    }
}
Ans : Yes. Compiler will treat ‘http’ as label and remaining part (of Line 7) will be commented.
8) What will be the output of the below program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class CodingExamples
{
    public static void main(String[] args)
    {
        String[] s1 = {"ONE", "TWO", "THREE", "FOUR"};
        String[] s2 = {"THREE", "TWO", new String("ONE")};
        System.out.println(s1[0] == s2[2]);
        System.out.println(s1[1] == s2[1]);
        System.out.println(s1[2] == s2[0]);
    }
}
Ans :
false
true
true
9) Open a notepad in your system through a java program?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ClassesAndObjects
{
    public static void main(String[] args)
    {
        try
        {
            Runtime.getRuntime().exec("notepad.exe");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
10) Write a Java program to print the current date in “dd MMM yyyy” format?

1
2
3
4
5
6
7
8
9
10
11
public class CurrentDate
{
    public static void main(String[] args)
    {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
        System.out.println(formatter.format(date));
    }
}