Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Interview Coding problems for Freshers- Part II

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 310
    Comment on it

    In the previous tutorial of Java Interview Questions and Answers Part 1, I have explained some basic coding problems of Java.This part is in continuation with the previous Java Interview Questions and Answers tutorial. In this part of the tutorial we are proceeding with more problems based on further concepts of Java and trying to guide you with the best possible explanations.

    11- Calculate Circle Area using radius

    /* This program shows how to calculate area of circle using it's radius.*/

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        public class CalculateCircleAreaExample {
        public static void main(String[] args) {
        int radius = 0;
        System.out.println("Please enter radius of a circle");
        try
        {
        //get the radius from console
        BufferedReader br = new BufferedReader(new
        InputStreamReader(System.in));
        radius = Integer.parseInt(br.readLine());
        }
        //if invalid value was entered
        catch(NumberFormatException ne)
        {
        System.out.println("Invalid radius value" + ne);
        System.exit(0);
        }
        catch(IOException ioe)
        {
        System.out.println("IO Error :" + ioe);
        System.exit(0);
        }
        /*
        * Area of a circle is
        * pi * r * r
        * where r is a radius of a circle.
        */
        //NOTE : use Math.PI constant to get value of pi
        double area = Math.PI * radius * radius;
        }
        System.out.println("Area of a circle is " + area);
        }
    
    

    Output of Calculate Circle Area using Java Example would be

    /*
    Please enter radius of a circle
    19
    Area of a circle is 1134.1149479459152
    */

    12 - Factorial of a number using recursion

    /* This program shows how to calculate

    Factorial of a number using recursion function. */

        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        public class JavaFactorialUsingRecursion {
        public static void main(String args[]) throws NumberFormatException,
        IOException{
        System.out.println("Enter the number: ");
        //get input from the user
        BufferedReader br=new BufferedReader(new
        InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
        //call the recursive function to generate factorial
        int result= fact(a);
        }
        System.out.println("Factorial of the number is: " + result);
        static int fact(int b)
        {
        if(b <= 1)
        //if the number is 1 then return 1
        return 1;
        else
        //else call the same function with the value - 1
        return b * fact(b-1);
        }
        }
    
    

    Output of this Java example would be

    /*
    Enter the number:
    5
    Factorial of the number is: 120
    */

    13 pyramid of numbers using for loops

    /* Generate Pyramid For a Given Number Example

    This Java example shows how to generate a pyramid of numbers for given number using for loop example. */

        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        public class GeneratePyramidExample {
        public static void main (String[] args) throws Exception{
        BufferedReader keyboard = new BufferedReader (new
        InputStreamReader(System.in));
        System.out.println("Enter Number:");
        int as= Integer.parseInt (keyboard.readLine());
        System.out.println("Enter X:");
        int x=
        Integer.parseInt (keyboard.readLine());
        int y = 0;
        for(int i=0; i<= as ;i++){
        for(int j=1; j <= i ; j++){
        System.out.print(y + "\t");
        y = y + x;
        }
        System.out.println("");
        }
        }
        }
    
    

    Output of this Java example would be

    /*
    Enter Number:
    5
    Enter X:
    1
    0
    1 2 3 4 5 6 7 8 9
    12 13
    10 11
    14
    ----------------------------------------------
    Enter Number:
    5
    Enter X:
    2
    0
    2 4
    6 8
    10
    12 14 16 18
    20 22 24 26
    28
    ----------------------------------------------
    Enter Number:
    5
    Enter X:
    3
    0
    3 6 9 12 15 18 21 24 27
    30 33 36 39
    42
    
    */
    

    14 To Find Maximum of Two Numbers.

    /* To Find Maximum of 2 Numbers using if else */

        class Maxoftwo{
        public static void main(String args[]){
        //taking value as command line argument.
        //Converting String format to Integer value
        int i = Integer.parseInt(args[0]);
        int j = Integer.parseInt(args[1]);
        if(i > j)
        System.out.println(i+" is greater than "+j);
        else
        System.out.println(j+" is greater than "+i);
        }
        }
    
    

    Output of this Java example would be

    /*
    i=10 , j=15
    15 is greater than 10
    */
    

    15 To Find Minimum of Two Numbers using conditional operator.

    /* To find minimum of 2 Numbers using ternary operator */

    class Minoftwo{
    public static void main(String args[]){
    //taking value as command line argument.
    //Converting String format to Integer value
    int i = Integer.parseInt(args[0]);
    int j = Integer.parseInt(args[1]);
    int result = (i<j)?i:j ;
    System.out.println(result+" is a minimum value");
    }
    }
    

    Output of this Java example would be

    /*
    i=10,j=15
    Output would be
    10 is a minimum value"
    */
    

    16-Write a program that will read a float type value from the print the following output.

    ->Small Integer not less than the number.

    ->Given Number.

    ->Largest Integer not greater than the number. */

        class ValueFormat{
        public static void main(String args[]){
        double i = 34.32; //given number
        System.out.println("Small Integer not greater than the number :
        "+Math.ceil(i));
        System.out.println("Given Number : "+i);
        System.out.println("Largest Integer not greater than the number :
        "+Math.floor(i));
        }
    
    

    Output of this Java example would be

    Small Integer not greater than the number :35.0                                                                                                                  
    Given Number : 34.32                                                                                                                                             
    Largest Integer not greater than the number :34.0 
    

    17 - Write a program to generate 5 Random nos. between 1 to 100, and it should not follow with decimal point.

        class RandomDemo{
        public static void main(String args[]){
        for(int i=1;i<=5;i++){
        System.out.println((int)(Math.random()*100));
        }
        }
        }
    
    

    Output of this Java example would be

    /*
    79                                                                                                                                                               
    97                                                                                                                                                               
    27                                                                                                                                                               
    94                                                                                                                                                               
    22 */
    

    18 - Write a program to display a greet message according to Marks obtained by student.

        class SwitchDemo{
        public static void main(String args[]){
        int marks = Integer.parseInt(args[0]);
        as command line argument.
        switch(marks/10){
        case 10:
        case 9:
        case 8:
        System.out.println("Excellent");
        break;
        case 7:
        System.out.println("Very Good");
        break;
        case 6:
        System.out.println("Good");
        break;
        case 5:
        System.out.println("Work Hard");
        break;
        case 4:
        System.out.println("Poor");
        break;
        case 3:
        //take markscase 2:
        case 1:
        case 0:
        System.out.println("Very Poor");
        break;
        default:
        System.out.println("Invalid value Entered");
        }
        }
        }
    
    

    Output of this Java example would be

    /*marks=89
    Excellent*/
    

    19 - Write a program to find SUM AND PRODUCT of a given Digit.

    class Sum&#95;Product&#95;ofDigit{
    public static void main(String args[]){
    int num = Integer.parseInt(args[0]);
    //taking value as command line argument.
    int temp = num,result=0;
    //Logic for sum of digit
    while(temp>0){
    result = result + temp;
    temp--;
    }
    System.out.println("Sum of Digit for "+num+" is : "+result);
    //Logic for product of digit
    temp = num;
    result = 1;
    while(temp > 0){
    result = result * temp;
    temp--;
    }
    System.out.println("Product of Digit for "+num+" is : "+result);
    }
    }
    

    Output of this Java example would be

    /*
    num=345
    
    Sum of Digit for 345 is : 12
    Product of Digit for 345 is : 60
    */
    
    

    20 - Write a program to find sum of all integers greater than 100 and less than 200 that are divisible by 7.

        class SumOfDigit{
        public static void main(String args[]){
        int result=0;
        for(int i=100;i<=200;i++){
        if(i%7==0)
        result+=i;
        }
        System.out.println("Output of Program is : "+result);
        }
        }
    
    

    Output of this Java example would be

    Output of Program is : 2107   
    

    In the next session , we will continue with other important Java coding questions asked by Interviewer.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: