Have a Question?

If you have any question you can ask below or enter what you are looking for!

Fresher Java interview Question page4

61. Write a program to display the following pattern(Floyd's Traingle).
1

23

456

78910

 

package com.java4us;

public class Program61 {

    public static void main(String[] args) {
        int k = 1;
        System.out.println("---Program to display the following pattern---");
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(k);
                k++;
            }
            System.out.println();
        }
    }
}
62. Write a program to display the following pattern.
*
* *
* *
* *
*
package com.java4us;

public class Program62 {
    public static void main(String[] args) {
        int i, j, k = 1;

        System.out.println("Program to display following Pattern");

        for (i = 1; i <= 3; i++) {
            for (j = 3; j > i; j--)
                System.out.print(" ");
            for (k = 1; k <= i; k++)
                System.out.print("* ");
            System.out.print("n");
        }

        for (i = 2; i >= 1; i--) {
            for (j = i; j < 3; j++)
                System.out.print(" ");
            for (k = 1; k <= i; k++)
                System.out.print("* ");
            System.out.println();

        }
    }
}
63. Write a program to display the following.

package com.java4us;

public class Program63 {
    public static void main(String[] args) {
        char ch1 = 61;
        System.out.print(ch1);

        char ch2 = 61;
        for (int i = 0; i < 22; i++)
            System.out.print(ch2);

        char ch3 = 61;
        System.out.println(ch3);

        char ch4 = 47;
        System.out.print(ch4);

        System.out.print(" Welcome to Java4us ");

        System.out.println(ch4);

        char ch5 = 61;
        System.out.print(ch5);

        for (int i = 0; i < 22; i++)
            System.out.print(ch2);

        char ch6 = 61;
        System.out.print(ch6);

    }
}
64. Write a program to display the following.

package com.java4us;

public class Program64 {
    public static void main(String[] args) {
        char ch1 = 61;
        System.out.print(ch1);

        char ch2 = 61;
        for (int i = 0; i < 22; i++)
            System.out.print(ch2);

        char ch3 = 61;
        System.out.println(ch3);

        char ch4 = 47;
        System.out.print(ch4);

        System.out.print("                      ");

        System.out.println(ch4);

        char ch5 = 61;
        System.out.print(ch5);

        for (int i = 0; i < 22; i++)
            System.out.print(ch2);

        char ch6 = 61;
        System.out.print(ch6);
        System.out.println();
        System.out.print(ch4);

        System.out.print("                      ");

        System.out.println(ch4);

        char ch7 = 61;
        System.out.print(ch7);

        for (int i = 0; i < 22; i++)
            System.out.print(ch2);

        char ch8 = 61;
        System.out.print(ch8);
    }
}
65. Write a program to display the following.

package com.java4us;

public class Program65 {
    public static void main(String[] args) {
        char ch1 = 201;
        System.out.print(ch1);

        char ch2 = 205;
        for (int i = 0; i < 6; i++)
            System.out.print(ch2);

        char chh1 = 203;
        System.out.print(chh1);

        for (int i = 0; i < 6; i++)
            System.out.print(ch2);

        char ch3 = 187;
        System.out.println(ch3);

        char ch4 = 186;

        for (int i = 0; i < 3; i++) {
            System.out.print(ch4);
            System.out.print("      ");
            System.out.print(ch4);
            System.out.print("      ");
            System.out.print(ch4);
            System.out.println();
        }

        char ch5 = 204;
        System.out.print(ch5);

        for (int i = 0; i < 6; i++)
            System.out.print(ch2);

        char chh2 = 206;
        System.out.print(chh2);

        for (int i = 0; i < 6; i++)
            System.out.print(ch2);

        char ch6 = 185;
        System.out.print(ch6);
        System.out.println();
        for (int i = 0; i < 3; i++) {
            System.out.print(ch4);
            System.out.print("      ");
            System.out.print(ch4);
            System.out.print("      ");
            System.out.print(ch4);
            System.out.println();
        }

        char ch7 = 200;
        System.out.print(ch7);
        for (int i = 0; i < 6; i++)
            System.out.print(ch2);

        char chh3 = 202;
        System.out.print(chh3);
        for (int i = 0; i < 6; i++)
            System.out.print(ch2);

        char ch8 = 188;
        System.out.print(ch8);
    }
}
66. Write a program to print the table from 2 to N in following formet.

package com.java4us;

import java.util.Scanner;

public class Program66 {

    public static void main(String[] args) {
        System.out.println("---Program to print the table from 2 to N in following formet---");
        System.out.println("Please enter N value");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= 10; j++) {
                System.out.print(i * j + "t");
            }
            System.out.println();
        }
    }
}
67. Write a program to print the table from 2 to N in following formet

package com.java4us;

import java.util.Scanner;

public class Program67 {

    public static void main(String[] args) {
        System.out.println("---Program to print the table from 2 to N in following formet---");
        System.out.println("Please enter N value");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i <= 10; i++) {
            for (int j = 2; j <= n; j++) {
                System.out.print(i * j + "t");
            }
            System.out.println();
        }
    }
}
68. Write a program to read a number in String and convert it to integer without usiing build in method
package com.java4us;

import java.util.Scanner;

public class Program68 {

    public static void main(String[] args) {
        System.out.println("---Program to read a number in String and convert it to integer without usiing build in method---");
        System.out.println("Please enter a Number");
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number:t");
        String str = scanner.next();
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            num = num * 10;
            num = num + str.charAt(i) - '0';
            // System.out.println(num);
        }
        System.out.println(num);
    }
}
69. Write a program to read a decimal number and convert it to binary number without using any build in method.
package com.java4us;

import java.util.Scanner;

public class Program69 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a decimal number and convert it to binary number without using any build in method---");
        System.out.println("Please enter a Decimal Number");
        Scanner scanner = new Scanner(System.in);
        int decimal_value = scanner.nextInt();
        int temp, j;
        String str1 = "";
        while (decimal_value >= 1) {
            j = decimal_value / 2;
            temp = decimal_value % 2;
            decimal_value = j;

            str1 = temp + str1;
        }
        System.out.println("binary Number is :" + str1);
    }
}
70. Write a program to read a decimal number and convert it to octal number without using build in method.
package com.java4us;

import java.util.Scanner;

public class Program70 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a decimal number and convert it to octal number without using build in method---");
        System.out.println("Please enter a Deciaml Number");
        Scanner scanner = new Scanner(System.in);
        int number = scanner.nextInt();
        int temp, j;
        String str1 = "";
        while (number > 7) {
            j = number / 8;
            temp = number % 8;
            number = j;

            str1 = temp + str1;
        }
        str1 = number + str1;
        System.out.println("octal value is :" + str1);
    }
}
71. Write a program to read a Decimal number and convert it to hexadecimal number without using any build in method.
package com.java4us;

import java.util.Scanner;

public class Program71 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a Decimal number and convert it to hexadecimal number without using any build in method---");
        System.out.println("Please enter a decimal value");
        Scanner scanner = new Scanner(System.in);
        int decimal_value = scanner.nextInt();
        int temp, j;
        String str1 = "";
        String str = "";
        while (decimal_value > 15) {
            j = decimal_value / 16;
            temp = decimal_value % 16;
            decimal_value = j;
            if (temp > 9) {
                switch (temp) {
                case 10:
                    str = "A";
                    break;
                case 11:
                    str = "B";
                    break;
                case 12:
                    str = "C";
                    break;
                case 13:
                    str = "D";
                    break;
                case 14:
                    str = "E";
                    break;
                case 15:
                    str = "F";
                    break;
                }
                str1 = str + str1;
            } else {
                str1 = temp + str1;
            }
        }
        if (decimal_value > 9) {
            switch (decimal_value) {
            case 10:
                str = "A";
                break;
            case 11:
                str = "B";
                break;
            case 12:
                str = "C";
                break;
            case 13:
                str = "D";
                break;
            case 14:
                str = "E";
                break;
            case 15:
                str = "F";
                break;
            }
            str1 = str + str1;
        }
        System.out.println("hexadecimal :" + str1);
    }
}
72. Write a program to read a binary number and convert it to decimal number without using build in method.
package com.java4us;

import java.util.Scanner;

public class Program72 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a binary number and convert it to decimal number without using build in method---");
        System.out.println("Please enter a Binary Number");
        Scanner scanner = new Scanner(System.in);
        String binary_value = scanner.next();
        long dec = 0;

        for (int i = 0; i < binary_value.length(); i++) {
            int num = binary_value.charAt(i) - '0';
            dec = 2 * dec + num;
            // System.out.println(dec);
        }
        System.out.println("decimal :" + dec);
    }
}
73. Write a program to read five number, store in integer array and display the occurrence of each number.
package com.java4us;

import java.util.Scanner;

public class Program73 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read five number, store in integer array and display the occurrence of each number---");
        System.out.println("Please enter five number");
        int arr[] = new int[5];
        // int count = 0;
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            arr[i] = scanner.nextInt();
        }
        for (int i = 0; i < 5; i++) {
            int count = 0;
            for (int k = 0; k < 5; k++) {
                if (arr[i] == arr[k])
                    count++;
            }
            Boolean flag = false;
            for (int k = 0; k < i; k++) {
                if (arr[i] == arr[k])
                    flag = true;
            }
            if (flag == false)
                System.out.println("occurance of  " + arr[i] + " is " + count + " times");
        }
    }
}
74. Write a program to read a String and store into charactor array without using any built in method.
package com.java4us;

import java.util.Scanner;

public class Program74 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a String and store into charactor array without using any built in method---");
        System.out.println("Please enter a String");
        Scanner scanner = new Scanner(System.in);
        String value = scanner.next();
        char ch[] = new char[value.length()];
        for (int i = 0; i < value.length(); i++) {
            ch[i] = value.charAt(i);
        }
        System.out.println(ch);
    }
}
75. Write a program to read a String and display in lower case without using any build in method
package com.java4us;

import java.util.Scanner;

public class Program75 {

    public static void main(String[] args) {
        System.out.println("Write a program to read a String and display in lower case without using any build in method---");
        System.out.println("Please enter a String");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (65 <= ch && ch <= 90) {
                ch = (char) ((ch + 32));
                System.out.print(ch);
            }
        }
    }
}
76. Write a program to read a String and display in Upper case without using any build in method.
package com.java4us;

import java.util.Scanner;

public class Program76 {

    public static void main(String[] args) {
        System.out.println("Write a program to read a String and display in Upper case without using any build in method---");
        System.out.println("Please enter a String");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch >= 97 && ch <= 122) {
                ch = (char) ((ch - 32));
                System.out.print(ch);
            }
        }
    }
}
77. Write a program to read a String and a charactor, display the index of first occurrence of given charactor in the given String without using any built in method.
package com.java4us;

import java.util.Scanner;

public class Program77 {

    public static void main(String[] args) {
        System.out.println(
            "---Write a program to read a String and a charactor, display the index of first occurrence of given charactor in the given String without using any built in method---");
        System.out.println("Please enter a String");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        System.out.println("Please enter a charactor value");
        boolean flag = false;
        int i;
        char ch = scanner.next().charAt(0);
        for (i = 0; i < str.length(); i++) {
            if (ch == str.charAt(i)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            System.out.println("First Occurrence is " + i);
        } else
            System.out.println("Charactor is not found in given String");
    }
}
78. Write a program to read a String and a charactor, display the index of Last occurrence of given charactor in the given String without using any built in method.
package com.java4us;

import java.util.Scanner;

public class Program78 {

    public static void main(String[] args) {
        System.out.println(
            "---Write a program to read a String and a charactor, display the index of Last occurrence of given charactor in the given String without using any built in method");
        System.out.println("Please enter a String");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        System.out.println("Please enter a charactor value");
        boolean flag = false;
        int i, last_occurrence = 0;
        char ch = scanner.next().charAt(0);
        for (i = 0; i < str.length(); i++) {
            if (ch == str.charAt(i)) {
                flag = true;
                last_occurrence = i;
            }
        }
        if (flag) {
            System.out.println("Last Occurrence is " + last_occurrence);
        } else
            System.out.println("Charactor is not found in given String");
    }
}
79. Write a program to read a String and display the occurrence of each unique charactor in the given String.
package com.java4us;

import java.util.Scanner;

public class Program79 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a String and display the occurrence of each unique charactor in the given String---");
        System.out.println("Please enter a String");
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        for (int i = 0; i < str.length(); i++) {
            int count = 0;
            for (int j = 0; j < str.length(); j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    count++;
                }
            }
            boolean flag = false;
            for (int j = 0; j < i; j++) {
                if (str.charAt(i) == str.charAt(j)) {
                    flag = true;
                }
            }
            if (flag == false) {
                System.out.println("Occrrunce of " + str.charAt(i) + " is " + count);
            }
        }
    }
}
80. Write a program to read a sentance and display the number of words available in the given sentance.
package com.java4us;

import java.util.Scanner;

public class Program80 {

    public static void main(String[] args) {
        System.out.println("---Write a program to read a sentance and display the number of words available in the given sentance---");
        Scanner sc = new Scanner(System.in);
        String str;
        char ch;
        int i, count = 1;
        System.out.println("Enter sentence:");
        str = sc.nextLine();
        for (i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            if (ch == ' ')
                ++count;
        }
        System.out.println("Number of words in sentence is:  " + count);
    }
}