This will be my last question for the evening and a while. I have worked my way through a 100 mark Java assessment and I am now stuck on my final two points. If anyone could help me out, it would be greatly appreciated. I am tired, feeling like a grade-A nub and just want it over with!
Study the two instance methods below and then select only the options that are correct.
public char[] methodA()
{
char[] alphas = {'s', 't', 'e', 'a', 'm'};
char temp = alphas[0];
int i = 0;
while (i < alphas.length - 1)//1
{
alphas[i] = alphas[i+1]; //2
i++;
}
alphas[alphas.length-1]=temp;
return alphas;
}
public char methodB()
{
char [] alphas = {'s','a','u','s','a','g','e'};
char first = alphas[0];
for (int i= 1; i < alphas.length; i++) //3
{
if (alphas[i] < first) //4
{
first = alphas[i];
}
}
return first;
}
-
The assignment statement labelled //2 will put a copy of the char
element one to the right of the current element in alphas into the
current element in alphas. -
The for loop header labelled //3 will be evaluated 7 times.
-
The if statement labelled //4 will update the value held by the
variable first if the value held in the current element in alphas
comes before the current value of first. -
The boolean condition in the line labelled //1 will evaluate to
false repeatedly until i takes the value 4. -
The returned value on invoking methodA is a char array containing
the values ‘t’, ‘e’, ‘a’, ‘m’ and ‘s’. -
The returned value from invoking methodB is the character ‘u’.
I believe 1 to be true. Not sure why.
I think 2 is false as the for loop is evaluated 6x, not 7.
Not sure on 3 or 4.
5 I got to be true
6 I got to be false.
If anyone can help I owe them a beer, a cookie and a cuddle!!
It is true because
alphas[i] = alphas[i+1]essentially will take element at positioniand and replace it with the next element in the array ati + 1(or another way to say it, its adjacent element).I believe this is false, the keyword here is
evaluated. A loop will evaluate to the stopping point, check the exiting condition, then kick out. So it will finish evaluating 1, 2, 3, 4, 5, 6, 7 <— evaluates the value and kicksThis will be true. the expression
if (alphas[i] < first)is asking if the value stored infirstis greater than the value inalphas[i]or inversely … ifalphas[i]is less thanfirst. This is essentially performing a max/min algorithm because the final number infirstwill be the smallest value in thealphasarray. In this case, the letters will be evaluated based upon their ascii values.This is false. The expression will evaluate to true until 4. If this were not the case then the question 1 would not be true because it would simply throw an
IndexOutOfBoundsException.This is true, because it is taking the adjacent char and placing it in the current position, then the line
alphas[alphas.length-1]=tempputs the first char into the last position. More specifically, after the while your array will look like this:{ t, e, a, m, m }then after the last line it will complete the set with{ t, e, a, m, s }The last one is false. Like I mentioned in question 3, it is essentially performing a min search. the character
uhas a greater valued ascii value than any other letter in the sequence.ais the lowest letter in the set.Great job on making an attempt.