• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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...
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic