reset password
Author Message
Anonymous
Posts: 166
Posted 20:16 Dec 09, 2013 |

1. The code snippet below checks whether a given number is a prime number. What will be the result of executing it?

public static void main(String[] args)

{

int j = 2;

int result = 0;

int number = 0;

Scanner reader = new Scanner(System.in);

System.out.println("Please enter a number: ");

number = reader.nextInt();

while (j <= number / 2) // better is while (j * j <= number)

{

if (number % j == 0)

{

result = 1;

}

j++;

}

if (result == 1)

{

System.out.println("Number: " + number + " is Not Prime.");

}

else

{

System.out.println("Number: " + number + " is Prime. ");

}

}

 

The answer to this was The code snippet will display desired result but shouldn't it be The code snippet will display an incorrect result. // incorrect if number is 1 since if the user inputs 1, which isn't a prime number, it will display that it is prime?

 

 

 

How do you fix this code snippet to make it print out the sum when the user enters Q?

 

System.out.print("Enter a value, Q to quit: ");

double sum = 0;

Scanner in = new Scanner(System.in);

boolean hasData = true;

do

{

double value = in.nextDouble();

sum = sum + value;

System.out.print("Enter a value, Q to quit: ");

}

while (in.hasNext());

System.out.println("sum " + sum);

 

How is the answer while(has.Data) and not while(in.hasNextDouble())? I tested both cases too and only the in.hasNextDouble worked.

 

9. Which code snippet produces the sum of the first n positive even numbers?

a)

int sum = 0;

for (int i = 1; i <= n; i++)

{

if (i % 2 == 0)

{

sum = sum + i;

}

}

b)

int sum = 0;

for (int i = 1; i <= n; i++)

{

sum = sum + i * 2;

}

 

Can you explain the difference between these two and how the answer is b and not a?

 

Thank you!

Eric Liao
Posts: 158
Posted 20:35 Dec 09, 2013 |

For 1:

1 is already excluded from the input from the following if statement:

if (number <= 1) {
    System.out.println(number + " is not greater than 1.");
    return;
}

Therefore, even though the input may be 1, the program will only print out the message "1 is not grater than 1.

 

8.

You are right that should be hasNextDouble()

 

9.

First one is correct that it only add even numbers due to the if statement inside of the for loop.

Second one is not adding the even number. However, it just adds 2 times each number.

For instance if n is 6. Following table will show the difference.

i sumA sumB
1 0 2 (0 + 2)
2 2 (0 + 2) 6 (2 + 4)
3 2 12 (6 + 6)
4 6 (2 + 4) 20 (12 + 8)
5 6 30 (20 + 10)
6 12 (6 + 6) 42 (30 + 12)

 

Last edited by Eric Liao at 20:37 Dec 09, 2013.
Anonymous
Posts: 166
Posted 21:18 Dec 09, 2013 |
Eric Liao wrote:

For 1:

1 is already excluded from the input from the following if statement:

if (number <= 1) {
    System.out.println(number + " is not greater than 1.");
    return;
}

Therefore, even though the input may be 1, the program will only print out the message "1 is not grater than 1.

 

8.

You are right that should be hasNextDouble()

 

9.

First one is correct that it only add even numbers due to the if statement inside of the for loop.

Second one is not adding the even number. However, it just adds 2 times each number.

For instance if n is 6. Following table will show the difference.

i sumA sumB
1 0 2 (0 + 2)
2 2 (0 + 2) 6 (2 + 4)
3 2 12 (6 + 6)
4 6 (2 + 4) 20 (12 + 8)
5 6 30 (20 + 10)
6 12 (6 + 6) 42 (30 + 12)

 

1. But that little snippet wasn't included within the quiz though as I have tested it already when I inputted 1.
9. Thank you! So the correct answer should have been "a" instead of "b" which was suggested in the quiz solutions, right? 

Eric Liao
Posts: 158
Posted 21:28 Dec 09, 2013 |

1.

I think I took the wrong code snippet.

Input "1" will be shown as a prime number, which is not correct.

8.

Just a note here that the answer should be hasNextDouble().

9.

Yes Correct answer should be A.

---

Thanks for catching the error, I will contact Dr. Kang about this. You will receive the updated grade shortly.

Anonymous
Posts: 166
Posted 21:58 Dec 09, 2013 |
Eric Liao wrote:

1.

I think I took the wrong code snippet.

Input "1" will be shown as a prime number, which is not correct.

8.

Just a note here that the answer should be hasNextDouble().

9.

Yes Correct answer should be A.

---

Thanks for catching the error, I will contact Dr. Kang about this. You will receive the updated grade shortly.

All right, thank you! Can you also explain the answer to this question?

14. What does the method below return?

 

int findSomething (String str)

{

int position = 0;

while (position < str.length() && (str.charAt (position) != 'e'))

{

position++;

}

return position;

}

Eric Liao
Posts: 158
Posted 22:05 Dec 09, 2013 |
Anonymous wrote:
Eric Liao wrote:

1.

I think I took the wrong code snippet.

Input "1" will be shown as a prime number, which is not correct.

8.

Just a note here that the answer should be hasNextDouble().

9.

Yes Correct answer should be A.

---

Thanks for catching the error, I will contact Dr. Kang about this. You will receive the updated grade shortly.

All right, thank you! Can you also explain the answer to this question?

14. What does the method below return?

 

int findSomething (String str)

{

int position = 0;

while (position < str.length() && (str.charAt (position) != 'e'))

{

position++;

}

return position;

}

For number 14, you can simply create a trace table as following:

val num output
0 null (Not created) +
0 0 ( for loop ended 0 < 0 ) +
1 null (Not created) ++
1 0 (for loop executed 0 < 1) ++0
2 null (not created) ++0+
2 0 ++0+0
2 1 ++0+00
3 null (not created) ++0+00+
3 0 ++0+00+0
3 1 ++0+00+00
3 2 ++0+00+000

Therefore, result is ++0+00+000

Eric Liao
Posts: 158
Posted 22:06 Dec 09, 2013 |

Updated Result of week 8 was just sent.

If you didn't receive any update email. It means your grade didn't change.

Last edited by Eric Liao at 22:07 Dec 09, 2013.
Anonymous
Posts: 166
Posted 22:11 Dec 09, 2013 |
Eric Liao wrote:

Updated Result of week 8 was just sent.

If you didn't receive any update email. It means your grade didn't change.

Thanks. You've forgotten to correct question #9 as well. I have "a" as the answer but I still got "-1" for it. Also, your trace table was for Q13, not 14.

Last edited by Anonymous at 22:11 Dec 09, 2013.
Eric Liao
Posts: 158
Posted 22:24 Dec 09, 2013 |

Oops, thought I saw no. 13. I will do no 14 now.

int findSomething (String str)

{

int position = 0;

while (position < str.length() && ( str.charAt(position) != 'e') )

{

position++;

}

return position;

}

In term of the loop, it will first initialize the position to be zero, then start the loop.

Lets give a concrete example, says str is "Game", and we call method findSomething(str);

findsomething( str ) will go through the following trace table.

position str str.charAt(position)
0 Game G
1 Game a
2 Game m
3 Game e

It will return after finding 'e', which is 3. However if the str doesn't have 'e' then?

Lets do an example on "Position"

position str str.charAt(position)
0 Position P
1 Position o
2 Position s
3 Position i
4 Position t
5 Position i
6 Position o
7 Position n
8 Position not executed

Then it will return 8. You may ask why the str.charAt(position) is not executed. The reason why it is not executed is because the position < str.length() is already false. In truth table A && B, if A is wrong, the result is wrong regardless of B. Therefore, in Java run time, the second statement will get ignored.