• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

exam cram question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, on pg 51, of Exam Cram for Java 2 book, on Question 1,
What will be the result of calling the following method with an input of 2?
public in adder( int N ) {
return 0.100 + N++ ;
}

choices:
a. The method will return 258.
b. The method will return 102.
c. The method will return 259.
d. The method will return 103.

On the book, the anwser for this question is a, which is 258.
I thought the N will be post incremented first because ++ has a higher precedence than +, this will make the answer be c, which is 259. Can someone explain to me?
Thanks.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
First of all the code is wrong beacause the method is
public in adder( int N ) (I assume that after public is an int)
returning an integer when it's return is giving double.
If we change into double the output will be 2.1
Hi, on pg 51, of Exam Cram for Java 2 book, on Question 1,
What will be the result of calling the following method with an input of 2?
public in adder( int N ) {
return 0.100 + N++ ;
}

choices:
a. The method will return 258.
b. The method will return 102.
c. The method will return 259.
d. The method will return 103.

On the book, the anwser for this question is a, which is 258.
I thought the N will be post incremented first because ++ has a higher precedence than +, this will make the answer be c, which is 259. Can someone explain to me?
Thanks.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you've made an error. In my copy of Exam Cram pg51 question 1 reads:
What will be the result of calling the following method with an input of 2?
1. public int adder(int N){
2. return 0x100 + N++;
3. }
a. The method will return 258.
b. The method will return 102.
c. The method will return 259.
d. The method will return 103.
I don't think you indicated that the value returned was in hexadecimal format.
 
Gene Woo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sean for correcting me.

Here is the correct version:
What will the method return?

1. public int adder(int N){
2. return 0x100 + N++;
3. }
a. The method will return 258.
b. The method will return 102.
c. The method will return 259.
d. The method will return 103.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So Hex 100 is 256. which is added to N which is 2. 258 will be returned. Now value of N is 3.
Om Sonie
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic