18 multiple choice 1 point how can we improve the following program? function main() { move(); move()…

18 multiple choice 1 point how can we improve the following program? function main() { move(); move(); move(); move(); move(); move(); move(); move(); } main(); use a while loop to repeat the move command fix the indentation of this program use a for loop to repeat the move command break down this program into more functions clear my selection 19 multiple choice 1 point which of the following is not a valid condition to go in an if statement for karel? ballspresent() beepersblocked() turnleft() frontisclear() clear my selection 20 multiple choice 1 point what is the purpose of using a for loop in code? to do something while a condition is true to make programs run faster to repeat something a fixed number of times to do something if a condition is true clear my selection 21 multiple choice 1 point say you want to write a program to have karel put down 300 tennis balls. which control structure would you use? nested while loop if statement for loop while loop clear my selection 22 multiple choice 1 point how can we teach karel new commands? the main function define a new function while loop for loop clear my selection 23 multiple choice 1 point karel starts at row 1 and column 1, facing east. after calling the stairstep function twice, where will karel be and what direction will karel be facing? (assume this is a superkarel world that is 10x10 in size) function stairstep() { move(); turnleft(); move(); turnright(); }
Answer
Brief Explanations:
- The code has multiple repeated
move()calls. Using a loop (eitherwhileorfor) can make the code more concise. Aforloop is better when you know the exact number of repetitions, which seems to be the case here. fromToClear()is not a typical valid condition for anifstatement in Karel programming. The others are common sensor - related conditions.- The purpose of a
forloop is to repeat something a fixed number of times. Awhileloop is used to do something while a condition is true. - To have Karel put down 300 tennis balls, since we know the exact number of times the action needs to be repeated, a
forloop is the appropriate choice. - Defining a new function is how we can teach Karel new commands. The main function is the entry - point of the program, and loops are for control flow, not for defining new commands.
- Analyzing the
stairStepfunction: The firstmovemoves Karel forward, theturnLeftturns it left, the secondmovemoves it up (assuming a grid - based world), and theturnRightturns it back to face East. After calling the function twice, Karel will be at Row 3, Column 1, facing East.
Answer:
- C. Use a for loop to repeat the move command
- D. fromToClear()
- C. To repeat something a fixed number of times
- C. For loop
- B. Define a new function
- Karel will be at Row 3, Column 1, facing East.