Question: Question 11 pts Consider the following class declaration. public class Car { private String carMake; private String carModel; public Car(String make, String model) { carMake

Question 11 pts

Consider the following class declaration.

public class Car { private String carMake; private String carModel; public Car(String make, String model) { carMake = make; carModel = model; } public String getMake() { return carMake; } public void setMake(String make) { carMake = make; } // There may be instance variables, constructors, and methods that are not shown. }

Assume that the following declaration has been made.

Car myRide = new Car(VW, Polo);

Which of the following statements is the most appropriate for changing the make of myRide from VW to Volkswagen?

myRide.setMake(Volkswagen);

Car.setMake(Volkswagen);

Car.carMake = Volkswagen;

myRide.getMake(Volkswagen);

myRide.carMake = Volkswagen;

Flag this Question

Question 21 pts

Consider the following method, hasRepeats, which is intended to return true if an array of integers contains the same value more than once and false otherwise.

 /** @param arr an array of integers * @return true if an element in the array is repeated */ public static boolean hasRepeats(int[] arr) { for (int k = 0; k < arr.length; k++) { for (/* missing code */) { if (arr[j] == arr [k]) return true; } } return false; }

Which of the following should be used to replace /* missing code */ so that hasRepeats works as intended?

int j = k + 1; j < arr.length; j++

int j = k + 1; j < arr.length - k; j++

int j = k; j < arr.length; j++

int j = 0; j < arr.length; j++

int j = 0; j < k + 1; j++

Flag this Question

Question 31 pts

Consider the following method

public static void mystery(ArrayList words) { int k = 0; int newSize = 3*words.size(); while (words.size() < newSize && words.size() > 0) { words.add(words.get(k)); k++; } } 

Which of the following describes what the method mystery() does to the ArrayList words?

The ArrayList words is extended to three times its size by replacing each single element in the list with that element repeated three times.

The ArrayList words is extended to three times its size by repeating the entire contents of the list three times in sequence.

The method generates an IndexOutOfBoundsException.

The ArrayList words is unchanged.

The ArrayList words is extended to three times its size by repeating the first element of the list at the end of the original list until the size of the list is three times its original size.

Flag this Question

Question 41 pts

public static int magic(int a) { int x = 9; while (x < 20) { x = 2*x; x = x - a; a--; } return x; } 

Which of the following is true about the behavior of method mystery?

The method will always return a number and never result in an infinite loop.

The method will result in an infinite loop for some arguments, but will return a number for all arguments which are sufficiently large.

The method will always return the same number, regardless of the argument passed to it.

The method will result in an infinite loop for some arguments, but will return a number for all arguments which are sufficiently small.

The method will never return a number as it always results in an infinite loop.

Flag this Question

Question 51 pts

Consider the following method.

 public static String shorten(String word, int cut) { return word.substring(0, cut) + word.substring(word.length() - cut); }

What value is returned as a result of the call shorten(debugging, 2)?

debing

deing

deng

debng

No value is returned because an IndexOutOfBoundsException will be thrown.

Flag this Question

Question 61 pts

Consider the following methods which appear within the same class.

 public static void mystery(int[] data) { for (int k = 0; k < data.length - 1; k+=2) { int t = data[k]; data[k] = data[k+1]; data[k + 1] = t; } } public static String toString(int[] data) { String str = ; for (int d : data) str = str + d +  ; return str; }

The following code segment appears in the main method of the same class.

 int[] nums = {1, 2, 7, 3, 5}; mystery(nums); System.out.println(toString(nums));

What is printed as a result of executing the code segment?

2 1 3 7 5

1 7 2 5 3

1 2 7 3 5

Nothing is printed because an ArrayIndexOutOfBoundsException is thrown during the execution of the method mystery.

2 7 3 5 1

Flag this Question

Question 71 pts

Consider the following method.

 public static boolean mystery(int num) { int temp = num; int newNum = 0; while (temp > 0) { newNum = 10*newNum + temp % 10; temp /= 10; } return (newNum == num); }

Which of the following calls to mystery will return true?

mystery(222555)

mystery(223388)

mystery(184481)

mystery(364364)

mystery(100000)

Flag this Question

Question 81 pts

Consider the following method.

 public static int mystery(int[] data) { int times = 0; int counter=0; for (int j = 0; j < data.length; j++) { counter = 0; for (int k = j; k < data.length; k++) { if (data[j] == data[k]) { counter ++; } } if (counter > times) { times = counter; } } return times; }

Assume that data has been declared and initialized as an array of integer values. Which of the following best describes the value returned by the call mystery(data)?

A most frequently occurring value in data

The number of times that a most frequently occurring value appears in data

An index of a most frequently occurring value in data

The maximum value in data

The number of times the maximum value appears in data

Flag this Question

Question 91 pts

Consider the following recursive method.

 public static void doWhat(int num) { if(num < 15) { doWhat(num + 4); System.out.print(num +  ); } }

What is printed as a result of the call doWhat(2)?

2 6 10 14 18

14

18 14 10 6 2

2 6 10 14

14 10 6 2

Flag this Question

Question 101 pts

Consider the following method

 public static String mashup(String str, int[] arr) { String result = ; for (int x : arr) result = result + str.substring(0, x); return result; }

The following code appears in another method in the same class.

 int[] nums = {1, 5, 3}; String word = program; System.out.println(mashup(word, nums));

What is printed when the code above is executed?

prprograprog

pro

programprogramprogram

pprogrpro

pprpro

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!