Question:

Multiple Choice Questions again, thanks

by  |  earlier

0 LIKES UnLike

4) What should be the most appropriate for statement to sum up all the values from 1 to 100? Assume the statement repeated is sum = sum index;

a) for (index = 0; index < 100; index )

b) for (index = 1; index < 100; index )

c) for (index = 0; index <= 100; index )

d) for (index = 1; index < 100; index )

My answer is a). The question asks for the most appropriate for statement... That means the other choices are acceptable? Which of them are also acceptable?

 Tags:

   Report

1 ANSWERS


  1. Y!A is a bit f-ed up at the moment...

    4) Choices b) and d) are identical. Given the inner statement we would need to count from 1 to 100.

    a) Counts from 0 to 99

    b) Counts from 1 to 99

    c) Counts from 0 to 100

    d) See b)

    The only option to count from 1 to 100 would be

    for (index = 1; index &lt;= 100; ++index)

    Another option would be to change the statement to

    sum = sum + (index + 1);

    Then we could use option a) instead.

    3) % is an integer operation and will never give a fraction. Also the operator precedence is wrong, as multiplicative operations are evaluated from left to right.

    8 * (3 - 1) % 4 &gt; 2 -&gt; First everything in the paranthesis

    8 * 2 % 4 &gt; 2 -&gt; The multiplicative operations from left to right

    16 % 4 &gt; 2

    0 &gt; 2 -&gt; Finally the comparison.

    false

    See http://www.uni-bonn.de/~manfear/javaoper... for

    Operators, their precedence and their associativity (the order in which we evaluate operators at the same precedence level).

    last one)

    Looking at the signature of the display function we can see that it takes one argument of type string and does not return any value at all.

    a) Same as b). Also we don&#039;t pass any argument. DOUBLEFAIL.

    b) We expect display to return a value, which it does not. FAIL

    d) We don&#039;t expect a return value and do pass an argument, but the argument we pass is of type char. FAIL.

    Leaving only

    c) - We pass the argument message which is of type String. OKAY.

    - We do not expect a return value. OKAY.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.