In: Computer Science
Find the output for the following statements.
int y = 4;
double z = x/y;
System.out.println(z);
Please up vote ,comment if any query . Thanks for Question .
Note : check attached image for output .
Answer :
1. System.out.println(3%2+1+(3/2)); //output will 3 how ?
how ? : -
first it will solve bracket so 3/2 = 1 both operand integer so 3/2 will be 1 now equation is
System.out.println(3%2+1+1);
in Java operator precedence + come after % in precedence
System.out.println(1+1+1); //3%2=1
System.out.println(3); //output
2. char ch =
69; //69 ascii value of 'E'
System.out.println(ch); //it will print E on console
3. int x =
30;
int y = 4;
double z = x/y; z=30/4
by mathematic the result should be 7.5 but output will be 7.
why both operand x and y are integer type so result will also integer type . to get double
we should
type cast the result z= (double)x/y = 7.5
System.out.println(z);
//it will print 7 without type casting
Output :
Please up vote ,comment if any query .