Reading Pseudocode

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rainxy
    New Member
    • Jan 2022
    • 1

    Reading Pseudocode

    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.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    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".

    Comment

    • wilsonmatt
      New Member
      • Mar 2025
      • 1

      #3
      Look for the pseudocode option that uses if-else statements to check the score and return the correct letter grade. The structure should start from the highest grade range (e.g., if score >= 90 return "A") and work down. Let me know if you need further clarification!

      Comment

      Working...