CS final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

In a micro-world like a LightBot level or a Jeroo island, the coordinates that identify the upper left corner of the world are: 1) (0,0) 2) (1,1) 3) (A,0) 4) (A,1)

(0,0)

What are the coordinates of the net on this island: (3, 6) (2, 5) (6, 3) (5, 2)

(3, 6)

What are the coordinates of the flower on this island: (6, 3) (2, 5) (3, 6) (5, 2)

(6, 3)

Which of the following conditions is true only when the integer variables a, b, and c contain three different values? a != b != c (a != b) || (a != c) || (b != c) (a != b) && (a != c) && (b != c) !((a == b) && (b == c) && (a == c))

(a != b) && (a != c) && (b != c)

Suppose you are writing a method to search through a list of actors to find the location of a specific actor, and you have produced the code shown below. Unfortunately, it contains a logic error at one spot. Where is the error? // Returns the target actor's position in the list, or -1 // if the target actor is not found. public int findActor(List<Actor> actors, Actor target) { int location = -1; // (a) int current = 0; // (b) while (current < actors.size()) // (c) { if (target.equals( // (d) (all 4 lines) actors.get(current))) // (d) { // (d) location = current; // (d) } else // (e) (all 4 lines) { // (e) location = -1; // (e) } // (e) current++; // (f) } return location; // (g) }

(e)

When we create a new class that inherits from an existing class, we use this keyword to specify the class to inherit from: 1) extends 2) implements 3) inherits 4) sublcasses

1) Extends

Some methods take additional values that provide information about what the method should do. These additional values are called: 1) parameters 2) variables 3) attributes 4) options

1) parameters

int a = 1; int d = 7; if (b > c) { a = 2; } else { d = a * 2; } Which of the following code fragments is equivalent to the version above? "Equivalent" means that both fragments would assign the same values to a and d, given the same values for b and c. (1) int a = 1; int d = 7; if (b < c) { d = a * 2; } else { a = 2; } (2) int a = 1; int d = 7; if (b <= c) { d = a * 2; } else { a = 2; } (3) int a = 1; int d = 7; if (b >= c) { a = 2; } else { d = a * 2; } (4) int a = 1; int d = 7; if (b > c) { d = a * 2; } else { a = 2; } (1) (4) (2) none are equivalent (3)

2

The set of values of all of the attributes defining an object are called its: 1) inspector 2) state 3) variables 4) location

2) State

Which of the following is not an attribute of a Jeroo object? 1) its location 2) its gender 3) the number of flowers in its pouch 4) the direction it is facing

2) its gender

A Jeroo method that requires you to specify a direction when you call it is: 1) pick() 2) hop() 3) turn() 4) plant()

3) turn

A special kind of method that is used to initialize a brand new object is called: 1) a creator 2) an initalizer 3) an instantiator 4) a constructor

4) Constructor

When we create a new class that inherits from another, we call this new class a: 1) child class 2) All of these are correct 3) subclass 4) derived class

4) Derived class

An instance of a class is called a: 1) memory location 2) variable 3) name 4) object

4) object

Consider the following code segment: int w = 0; for (int x = 0; x < 5; x++) { w = -1; for (int y = 1; y < x; y++) { w = w + y; } } System.out.println(w); What value is printed? 0 8 7 2 5 1 3 6 -1 None of these 4

5

Consider the following Java code segment: int count = 0; int number = 2; boolean done = false; while (!done) { count++; number = number * 2; done = (number > 64); } System.out.println(count); What value is printed for the count variable? 10 1 6 5 3 0

6

Consider this code segment: List<Integer> aList = new ArrayList<Integer>(); for (int i = 4; i < 10; i++) { aList.add(i); } What output is printed by the following statement? aList.add(1, 10); aList.remove(0); System.out.println(aList.get(2)); 7 6 4 an error occurs 5 10

6

What value is printed for the variable i by the following code? int i = 12; int j = 0; while (i > 8) { i = i - 1; j = j + i; } System.out.println(i); 9 none of these 5 7 6 8 10 4

8

There are 5 marbles on the board. The marbles are located at (5, 0), (7, 1), (5, 2), (0, 3), and (1, 4). What will be the location of the first marble - (5, 0) - after executing the act() method on it? A) (6, 0) B) (5, 0) C) (4, 0) D) (3, 0)

A (6,0)

onsider this code segment: List<Integer> aList = new ArrayList<Integer>(); for (int i = 11; i < 15; i++) { aList.add(i); } What output is printed by the following statement? System.out.println(aList.get(aList.size() - 2)); A) 13 B) 11 C) 12 D) an error occurs E) 15 F) 14

A) 13

What value is printed for the variable i by the following code? int i = 12; int j = 0; while (i > 8) { i = i - 1; j = j + i; } System.out.println(i); A) 8 B) 6 C) 5 D) 4 E) 10 F) none of these G) 9 H) 7

A) 8

In which of the following situations is it probably better to use a fixed-size array over another more flexible option, like ArrayList? (Select all that apply, if any do.) A) A situation where you want to store a collection whose size won't change. b) A situation where you want a collection of primitive types. c) A situation where you are specifically asked to use an ArrayList. d) A situation where you don't know how many elements you'll be storing.

A) A situation where you want to store a collection whose size won't change. b) A situation where you want a collection of primitive types.

How does Java indicate when something goes wrong while running your code? A) An Exception will be thrown. B) A try-catch block will be created. C) Java will crash your compiler, shut down your computer, set your computer on fire, and then (to be nice) call the fire department. D) A JavaError will be thrown

A) An Exception will be thrown.

What does delegation mean? A) Delegation means an object uses another object to perform an action. B) Delegation means the object contains methods for a variety of specific situations. C) Delegation means an object creates another object. D) Delegation means the object's methods are split into simple functions.

A) Delegation means an object uses another object to perform an action.

What is a stream? A) It is a sequence of text or data coming from some source or going to some destination. B) It is the memory used by your program. C) It is the text located inside of a file. D) It is a sequence of method calls in a program.

A) It is a sequence of text or data coming from some source or going to some destination.

Examine the following code segment. public class Thing extends OtherThing { //TODO: Think of better names... } Which of the following things are true about this code segment? (Select all that apply, if any do.) A) OtherThing is the parent class of Thing. B) OtherThing is a subclass of Thing. C) OtherThing is the superclass of Thing. D) Thing will inherit all of methods and attributes of OtherThing.

A) OtherThing is the parent class of Thing. C) OtherThing is the superclass of Thing. D) Thing will inherit all of methods and attributes of OtherThing.

Bird, Duck, Crow inheritance hierarchy The simplified diagram above shows the relationships among classes Bird, Crow, and Duck. Assume Russell is an instance of Crow. Assume Donald is an instance of Duck. Which of the following statements is incorrect? A) Russell code cannot invoke the methods of Bird. B) Donald and Russell have instance fields and methods in common. C) Duck is a subclass of Bird. D) Bird is the superclass of Crow. E) Donald has the capabilities and attributes of a Bird.

A) Russell code cannot invoke the methods of Bird. A useful way to look at classes and instances is to use sets: a class describes a set of instances that share the same attributes and capabilities, and instantiating from a set produces a member of that set. From this perspective, a class's superclass is a superset of that class, defined by attributes and capabilities that are subsets of those of the class. So: all instances of a class are instances of all of its class's ancestors, and all instances of a class share the attributes and capabilities of that class. Thus Crow is not an instance of Bird.

What is printed when the following myProgram() method in the SimpleIsland class is executed? public class SimpleIsland { public boolean getTrue() { System.out.print("T"); return true; } public boolean getFalse() { System.out.print("F"); return false; } public static void myProgram() { if ( getTrue() || getFalse() ) System.out.println( ); } } A) T B) F C) Nothing is printed. D) TF E) TT

A) T Boolean short circuiting prevents the second method from executing.

Why are the values HERE, EAST, and AHEAD (among others) written in all uppercase? A) They are constant values and constant values are usually written in all uppercase in Java. B) All variable names in Java should be written in all uppercase. C) They are important values and important values are usually written in all uppercase in Java. D) It is a mistake that someone was too lazy to fix.

A) They are constant values and constant values are usually written in all uppercase in Java.

A test fixture in test class is recreated (i.e. re-executed) at the start of every test (i.e. before the execution of each test method), in order for each test to be carried out in the same initial conditions. A) True B) False

A) True

What is the correct syntax to declare a for-each loop? Assume you have a list of Minnow objects. A) for (Minnow minnow : list) b) for (Minnow minnow : List<Minnow> list) c) for (minnow : List<Minnow> list) d) for (minnow : list)

A) for (Minnow minnow : list)

Consider this declaration: Jeroo jessica = new Jeroo(3, 1, SOUTH, 9); Which of the following is the variable in the declaration above? A) jessica B) Jeroo C) none of these D) new

A) jessica

Consider the following class: public class RGB { private int r; private int g; private int b; public RGB(int red, int green, int blue) { r = red; g = green; b = blue; } // getter/setter methods present, but not shown ... public void makeGreen(int g) { r = g; g = this.g; b = g; } } What are the values of the fields in c1 after the following code segment? RGB c1 = new RGB(8, 16, 32); c1.makeGreen(4); A) r == 4, g == 16, b == 16 B) r == 4, g == 4, b == 4 C) r == 16, g == 16, b == 16 D) r == 8, g == 16, b == 32 E) r == 16, g == 4, b == 16 F) none of these G) r == 4, g == 16, b == 4

A) r == 4, g == 16, b == 16

Consider this partial class: public class MyJeroo extends Jeroo { public MyJeroo(int x, int y, CompassDirection direction, int numFlowers) { __________ // choose a line to replace this one } } Which of the following statements should fill in the blank to complete the constructor correctly? A) super(x, y, direction, numFlowers); B) new Jeroo(x, y, direction, numFlowers); C) this(x, y, direction, numFlowers); D) this(x, y); E) Jeroo(x, y); F) super(x, y); G) Jeroo(x, y, direction, numFlowers);

A) super(x, y, direction, numFlowers);

In the overridden hop method, what method do you call to make the CopyingJeroo object hop? A) super.hop() B) super() C) this.hop() D) this.move()

A) super.hop()

How would you reference the default constructor inside of a parameterized constructor? A) this(); B) super(); C) this(new Object()); D) super(new Object());

A) this();

Consider this code segment: boolean x = false; boolean y = true; boolean z = true; System.out.println( (x && !y || z) ); What value is printed? A) true B) false C) Nothing, there is a syntax error

A) true

In OOP, programmers often arrange classes into inheritance hierarchies as opposed to implementing isolated classes. Which of the following is NOT a valid reason for doing so? All of the above are valid reasons for using inheritance hierarchies. Objects from different subclasses can be passed as arguments to a method designed to accept objects of a superclass. Objects from different subclasses can be stored in the same array. More abstract classes at the top of the hierarchy can easily be extended in the project or reused in other projects. Methods from a superclass can often be reused in its subclasses without duplication of code.

All of the above are valid reasons for using inheritance hierarchies.

The Food object is on the left vertical edge of the map and is facing east. What can the object do? All of these actions. The object can move forward. The object can turn right. The object can turn left.

All of these actions.

Suppose you are writing a method to have a jeroo pick an entire row of flowers that lay in front of it. Further, once the jeroo reaches the end of the line of flowers, it should turn left and continue--giving the effect of "spiraling in" to pick an entire rectangular field of flowers. Complete the following method for this task by choosing the best way to fill in each blank: public void harvest() { while ( ) // line 1 { this.hop(); ; // line 2 if ( ) // line 3 { ; // line 4 } } }

Answer 1: this.seesFlower(AHEAD) Answer 2: this.pick() Answer 3: !this.seesFlower(AHEAD) Answer 4: this.turn(LEFT)

Suppose you are writing a method to navigate a jeroo clockwise around the shoreline of a square island, where the jeroo will begin its journey facing east in the northwest (upper left) corner of the island, at stop when it completes one lap. Complete the following method for this task by choosing the best way to fill in each blank: public void circumnavigate() { while ( ) { if ( ) { ; } else { this.hop(); } } }

Answer 1: !this.isFacing(NORTH) || !this.seesWater(AHEAD) Answer 2: this.seesWater(AHEAD) Answer 3: this.turn(RIGHT)

Consider the following classes: public class A { private int myNum; public A(int x) { myNum = x; } public int getNumber() { return myNum; } public String getLetters() { return "A"; } public String getMessage() { return this.getLetters() + "-" + this.getNumber(); } } public class AB extends A { public AB(int x) { super(x * 3); } public int getNumber() { return super.getNumber() + 2; } public String getLetters() { return "AB"; } } What is the output of the following code segment? A test = new A(4); System.out.print(test.getMessage()); What is the output of the following code segment? A test = new AB(4); System.out.print(test.getMessage());

Answer 1: A-4 Answer 2: AB-14

The reverseValues() method is intended to take a List of String objects as a parameter. When the method returns, it will have reversed the order of the objects in the list and returned the number of times objects were swapped to reorder the list. Complete this method so that it works as intended: public int reverseValues(List<String> list) { int numSwaps = 0; int low = 0; int high = __________; // Line A while (low < high) { // swap the elements at index positions low String temp = ____________________; // Line B // Note: set() changes the value at the specified location list.set(low, ____________________); // Line C list.set(high, ____________________); // Line D numSwaps++; low++; high--; } return numSwaps; }

Answer 1: list.size() - 1 Answer 2: list.get(low) Answer 3: list.get(high) Answer 4: temp

In computing one often needs to know: ⌈log2N⌉ . The ⌈ ⌉ symbols represent the ceiling function. The value of ⌈x⌉ is the smallest integer that is >= x. Thus, ⌈log2N⌉ is the smallest p such that 2p >= N. Fill in the blanks to complete this method so that it works as intended to compute ⌈log2N⌉ : // Returns the ceiling of the log base 2 of n, // that is the first power of 2 that is >= n. public int ceilingLog2(int n) { int exp = 0; int pow2 = 1; while ( pow2 < ______________ ) // Line A { pow2 *= 2; _____________ ++; // Line B } return _________________; // Line C } What is the best for filling in the blank on line A? What is the best for filling in the blank on line B? What is the best for filling in the blank on line C?

Answer 1: n Answer 2: Correct Answer exp Answer 3: exp

What does the Scanner class consider to be a word (assuming the default delimiters are used)? Any series of non-whitespace characters Whitespace characters Any series of characters A single character

Any series of non-whitespace characters

The following code for minVal() contains a logic error on a single line in the method body, on one of the four lines indicated by comments. Which line contains the logic error? /* This method returns the value of the minimum element in the * subsection of the array "y", starting at position * "first" and ending at position "last". */ public int minVal(List y, int first, int last) { int bestSoFar = y.get(first); // Line A for (int i = first + 1; i <= last; i++) { if ( y.get(i) < y.get(bestSoFar) ) // Line B { bestSoFar = y.get(i); // Line C } } return bestSoFar; // Line D } A B C D

B

How many objects are created by the following code? List<String> actors = ArrayList<String>(4); A) 3 B) 1 C) none of these D) 5 E 4 F) 0 G) 2

B) 1

After the execution of the following code how many Jeroo objects have been created? Jeroo jenny; Jeroo james; Jeroo jane; jenny = new Jeroo(); james = jenny; A) 2 B) 1 C) 4 D) 0 E) 3

B) 1 Each object creation requires a new operator execution.

What value is printed by this code segment? int sum = 0; for (int i = 0; i < 6; i++) { sum = 0; for (int j = 0; j <= i; j++) { sum += j; } } System.out.println(sum); A) 0 B) 15 C) 14 D) none of these E) 5 F)16 G) 17 H) 1

B) 15

Suppose you have a List of seven Integer objects called pop containing the following data: index 0 1 2 3 4 5 6 pop 15 -18 2 -9 -22 10 9 Trace the execution of the following code (remember that Java will automatically convert between primitive int values and Integer wrapper objects where needed): int count = 7; int sum = 0; for (int i = 0; i < count; i++) { if (pop.get(i) > 0) { sum = sum + pop.get(i); } } What is the value of sum when this loop is done? A) 20 B) 36 C) 21 D) 47 E) 27 F) none of these G) -13

B) 36

Consider the following method: public void fn() { int sum = 0; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { sum++; } System.out.println(sum); } } What value is printed on the second line of output produced by this method? A) none of these B) 6 C) 5 D) 3 E) 4 F) 2 G)0 H) 1

B) 6

Consider the following fields in the CopyingJerooTest class: private Island island; public Jeroo copier; private CopyingJeroo jeroo; Which fields can be accessed within the CopyingJerooTest class? A) None of the fields can be accessed B) All of the fields can be accessed C) island and jeroo D) copier

B) All of the fields can be accessed

The simplified diagram above shows the relationships among classes Bird, Crow, and Duck. Suppose Russell is an instance of Crow and Howard is an instance of Duck. Which of the following statements is incorrect? A) Bird is the superclass of Duck. B) Crow is an instance of Bird. C) Howard is an instance of Bird. D) Russell has the capabilities and attributes of a Bird.

B) Crow is an instance of Bird. A useful way to look at classes and instances is to use sets: a class describes a set of instances that share the same attributes and capabilities, and instantiating from a set produces a member of that set. From this perspective, a class's superclass is a superset of that class, defined by attributes and capabilities that are subsets of those of the class. So: all instances of a class are instances of all of its class's ancestors, and all instances of a class share the attributes and capabilities of that class. Thus Crow is not an instance of Bird.

True or False: The Java standard class library consists entirely of unrelated, arbitrary classes that serve no real practical purpose. A) True. B) False; the Java library uses relationships to organizes its classes, many of which can be very useful in certain situations. C) False; the Java library uses relationships to organizes its classes, all of which are extremely useful in all situations and should be memorized.

B) False; the Java library uses relationships to organizes its classes, many of which can be very useful in certain situations.

The following is real documentation from the String class. Examine it and answer the question that follows. startsWith public boolean startsWith(String prefix, int toffset) Tests if the substring of this string beginning at the specified index starts with the specified prefix. Parameters: prefix - the prefix. toffset - where to begin looking in this string. Returns: true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset; false otherwise. The result is false if toffset is negative or greater than the length of this String object; otherwise the result is the same as the result of the expression this.substring(toffset).startsWith(prefix) Which of the following calls to startsWith will return true? A) String str = "Hi, there!"; str.startsWith("Hi", 4); B) String str = "Hi, there!"; str.startsWith("the", 4); C) String str = "abc"; str.startsWith("xyz", 10); D) String str = "Hi, there!"; str.startsWith("the", 0);

B) String str = "Hi, there!"; str.startsWith("the", 4);

True or False: In our Jeroo simulation, the island's x-axis and y-axis both start at zero in the northwest corner. A) False; the island's x-axis and y-axis both start at zero in the northeast corner. B) True. C) False; the island's x-axis and y-axis both start at zero in the southwest corner. D) False; the island's x-axis and y-axis both start at zero in the southeast corner.

B) True.

True or False: The following code uses correct Java syntax and will compile without errors. int i = 9; ArrayList<Integer> list = new ArrayList<Integer>(); list.add(i); A) False; an int cannot be added to an ArrayList of Integer objects. B) True. C) False; Integer is not an object type. D)False; you cannot create an ArrayList of Integer objects.

B) True.

Consider the following two versions of a search() method, both of which are intended to return true if the given list contains the target value, and false otherwise: Version 1: public boolean search(List<Object> list, Object target) { for (int i = 0; i < list.size(); i++) { if (target.equals(list.get(i))) { return true; } } return false; } Version 2: public boolean search(List<Object> list, Object target) { boolean found = false; for (int i = 0; i < list.size(); i++) { if (target.equals(list.get(i))) { found = true; } } return found; } Answer the following: Does Version 1 work as intended? Does Version 2 work as intended? Are these two versions equally efficient, or does one more often take less time to execute than the other? A) Version 1 works, Version 2 does not work, Version 1 is more efficient B) Version 1 works, Version 2 works, Version 1 is more efficient C) Version 1 works, Version 2 does not work, Version 2 is more efficient D) Version 1 does not work, Version 2 does not work, Version 2 is more efficient E) Version 1 does not work, Version 2 works, Version 2 is more efficient F) Version 1 does not work, Version 2 does not work, Version 1 is more efficient G) Version 1 works, Version 2 works, they are equally efficient H) Version 1 does not work, Version 2 works, they are equally efficient

B) Version 1 works, Version 2 works, Version 1 is more efficient

Consider the following class: public class RGB { private int r; private int g; private int b; public RGB(int red, int green, int blue) { r = red; g = green; b = blue; } // getter/setter methods present, but not shown ... } What value is assigned to the variable b1 in the following code segment? RGB c1 = new RGB(255, 0, 255); RGB c2 = new RGB(255, 0, 255); boolean b1 = (c1 == c2); A) an error occurs B) false C) true

B) false

How many times does the following Java code print "hello world"? int i=10; while( i >= 4 ) { System.out.print("hello world"); i = i + 1; } A) 0 B) none of the these. C) 6 D) 6 E) 1

B) none of the these. This is an infinite loop, because the update condition increments i, but we are starting at 10.

Consider this partial Java class definition: public class DancingJeroo extends Jeroo { public void pick() { _______________ // an appropriate action goes here this.hop(); } } In the DancingJeroo class, the pick() method changes the behavior inherited from the Jeroo class. What is the programming term for this object-oriented mechanism? A) referencing B) none of these C) exception D) recursion E) messaging F) packaging

B) none of these

You see the expression n = 100000 in some code that successfully compiles. What type can variable n NOT be? A) int B) short C) long D) float E)double

B) short Shorts can only hold values in [-32768, 32767].

Consider this class: public class HoppyJeroo extends Jeroo { // alter hop() to get a flower and then move public void hop() { if ((this.isClear(AHEAD) && this.seesFlower(HERE)) { this.pick(); ____________ // choose a line to replace this one } } } Which of the following statements should fill the blank above to complete the method correctly? A) super(); B) super.hop(); C) this.hop(); D) hop.super(); E) none of these

B) super.hop();

Consider the following class: What value is assigned to the variable bool during the execution of the following code segment? A) false B) true C) an error occurs

B) true

Suppose I have written the following code: Scanner scan = IOHelper.createScanner("input.txt"); while (scan.hasNext()) { System.out.println(scan.next()); } scan.close(); Suppose also that input.txt reads as follows: IO is super fun! What will be printed to the terminal when the code is executed? A) fun! super is IO B) None of the other options are correct. The execution will cause an infinite loop. C) IO is super fun! D) IO is super fun!

C) IO is super fun!

Given the following Java boolean expression: !((x > y) && (a <= b)) Which expression is equivalent? A) (!(x > y) || !(a >= b)) B) !((x <= y) || (a > b)) C) ((x <= y) || (a > b)) D) (!(x < y) || !(a >= b))

C) ((x <= y) || (a > b))

Consider the following information: A CopyingJeroo object is at (1, 1) and is facing east. The associated Jeroo object is at (10, 1) and is facing south. If the CopyingJeroo hops two times, what will the location of the Jeroo object be? A) (1, 3) B) (3, 10) C) (10, 3) D) (3, 1)

C) (10, 3)

Consider the following code: double minDistance = 0; for (Minnow minnow : list) { if (distanceTo(minnow) < minDistance) { minDistance = distanceTo(minnow); } } Assume the minnows have distances 5, 13.5, and 10 from the Shark object. What will the value of minDistance be? A) 10 B) 13.5 C) 0 D) 5

C) 0

Consider this code segment: int i = 1; int sum = 0; while (i What value is printed? A) 4 B) 1 D) 9 E)2 F) 7

C) 10

Consider this code segment: String one = "1"; String two = "2"; String three = "3"; String four = "4"; ArrayList<String> aList = new ArrayList<String>(); aList.add(one); aList.add(two); aList.add(three); aList.add(four); What output is printed by the following statements? aList.remove(1); aList.add("5"); System.out.println(aList.get(1)); A) an error occurs B) 4 C) 3 D) 1 E) 2 F) none of these G) 5

C) 3

Which of the following statements about constructors is NOT true? A) A constructor can never have a return type. B) All constructors must have the same name as the class in which they exist. C) A constructor of a subclass cannot use the keyword this. D) A constructor is called when a program creates an object with the new operator. E) A constructor of a subclass can call a constructor of its superclass using the Java reserved word super. F) A class may have a constructor that takes no parameters.

C) A constructor of a subclass cannot use the keyword this. In Java, a constructor does not have a return type, unlike a method.

Suppose I create a new kind of Java object, called "Kangaroo". Further suppose that at any given time, all instances of this new object type have some facts associated with them. These include the Kangaroo's location, stomach capacity, and current attitude towards pea soup. What is another name for these facts? A) Associations B) object parts C) Attributes D) Parameters

C) Attributes

What value type does getOneIntersectingObject(Food.class) return? A) It returns a Predator object if one is found. B) It returns all Predator objects in the world. C) It returns a Food object if one is found. D) It returns all Food objects in the world.

C) It returns a Food object if one is found.

A turtle object is at (1, 1). What does this.getOneIntersectingObject(Marker.class) return if no Marker object is at (1, 1)? A) It will return false. B) It will return an empty Marker object. C) It will return null. D) It will return 0.

C) It will return null.

Suppose you have a List of seven Integer objects called flips containing the following data: index 0 1 2 3 4 5 6 flips -19 24 5 11 -36 12 99 Trace the execution of the following code: for (int i = 3; i < 5; i++) { flips.set(6, flips.get(i)); flips.set(i, flips.get(i - 1)); flips.set(i - 1, flips.get(6)); } What values are in the flips list when this loop is done? A) [-19, 24, 5, 11, -36, 12, 11] B) [-19, 24, 11, -36, -36, 12, -36] C) [-19, 24, 11, -36, 5, 12, -36] D) [-19, 11, 24, 11, -36, 12, 11] E) [-19, 5, 11, 24, -36, 12, 24] F)none of these

C) [-19, 24, 11, -36, 5, 12, -36]

Some classes, like the ArrayList class, use angle brackets in their documentation and their declaration. For example, the Java documentation for the ArrayList class starts with this: public class ArrayList<E>. What is the name for classes that have extra "parameter(s)" in angle brackets like this? A) type classes B) hard classes C) generic classes D) specific classes

C) generic classes

What output will the following code fragment produce? int grade = 90; int level = -1; if (grade > 90) { if (level <= -2) { System.out.println("A-level"); } else { System.out.println("B-status"); } } A) "B-status" B) "A-level" C) no output is produced D) A-level E) B-status

C) no output is produced Due to the braces around the body of the outer if statement the else is associated with the second (inner) if, despite the indentation. Since the outer if statement's condition is false no output is produced.

int sum = 0; for (int i = 1; i < 4; i++) { sum = sum + i; } System.out.println("sum = " + sum); What is wrong with the code fragment above, which is intended to add the numbers from 1 to 4 together and print out the result, which should be 10? A) the loop control variable i must be declared prior to the loop B) the loop control variable i must be initialized to zero C) the test condition must be <= D) the sum variable must be initialized to one E)none of these (the code is correct) F) the test condition must be ==

C) the test condition must be <=

Determine if Java would evaluate the following boolean expression as true or false, or if there is something (syntactically) wrong with the expression: (! z || y || ! y && z) A) false B) syntax error C) true

C) true

Determine if Java would evaluate the following boolean expression as true or false, or if there is something (syntactically) wrong with the expression: (! z || y || ! y && z) A) syntax error b) false C) true

C) true

Given the following Boolean expression: ((jessica.getGridX() > 3) && (jessica.getGridY() <= 2)) Which Boolean expression below is logically equivalent? A) ((jessica.getGridX() <= 3) || (jessica.getGridY() > 2)) B) !((jessica.getGridX() < 3) || (jessica.getGridY() >= 2)) C)!((jessica.getGridX() > 3) || (jessica.getGridY() <= 2)) D) !((jessica.getGridX() <= 3) || (jessica.getGridY() > 2))

D) !((jessica.getGridX() <= 3) || (jessica.getGridY() > 2))

How many objects are created by the following code? ArrayList<String> strings5; A) 2 B) none of these C) 4 D) 0 E) 5 F) 1 G) 3

D) 0

What are the possible values of randomValue for the following statement: int randomValue = Random.generator().nextInt(26); A) 1 through 26 B) 1 through 25 C) 0 through 26 D) 0 through 25

D) 0 through 25

After the execution of the following code how many Jeroo objects have been created? Jeroo jenny; Jeroo james; Jeroo jane; Jeroo jerry; jenny = new Jeroo(); james = jenny; jane = new Jeroo(); jerry = jane; jane = new Jeroo(); A) 5 B) 1 C) 4 D) 3 E) 0 F) 2

D) 3 Each object creation requires a new operator execution.

Consider these two methods: public void fuzzy(int x) { x = 73; System.out.print(x + " "); } public void bunny() { int x = 29; fuzzy(x); System.out.println(x); } What is printed when bunny() is called? A) none of these B) 29 29 C) 29 73 D) 73 29 E) 73 73

D) 73 29

Which of the following statements about constructors is not true? A) A class may have a constructor that takes no parameters. B) A constructor of a subclass can call a constructor of its superclass using the Java reserved word super. C) A constructor is called when a program creates an object with the new operator. D) A constructor must be declared with a return type of void. E) A constructor must have the same name as the class it is in.

D) A constructor must be declared with a return type of void.

Note: This question deals with material from chapter 7. What type of loop is the Java "while" loop? A) A pretest until loop. B) A posttest until loop. C) A posttest while loop. D) A pretest while loop.

D) A pretest while loop.

The Food object is on the left vertical edge of the map and is facing east. What can the object do? A) The object can turn right. B) The object can move forward. C) The object can turn left. D) All of these actions.

D) All of these actions.

Note: This question deals with material from chapter 6. What is a boolean method (also known as a predicate)? A) Any method that doesn't return a result. B) Any method that produces a number as a result. C) Any method that produces an object as a result. D) Any method that produces either true or false as a result.

D) Any method that produces either true or false as a result.

True or False: The following code uses correct Java syntax and will compile without errors. int i = 9; ArrayList<Integer> list = new ArrayList<Integer>(); list.add(i); A) False; you cannot create an ArrayList of Integer objects. B) False; an int cannot be added to an ArrayList of Integer objects. C) False; Integer is not an object type. D) True.

D) True

What method should you use to ensure the Scanner object still contains information? A) hasNextWord B) nextWord() C) next() D) hasNext()

D) hasNext()

Consider the following statement: jessica.hop(); Which of the following is the message in the statement above? A) jessica B) . (the dot) C) none of these D)hop()

D) hop()

Consider this partial Java class definition: public class DancingJeroo extends Jeroo { public void pick() { _______________ // choose a line to replace this one this.hop(); } } In the class above, we want DancingJeroos to automatically hop() immediately after they pick up a flower. Which of the following statements should be used to fill in the blank to achieve this behavior? A) none of these B) pick(); C) this.pick(); D) super.pick(); E) Jeroo.pick();

D) super.pick();

What method do you use to turn a Shark object towards the Minnow object minnow? A) turnTowards(Minnow.class); B) turnTowards(); C) turnTowards(distance(minnow)); D) turnTowards(minnow);

D) turnTowards(minnow);

Consider the following Java variable declaration: Jeroo jessica; How many objects are instantiated in this declaration? A) 2 B) 3 C) none D) This declaration is illegal in Java E) 1

E) 0

What value is printed by this code segment? int sum = 0; for (int i = 0; i < 6; i++) { sum = 0; for (int j = 0; j <= i; j++) { sum += j; } } System.out.println(sum); A) 1 b) 5 C) none of these D) 16 E) 15 F) 17 G) 0 H) 14

E) 15

Which of the following recommendations for testing software is not good advice? A) If possible, use automated testing procedures or read test data from files so that you can re-run the tests after corrections have been made. B) Design test data that exercises as many different paths through the code as is practical. C) Test on data that is at the boundary of program conditionals to check for "off by one" errors. D) When testing a large program, test the smaller pieces individually before testing the entire program. E) Test a program with all possible values of input data.

E) Test a program with all possible values of input data.

In order for a subclass to inherit from its base or parent class which of the following words must appear in its class definition statement? A) implements B) import C) this D) super E) extends

E) extends A subclass extends its base class.

Consider this class: public class MyJeroo extends Jeroo { // no methods defined here } Then examine the following code segment from an island subclass method: MyJeroo jeroo = new MyJeroo(); this.add(jeroo); jeroo.hop(); jeroo.turn(LEFT); jeroo.hop(); jeroo.turn(RIGHT); This code segment is valid (all methods are recognized) due to which object-oriented programming language feature? A) assertion B) none of these C) instantiation D) delegation E) inheritance F) overriding

E) inheritance

Consider the following code: if (!somethingIsFalse()) { return false; } else { return true; } Which of the following replacements for this code will produce the same result? A) return !somethingIsFalse(); B) none of these C) return true; D) return false; E) return somethingIsFalse();

E) return somethingIsFalse();

For any JUnit test class, when is the setUp() method executed? Once before all the test methods are executed. Only after the test class constructor method is executed. Only when explicitly invoked. Each time before a test method is executed.

Each time before a test method is executed.

What is the value of the variable z after the following code is executed? int w = 5; int x = 9; int y = 5; int z = 1; if (x % y >= w - z) { z--; if (y - 3 * w >= -x) { z++; } else { z--; } } else { z = 3; } A) 1 B) none of these C) 3 D) 2 E) 0 F) -1

F) -1

How many objects are created by the following code? List<Actor> actors = new ArrayList<Actor>(); A) 5 B) 4 C) 2 D) none of these E) 0 F) 1 G) 3

F) 1

Consider this class: public class MyJeroo extends Jeroo { // no methods defined here } Then examine the following code segment from an island subclass method: MyJeroo jeroo = new MyJeroo(); this.add(jeroo); jeroo.hop(); jeroo.turn(LEFT); jeroo.hop(); jeroo.turn(RIGHT); This code segment is valid (all methods are recognized) due to which object-oriented programming language feature? A) none of these B) overriding C) delegation D) instantiation E) assertion F) inheritance

F) inheritance

Determine if Java would evaluate the following boolean expression as true or false, or if there is something (syntactically) wrong with the expression: (!x && !y) && (y && z) A) syntax error B) true C) false

False

What is the value printed for the variable delta if the following code is executed? int delta = -1; int x = 3; if (x / 2 == 1) { delta = delta + x; } x--; if (x / 2 == 0) { delta = delta + x; } x--; if (x / 2 == 0) { delta = delta + x; } System.out.println(delta); A) 1 B) 2 C) 0 D) 6 E) none of these F) 5 G) 3 H) 4

G) 3

What value is printed by this code segment? int x = 0; int w = 0; while (x &lt= 4) { int y = 1; w = -1; while (y < x) { w = w + y; y++; } x++; } System.out.println(w); A) 8 B) 6 C) 7 D) -1 E) 10 F) none of these G) 5 H) 9

G) 5

Consider the following Java declaration: Jeroo jessica = new Jeroo(3, 1, SOUTH, 9); Which of the following methods in the Jeroo class are invoked by this declaration? A) destructor B) turn C) null E) hop F) no method is executed G) constructor H) new

G) constructor

Consider the following state of a Jeroo island: DorothyIsland.png Assume that the Jeroo at location (3, 4) facing South executes the following code: while (!this.seesWater(AHEAD)) { if (this.seesFlower(LEFT)) { this.turn(LEFT); } else if (this.seesFlower(RIGHT)) { this.turn(RIGHT); } this.hop(); } How many times will the hop() method be invoked by the code above in this situation? 10 13 28 Infinite 7 4

Infinite

What does the act method do? It determines what the object will do for other Actors' turns. It defines the state of the object. It determines what the starting values of the object are. It determines what the object will do for its current turn.

It determines what the object will do for its current turn.

What does the toArray() method do when called on a list? It returns all of the elements that are in the list in a new array. It creates and returns a new empty array. It returns a string that lists all of the elements in the list. When called on a LinkedList, it turn that list into an ArrayList.

It returns all of the elements that are in the list in a new array.

suppose your friend has the following lines of code that intend to find the first index of the first positive integer in the array, where array is an array of N integers: int i = 0; while (array[i] >= 0) { i++; } int location = i; Will your friend's code work as intended? It works as intended It works when the array contains at least one negative integer It never works It works when there are no negative integers It usually works, but sometimes unexpectedly fails

It works when the array contains at least one negative integer

Which of the following type of programming errors can software testing discover? Compiler errors Hardware errors Logic errors Syntax errors

Logic errors

Consider the following two simple Java classes: public class Ant { // superclass declarations go here ... } public class Queen extends Ant { // subclass declarations go here ... } Which of the following code snippets is/are legal? None of these. Ant a = new this(); Queen q = new Super(); All of these. Ant a = new Ant(); Queen q = a; Queen q = new Queen(); Ant a = q;

Queen q = new Queen(); Ant a = q;

How often will the act method execute? The act method will execute for every 'clock tick' of the program. The act method will only execute when it is called in the myProgram() method. The act method never executes; it is similar to a setup method. The act method will only execute once.

The act method will execute for every 'clock tick' of the program.

For any JUnit test class, what code should be placed in the setUp() method? Internal Island objects, (nets & flowers), and their initial placement. Assertion code to test the correct construction of class object instance fields. The test suite code that only needs to be executed once. The text fixture code common to all tests to establish the initial test state. The class test case that needs to be checked at the end of each test.

The text fixture code common to all tests to establish the initial test state.

Suppose I have written this line of code: public static final int MY_FAVORITE_NUMBER = 10; Which of the following statements regarding this variable are true? (Select all that apply, if any do.) a) This variable is visible to other classes, outside of the one it is declared in. b)This variable is constant and its value cannot be changed. c) This variable is a class variable (one not associated with a specific object, but rather the class itself). d) This variable is only visible in the class that it was declared in.

a) This variable is visible to other classes, outside of the one it is declared in. b) This variable is constant and its value cannot be changed. c)This variable is a class variable (one not associated with a specific object, but rather the class itself).

Consider this code segment: boolean x = false; boolean y = true; boolean z = true; System.out.println( (!x && y) || (x || !z) ); What value is printed? A) false b) true C) Nothing, there is a syntax error

b) true

What is a HashMap? a) A HashMap is a more advanced ArrayList used to store values. These values can be accessed using positive or negative indices. b) A HashMap is type of diagram used to show connections between classes and interfaces. c) A HashMap is a collection used to stored values. These values can be accessed by using a corresponding key.

c) A HashMap is a collection used to stored values. These values can be accessed by using a corresponding key.

A family of objects that all understand and respond to the same methods is defined by a: class set group collection

class

When we edit our program code, we might make some mistakes while typing. When we compile it to convert it to a computer-executable form, the compiler might produce a: compiler error syntax problem punctuation error spelling mistake

compiler error

A special kind of method that is used to initialize a brand new object when it is created is called a: constructor initializer creator builder

constructor

Consider this code segment: String one = "1"; String two = "2"; String three = "3"; String four = "4"; ArrayList<String> aList = new ArrayList<String>(); aList.add(one); aList.add(two); aList.add(three); aList.add(four); What output is printed by the following statements? aList.remove(2); System.out.println(aList.get(2)); a) 1 b) an error occurs c) 3 d) 4 e) 2 F) none of these

d) 4

What is the purpose of wrapper classes? A) They serve no purpose. b) They allow for data abstraction by wrapping and hiding certain parts of classes. c) They allow key-value pairs to be stored in containers besides maps. d) They allow primitive types (like int) to be used in cases where only objects can be used.

d) They allow primitive types (like int) to be used in cases where only objects can be used.

Consider this code segment: String aaa = "A"; String bbb = "B"; String ccc = "C"; String ddd = "D"; ArrayList<String> aList = new ArrayList<String>(); aList.add(ddd); aList.add(ccc); aList.add(bbb); aList.add(aaa); What output is printed by the following statement? System.out.println(aList.get(aList.size() - 1)); a) none of these b) B c) D d) an error occurs e) A f) C

e) A

Consider the following class: public class Point { private int xCoord; private int yCoord; public Point(int x, int y) { xCoord = x; yCoord = y; } // other methods omitted for space ... public int crossCopy(Point p2) { p2.xCoord = yCoord; p2.yCoord = this.xCoord; } What value is printed by the following code? Point alpha = new Point(1, 2); Point beta = new Point(2, 3); beta.crossCopy(alpha); System.out.println(alpha); a) X2Y3 b) X1Y1 c) X1Y3 d) X2Y2 e) none of these f) X1Y2 g) X3Y3

e) none of these

Assume that a Jeroo is standing on a flower--in fact, on many flowers, all in the same grid cell. Consider the following incomplete method for picking all the flowers: /** * Pick up all the flowers where the Jeroo is right now. */ public void pickAllFlowers() { if (this.seesFlower(HERE)) { this.pick(); __________; // Complete this line } } Select the statement that most appropriately fills in the blank to complete this method. a) while b) super.hop(); c) this.hop(); d) none of these choices will work e) this.pickAllFlowers(); f) super.pick();

e) this.pickAllFlowers();

Suppose I have written the following code: List earlyTimes = new ArrayList(); earlyTimes.add("1pm"); List otherTimes = new ArrayList(); otherTimes.add("1pm"); if (/* missing condition */) { System.out.println("You look great!"); //indicated line } Which of the following conditions will cause the indicated line to be executed? earlyTimes == otherTimes earlyTimes.equals("1pm") earlyTimes.equals(otherTimes) earlyTimes == "1pm"

earlyTimes.equals(otherTimes)

When creating a subclass of an existing subclass, which keyword is used to express the relationship between the two in your class definition? subclass extends inherits derives

extends

Consider this declaration: public class RunningJeroo extends Jeroo What is the derived class (or subclass) in this declaration? a) extends b) Jeroo c) none of these d) class e) public f) RunningJeroo

f) RunningJeroo

Used to store information (objects or values) inside an object to represent the object's state.

field

A Jeroo can carry these: 1) Flowers 2) water 3) other jeroos 4) nets

flowers

If you are writing a Java class and want to use the Jeroo class in it, which of the following statements must appear before your class' declaration? (Hint: Greenfoot4Sofia inserts it automatically in most cases, but it is always required.) implements sofia.micro.jeroo.*; extends Jeroo implements Jeroo import Jeroo; extends sofia.micro.jeroo.*; import sofia.micro.jeroo.*;

import sofia.micro.jeroo.*;

Assume you have a Scanner object reading from the following text, and the Scanner has currently read just past the word 'first': This is the first line of text. Here is the second sentence. What will be returned if you call scanner.next()? first line of text.

line

A temporary name inside a method, used to refer to an object or value in one or more places within that method.

local variable

Actions that an object can perform, or behaviors that an object can carry out, are defined in: methods behaviors actions messages

methods

Consider the following class: public class Point { private int xCoord; private int yCoord; public Point(int x, int y) { xCoord = x; yCoord = y; } // other methods omitted for space ... public int crossCopy(Point p2) { p2.xCoord = yCoord; p2.yCoord = this.xCoord; } What value is printed by the following code? Point alpha = new Point(1, 2); Point beta = new Point(2, 3); beta.crossCopy(alpha); System.out.println(alpha); X1Y3 X2Y2 X1Y2 X3Y3 none of these X1Y1 X2Y3

none of these

Stands for nothing, or no object.

null

What does the following Java code print? int inner = 0; int outer = 0; for (int i = 0; i < 6; i++) { outer++; for (int j = 0; j <= 3; j++) { inner++; } } System.out.println("outer " + outer + ", inner" + inner); outer 7, inner 4 outer 6, inner 18 outer 7, inner 24 outer 6, inner 3 outer 6, inner 24

outer 6, inner 24

An object or value passed into a method to provide additional information the method needs in order to run.

parameter

When you call a method, the receiver determines which how to respond (that is, determines which implementation of the method will be executed).

polymorphism

The incomplete method below returns the centered average of an ArrayList of Integers. The centered average we'll say is the mean average of the values, except ignoring the largest and smallest values in the list. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Examples: centeredAverage({1, 2, 3, 4, 100}) → 3.0 centeredAverage({1, 1, 5, 5, 10, 8, 7}) → 5.2 centeredAverage({-10, -4, -2, -4, -2, 0}) → -3.0 Give the correct completed version of the method below by replacing the blanks. (Copy and paste the incomplete method below into entry field at the bottom, replacing the blanks with Java code.) public double centeredAverage( ArrayList<Integer> nums ) { //check simple cases if ( (nums == null) || (nums.______ < 3) ) return 0.0; //initialize all to the first list element double sum = ______; int smallest = ______; int largest = ______; //sum all values & find smallest, largest for (int i=______; i < nums.______; i++) { //store ith list element in variable value. int value = ______; sum = sum + value; if (____________) smallest = value; else if (____________) largest = value; } //decrease sum by largest & smallest sum = ____________; //compute average return ( sum / (____________) ); } Copy and paste the incomplete method above into the entry field bellow, replacing the blanks with Java code.)

public double centeredAverage( ArrayList<Integer> nums ) { //check simple cases if ( (nums == null) || (nums.__length____ < 3) ) return 0.0; //initialize all to the first list element double sum = _0_____; int smallest = _0_____; int largest = _-_____; //sum all values & find smallest, largest for (int i=__1____; i < nums.__length____; i++) { //store ith list element in variable value. int value = ______; sum = sum + value; if (__value<min__________) smallest = value; else if (_value>max___________) largest = value; } //decrease sum by largest & smallest sum = ____________; //compute average return ( sum / (____nums.length-2________) ); }

Consider the jeroo on this island: Suppose you are writing a method that you will add to the subclass from which this jeroo was created, and you want it to move the jeroo along the north shore to the eastern edge of the island and then turn to face south. Which definition is the best choice for this task? public void combShore() { this.hop(5); this.turn(LEFT); } public void combShore() { this.hop(); this.hop(); this.hop(); this.hop(); this.hop(); this.turn(LEFT); } public void combShore() { this.hop(5); this.turn(RIGHT); } public combShore() { this.hop(5); this.turn(RIGHT); }

public void combShore() { this.hop(5); this.turn(RIGHT); }

When writing a method call, the object that will perform the method is called the: this caller receiver self

receiver

Used to invoke inherited methods that are overridden in a subclass, or to invoke an inherited constructor.

super

What should fill in the blank to complete this constructor correctly? public class MyJeroo extends Jeroo { public MyJeroo(int x, int y) { __________ // choose a line to replace this one } } this(int x, int y); new Jeroo(x, y); this(x, y); super(x, y); Jeroo(x, y); super(int x, int y);

super(x, y);

Select the correct action in the following method to complete it correctly. public class JohnnyJeroo extends Jeroo { //safely move and then plant a flower public void plant() { if (this.isClear(AHEAD)) { this.hop(); } if (!this.seesFlower(HERE)) { -------------------- } } }

super.plant();

When writing the instructions a LightBot object or a Level object will carry out when the Run button is pressed, we place them in: the myProgram() method the constructor the run() method the start action

the myProgram() method

How would you reference the default constructor inside of a parameterized constructor? this(new Object()); super(new Object()); this(); super();

this ();

Select the correct action in the following method to complete it correctly. public class Hopper extends Jeroo { //safely move public void safeHop() { if (this.seesFlower(HERE)) { this.pick(); } if (this.seesNet(AHEAD)) { this.hop(); } if (this.isClear(AHEAD)) { --------------------- } } } Answer 1:

this.toss();

Which method do you use to instruct a LightBot to light up a blue tile? turnLightOn() on() switchOnLight() setLightOn()

turnLightOn()

Used to indicate that a method does not return a value, and instead represents a command.

void


Kaugnay na mga set ng pag-aaral

Patho 293: Fluid and Electrolyte Balance

View Set

EXAM 4 chapter 48 Urinary elimination

View Set

Patient Care Dysrhythmias Study Guide

View Set