Q:

Java find output programs (if else) | set 2

0

Find the output of Java programs | if else | Set 2: Enhance the knowledge of Java if else concepts by solving and finding the output of some Java programs.

Question 1:

public class Main {
  public static void main(String[] args) {
    int SUB1 = 60;
    int SUB2 = 68;
    int SUB3 = 97;
    int SUB4 = 48;
    int SUB5 = 57;

    float PER = 0.0;

    PER = (SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;

    if (PER > 60) 
        System.out.println("First Division");
    else if (PER > 45) 
        System.out.println("Second Division");
    else if (PER > 35) 
        System.out.println("Third Division");
    else 
        System.out.println("Fail");
  }
}

Question 2:

public class Main {
  public static void main(String[] args) {
    int SUB1 = 60;
    int SUB2 = 68;
    int SUB3 = 97;
    int SUB4 = 48;
    int SUB5 = 57;

    float PER = 0.0F;

    PER = (SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;

    if (PER > 60) 
        System.out.println("First Division");
    else if (PER > 45) 
        System.out.println("Second Division");
    else if (PER > 35) 
        System.out.println("Third Division");
    else 
        System.out.println("Fail");
  }
}

Question 3:

public class Main {
  public static void main(String[] args) {
    int SUB1 = 60;
    int SUB2 = 60;
    int SUB3 = 77;
    int SUB4 = 48;
    int SUB5 = 47;

    float PER = 0.0F;

    PER = (SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;

    if (PER > 60) 
        System.out.println("First Division");
    if (PER > 45) 
        System.out.println("Second Division");
    if (PER > 35) 
        System.out.println("Third Division");
    else 
        System.out.println("Fail");
  }
}

Question 4:

public class Main {
  public static void main(String[] args) {
    int A = 10;
    int B = 20;
    int C = 30;

    if (B > A) {
      if (A > C) {
        System.out.println("ABC");
      }
      else {
        if (C > B) {
          System.out.println("GHI");
        }
        else {
          System.out.println("LMN");
        }
      }
    }
    else {
      System.out.println("PQR");
    }
  }
}

Question 5:

public class Main {
  public static void main(String[] args) {
    int X = 10;
    int Y = 20;

    if (X > Y ? true: false) 
        System.out.println("www.includehelp.com");
    else 
        System.out.println("www.google.com");
  }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Question 1:

Output:

Main.java:9: error: incompatible types: possible lossy conversion from double to float
    float PER = 0.0;
                ^
1 error

Explanation:

In the above program, we created a class Main that contains a main() method, the main() method is the entry point of the program, here we initialized variable PER with 0.0, which is a double value, that's why syntax error will be generated. We need to use suffix 'F' to initialize a float number. The correct way is given below:

float PER = 0.0F;

Answer Question 2:

Output:

First Division

Explanation:

In the above program, we created a class Main that contains a main() method, the main() method is the entry point of the program, here we created 5 integer variables for subjects and variable PER of float type to calculate the percentage.

Now look to the expression:

PER =(SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;
PER = (60+68+97+48+57)/5;
PER = (330)/5;
PER = 66;

Then "First Division" will be printed on the console screen.

Answer Question 3:

Output:

Second Division
Third Division
Press any key to continue . . .

Explanation:

In the above program, we created a class Main that contains a main() method, the main() method is the entry point of the program, here we created 5 integer variables for subjects and variable PER of float type to calculate the percentage.

Now look to the expression:

PER =(SUB1 + SUB2 + SUB3 + SUB4 + SUB5) / 5;
PER = (60+60+77+48+47)/5;
PER = (60+60+77+48+47)/5;
PER = (292)/5;
PER = 58;

Then conditions if (PER > 45) and if (PER > 45) will true then message "Second Division" and "Third Division" will print on the console screen.

Answer Question 4:

Output:

GHI

Explanation:

In the above program, we created a class Main that contains a main() method, the main() method is the entry point of the program, here we declared three variables A, B, and C initialized with 10, 20, and 30 respectively.

Let's look at the conditions:

The condition if (B > A) that is if(20>10) condition will true, and then condition if (A > C) that is if(10>30), will false and then else part will execute and condition if (C > B) will true and print "GHI" on the console screen.

Answer Question 5:

Output:

www.google.com

Explanation:

In the above program, we created a class Main that contains a main() method, the main() method is the entry point of the program, here we declared two integer variables X and Y that are initialized with 10, 20 respectively.

if (X > Y ? true : false)

In the above condition, we used a conditional operator inside the if condition. The conditional operator will return false. That's why the "else" part will execute and print "www.google.com" on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now