






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Fall 2005 Page 1 of 11
Failure to read and follow the instructions below may result in severe penalties. Failure to adhere to these directions will not constitute an excuse or defense.
Name (print: Last name, First)
VT PID: (print: campus email address)
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
Fall 2005 Page 2 of 11
1) Consider the following Java robot task class instance variable declaration:
VPIRobot karel;
How many objects are instantiated by the statement above?
2) Consider the following Java robot statements:
VPIRobot karel = new VPIRobot(); int ave = karel.avenue();
Which of the following is the value of ave after the above code executes?
3) In order for a Java class to indicate that it will adhere to an interface class, which of the following statements must appear in the class declaration?
extends 2) implements
import 4) inherits
public 6) None of the above
4) Consider the partial Java class code below:
public class Exam1Robot extends VPIRobot { //alter move() to get a beeper & then move public void move() { if ( (frontIsClear() && (NextToABeeper()) ) { pickBeeper();
_________________________________ // choose a line to replace this one } }
Which of the following statements should replace the underscored line above to complete the method correctly?
move(); 2) super();
move.super(); 4) super.move();
None of the above
Fall 2005 Page 4 of 11
Consider the Java robot class method code below:
// determine the number of beeper(s) that a robot could reach in one move or less public ____________ beepersAdjacent() // line 9 { int numCloseBeeps = 0; //count of adjacent beepers int corners2Check = 4; while ( _______________ ) // line 10 { if ( frontIsClear() ) // check current direction for beepers { move(); numCloseBeeps = _____________________; //Line 11 turnLeft(); turnleft(); move(); _______________; //try next clockwise direction //Line 12 } else { turnRight(); } --corners2Check; } //end of while
___________________________________; //Line 13
return ___________________; //Line 14
}
For the next six questions (9-14), select the statement that should replace the underscored line above to implement the method correctly. Note that the beepers in the world (if any) should be unchanged after this method has executed.
9) What should fill the blank on Line 9?
10) What should fill the blank on Line 10?
corners2Check > 1 2) corners2Check >= 1
corners2Check > -1 4) corners2Check >= 0
11) What should fill the blank on Line 11?
numCloseBeeps++ 2) countBeepersInPile()
++numCloseBeeps + numCloseBeeps 4) countBeepersInPile() + numCloseBeeps
12) What should fill the blank on Line 12?
Fall 2005 Page 5 of 11
13) What should fill the blank on Line 13?
//nothing 2) turnLeft())
turnRight() 4) numCloseBeeps = numCloseBeeps + countBeepersInPile()
14) What should fill the blank on Line 14?
true 2) false 3) corners2Check
numCloseBeeps 5) countBeepersInPile() 6) 5
For the next two questions , (15–16), consider the execution of the following code in an empty world:
VPIRobot karel = new VPIRobot( 3, 3, West, Infinity);
while (! karel.facingNorth () ) // careful this is tricky { if (! karel.facingEast() ) { karel.putBeeper(); karel.turnLeft(); karel.move(); if (! karel.nextToABeeper() ) { karel.putBeeper(); } } else { karel.turnLeft (); } }
15) How many times does the robot turn left?
16) How many beepers does the above code cause the robot place in the world?
Fall 2005 Page 7 of 11
Assume that an Irobot is a subclass of a VPIRobot. Further assume that an Irobot has been instantiated in a task class and the following method (located in the Irobot class) has been invoked upon the Irobot object. The Irobot object’s beeper bag is empty to start and the corner the robot is on has a pile of beepers. Consider the following incomplete Java method for picking up all of the beepers:
// this method is for picking up some number of beepers on a corner public void pickBeeper() // line 1 { if ( nextToABeeper() ) // line 2 { _________ ; //grab a beeper // line 3 _________ ; //get remaining beepers // line 4 } // move(); // see question 24 }
22) Select from the list of statements below to replace the underscore in line 3 above to complete the implementation of this method:
move() 2) countBeepersInPile() 3) pickUpAllBeepers()
super.pickBeeper() 5) super.move() 6) it can’t be done
23) Select from the list of statements below to replace the underscore in line 4 above to complete the implementation of this method:
emptyBeeperBag() 2) countBeepersInPile() 3) pickUpAllBeepers()
super.pickBeeper() 5) pickBeeper() 6) it can’t be done
24) If the move() command after the end of the if was not commented out which of the following resulting actions (if any) would occur?
Fall 2005 Page 8 of 11
Consider the following Java test class code for the beepersAdjacent() method from questions 9-14:
public class Exam1RobotTest extends junit.framework.TestCase { Exam1Robot testRobot914 = null; World world914 = null;
public void setUp() { // beepersAdjacent33.kwld contains beepers only around 3, 3 World.startFromURL( “beepersAdjacent33.kwld” ); testRobot914 = new Exam1Robot(3, 3, North, 0); }
public void testBeepersAdjacent() // from questions 9- { int beepers = testRobot914.beepersAdjacent(); world914 = testRobot914.getWorldAsObject();
// test that beepersAdjacent() returns correct # of beepers world914.assertBeepersInWorld( ________________); // 25
// test that beeper bag has correct contents _________________________________________________; // 26
// test that robot is at correct location _________________________________________________; // 27 }
public void testSetup() // tests world description { testRobot914.move(); testRobot914.move(); testRobot914.move(); //now at 6,3 – should be no beepers here testRobot914.assertNotNextToABeeper(); } }
25) The test class method testBeepersAdjacent() invokes the method under test and gets a world reference for testing. Select from the list of expressions below to replace the underscore in the call to the world914.assertBeepersInWorld() method above:
testRobot914.beepersInBag() 2) testRobot914.beepersPickedUp()
testRobot914.countBeepersInPile() 4) testRobot914.neighbors();
beepers 6) None of the above
26) The Junit test method assertEquals() returns true if the two expressions passed to it evaluate to the same value. Select from the list of expressions below to replace the second underscore in the test method above:
testRobot914.assertBeepersInBeeperBag()
testRobot914.assertNoBeepersInBeeperBag()
assertEquals( beepers , world914.beepersinWorld() )
assertEquals( testRobot914.beepersInBag() , world914.beepersinWorld() )
Fall 2005 Page 10 of 11
27) Select from the list of expressions below to replace the third underscore in the test method above:
testRobot9_14.assertAt( 3, 3 )
testRobot9_14.assertNotAt( 3, 3 )
world9_14.assertRobotNotAt( 3 , 3 );
world9_14.assertRobotInWorldAt( 3 , 3 );
None of the above
28) Assume that all of the test methods in the Exam1RobotTest class above have just been executed. How many times has the setUp() method been invoked?
1 2) 2 3) 3
4 5) 0 6) None of the above
29) Given the following Boolean expression:
( (karel.street() > 3) && (karel.avenue() <= 2) )
Select the logically equivalent Boolean expression below:
2)! ( (karel.street() <= 3) || (karel.avenue() > 2) )
3)! ( (karel.street() > 3) || (karel.avenue() <= 2) )
4)! ( (karel.street() < 3) || (karel.avenue() >= 2) )
Fall 2005 Page 11 of 11
Consider the following complete Java method code below for positioning a robot to face west:
// this method positions a robot to face west public void faceWest() { // replace lines 1- while ( !facingWest() ) // line 1 { // line 2 turnLeft(); // line 3 } // line 4 }
30) Select logically equivalent implementations for lines 1-4 above, from the code segments below:
if ( facingNorth() || facingEast() || facingSouth() ) { turnLeft(); } else { faceWest(); }
if ( !facingNorth() && !facingEast() && !facingSouth() ) { faceWest(); } else { turnLeft(); }
if ( !facingWest() ) { faceWest(); } else { turnLeft(); }
if ( facingNorth() || facingEast() || facingSouth() ) { turnLeft(); faceWest(); }
1 and 3 are both equivalent to the original
None of the above are equivalent to the original
2 and 4 are both equivalent to the original