Docsity

30 Multiple Choice Questions on Introduction to Programming in Java | CS 1054, Exams of Java Programming

Toggle sidebar
bg1
CS 1705 Intro to Object-Oriented Development Test 1
Fall 2005 Page 1 of 11
READ THIS NOW!
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.
• Print your name in the space provided below.
• Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form.
• Choose the single best answer for each question — some answers may be partially correct. If you mark more
than one answer, it will be counted wrong.
• Unless a question involves determining whether given Java code is syntactically correct, assume that it is. The
given code has been compiled and tested, except where there are deliberate errors. Unless a question
specifically deals with Java import directives, you should assume the necessary library files have been
imported.
• When you have completed the test, sign the pledge at the bottom of this page and turn in the test and your
Opscan.
• This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used
during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this
examination with any student who has not taken it. You must return this test form when you complete the
examination. Failure to adhere to any of these restrictions is an Honor Code violation.
• Mark your answers on the test form, for future reference, and on the Opcan. The answers you mark on the
Opscan form will be considered your official answers.
• There are 30 multiple-choice questions.
Do not start the test until instructed to do so!
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.
signature
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download 30 Multiple Choice Questions on Introduction to Programming in Java | CS 1054 and more Exams Java Programming in PDF only on Docsity!

Fall 2005 Page 1 of 11

READ THIS NOW!

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.

  • Print your name in the space provided below.
  • Print your name and ID number on the Opscan form; be sure to code your ID number on the Opscan form.
  • Choose the single best answer for each question — some answers may be partially correct. If you mark more than one answer, it will be counted wrong.
  • Unless a question involves determining whether given Java code is syntactically correct, assume that it is. The given code has been compiled and tested, except where there are deliberate errors. Unless a question specifically deals with Java import directives, you should assume the necessary library files have been imported.
  • When you have completed the test, sign the pledge at the bottom of this page and turn in the test and your Opscan.
  • This is a closed-book, closed-notes examination. No calculators or other electronic devices may be used during this examination. You may not discuss (in any form: written, verbal or electronic) the content of this examination with any student who has not taken it. You must return this test form when you complete the examination. Failure to adhere to any of these restrictions is an Honor Code violation.
  • Mark your answers on the test form, for future reference, and on the Opcan. The answers you mark on the Opscan form will be considered your official answers.
  • There are 30 multiple-choice questions.

Do not start the test until instructed to do so!

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.

signature

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?

  1. 1 2) 2 3) 0 4) none of these

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?

  1. 1 2) 2 3) 0 4) none of these

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?

  1. extends 2) implements

  2. import 4) inherits

  3. 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?

  1. move(); 2) super();

  2. move.super(); 4) super.move();

  3. 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?

  1. boolean 2) int 3) void 4) None of these

10) What should fill the blank on Line 10?

  1. corners2Check > 1 2) corners2Check >= 1

  2. corners2Check > -1 4) corners2Check >= 0

11) What should fill the blank on Line 11?

  1. numCloseBeeps++ 2) countBeepersInPile()

  2. ++numCloseBeeps + numCloseBeeps 4) countBeepersInPile() + numCloseBeeps

12) What should fill the blank on Line 12?

  1. turnLeft() 2) turnRight() 3) move() 4) None of these

Fall 2005 Page 5 of 11

13) What should fill the blank on Line 13?

  1. //nothing 2) turnLeft())

  2. turnRight() 4) numCloseBeeps = numCloseBeeps + countBeepersInPile()

14) What should fill the blank on Line 14?

  1. true 2) false 3) corners2Check

  2. 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?

  1. 1 2) 2 3) 3 4) 4 5) 5

6) 6 7) 7 8) 8 9) 0 10) ∞ (infinite)

16) How many beepers does the above code cause the robot place in the world?

  1. 1 2) 2 3) 3 4) 4 5) 5

6) 6 7) 7 8) 8 9) 0 10) ∞ (infinite)

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:

  1. move() 2) countBeepersInPile() 3) pickUpAllBeepers()

  2. 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:

  1. emptyBeeperBag() 2) countBeepersInPile() 3) pickUpAllBeepers()

  2. 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?

  1. After the last beeper was picked up the robot would move one corner.
  2. After each beeper is picked up the robot would move one corner.
  3. After the last beeper was picked up the robot would move as many corners as beepers in its bag.
  4. Nothing would happen the move() operation would never be executed.

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:

  1. testRobot914.beepersInBag() 2) testRobot914.beepersPickedUp()

  2. testRobot914.countBeepersInPile() 4) testRobot914.neighbors();

  3. 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:

  1. testRobot914.assertBeepersInBeeperBag()

  2. testRobot914.assertNoBeepersInBeeperBag()

  3. assertEquals( beepers , world914.beepersinWorld() )

  4. 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:

  1. testRobot9_14.assertAt( 3, 3 )

  2. testRobot9_14.assertNotAt( 3, 3 )

  3. world9_14.assertRobotNotAt( 3 , 3 );

  4. world9_14.assertRobotInWorldAt( 3 , 3 );

  5. 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. 1 2) 2 3) 3

  2. 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:

  1. ( (karel.street() <= 3) || (karel.avenue() > 2) )

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. 1 and 3 are both equivalent to the original

  2. None of the above are equivalent to the original

  3. 2 and 4 are both equivalent to the original