Sharpen Your Skills: Programming Languages Practice Q&A

Programming practice questions

Java Programming Language

 
1

Question- Write a program to find sum of two integer number which is enter by user?



Answer-
 //File name must save with Program1.java
 package csdt;
 import java.util.Scanner;
 class Program1
  {
     public static void main(String []args)
    {
    Scanner sc=new Scanner(System.in);
      System.out.println("Enter 1st Number");
        int n1=sc.nextInt();
       System.out.println("Enter 2nd Number");
       int n2=sc.nextInt();
       int sum=n1+n2;
       System.out.println("Addition is "+sum);
     }
  }
2

Question- Write a program to enter cost of pen from user and find cost of 50 pen cost with 10% discount?



Answer-

   package csdt;

   import java.util.Scanner;

   class Program2

        {

           public static void main(String []args)

            {

                Scanner sc=new Scanner(System.in);

                System.out.println("Enter Cost of a Pen");

                int cost=sc.nextInt();

                int totalcost=cost*50;

                int dis=totalcost*10/100;

                int pay_amt=totalcost-dis;

                System.out.println("Total Cost is"+totalcost);

                System.out.println("Discount is "+dis);

                System.out.println("You pay only"+pay_amt);

            }

       }

3

Question- Question- Write a program to find the area of circle?



Answer-
 import java.io.*;
  import java.util.Scanner;
  public class TestClass {
  public static void main(String[] args)
      { 
        Scanner input=new Scanner(System.in);
        double x=input.nextDouble();
        double ar= Math.PI*x*x;
        System.out.printf("%.2f",ar);
        }
    }
4

Question- Write a program to find area of rectangle in JAVA?



Answer-
 import java.io.*;
 import java.util.Scanner;
 public class TestClass {
public static void main(String[] args) {
     Scanner sc=new Scanner(System.in);
     int length=sc.nextInt();
     int breadth=sc.nextInt();
     int area;
     area=length*breadth;
     System.out.println("Area="+area);
     }
   }

5

Question- Write a program to find Area of Rhombus in JAVA?



Answer-
  import java.io.*;
  import java.util.Scanner;
  public class TestClass {
public static void main(String[] args) { 
     Scanner sc=new Scanner(System.in);
     int a=sc.nextInt();
     int b=sc.nextInt();
     int c=(a*b)/2;
     System.out.println("Area of Rhombus is "+c);
}
   }
6

Question- Write a program to find area of the room in JAVA?



Answer-
  import java.io.*;
  import java.util.Scanner;
  public class TestClass {
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in);
       float l=sc.nextFloat();
       float b=sc.nextFloat();
       System.out.print("The area of the room is "+String.format("%.2f",(l*b))+" square feet");
}
     }
7

Question- Write a program to find area of the field in JAVA?



Answer-
  import java.io.*;
  import java.util.Scanner;
  public class TestClass {
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in);
       double area;
       double l=sc.nextDouble();
       double w=sc.nextDouble();
        area=(l*w);
       area=area/43560;
       System.out.printf("The area of the field is "+String.format("%.2f",area)+" acres");
}
      }
8

Question- Write a program to find area of the Triangle in JAVA?



Answer-
   import java.io.*;
   import java.util.Scanner;
   public class TestClass {
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in);
       float l=sc.nextFloat();
       float b=sc.nextFloat();
       System.out.print("Area of Triangle is "+(0.5*l*b));
}
   }
9

Question- Write a program to Find ceil of a/b without using ceil() function in JAVA?



Answer-
Find ceil of a/b without using ceil() function

Input : a = 5, b = 4 
Output : 2 
Explanation: a/b = ceil(5/4) = 2 

Input : a = 10, b = 2
Output : 5 
Explanation: a/b = ceil(10/2) = 5 

 Formula for finding ceil value without using function:

ceilVal = (a+b-1) / b

 Using simple maths, we can add the denominator to the numerator and subtract 1 from it and then divide it  by denominator to get the ceiling value.

  import java.io.*;
  import java.util.Scanner;
  public class TestClass {
public static void main(String[] args) { 
     Scanner sc=new Scanner(System.in);
     int a=sc.nextInt();
     int b=sc.nextInt();
     int val = (a + b - 1) / b; 
    System.out.println("The ceiling value of a/b is "
                        + val);
     }
    }
10

Question- Program for display camel case in java.



Answer-
Java uses CamelCase as a practice for writing names of methods, variables, classes, packages and constants. Camel case in Java Programming : It consists of compound words or phrases such that each word or abbreviation begins with a capital letter or first word with a lowercase letter, rest all with capital.

    import java.io.*;
    import java.util.*;
    public class TestClass 
         {
           public static void main(String[] args) 
                          {
                 Scanner sc = new Scanner(System.in);
                     String text = sc.nextLine();
        String[] words = text.split("[A-Z]");
        System.out.println(words.length);
        sc.close();
       }
         }

11

Question- Write a program to convert centigrade to fahrenheit.



Answer-
     import java.io.*;
     import java.util.Scanner;
     public class TestClass {
public static void main(String[] args) { 
       
                             float cen;
                            double fah;
                            Scanner scan = new Scanner(System.in);
                           cen = scan.nextFloat();
                           fah = (1.8*cen) + 32;
                           System.out.print( + fah);
                      }
                       }
12

Question- Write a program to concat two string.



Answer-
     import java.io.*;
     import java.util.*;
      public class TestClass {
      public static void main(String[] args) { 
Scanner sc = new Scanner(System.in);
       String a = sc.nextLine();
       String b = sc.nextLine();
       String c = a+b;
       System.out.println(c);
}
        }
13

Question- Write a program to convert integer number to float.



Answer-
           import java.io.*;
           import java.util.Scanner;
           public class TestClass {
public static void main(String[] args) { 
Scanner in = new Scanner(System.in);
       int a=in.nextInt();
       float b=a;
       System.out.println(b);
}
        }
14

Question- Write a program to find volume of cylinder.



Answer-
      import java.io.*;
      import java.util.Scanner;
      public class TestClass {
public static void main(String[] args) { 
       Scanner sc=new Scanner(System.in);
       double PI=3.14;
       float radius=sc.nextFloat();
       float length=sc.nextFloat();
       double area=radius*radius*PI;
       double volume=area*length;
       System.out.println(String.format("%.02f",area));
       System.out.println(String.format("%.02f",volume));
}
           }
15

Question- Write a program to find number of year and days from given minutes in Java.



Answer-
    import java.io.*;
     import java.util.Scanner;
     public class TestClass {
public static void main(String[] args) { 
        Scanner input = new Scanner(System.in);
        int minutes = input.nextInt();
    int year = minutes / 525600;
    int day = minutes / 21600;
       int remainingMinutes = day % 21600;
       
    System.out.println( year);
System.out.println(remainingMinutes);
}
    }
16

Question- Write a program for finding year and hours from given minutes in Java.



Answer-
       import java.io.*;  
      import java.util.Scanner;
      public class TestClass {
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in);
       long min=sc.nextLong();
       long yr=min/(365*24*60);
       long hours=(min%(365*24*60))/(24*60);
       System.out.print(yr+"\n"+hours);
       
}
          }
17

Question- Write a program for swaping two integer number.



Answer-     package csdtPatna;
   import java.util.Scanner;
   public class SwapNumber
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 1st number");
  int n1=sc.nextInt();
  System.out.println("Enter 2nd number");
  int n2=sc.nextInt();
  System.out.println("Befour swaping values are n1="+n1+" and n2 is "+n2);
  int temp=n1;
  n1=n2;
  n2=temp;
  System.out.println("After swaping values are n1="+n1+" and n2 is "+n2);

  }

  }
18

Question- Write a program for swaping two integer number without using third variable.



Answer-
   package csdtPatna;
   import java.util.Scanner;
   public class SwapNumber
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 1st number");
  int n1=sc.nextInt();
  System.out.println("Enter 2nd number");
  int n2=sc.nextInt();
  System.out.println("Befour swaping values are n1="+n1+" and n2 is "+n2);
  
  n1=n1+n2;
  n2=n1-n2;
  n1=n1-n2;
  System.out.println("After swaping values are n1="+n1+" and n2 is "+n2);

  }

  }
19

Question- Write a program for finding percentage of 3 enter subject marks.



Answer-
package csdtPatna;
   import java.util.Scanner;
   public class SubjectMarks
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 1st Subject marks math is:");
  int n1=sc.nextInt();
  System.out.println("Enter 2nd Subject marks phy is:");
  int n2=sc.nextInt();
  System.out.println("Enter 3rd Subject marks english is:");
  int n3=sc.nextInt();
  int TotalMarks=n1+n2+n3;
  int per=TotalMarks/3;

  System.out.println("Total Subject Marks is: "+TotalMarks+" and Percentage  is "+per);

  }

  }
20

Question- Write a program for finding reverse of 3 digits number without using loop.



Answer-
package csdtPatna;
   import java.util.Scanner;
   public class FindReverse
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 3 digits number:");
  int n1=sc.nextInt();
  int t=n1;
  int rev=0;
  rev=rev*10+n1%10;
  n1=n1/10;  //Ater this process number become 2 digits
  rev=rev*10+n1%10;
  n1=n1/10; //Ater this process number become 1 digits
  rev=rev*10+n1;
  System.out.println("Given number is: "+t+" and Reverse  is "+rev);

  }

  }

21

Question- Write a program for finding sum of 3 digits number without using loop.



Answer-
package csdtPatna;
   import java.util.Scanner;
   public class FindSumOfDigit
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 3 digits number:");
  int n1=sc.nextInt();
  int t=n1;
  int s=0;
  s=s+n1%10;
  n1=n1/10;  //Ater this process number become 2 digits
  s=s+n1%10;
  n1=n1/10; //Ater this process number become 1 digits
  s=s+n1;
  System.out.println("Given number is: "+t+" and Sum of 3 digits number  is "+s);

  }

  }
22

Question- Write a program for finding Multiplication of 3 digits number without using loop.



Answer-
package csdtPatna;
   import java.util.Scanner;
   public class FindSumOfDigit
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter 3 digits number:");
  int n1=sc.nextInt();
  int t=n1;
  int m=1;
  m=m*n1%10;
  n1=n1/10;  //Ater this process number become 2 digits
  m=m*n1%10;
  n1=n1/10; //Ater this process number become 1 digits
  m=m*n1;
  System.out.println("Given number is: "+t+" and multilication of 3 digits number  is "+m);

  }

  }
23

Question- Write a program which accept temperature in Farenheit and print it in centigrade.



Answer-
package csdtPatna;
   import java.util.Scanner;
   public class Temperature
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter temperature in Farenheit : ");
  int F=sc.nextInt();
  int C=5*(F-32)/9;
  
  System.out.println("Temperature in celcius is : "+C);

  }

  }
24

Question- Write a program which accept principle, rate and time from user and print the simple interest.



Answer-
package csdtPatna;
   import java.util.Scanner;
   public class SimpleInterest
  {
 
   public static void main(String []args)
  {
  //Creating object of Scanner class for taking input from user

  Scanner sc=new Scanner(System.in);
  System.out.println("Enter Principle :  ");
  int P=sc.nextInt();
  System.out.println("Enter Rate :  ");
  int R=sc.nextInt();
  System.out.println("Enter Time :  ");
  int T=sc.nextInt();
  int si=(P*R*T)/100;
  
  System.out.println("Simple Interest is : "+si);

  }

  }