Assignment: String x, int y paper

Assignment: String x, int y paper

Assignment: String x, int y paper

Consider the following methods involving strings and integers.

public void update(String x, int y)

{

x = x + “Retriever”;

y = y * 3;

}

public void myTest()

{

String s = “Golden”;

int num = 7;

update(s, num);

/*end of method*/

}

When a call to myTest() is invoked, what are the values of s and num at the point indicated in by /end of method/?

s is the string “Golden”; num = 7
s is the string “GoldenRetriever”; n = 7
s is the string “Golden”; n = 21
s is the string “GoldenRetriever”; n = 21
s is the string “Retriever”; n = 21

Question: 2

The following method

public String removeFromString(String old, String frag)

removes all occurrences of the string frag from the string old. For example,

removeFromString(“hello there!”, “he”) returns “llo tre!”

removeFromString(“lalalala”, “l”) returns “aaaa”

Which of the following blocks of code will successfully implement removeFromString()?

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + frag.length());

old = old.substring(0, index);

index = old.indexOf(frag);

}

}

return old;

}

private String removeFromString(String old, String frag)

{

int i = old.indexOf(frag);

while (i > -1) {

String rest = old.substring(i + frag.length());

old = old.substring(0, i);

old = old + rest;

i = old.indexOf(frag);

}

return old;

}

private String removeFromString(String old, String frag)

{

int i = old.indexOf(frag);

while (i > -1) {

String rest = old.substring(i + frag.length());

old = old.substring(0, i+1);

old = old + rest;

i = old.indexOf(frag);

}

return old;

}

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + frag.length());

old = old.substring(0, index + 1);

index = old.indexOf(frag);

}

}

return old;

}

private String removeFromString(String old, String frag)

{

int index = old.indexOf(frag);

for (int i = 0; i < old.length(); i++) {

if (index > -1) {

String rest = old.substring(index + 1 + frag.length());

old = old.substring(0, index + 1);

index = old.indexOf(frag);

}

}

return old;

}

Assignment: String x, int y paper Question: 3

Which of the following will execute without throwing an exception error?

String str1 = null;

String str2 = “”;

if (str1.equals(str2))

System.out.println(“empty strings?”);

String str1 = “Golden”;

String str2 = “Retriever”;

if (str1.equals(str2))

System.out.println(“Golden Retriever!”);

String str1 = “Golden”;

String str2 = str1.substring(4);

System.out.println(str1 + str2);

None of the above
B and C above.

ORDER CUSTOMIZED PAPERS AT 5% DISCOUNT NOW

Question: 4

What is an instance method?

An instance method is piece of code called on a specific instance of the class. It is called with a receiver object.
An instance method is a piece of code that does not depend on any specific instances, just on the general class.
An instance method adds functionality to a class by creating private fields.
An instance method adds functionality to the class by printing out a result.

Question: 5

Refer to the Card and Deck classes shown below.

public class Card

{

private string suit;

private int value; //13 values for each suit in deck (0 to 12)

public Card (String cardSuit, int cardValue)

{ /* implementation */}

public String getSuit()

{return suit;}

public int getValue()

{return value;}

public String toString()

{

String faceValue = “”;

if (value >= 2 && value <=10)

return value + “ of “ + suit;

if (value == 11)

faceValue = “Jack”;

else if (value == 12)

faceValue = “Queen”;

else if (value == 0)

faceValue = “King”;

else if (value == 1)

faceValue = “Ace”;

else

return faceValue + “ of “ + suit;

}

}

public class Deck

{

private Card[] deck;

public final static int NUMCARDS = 52;

public Deck()

{ /* implementation */}

/** Shuffle deck. */

public void shuffle()

{ /* implementation */}

//Other methods not shown.

}

Consider a displayDeck method that is added to the Deck class.

/** Show the cards in the deck so they display one per line. */

public void displayDeck()

{

/* implementation */

}

Which of the following is correct /* implementation */ code?

I.

System.out.println(deck);

II.

for (card card : deck)

System.out.println((String) card);

III.

for (card card : deck)

System.out.println(card);

I only
II only
III only
I and II only
I and III only

Question: 6

Which of these is an example of calling a static method?

point.setX(x)
Math.abs(x)
student.getName()
square(x)

Question: 7

Which variables are in scope at the point labeled // POINT A? In other words, which variables exist at that point in the code?

public class ScopeQuiz extends ConsoleProgram

{

private int count = 0;

public void run()

{

int sum = 0;

// POINT A

for(int i = 0; i < 10; i++)

{

sum += i;

}

int result = sum + count;

}

}

Only sum
sum and count
sum, count, and i
sum and result
sum, count, i, and result

Question: 8

What is printed by the following program?

public class PrintQuestion extends ConsoleProgram

{

private String phrase = “Hello World!”;

public void run()

{

String phrase = “hi”;

System.out.println(phrase);

phrase = “hello”;

}

}

Hello World!
hi
hello
Nothing, this program will not run because it is not possible to have two variables with the same name.

Question: 9

What is the this keyword?

The this keyword refers to a convention in Java for talking about classes.
The this keyword refers to the current class name.
The this keyword refers to the current instance of the class.
The this keyword is a special keyword that can only be used in class methods.

Question: 10

Given the following Circle class

public class Circle

{

private int radius;

public Circle(int theRadius)

{

radius = theRadius;

}

public void setRadius(int newRadius)

{

radius = newRadius;

}

public boolean equals(Circle other)

{

return radius == other.radius;

}

}

What is the output of the following code snippet?

Circle one = new Circle(10);

Circle two = new Circle(10);

System.out.println(one == two);

true
false

Question: 11

Given the following Circle class

public class Circle

{

private int radius;

public Circle(int theRadius)

{

radius = theRadius;

}

public void setRadius(int newRadius)

{

radius = newRadius;

}

public boolean equals(Circle other)

{

return radius == other.radius;

}

}

What is the output of the following code snippet?

public void run()

{

Circle one = new Circle(5);

Circle two = new Circle(10);

foo(two);

System.out.println(one.equals(two));

}

public void foo(Circle x)

{

x.setRadius(5);

}

true
false

Question: 1

Given this code snippet,

public class Person

{

public String name;

public Person(String name)