




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The Karel Programming Ultimate Exam provides a comprehensive assessment of programming fundamentals using Karel. It covers topics such as loops, conditionals, functions, and debugging. The exam emphasizes hands-on problem-solving and prepares learners for advanced programming languages.
Typology: Exams
1 / 171
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which primitive command makes Karel move one intersection forward in the direction it is currently facing? A) turnLeft() B) move() C) pickBeeper() D) putBeeper() Answer: B Explanation: The move() command advances Karel one space ahead; the other commands perform rotations or beeper actions. Question 2. In Karel’s world, avenues run in which direction? A) North‑South B) East‑West C) Vertical (up‑down) D) Diagonal Answer: C Explanation: Avenues are the vertical lines of the grid, while streets run horizontally. Question 3. What is the result of calling turnLeft() when Karel is already facing West? A) Karel faces North B) Karel faces South C) Karel faces East D) Karel faces West Answer: A Explanation: turnLeft() rotates Karel 90° counter‑clockwise; from West it becomes North.
Question 4. Which condition returns true if there is a beeper on Karel’s current corner? A) frontIsClear() B) beepersPresent() C) leftIsClear() D) carryingBeeper() Answer: B Explanation: beepersPresent() checks for one or more beepers at the current location. Question 5. If Karel executes the following code, how many beepers will be placed on the starting corner?
putBeeper(); putBeeper(); putBeeper();A) 0 B) 1 C) 2 D) 3 Answer: D Explanation: Each call to putBeeper() deposits one beeper, so three calls leave three beepers. Question 6. Which error occurs when Karel tries to pick up a beeper but none exist on the current corner?
Question 9. In a while loop, which condition would cause Karel to stop moving when it reaches a wall directly ahead? A) while(frontIsClear()) { move(); } B) while(!frontIsClear()) { move(); } C) while(beepersPresent()) { move(); } D) while(carryingBeeper()) { move(); } Answer: A Explanation: The loop continues while the front is clear; when a wall blocks the front, the condition becomes false and the loop ends. Question 10. Which of the following best describes a fencepost error? A) Forgetting to close a loop brace B) Placing one too many or one too few beepers while iterating a line C) Using turnLeft() instead of turnRight() D) Declaring a function without a return type Answer: B Explanation: Fencepost errors are off‑by‑one mistakes, often seen when counting steps or beepers. Question 11. If Karel is currently facing North, which sequence of primitive commands will make it face South? A) turnLeft(); turnLeft(); B) turnLeft(); turnLeft(); turnLeft(); C) turnLeft(); turnLeft(); turnLeft(); turnLeft(); D) turnLeft(); Answer: A
Explanation: Two left turns rotate 180°, changing direction from North to South. Question 12. Which boolean condition is FALSE when Karel is standing on a corner that has no beepers and no walls directly ahead? A) frontIsClear() B) beepersPresent() C) leftIsClear() D) rightIsClear() Answer: B Explanation: beepersPresent() returns false if there are zero beepers on the current corner. Question 13. What is the purpose of a precondition in a Karel function definition? A) To specify the state of the world after the function finishes B) To list the variables used inside the function C) To describe what must be true before the function is called D) To indicate the function’s return type Answer: C Explanation: A precondition states the required conditions before execution, ensuring correct usage. Question 14. Which of the following is NOT a valid Karel primitive command? A) move() B) turnRight() C) pickBeeper() D) putBeeper()
Question 16. Which loop will cause an infinite loop if the world contains no walls in front of Karel? A) while(frontIsClear()){ move(); } B) while(beepersPresent()){ move(); } C) while(carryingBeeper()){ turnLeft(); } D) while(leftIsClear()){ turnLeft(); } Answer: A Explanation: If the front is always clear, the while condition never becomes false, leading to endless movement. Question 17. When defining a function that places a beeper on every corner of a 5×5 square, which of the following is a necessary postcondition? A) Karel is back at the starting corner facing East B) All beepers in the world are removed C) Karel is holding exactly one beeper in its bag D) The world contains exactly 25 beepers Answer: D Explanation: The function’s goal is to leave a beeper on each of the 25 corners, so the postcondition reflects that. Question 18. What does the statement while(beepersPresent()) { pickBeeper(); } accomplish? A) Places a beeper on the current corner until none remain B) Removes all beepers from the current corner C) Moves Karel forward while there are beepers ahead D) Causes a runtime error if no beepers are present Answer: B
Explanation: The loop repeats as long as a beeper exists, picking one each iteration, thus clearing the stack. Question 19. Which of the following is a correct way to implement the Right‑Hand Rule for maze navigation? A) Always turnRight() if frontIsClear() B) If rightIsClear() then turnRight() and move(); else if frontIsClear() move(); else turnLeft(); C) Move forward until a wall, then turnLeft() once D) Randomly choose a direction at each intersection Answer: B Explanation: The Right‑Hand Rule checks the right side first, then forward, then left, ensuring the wall on the right is always followed. Question 20. In Karel’s coordinate system, what are the coordinates of the bottom‑left corner of a world with 10 streets and 10 avenues? A) (0,0) B) (1,1) C) (10,10) D) (0,1) Answer: B Explanation: Karel worlds start counting at 1, so the southwest corner is (1,1). Question 21. Which statement correctly describes the difference between a syntax error and a logic error in Karel programs? A) Syntax errors prevent compilation; logic errors cause incorrect behavior at runtime. B) Syntax errors cause infinite loops; logic errors crash the program.
while(frontIsClear()){ move(); } turnLeft(); while(frontIsClear()){ move(); }
A) Move Karel to the upper‑right corner of the world B) Move Karel to the lower‑left corner of the world C) Move Karel to the farthest point north on the same street, then eastward D) Move Karel to the farthest point east on the same street, then northward Answer: D Explanation: First loop moves east until a wall, turn left faces north, second loop moves north to the top edge. Question 25. Which of the following is a correct precondition for a function `clearColumn()` that removes all beepers from Karel’s current column? A) Karel is at the bottom of the column facing North B) The column contains exactly one beeper per corner C) Karel’s beeper bag is empty D) There are no walls in the world Answer: A Explanation: The function assumes Karel starts at the column’s base and faces upward to traverse it. Question 26. How many times will the body of the following loop execute?int i = 0; while(i < 5){ move(); i = i + 2; }
A) 2 B) 3 C) 5 D) 0 Answer: A Explanation: i starts at 0, loop runs (i=0→2), then (i=2→4), then (i=4→6) stops; total of 2 iterations. Question 27. Which condition should be used to detect that Karel is at the western edge of the world? A) frontIsClear() B) leftIsClear() C) beepersPresent() D) carryingBeeper() Answer: B Explanation: At the western boundary, there is a wall to the left when Karel faces North; leftIsClear() will be false. while(frontIsClear()){ putBeeper(); move(); }C)
repeat(9){ putBeeper(); move(); }D)
for(int i=0;i<9;i++) putBeeper();Answer: A Explanation: Option A walks a serpentine pattern covering each row, placing beepers on every corner. The others either stop early or place multiple beepers on the same spot without moving. Question 30. What is the effect of the statement if(!frontIsClear()) turnLeft();? A) Karel turns left only when there is a clear path ahead B) Karel turns left only when a wall blocks the front C) Karel always turns left regardless of the front
D) Karel never turns left Answer: B Explanation: The ! negates the boolean; the turn occurs when frontIsClear() is false (i.e., a wall ahead). Question 31. Which loop construct is unavailable in the standard Karel language (as used in Stanford’s CS106A)? A) for loop B) while loop C) do‑while loop D) repeat loop (a library function) Answer: C Explanation: Karel’s Java‑based environment provides for and while loops but not the C‑style do‑while loop. Question 32. If Karel’s world contains a single beeper stack of 4 beepers at (2,2), what does the following code accomplish?
while(beepersPresent()){ pickBeeper(); turnLeft(); move(); turnRight(); putBeeper(); }Question 35. Which of the following best describes a “generalized” Karel program? A) It works only for a specific world size and beeper layout B) It uses hard‑coded coordinates for movement C) It adapts to any world dimensions and beeper configurations using loops and conditionals D) It contains no functions or loops Answer: C Explanation: Generalization means the program is written to handle arbitrary world parameters. Question 36. When debugging, what does “tracing” refer to? A) Running the program with a debugger tool B) Manually following each command to track Karel’s position and orientation step‑by‑step C) Printing the source code to the console D) Measuring the program’s execution time Answer: B Explanation: Tracing is the mental or paper‑and‑pencil process of simulating program execution. Question 37. Which of the following statements about frontIsClear() is true? A) It returns true if there is a wall directly ahead B) It returns false when Karel is at the edge of the world facing outward C) It can be used to detect beepers in front of Karel D) It changes Karel’s direction Answer: B Explanation: frontIsClear() is false when a wall blocks the front, including world boundaries.
Question 38. What is the minimum number of turnLeft() calls needed to make Karel face West when it starts facing North? A) 1 B) 2 C) 3 D) 4 Answer: C Explanation: Three left turns rotate 270°, changing direction from North to West. Question 39. Which of the following programs will cause Karel to enter an infinite loop in any world that contains at least one beeper?
while(beepersPresent()){ turnLeft(); }A) It will terminate immediately B) It will loop forever because beepersPresent() never changes inside the loop C) It will cause a compile‑time error D) It will move Karel forward indefinitely Answer: B Explanation: The condition never changes (no pickBeeper()), so the loop never ends. Question 40. In Karel, what does the command move(); do if Karel’s front is blocked by a wall? A) Karel stays in place and the program continues
Question 43. When using a for loop to draw a vertical line of beepers of length N, which of the following loop headers is correct? A) for(int i=0;i Explanation: Three left turns equal one right turn (90° clockwise). Question 46. Which of the following is a correct way to test whether Karel is at the origin (1,1) regardless of its orientation? A) if(frontIsClear() && leftIsClear()) … B) if(street==1 && avenue==1) … C) if(beepersPresent()) … D) if(carryingBeeper()) … Answer: B Explanation: Directly checking the street and avenue coordinates confirms the position. Question 47. What will be the output of the following code snippet (assume Karel starts at (1,1) facing East and the world is at least 4 streets wide)?
for(int i=0;i<4;i++){ move(); } turnLeft();A) Karel ends at (5,1) facing North B) Karel ends at (4,1) facing North C) Karel ends at (4,1) facing West D) Karel ends at (5,1) facing East Answer: B