Why these expressions are giving me output instead of compile error?

user4834352

I was giving a exam when i got this question What is the Output of following question

 public class ClassicQuestion {
        public static void main(String[] args) 
        {
            int i = 25;
            double j = 95.0;
            System.out.println(j+=i);
            System.out.println(i+=j);

        }

}

And the option for answer was

  1. compile time error
  2. run time error
  3. 120.0 145
  4. 120.0 120

I don't know the correct answer but i thought correct answer would be one, but showed me incorrect answer? how can we assign one variable to different types of data types without conversion ?

Ankur Anand

Correct Answer 3.

Because according to

jls

Compound Assignment operators of the form E1 op= E2 evalute to E1 = (T) ((E1) op (E2)) where T is the type of E1, except that E1 is evaluated only once

so your j+=i changes to j=(double)(j+i) and since now j value is 120.0 next time i does i+=j it changes to i=(int)(i+j) hence 145

so OUTPUT is 120.0 145

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Checking for repeated characters not giving me the desired output

분류에서Dev

Why is it giving me an error when i'm calling it from inside the 'student class?

분류에서Dev

why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

분류에서Dev

Why is my Makefile giving me problems?

분류에서Dev

Heapify is giving me an incorrect output for a min-heap

분류에서Dev

Swift bitwise shift operations giving compile-time error

분류에서Dev

Don't understand why my code giving me an IndexError

분류에서Dev

Woocommerce add_to_cart giving me fatal error

분류에서Dev

Cannot figure out why this curly brace is giving out an error

분류에서Dev

Why isn't IPython giving me a full traceback for a module I've written?

분류에서Dev

Why does this provide me with a stack overflow error?

분류에서Dev

Why is this code giving NoSuchElementException?

분류에서Dev

Generic collection compile error without explicit cast - why?

분류에서Dev

Why does devise me redirect me to /users instead of /users/edit when my account update fails?

분류에서Dev

SqlCeCommand keeps giving me an exception

분류에서Dev

Nginx Autoindex giving me a 403

분류에서Dev

Recursive BubbleSort giving wrong output

분류에서Dev

Handler in android giving error

분류에서Dev

Android - openConnection giving error

분류에서Dev

giving error on getElementById

분류에서Dev

Why does tilde V output ~V [LogLevel ERROR]?

분류에서Dev

PyTorch is giving me a different value for a scalar

분류에서Dev

NSDateFormatter giving incorrect output with am/ pm

분류에서Dev

Hive's RegexSerDe not giving the correct output

분류에서Dev

Parallel detectCores() giving incorrect output on a Virtual machine

분류에서Dev

java double number division giving strange output

분류에서Dev

Deferring Javascript parsing giving error

분류에서Dev

eval is giving error in shell script

분류에서Dev

AngularJS code is giving back error?

Related 관련 기사

  1. 1

    Checking for repeated characters not giving me the desired output

  2. 2

    Why is it giving me an error when i'm calling it from inside the 'student class?

  3. 3

    why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error?

  4. 4

    Why is my Makefile giving me problems?

  5. 5

    Heapify is giving me an incorrect output for a min-heap

  6. 6

    Swift bitwise shift operations giving compile-time error

  7. 7

    Don't understand why my code giving me an IndexError

  8. 8

    Woocommerce add_to_cart giving me fatal error

  9. 9

    Cannot figure out why this curly brace is giving out an error

  10. 10

    Why isn't IPython giving me a full traceback for a module I've written?

  11. 11

    Why does this provide me with a stack overflow error?

  12. 12

    Why is this code giving NoSuchElementException?

  13. 13

    Generic collection compile error without explicit cast - why?

  14. 14

    Why does devise me redirect me to /users instead of /users/edit when my account update fails?

  15. 15

    SqlCeCommand keeps giving me an exception

  16. 16

    Nginx Autoindex giving me a 403

  17. 17

    Recursive BubbleSort giving wrong output

  18. 18

    Handler in android giving error

  19. 19

    Android - openConnection giving error

  20. 20

    giving error on getElementById

  21. 21

    Why does tilde V output ~V [LogLevel ERROR]?

  22. 22

    PyTorch is giving me a different value for a scalar

  23. 23

    NSDateFormatter giving incorrect output with am/ pm

  24. 24

    Hive's RegexSerDe not giving the correct output

  25. 25

    Parallel detectCores() giving incorrect output on a Virtual machine

  26. 26

    java double number division giving strange output

  27. 27

    Deferring Javascript parsing giving error

  28. 28

    eval is giving error in shell script

  29. 29

    AngularJS code is giving back error?

뜨겁다태그

보관