Question 11 Pts Consider Following Class Declaration Public Class Car Private String Carma Q43821602
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 forchanging 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 toreturn true if an array of integers contains the same value morethan 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() doesto the ArrayList words?
The ArrayList words is extended to three times its size byreplacing each single element in the list with that elementrepeated three times.
The ArrayList words is extended to three times its size byrepeating the entire contents of the list three times insequence.
The method generates an IndexOutOfBoundsException.
The ArrayList words is unchanged.
The ArrayList words is extended to three times its size byrepeating the first element of the list at the end of the originallist until the size of the list is three times its originalsize.
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 methodmystery?
The method will always return a number and never result in aninfinite loop.
The method will result in an infinite loop for some arguments,but will return a number for all arguments which are sufficientlylarge.
The method will always return the same number, regardless of theargument passed to it.
The method will result in an infinite loop for some arguments,but will return a number for all arguments which are sufficientlysmall.
The method will never return a number as it always results in aninfinite 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 callshorten(debugging, 2)?
“debing”
“deing”
“deng”
“debng”
No value is returned because an IndexOutOfBoundsException willbe thrown.
Flag this Question
Question 61 pts
Consider the following methods which appear within the sameclass.
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 thesame 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 isthrown 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 arrayof integer values. Which of the following best describes the valuereturned by the call mystery(data)?
A most frequently occurring value in data
The number of times that a most frequently occurring valueappears 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 sameclass.
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
Expert Answer
Answer to Question 11 pts Consider the following class declaration. public class Car { private String carMake; private String carM…
OR