Q:

Java find output programs (if else) | set 1

0

Find the output of Java programs | if else | Set 1: 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 num = 101;

    if (num = 101) {
      System.out.println("Hello");
    }
    else {
      System.out.println("Hiiii");
    }
  }
}

Question 2:

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

    C = ++A * --B + C;

    if (A == (++B * 2)) {
      System.out.println("Hello: " + C);
    }
    else {
      System.out.println("Hiii: " + C);
    }
  }
}

Question 3:

public class Main {
  public static void main(String[] args) {
    float A = 2.34F;
    double B = 2.34;

    if (A == B) {
      System.out.println("www.includehelp.com");
    }
    else {
      System.out.println("www.google.com");
    }
  }
}

Question 4:

public class Main {
  public static void main(String[] args) {
    int A = -2;

    if (A > null) {
      System.out.println("www.includehelp.com");
    }
    else {
      System.out.println("www.google.com");
    }
  }
}

Question 5:

public class Main {
  public static void main(String[] args) {
    int NUM = 0;
    String STR = "Hello World";

    NUM = STR.length();

    if (NUM % 2 == 0) {
      System.out.println("NUM is EVEN number");
    }
    else {
      System.out.println("NUM is ODD number");
    }
  }
}

 

All Answers

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

Answer Question 1:

Output:

Main.java:5: error: incompatible types: int cannot be converted to boolean
    if (num = 101) {
            ^
1 error

Explanation:

The above program will generate a syntax error because the if condition in Java required boolean value. In the if condition, we assign variable num with 101. The condition will be:

if(101)

We cannot use integer value for conditions in Java.

Answer Question 2:

Output:

Hiii: 239

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 integer variables A, B, and C initialized with 10, 20, 30.

Now evaluate the expression:

C = ++A * --B + C;

First, evaluate ++A, then A becomes 11, and then evaluate --B, then B becomes 19.

C = A *B + C; 
C = 11*19+30;
C = 209+30
C = 239

Let's check the condition given below,

if (A==(++B*2))
if (A==(20*2))
if (A==400)

A contains 11 then the if condition will false that's why "Hiii: 239" will be printed on the console screen.

Answer Question 3:

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 variables A and B that are initialized with "2.34F" and "2.34" respectively.

Here, A is float type, and B is double type then both numbers will not equal then condition get false and www.google.com will print on the console screen.

Answer Question 4:

Output:

Main.java:5: error: bad operand types for binary operator '>'
    if (A > null) {
          ^
  first type:  int
  second type: <null>
1 error

Explanation:

The above program will generate a syntax error because we cannot compare the integer variable with null.

Answer Question 5:

Output:

NUM is ODD number

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 variables NUM and STR that are initialized with 0 and "Hello World" respectively.

NUM = STR.length();

Here, STR.Length() method will return 11 and assigned to the variable NUM.

if (NUM%2==0)
if (11%2==0)

In the above condition, we will get remainder 1, and then the condition gets false and print "NUM is ODD number" 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