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
    • 334
    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.*/

    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStreamReader;
    4. public class CalculateCircleAreaExample {
    5. public static void main(String[] args) {
    6. int radius = 0;
    7. System.out.println("Please enter radius of a circle");
    8. try
    9. {
    10. //get the radius from console
    11. BufferedReader br = new BufferedReader(new
    12. InputStreamReader(System.in));
    13. radius = Integer.parseInt(br.readLine());
    14. }
    15. //if invalid value was entered
    16. catch(NumberFormatException ne)
    17. {
    18. System.out.println("Invalid radius value" + ne);
    19. System.exit(0);
    20. }
    21. catch(IOException ioe)
    22. {
    23. System.out.println("IO Error :" + ioe);
    24. System.exit(0);
    25. }
    26. /*
    27. * Area of a circle is
    28. * pi * r * r
    29. * where r is a radius of a circle.
    30. */
    31. //NOTE : use Math.PI constant to get value of pi
    32. double area = Math.PI * radius * radius;
    33. }
    34. System.out.println("Area of a circle is " + area);
    35. }

    Output of Calculate Circle Area using Java Example would be

    1. /*
    2. Please enter radius of a circle
    3. 19
    4. Area of a circle is 1134.1149479459152
    5. */

    12 - Factorial of a number using recursion

    /* This program shows how to calculate

    Factorial of a number using recursion function. */

    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStreamReader;
    4. public class JavaFactorialUsingRecursion {
    5. public static void main(String args[]) throws NumberFormatException,
    6. IOException{
    7. System.out.println("Enter the number: ");
    8. //get input from the user
    9. BufferedReader br=new BufferedReader(new
    10. InputStreamReader(System.in));
    11. int a = Integer.parseInt(br.readLine());
    12. //call the recursive function to generate factorial
    13. int result= fact(a);
    14. }
    15. System.out.println("Factorial of the number is: " + result);
    16. static int fact(int b)
    17. {
    18. if(b <= 1)
    19. //if the number is 1 then return 1
    20. return 1;
    21. else
    22. //else call the same function with the value - 1
    23. return b * fact(b-1);
    24. }
    25. }

    Output of this Java example would be

    1. /*
    2. Enter the number:
    3. 5
    4. Factorial of the number is: 120
    5. */

    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. */

    1. import java.io.BufferedReader;
    2. import java.io.InputStreamReader;
    3. public class GeneratePyramidExample {
    4. public static void main (String[] args) throws Exception{
    5. BufferedReader keyboard = new BufferedReader (new
    6. InputStreamReader(System.in));
    7. System.out.println("Enter Number:");
    8. int as= Integer.parseInt (keyboard.readLine());
    9. System.out.println("Enter X:");
    10. int x=
    11. Integer.parseInt (keyboard.readLine());
    12. int y = 0;
    13. for(int i=0; i<= as ;i++){
    14. for(int j=1; j <= i ; j++){
    15. System.out.print(y + "\t");
    16. y = y + x;
    17. }
    18. System.out.println("");
    19. }
    20. }
    21. }

    Output of this Java example would be

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

    14 To Find Maximum of Two Numbers.

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

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

    Output of this Java example would be

    1. /*
    2. i=10 , j=15
    3. 15 is greater than 10
    4. */

    15 To Find Minimum of Two Numbers using conditional operator.

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

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

    Output of this Java example would be

    1. /*
    2. i=10,j=15
    3. Output would be
    4. 10 is a minimum value"
    5. */

    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. */

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

    Output of this Java example would be

    1. Small Integer not greater than the number :35.0
    2. Given Number : 34.32
    3. 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.

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

    Output of this Java example would be

    1. /*
    2. 79
    3. 97
    4. 27
    5. 94
    6. 22 */

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

    1. class SwitchDemo{
    2. public static void main(String args[]){
    3. int marks = Integer.parseInt(args[0]);
    4. as command line argument.
    5. switch(marks/10){
    6. case 10:
    7. case 9:
    8. case 8:
    9. System.out.println("Excellent");
    10. break;
    11. case 7:
    12. System.out.println("Very Good");
    13. break;
    14. case 6:
    15. System.out.println("Good");
    16. break;
    17. case 5:
    18. System.out.println("Work Hard");
    19. break;
    20. case 4:
    21. System.out.println("Poor");
    22. break;
    23. case 3:
    24. //take markscase 2:
    25. case 1:
    26. case 0:
    27. System.out.println("Very Poor");
    28. break;
    29. default:
    30. System.out.println("Invalid value Entered");
    31. }
    32. }
    33. }

    Output of this Java example would be

    1. /*marks=89
    2. Excellent*/

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

    1. class Sum&#95;Product&#95;ofDigit{
    2. public static void main(String args[]){
    3. int num = Integer.parseInt(args[0]);
    4. //taking value as command line argument.
    5. int temp = num,result=0;
    6. //Logic for sum of digit
    7. while(temp>0){
    8. result = result + temp;
    9. temp--;
    10. }
    11. System.out.println("Sum of Digit for "+num+" is : "+result);
    12. //Logic for product of digit
    13. temp = num;
    14. result = 1;
    15. while(temp > 0){
    16. result = result * temp;
    17. temp--;
    18. }
    19. System.out.println("Product of Digit for "+num+" is : "+result);
    20. }
    21. }

    Output of this Java example would be

    1. /*
    2. num=345
    3.  
    4. Sum of Digit for 345 is : 12
    5. Product of Digit for 345 is : 60
    6. */

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

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

    Output of this Java example would be

    1. 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
Reset Password
Fill out the form below and reset your password: