CS 1114 Test 2

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

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? (10, 3) (3, 10) (1, 3) (3, 1)

(10, 3)

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

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

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; } Correct Answer (2) (3) none are equivalent (4) You Answered (1) more than one is equivalent

(2)

Consider 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? aList.add(1, 10); aList.remove(0); System.out.println(aList.get(0)); 13 an error occurs 12 11 10 14

10

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); 1 5 15 17 0 16 14 none of these

15

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)); an error occurs 1 2 3 none of these 5 4

3

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)); an error occurs none of these You Answered 5 2 3 4 1

3

What value is printed for the variable j by the following code? int i = 12; int j = 0; while (i > 8) { i = i - 1; j = j + i; } System.out.println(j); 30 11 38 0 21 45 none of these 4

38

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 int sum() { int r = 0; int g = 1; int b = 2; return this.r + this.g + this.b; } } What value is printed by the following code segment? RGB c1 = new RGB(0, 127, 255); System.out.println(c1.sum()); 127 2 255 none of these 0 1 3 382

382

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); 6 8 5 9 10 none of these 7 -1

5

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 + 1); } public int getNumber() { return super.getNumber() + 1; } public String getLetters() { return "AB"; } } What is the output of the following code segment? A test = new AB(0); System.out.print(test.getMessage()); A-2 none of these You Answered AB-1 A-1 AB-0 A-0 AB-2

AB-2

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? Object parts Associations Parameters Attributes

Attributes

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

B-status

Which of the following are other correct names for a subclass? (Select all that apply, if any do.) You Answered Inheriting class. Derived class. Superclass. Child class.

Derived class. Child class.

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 statements? aList.remove(3); aList.add("E"); System.out.println(aList.get(3)); A C E an error occurs none of these D B

E

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 statements? aList.remove(3); aList.add("E"); System.out.println(aList.get(3)); B E D A none of these an error occurs C

E

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

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

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

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

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

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)? It will return null. It will return false. It will return 0. It will return an empty Marker object.

It will return null.

What does System.out.println() do? Reads in input from the terminal. Prints a line of text to the system's log file. Prints a line of text to the standard output file: output.txt. Prints a line of text to the program's standard output stream, which is usually connected to the terminal.

Prints a line of text to the program's standard output stream, which is usually connected to the terminal.

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? String str = "Hi, there!"; str.startsWith("the", 4); String str = "Hi, there!"; str.startsWith("Hi", 4); String str = "abc"; str.startsWith("xyz", 10); String str = "Hi, there!"; str.startsWith("the", 0);

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

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

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

When a class is instantiated, what is responsible for initializing the new object's attributes? A new object's attributes are randomly initialized by the Java compiler. The class's myProgram() method. The class's constructor. The class's main() method.

The class's constructor.

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

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

What value is the variable result initialized with? int x = 15; double y = 2; int result = x / y; 7.5 8 Will not compile: incompatible types 7

Will not compile: incompatible types

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(i - 1, flips.get(i)); } What values are in the flips list when this loop is done? [-19, 5, 11, 24, -36, 12, 99] You Answered [-19, 24, 11, -36, 5, 12, 99] none of these [-19, 24, 5, 11, -36, 12, 99] [-19, 11, 24, 11, -36, 12, 99] [-19, 24, 11, -36, -36, 12, 99]

[-19, 24, 11, -36, -36, 12, 99]

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.equals("1pm") earlyTimes.equals(otherTimes) earlyTimes == "1pm" earlyTimes == otherTimes

earlyTimes.equals(otherTimes)

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

false

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

false

What method should you use to add an object to the end of the List<Actor> list? list.add(object); list.insert(object); list.insert(0, object); list.add(0, object);

list.add(object);

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

no output is produced

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); none of these X1Y1 X2Y2 X1Y2 X2Y3 X1Y3 X3Y3

none of these

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); none of these Correct! r == 4, g == 16, b == 16 r == 4, g == 4, b == 4 r == 4, g == 16, b == 4 r == 16, g == 4, b == 16 r == 8, g == 16, b == 32 r == 16, g == 16, b == 16

r == 4, g == 16, b == 16

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? the test condition must be == the loop control variable i must be declared prior to the loop none of these (the code is correct) the test condition must be <= the loop control variable i must be initialized to zero the sum variable must be initialized to one

the test condition must be <=

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; } (3) (4) (1) more than one is equivalent (2) none are equivalent

(2)

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 third marble - (5, 2) - after executing the act() method on it? (7, 2) (6, 2) (4, 2) (3, 2)

(4, 2)

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 third marble - (5, 2) - after executing the act() method on it? (7, 2) (6, 2) (3, 2) (4, 2)

(4, 2)

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? You Answered (5, 0) (3, 0) (4, 0) (6, 0)

(6, 0)

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; } -1 2 none of these 3 1 0

-1

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; } 0 1 3 -1 2 none of these

-1

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? 13.5 10 0 5

0

How many objects are created by the following code? ArrayList<String> strings5; none of these 3 5 4 1 0 2

0

Examine the following code segment: int[] nums = {0, 1, 2, 3, 4}; for (int number : nums) { System.out.print(number + " "); System.out.println(number < 3 ? "is less than 3." : "is more than 3."); } What is printed to the terminal when the above code is executed? (Note: my math may be wrong....) Nothing will be printed. This code contains incorrect syntax and will not compile. 0 true 1 true 2 true 3 false 4 false 0 is less than 3. 1 is less than 3. 2 is less than 3. 3 is more than 3. 4 is more than 3. 0 number < 3 ? " is less than 3." : " is more than 3." 1 number < 3 ? " is less than 3." : " is more than 3." 2 number < 3 ? " is less than 3." : " is more than 3." 3 number < 3 ? " is less than 3." : " is more than 3." 4 number < 3 ? " is less than 3." : " is more than 3."

0 is less than 3. 1 is less than 3. 2 is less than 3. 3 is more than 3. 4 is more than 3.

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

0 through 25

Consider 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? aList.add(1, 10); aList.remove(0); System.out.println(aList.get(0)); 14 12 11 13 10 an error occurs

10

Consider this code segment: int i = 1; int sum = 0; while (i What value is printed? 9 7 0 2 10 6 1 5 4 none of these 3 8

10

public void big() { int i = 11; int y = 22; bang(x, y); System.out.println(x + " " + y); } What is printed when the method big is invoked? 22 22 11 22 none of these 11 11 22 11

11 22

Consider 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? for (int n : aList) { n = n + 1; } System.out.println(aList.get(1)); 15 11 14 12 an error occurs 13

12

Consider 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)); an error occurs 11 13 12 14 15

13

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); 16 0 none of these 17 You Answered 5 14 1 15

15

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 statement? System.out.println(aList.get(1)); 1 an error occurs 3 4 2 none of these

2

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 statement? System.out.println(aList.get(1)); none of these 4 2 3 an error occurs 1

2

How many times will the following for loop execute; assume value = 3. for (int i = 1; i <= value; i++) { turtle.move(2); } 2 times 3 times 4 times The loop will execute forever.

3 times

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)); none of these 2 an error occurs 4 1 3

4

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? 1 2 0 4 -1 None of these 5 3 8 6 7

5

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); none of these 10 7 6 8 9 -1 5

5

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? 4 3 1 0 2 6 5 none of these

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); 10 7 5 6 4 none of these 8 9

8

Consider the following code segment: forQ4.png What value is printed when the above code is executed? 20 7 You Answered None of these 10 13 21 14 15 9 17 11

9

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 + 1); } public int getNumber() { return super.getNumber() + 1; } public String getLetters() { return "AB"; } } What is the output of the following code segment? A test = new AB(0); System.out.print(test.getMessage()); You Answered AB-1 A-2 A-1 A-0 AB-0 AB-2

AB-2

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? copier island and jeroo All of the fields can be accessed None of the fields can be accessed

All of the fields can be accessed

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

All of these actions.

What is the output of the following code segment? And curry = new One(2); System.out.print( curry.scoreBoard() ); AndOne:2 AndOne:3 none of these AndOne:4 And:2 And:4 And:3

AndOne:4

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

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

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? More abstract classes at the top of the hierarchy can easily be extended in the project or reused in other projects. Objects from different subclasses can be stored in the same array. Methods from a superclass can often be reused in its subclasses without duplication of code. Objects from different subclasses can be passed as arguments to a method designed to accept objects of a superclass. All of the above are valid reasons for using inheritance hierarchies.

More abstract classes at the top of the hierarchy can easily be extended in the project or reused in other projects.

A turtle object is at (2, 5) and is facing east. Marker objects are at (2, 5), (3, 5), and (2, 6). If the move(2) method is called on the turtle, what will be the state of the Marker objects? No new marker objects will be added. Marker objects will be added at (4, 5) and (5, 5). You Answered A marker object will be added at (3, 6). A marker object will be added at (4, 5).

No new marker objects will be added.

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.) OtherThing is the parent class of Thing. OtherThing is the superclass of Thing. OtherThing is a subclass of Thing. Thing will inherit all of methods and attributes of OtherThing.

OtherThing is the parent class of Thing. OtherThing is the superclass of Thing. Thing will inherit all of methods and attributes of OtherThing.

What does System.out.println() do? Prints a line of text to the standard output file: output.txt. Prints a line of text to the system's log file. Prints a line of text to the program's standard output stream, which is usually connected to the terminal. Reads in input from the terminal.

Prints a line of text to the program's standard output stream, which is usually connected to the terminal.

When a class is instantiated, what is responsible for initializing the new object's attributes? The class's main() method. A new object's attributes are randomly initialized by the Java compiler. The class's myProgram() method. The class's constructor.

The class's constructor.

Consider your lab assignment when reviewing the following code that will be placed in the CopyingJeroo class: public void atFlower() { if (this.seesFlower(HERE)) { copier.plant(); this.pick(); } } What would happen if this code was integrated in your solution? The image would be removed from both islands. The image would only be on the first island. The image would be present on both islands. The image would be created on the second island and removed from the first island.

The image would be created on the second island and removed from the first island.

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

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

Suppose that I have written the following class: public class Thing { public Thing() { //This constructor intentionally does nothing. } //In fact, this whole class intentionally does nothing. } What do I have to name the file that contains this class in order for it to compile in Java? It can be named anything; the file's name does not matter. It can be named either "Thing.java" or "thing.java". thing.java Thing.java

Thing.java

Suppose that I have written the following class: public class Thing { public Thing() { //This constructor intentionally does nothing. } //In fact, this whole class intentionally does nothing. } What do I have to name the file that contains this class in order for it to compile in Java? It can be named either "Thing.java" or "thing.java". It can be named anything; the file's name does not matter. thing.java Thing.java

Thing.java

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

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); False; Integer is not an object type. False; an int cannot be added to an ArrayList of Integer objects. True. False; you cannot create an ArrayList of Integer objects.

True

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 void set(int x, int y) { xCoord = x; yCoord = y; } public String toString() { return "X" + xCoord + "Y" + yCoord; } What value is printed for the variable beta by the following code? Point alpha = new Point(3, 0); Point beta = alpha; alpha = new Point(3, 1); System.out.println(beta); X3Y1 null none of these X + 3 + Y + 0 X + 3 + Y + 1 X3Y0

X3Y0

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 void set(int x, int y) { xCoord = x; yCoord = y; } public String toString() { return "X" + xCoord + "Y" + yCoord; } What value is printed for the variable beta by the following code? Point alpha = new Point(4, 0); Point beta = alpha; alpha.set(4, 1); System.out.println(beta); none of these X4Y0 X + 4 + Y + 1 null X4Y1 X + 4 + Y + 0

X4Y1

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 = 1; i < 4; i++) { flips.set(0, flips.get(7 - i)); flips.set(7 - i, flips.get(i)); flips.set(i, flips.get(0)); } What values are in the flips list when this loop is done? [99, 99, 5, 11, -36, 12, 24] [99, 12, -36, 11, 5, 24, -19] none of these [-19, 24, 5, 11, -36, 12, 99] [12, 99, 12, 11, -36, 5, 24] [-36, 99, 12, -36, 11, 5, 24]

[-36, 99, 12, -36, 11, 5, 24]

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 = 1; i < 4; i++) { flips.set(0, flips.get(7 - i)); flips.set(7 - i, flips.get(i)); flips.set(i, flips.get(0)); } What values are in the flips list when this loop is done? [99, 12, -36, 11, 5, 24, -19] [-36, 99, 12, -36, 11, 5, 24] [-19, 24, 5, 11, -36, 12, 99] [12, 99, 12, 11, -36, 5, 24] [99, 99, 5, 11, -36, 12, 24] none of these

[-36, 99, 12, -36, 11, 5, 24]

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

for (Minnow minnow : list)

In the last lab, you learned more about the for-each loop. Given the for-each loop declaration below, what is the equivalent arithmetic for loop declaration? for (Marble m : marbleList) Correct Answer for (int i = 0; i < marbleList.size(); i++) { Marble m = marbleList.get(i); ... } for (int i = 1; i <= marbleList.size(); i++) { Marble m = marbleList.get(i); ... } for (int i = 1; i < marbleList.size(); i++) { Marble m = marbleList.get(i); ... } for (int i = 0; i <= marbleList.size(); i++) { Marble m = marbleList.get(i); ... }

for (int i = 0; i < marbleList.size(); i++) { Marble m = marbleList.get(i); ... }

What output will the following code fragment produce? public void fn() { int grade = 81; int level = -1; if (grade >= 90) if (level <= -2) System.out.println("A-level"); else System.out.println("B-status"); } "A-level" Correct Answer no output is produced You Answered B-status "B-status" A-level

no output is produced need { }

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 x = -1; for (int i = 0; i < count; i++) { if (pop.get(i) < 0) { x = pop.get(i); } } What is the value of x when this loop is done? -1 -9 none of these 4 9 6 10

none of these

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 x = -1; for (int i = 0; i < count; i++) { if (pop.get(i) < 0) { x = pop.get(i); } } What is the value of x when this loop is done? -1 6 4 10 none of these 9 -9

none of these

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 x = -1; for (int i = 0; i < count; i++) { if (pop.get(i) < 0) { x = pop.get(i); } } What is the value of x when this loop is done? -1 9 -9 10 4 6 none of these

none of these

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 x = -1; for (int i = 0; i < count; i++) { if (pop.get(i) < 0) { x = pop.get(i); } } What is the value of x when this loop is done? -9 9 -1 6 10 none of these 4

none of these

int sum = 0; for (int i = 0; i <10; i++) { sum = sum (i+1); } System.out.println("sum = " + sum); What is wrong with the code fragment above, which is intended to add the integers from 1 to 10 together and print out the result, which should be 55? the sum variable must be declared inside the loop the sum variable must be initialized to one The statement in the loop must be sum += i++; the loop control variable i must be declared prior to the loop the test condition must be <= the test condition must be == none of these (the code is correct) The statement in the loop must be sum += ++i;

none of these (the code is correct)

Which of the following access modifiers enforces information hiding? hidden private open public

private

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); r == 4, g == 16, b == 4 none of these r == 16, g == 4, b == 16 r == 4, g == 16, b == 16 r == 4, g == 4, b == 4 r == 16, g == 16, b == 16 r == 8, g == 16, b == 32

r == 4, g == 16, b == 16

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? the test condition must be == none of these (the code is correct) the sum variable must be initialized to one You Answered the loop control variable i must be initialized to zero the loop control variable i must be declared prior to the loop the test condition must be <=

the test condition must be <=

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

this();

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

true

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

true


Kaugnay na mga set ng pag-aaral

Evan Millinger - Islam Achievements Quiz

View Set

Chp 2 Determinants of Interest Rates

View Set

NREMT prep - mixed review / assessment and miscellaneous

View Set

All of AP Euro Sets (Duncan's sets Combined)

View Set

AP Art History Exam May 2019 (ALL 250 PIECES)

View Set

BIO 103 Test Your Knowledge Chapter 19

View Set

NURS 204 - Exam 3 - Chapters 61, 62, and 63

View Set