Find the output of Java programs | Operators | Set 1: Enhance the knowledge of Java Operators concepts by solving and finding the output of some Java programs.
Question 1:
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 0;
c = ++a + ++b * 2 + a++;
System.out.println(a + "," + b + "," + c);
}
}
Question 2:
public class Main {
public static void main(String[] args) {
double Z = 0.0;
float X = 3.5F;
Z = (X * 10 - 23) * Math.pow(2, 0);
System.out.println(Z);
}
}
Question 3:
public class Main {
public static void main(String[] args) {
int Z = 0;
Z += (Float.SIZE / 8) + (Math.pow(2, 3) % 3);
System.out.println(Z);
}
}
Question 4:
public class Main {
public static void main(String[] args) {
int Z = 10;
Z += (Char.SIZE / 8) + (Math.pow(2, 3) % 3);
System.out.println(Z);
}
}
Question 5:
public class Main {
public static void main(String[] args) {
const float A = 4.23F;
float B = 3.23F;
float Z = 2;
Z *= ++B + A % B;
System.out.println(Z);
}
}
Answer Question 1:
Output:
Explanation:
In the above program, we created three integer variables a, b, and c initialized with 10, 20, and 0 respectively. Here, we used pre and post-increment operators with variable 'a' and 'b'. The pre-increment operator will evaluate before the expression and post-increment operator will evaluate after the expression.
The value of variable 'a' will be 11 and the value of 'b' will be 21 because of pre-increment operator. Let's evaluate the expression,
c = ++a + ++b * 2+a++;
Now remove all pre and post-increment operators from the expression,
c = a +b * 2+a; c = 11+21*2+11; c = 11+42+11’ c = 64;
Then post increment operator will evaluate for variable a then the final value of a will be 22.
Answer Question 2:
Output:
Explanation:
In the above program, we created a class Main that contains the main() method. The main() method is the entry point of the program. Here, we created two local variables Z and X initialized with 0.0 and 3.5F respectively.
Now evaluate the expression:
Z = (X*10-23)*Math.pow(2,0); Z = (3.5*10-23)*1; Z = (35.0-23)*1; Z = (12.0)*1; Z = 12.0
Answer Question 3:
Output:
Explanation:
In the above program, we created a class Main that contains the main() method. The main() method is the entry point of the program. Here, we created a local variables Z initialized with 0.
Now evaluate the expression:
Z += (Float.SIZE/8)+ (Math.pow(2,3)%3); Z += (32/8)+(8%3); Z += (4)+(2); Z += 6; Z = Z + 6; Z = 0 + 6; Z = 6;
Answer Question 4:
Output:
Explanation:
The above program will generate syntax error because Char is not a built-in class in Java.
Answer Question 5:
Output:
Explanation:
The above program will generate syntax error because of const, to define a constant in java we need to use final keyword instead of const.
need an explanation for this answer? contact us directly to get an explanation for this answer