Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

another 3 questions

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all in advance.
I encounter another several problems in mock exam of go4java. Please help me.
1. public class Q4
2. {
3. public static void main(String[] args)
4. {
5. double d = Integer.MAX_VALUE;
6. float f= Integer.MAX_VALUE;
7. if (f == d)
8. System.out.println("equal");
9. else
10. System.out.println(" not equal ");
11. }
12. }
The answer is "Program compiles correctly and print "not equal" when executed." and the explanation is "It will print "not equal" due to their precision(accuracy) difference between float and double value.". Why precision does matter here, since they are in fact the same one?
2. Part of some code is as follows:
static int call(int x)
4. {
5. try
6. {
7. System.out.println(x---x/0);
8. return x--;
9. }
10. catch(Exception e)
11. {
12. System.out.println(--x-x%0);
13. return x--;
14. }
I don't understand what is "x---x/0" and what is "--x-x%0".
3. public class Q39
2. {
3. public static void main(String args[])
4. {
5. int x = 10;
6. final int y;
7. if(x++ > 10)
8. y = 1;
9. else
10. y = 2;
11. System.out.println("y is " + y);
12. }
13. }
The output is "y is 2" is printed. I am confused. I think y should be initialilzed where it is and should not be changed afterwards since it is final. Am I right?
Thanks alot.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the answer to the third question. Final variables must be initialised when they are created as member variables or else they are initialised in every constructor. This doeant hold true when they are created in a method. Thats what i think after i compiled and saw. Correct me if i am wrong.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For question 3, Since the if condition is false, Y is assinged value 2. Remember (x++ >10 ) , here it is a post increment operator,so condition is false.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by wei luo:
Thanks to all in advance.
I encounter another several problems in mock exam of go4java. Please help me.
1. public class Q4
2. {
3. public static void main(String[] args)
4. {
5. double d = Integer.MAX_VALUE;
6. float f= Integer.MAX_VALUE;
7. if (f == d)
8. System.out.println("equal");
9. else
10. System.out.println(" not equal ");
11. }
12. }
The answer is "Program compiles correctly and print "not equal" when executed." and the explanation is "It will print "not equal" due to their precision(accuracy) difference between float and double value.". Why precision does matter here, since they are in fact the same one?


The explanation is absolutely correct. Try printing these values, they are 2.1483647E9 for double and 2.148365E9. They are not equal and hence the result.


2. Part of some code is as follows:
static int call(int x)
4. {
5. try
6. {
7. System.out.println(x---x/0);
8. return x--;
9. }
10. catch(Exception e)
11. {
12. System.out.println(--x-x%0);
13. return x--;
14. }
I don't understand what is "x---x/0" and what is "--x-x%0".


Both will result in ArithmeticException: / by 0;One is an attempt to divide by 0 and the other tries to return the remainder of division by 0.


3. public class Q39
2. {
3. public static void main(String args[])
4. {
5. int x = 10;
6. final int y;
7. if(x++ > 10)
8. y = 1;
9. else
10. y = 2;
11. System.out.println("y is " + y);
12. }
13. }
The output is "y is 2" is printed. I am confused. I think y should be initialilzed where it is and should not be changed afterwards since it is final. Am I right?
Thanks alot.


Where is the value of y changing? U are intializing y based on a condition which gets satisfied in the else part of your code.
Hope that helps
Aakanksha
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Aakansha is right,
u r no where changing the value of final variable y, ur initializing the variable y based on the condition is true or false. It may throw an exception if u initialize it, where u declare it first.
but not in this case.
Hope this helps
And ur first question is ununderstandable b'coz we are no where providing the identifier in the arithmetic operation it must throw an exception at runtime.
If any one knows the exact reason pleaze let us know.
Thanx
Nisheeth.
 
Yup, yup, yup. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic