Ask Question

What will the following segment of code output? Assume the user enters a grade of 90 from the keyboard. cout <> test_score;

+1
Answers (1)
  1. 12 April, 15:02
    0
    Complete Question:

    What will the following segment of code output?

    Assume the user enters a grade of 90 from the keyboard.

    cout << "Enter a test score: ";

    cin >> test_score;

    if (test_score < 60);

    cout << "You failed the test!" << endl;

    if (test_score > 60)

    cout << "You passed the test!" << endl;

    else

    cout << "You need to study for the next test!";

    Answer:

    You failed the test!

    You passed the test!

    Explanation:

    With the assumption that test_score has been declared as an int variable

    The output of: You failed the test! You passed the test! May be unexpected since the test_score is greater than 90 so it looks like the code has a logical error somewhere.

    This error is as a result of putting a semi colon at then end of the first if statement if (test_score < 60);

    An if statement does not end a thought as such there should'nt be a semi colon ending it. Take the semi-colon out and the program will give the expected output of You passed the test! since test_score of 90 is greater than 60
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What will the following segment of code output? Assume the user enters a grade of 90 from the keyboard. cout test_score; ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers