470,590 Members | 2,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,590 developers. It's quick & easy.

Reading Pseudocode

1
Hello, I have to take a test for my job. I am not a programmer but a teacher. They want me to teach computer science to elementary school kids. Unfortunately, the test is also based on content from the computer science taught in high school. I will have to read a lot of pseudocode on the test. I just need to pick up the convention of reading the code. I have a math and science background.

I need help with the following problem. I would love an explanation. Also, is there a site in which I can connect to live programmers for tutoring.

The pseudocode procedure assignGrade is intended to return the String representing the letter grade associated with a given integer parameter score as defined in the given table. Which of the following pseudocode procedures correctly implement the assignGrade procedure?

Select all that apply.

A
String assignGrade ( int score )
String result ← "F"
if ( score ≥ 90 )
result ← "A"
end if
if ( score ≥ 80 )
result ← "B"
end if
if ( score ≥ 70 )
result ← "C"
end if
return result
end assignGrade

B
String assignGrade ( int score )
if ( score < 70 )
return "F"
end if
if ( score < 80 )
return "C"
end if
if ( score < 90 )
return "B"
end if
return "A"
end assignGrade

C
String assignGrade ( int score )
if ( score < 70 )
return "F"
if ( score < 80 )
return "C"
if ( score < 90 )
return "B"
end if
end if
end if
return "A"
end assignGrade

This is the explanation that I get. I do not understand why B is the answer. When I wrote the code in Pycharm it did work. However, I am thinking that if I write 64 as the score that since the last thing in the B function is return A. so it will always be A. I am thinking the as it goes down the function that the 64 is less than 70 so you will get an F, but the 64 is less than 80 in the next if statement so you will get a C, so on and so on.

Explanation that I need elaboration on. Thank you so much for helping me
Option (B) is correct. It correctly returns "F" for scores below 70, and then "C", "B", or "A" for progressively higher scores. Option (A) is incorrect. It fails for any score greater than 80, since result would be assigned "C" before being returned. Option (C) is also incorrect. It returns a letter grade of "F" for a score less than 70 and a letter grade of "A" for a score greater than or equal to 70. The statements that come after the first return statement are never executed.
Jan 26 '22 #1
1 6832
dev7060
569 Expert 512MB
This is the explanation that I get. I do not understand why B is the answer. When I wrote the code in Pycharm it did work. However, I am thinking that if I write 64 as the score that since the last thing in the B function is return A. so it will always be A.
The last executed thing in option (B) would be the return statement of the 'if' body that satisfies the condition.

I am thinking the as it goes down the function that the 64 is less than 70 so you will get an F, but the 64 is less than 80 in the next if statement so you will get a C, so on and so on.
Once a return statement is executed, the program control moves back to the caller function.

In option (A); for any score >= 70, all the 'if' conditions would evaluate to true hence all the if blocks would be executed. The latest assignment would always be C, hence the return value.

In option (C); the nested second and third if blocks would never be executed. There is no way to return "B" or "C".
Jan 28 '22 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by bearophileHUGS | last post: by
2 posts views Thread by Krisitne | last post: by
1 post views Thread by une | last post: by
2 posts views Thread by Joah Senegal | last post: by
10 posts views Thread by HardySpicer | last post: by
7 posts views Thread by popeyerayaz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.