ADVERTISEMENT
ADVERTISEMENT

Java find output programs (if else) | set 2

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.
Submitted by Nidhi, on January 30, 2021

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");
  }
}

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;

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");
  }
}

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.

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");
  }
}

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.

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");
    }
  }
}

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.

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");
  }
}

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.



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.