• 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

Certification Question - Need an explanation

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Quick question for you. I am trying to figure out why the answer to one of the test certification questions is what it is.



Which statement contains an error?
a. for (i = 0; i < arguments.length & i < 10; i++) {
b. sum += temps[i];
c. System.out.println(�Average: � + (sum / i));
d. None; the program is correct.

Apparently, the answer is "c.". However, I ran the code and even if I fix it according to the solution at:

http://www.cadenhead.org/book/java-21-days/certification.php/7

I still get a NumberFormatException.

Can anybody explain why?

Thanks,

Christopher
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The single "&" in the line "for (i = 0; i < arguments.length & i < 10; i++) {" looks wrong at first sight. Shouldn't this be "&&"?

Aren't you supposed to get a NumberFormatException, which is caught by the program? The page with the solution says:

For example, if you ran the application like this:

java AverageTest 1 1 1 x

The output would be the following:

Invalid input: x (Happens because 'x' is not a number)
Average value: 0.75


Anyway, the page with the solution also clearly explains why c) is right; the calculation "sum / i" does not give you the average.
 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christopher Frankland:

Which statement contains an error?
a. for (i = 0; i < arguments.length & i < 10; i++) {
b. sum += temps[i];
c. System.out.println(�Average: � + (sum / i));
d. None; the program is correct.

Christopher



even though average may print wrong value. but this code compiles and runs successfully. so, i think from exam point of view, option is d. am i right?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I have copied the following code as it is and compiled it gave compilation errors at line

and


So I have tried the following code and it is weird to see the o/p that both '�' and '"' are different indeed!



I am hoping we will not get such weird questions in actual exam!

Murali...
 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compile time error at this line

System.out.println(``Average:`` + (sum / i));

Because that have used (``) instead of (")
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vikas chess:
even though average may print wrong value. but this code compiles and runs successfully. so, i think from exam point of view, option is d. am i right?


You're right, but it depends on what is exactly meant with "...contains an error". The program compiles and runs successfully, but it doesn't print the output that it is supposed to print. Unfortunately exam questions are not always phrased in a very exact way...

Originally posted by sid sree:
Compile time error at this line

System.out.println(``Average:`` + (sum / i));

Because that have used (``) instead of (")


That would indeed be an error, but I don't think that this is what is meant in the question here...
 
I don't even know how to spell CIA. But this tiny ad does:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic