24.9k views
3 votes
A student developed a program that outputs the name of a fruit that begins with a letter, dependent upon the variable keyPressed, which stores the letter (from A to Z only) a user types on the keyboard. Below is a section of code from her program:

Which of the following descriptions below would be a way that this student might have written the rest of her code so that it executes as intended? Select two answers.
Option 1: Using a switch statement with cases for each letter of the alphabet, where each case contains code to output the corresponding fruit name.
Option 2: Using a series of if-else statements, where each if statement checks the value of the variable keyPressed and outputs the corresponding fruit name.
Option 3: Utilizing a loop that iterates through an array of fruit names, comparing the first letter of each fruit name to the value stored in the variable keyPressed, and then outputting the matching fruit name.
Option 4: Defining a function for each letter of the alphabet, and calling the appropriate function based on the value of the variable keyPressed to output the corresponding fruit name.

1 Answer

2 votes

Final answer:

Option 1 and Option 2 are ways the student might have written the rest of her code to execute as intended. The student could use either a switch statement or a series of if-else statements to match a letter to a fruit name and output it based on the keyPressed variable. Options 3 and 4 are less practical or efficient. Correct option is option 1.

Step-by-step explanation:

Option 1: Using a switch statement is one possible way to write the code. The switch statement would have cases for each letter of the alphabet, and each case would contain code to output the corresponding fruit name.

Option 2: Another way is to use a series of if-else statements. Each if statement would check the value of the variable keyPressed and output the corresponding fruit name.

Both Option 1 and Option 2 allow for straightforward mapping between the variable keyPressed and the corresponding fruit name, ensuring the program executes as intended.

The student could use either a switch statement or a series of if-else statements to match a letter to a fruit name and output it based on the keyPressed variable. Options 3 and 4 are less practical or efficient.

The student's program is designed to output the name of a fruit that begins with a letter based on user input stored in the variable keyPressed. There are multiple ways to write such a program. Two options that would allow the program to execute as intended are:

Option 1: Using a switch statement with cases for each letter of the alphabet. Each case includes code to display a fruit name starting with that letter.

Option 2: Using a series of if-else statements, where each conditional checks for the value of keyPressed and upon a match, it outputs the fruit name starting with that letter.

Both methods are viable and would perform the intended function of matching a letter to a fruit name and displaying it. Option 3 involving iteration over an array could work if such an array were created with each element's index corresponding to a letter, but it is less direct than Options 1 and 2. Option 4 is not practical, as creating a separate function for each letter of the alphabet is unnecessarily complex and does not align well with efficient coding practices.

User Crush
by
8.8k points