Close
Close

Practice questions on Java Operators

Level 1

1.
Length and breadth of a rectangle are 5 and 7 respectively. Write a program to calculate the area and perimeter of the rectangle.
class Ans{
  public static void main(String[] args){
    int x = 5
    int y = 7
    System.out.println("Area is "+(5*7)+"\nPerimeter is "+(2*(5+7)));
  }
}

2.
Write a program to calculate the perimeter of a triangle having sides of length 2,3 and 5 units.

3.
Write a program to add 8 to the number 2345 and then divide it by 3. Now, the modulus of the quotient is taken with 5 and then multiply the resultant value by 5. Display the final result.
class Ans{
  public static void main(String[] args){
    System.out.println((((8+2345)/3)%5)*5);
  }
}

4.
Now, solve the above question using assignment operators (eg. +=, -=, *=).

5.
Write a program to check if the two numbers 23 and 45 are equal.
class Ans{
  public static void main(String[] args){
    System.out.println(23 == 45);
  }
}

6.
Write a program to print the power of 7 raised to 5.
import java.lang.Math;
class Ans{
  public static void main(String[] args){
    System.out.println(Math.pow(7,5));
  }
}

7.
Assign values of variables 'a' and 'b' as 55 and 70 respectively and then check if both the conditions 'a < 50' and 'a < b' are true.
class Ans{
  public static void main(String[] args){
    int a = 55;
    int b = 70;
    System.out.println(a < 50 && a < b);
  }
}

8.
Now solve the above question to check if atleast one of the conditions 'a < 50' or 'a < b' is true.
class Ans{
  public static void main(String[] args){
    int a = 55;
    int b = 70;
    System.out.println(a < 50 || a < b);
  }
}

9.
If the marks of Robert in three subjects are 78,45 and 62 respectively (each out of 100 ), write a program to calculate his total marks and percentage marks.

10.
Suppose the values of variables 'a' and 'b' are 6 and 8 respecrtively, write two programs to swap the values of the two variables.
1 - first program by using a third variable
2 - second program without using any third variable
( Swapping means interchanging the values of the two variables E.g.- If entered value of x is 5 and y is 10 then after swapping the value of x and y should become 10 and 5 respectively.)
class Ans{
  public static void main(String[] args){
    //using third variable
    //a = c
    //a = b
    //b = c
    //without using third variable
    //b = b-a
    //a = b+a
    //b = -(b-a)
  }
}

11.
Write a program to convert Fahrenheit into Celsius.

12.
The total number of students in a class are 90 out of which 45 are boys. If 50% of the total students secured grade 'A' out of which 20 are boys, then write a program to calculate the total number of girls getting grade 'A'.

13.
Write a program to calculate the sum of the first and the second last digit of a 5 digit.
E.g.- NUMBER : 12345        OUTPUT : 1+4=5
class Ans{
  public static void main(String[] args){
    int n, first, second, third, forth, fifth, sum;
    n = 23462;
    /*Now we will take out each digit of this number and then finally add the first and the second last digits*/
    first = n/10000;     //first digit
    n = n%10000;
	
    second = n/1000;     //second digit
    n = n%1000;
	
    third = n/100;       //third digit
    n = n%100;
	
    forth = n/10;        //forth digit
    fifth = n%10;        //fifth digit
	
    sum = first + forth;
    System.out.println("sum : "+sum);
  }
}

14.
Take a 4 digit number. Write a program to display a number whose digits are 2 greater than the corresponding digits of the number TAKEN.
For example, if the number which was taken is 5696, then the displayed number should be 7818.

15.
Write a program to calculate the sum of the digits of a 3-digit number.
Number : 132        Output : 6

16.
Write a program to reverse a 3-digit number. E.g.-Number : 132        Output : 231
class Ans{
  public static void main(String[] args){
    int n, first, second, third, reverse;
    /*finding the digits of the entered number n*/
    n = 123;
    first = n/100;     //first digit
    n = n%100;
	
    second = n/10;     //second digit
    third = n%10;      //third digit
	
    /*reverse number */
    reverse = third*100 + second*10 + first;
    System.out.println("reverse number : "+reverse);
  }
}

Level 2

Level 3

Ask Yours
Post Yours