ADVERTISEMENT
ADVERTISEMENT

Java find output programs (if else) | set 3

Find the output of Java programs | if else | Set 3: 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 X = 20;
    int Y = 10;
    int Z = 30;

    if (X > Y && X == 10) System.out.println("ABC");
    else if (Z > Y && Z = 20) System.out.println("PQR");
    else if (Z == 30 && Z == 10) System.out.println("XYZ");
  }
}

Output:

Main.java:8: error: bad operand types for binary operator '&&'
    else if (Z > Y && Z = 20) System.out.println("PQR");
                   ^
  first type:  boolean
  second type: int
1 error

Explanation:

The above program will generate a syntax error.

else if(Z>Y && Z=20)

In the above condition, we used the assignment operator "=" instead of equal to "==" operator. Then it will return an integer value that cannot be used as an operand with the logical end "&&" operator.

Question 2:

public class Main {
  public static void main(String[] args) {
    int A = 20;
    float B = 20.0F;

    if (sizeof(A) == sizeof(B)) 
        System.out.println("Hello");
    else 
        System.out.println("Hiii");
  }
}

Output:

Main.java:6: error: cannot find symbol
    if (sizeof(A) == sizeof(B)) 
        ^
  symbol:   method sizeof(int)
  location: class Main
Main.java:6: error: cannot find symbol
    if (sizeof(A) == sizeof(B)) 
                     ^
  symbol:   method sizeof(float)
  location: class Main
2 errors

Explanation:

The above program will generate syntax errors because the sizeof() operator does not exist in java.

Question 3:

public class Main {
  public static void main(String[] args) {
    double X = 20.23;
    float Y = 20.23F;

    if (X > Y) System.out.println("India");
    else System.out.println("Australia");
  }
}

Output:

India

Explanation:

The above program will print "India" on the console screen. Because, X is the double type and Y is float type. In Java, a double number is always treated greater in case of equality. That's why the if condition will true and print "Hello" on the console screen.

Question 4:

public class Main {
  public static void main(String[] args) {
    String STR1 = "INDIA";
    String STR2 = "india";

    if (STR1 > STR2) System.out.println("WWW.INCLUDEHELP.COM");
    else System.out.println("www.includehelp.com");
  }
}

Output:

Main.java:6: error: bad operand types for binary operator '>'
    if (STR1 > STR2) System.out.println("WWW.INCLUDEHELP.COM");
             ^
  first type:  String
  second type: String
1 error

Explanation:

The above program will generate syntax error because we cannot use greater than the operator with string operands.

Question 5:

public class Main {
  public static void main(String[] args) {
    String ABC = "INDIA";
    String XYZ = "india";

    if (ABC == XYZ) System.out.println("WWW.INCLUDEHELP.COM");
    else System.out.println("www.includehelp.com");
  }
}

Output:

www.includehelp.com

Explanation:

The above program will print "www.includehelp.com" on the console screen. Because, ABC and XYZ do not contain the same values. That's why condition will false and else part will execute then it will print "www.includehelp.com" on the console screen.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.