cs final

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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

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

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

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

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?

(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

How many objects are created by the following code? List<Actor> actors;

0

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;

1

How many objects are created by the following code? List<String> actors = ArrayList<String>(4);

1

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));

10

Consider this code segment: int i = 1; int sum = 0; while (i What value is printed?

10

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));

12

Consider the code segment below has executed: What output is printed by the execution of the following statements?

13

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));

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);

15

Consider this code segment: What output is printed by the execution of the following statements?

2

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();

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));

3

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?

36

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);

38

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());

382

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));

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?

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);

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?

6

How many times does the following Java code print "hello world"?

7

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);

8

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

Examine the following code segment. PrintWriter outStream = IO.Helper.createPrintWriter("output.txt"); outStream.write(65); // This prints an "A". outStream.print(" <- That's an A!"); outStream.println(); outStream.println("It sure is!"); outStream.close(); What output will be written into output.txt when the above code is executed?

A <- That's an A! It sure is!

Which of the following statements about constructors is not true?

A constructor must be declared with a return type of void

Which of the following statements about constructors is NOT true?

A constructor of a subclass cannot use the keyword this

Which of the following statements about constructors is NOT true?

A constructor's return type must be declared void.

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 situation where you want a collection of primitive types. A situation where you want to store a collection whose size won't change.

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());

AB-2

What does the Scanner class consider to be a word (assuming the default delimiters are used)?

Any series of non-whitespace characters

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?

Attributes

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?

Crow is an instance of Bird.

What does delegation mean?

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

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));

E

For any JUnit test class, when is the setUp() method executed?

Each time before a test method is executed.

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, many of which can be very useful in certain situations.

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.

Consider this declaration: public class RunningJeroo extends Jeroo What is the base class (or superclass) in this declaration?

Jeroo

Consider this declaration: public class RunningJeroo extends Jeroo What is the derived class (or subclass) in this declaration?

RunningJeroo

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( ); } }

T

Which of the following recommendations for testing software is not good advice?

Test a program with all possible values of input data.

For any JUnit test class, what code should be placed in the setUp() method?

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

What is the purpose of wrapper classes?

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

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.

Consider the following input: This is the first line of text. This is the second line of text. Assuming you have a brand new Scanner object named scanner that was created to read from this text. Assume nothing has been read from the Scanner yet. If scanner.nextLine() is called, what will be returned?

This is the first line of text.

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.

True

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

True

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

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?

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

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);

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);

X4Y1

Each time you write a new method, you can write and run new test cases just for that method before you move on to writing other methods. Some benefits that programmers observe from this approach are:

You discover bugs sooner, before the whole class is complete

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, 24, 11, -36, -36, 12, 99]

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); aList.remove(3); System.out.println(aList.get(1));

an error occurs

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);

false

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)

false

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)

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

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?

generic classes

Consider the following statement: jessica.hop(); Which of the following is the message in the statement above?

hop()

Given the following if statement: if ( (this.seeJeroo(AHEAD)) || (this.seeFlower(AHEAD)) || (this.seeNet(AHEAD)) || (this.seeWater(AHEAD)) ) { this.turn(RIGHT); } else { this.hop(); } Which of the following if statements is logically equivalent?

if ( (!this.seeJeroo(AHEAD)) && (!this.seeFlower(AHEAD)) && (!this.seeNet(AHEAD)) && (!this.seeWater(AHEAD)) ) { this.hop(); } else { this.turn(RIGHT); }

What is printed when the following myProgram() method in the SimpleIsland class is executed?

if else if

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?

inheritance

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

jessica

What is output by the execution of the following method?

jessica has NO flowers.

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()?

line

Consider the following Java declaration: Jeroo jessica = null; Which of the following methods in the Jeroo class are invoked by this declaration?

no method is executed

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"); } }

no output is produced

Consider the following Java variable declaration: Jeroo jessica; How many objects are instantiated in this declaration?

none

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; }

none of the these.

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

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?

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?

none of these

Consider the following code: if (!somethingIsFalse()) { return false; } else { return true; } Which of the following replacements for this code will produce the same result?

return somethingIsFalse();

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?

super.pick();

How would you reference the default constructor inside of a parameterized constructor?

this();

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)

true

What method do you use to turn a Shark object towards the Minnow object minnow?

turnTowards(Minnow.class);


Set pelajaran terkait

Study Guide for Final Exam - Chapter 85 & 86 Questions

View Set

Chapter 7 InQuizitive: The Legislature

View Set

BA 325 Practice Questions (Midterm 1-2)

View Set

PR Writing - Giving Speeches & Presentations

View Set

REDBOOK (FEMALE PELVIS ANATOMY) Ch 16

View Set