lummers
lummers

Reputation: 779

'if' and 'for' loop questions

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

  2. The for loop header labelled //3 will be evaluated 7 times.

  3. 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.

  4. The boolean condition in the line labelled //1 will evaluate to false repeatedly until i takes the value 4.

  5. The returned value on invoking methodA is a char array containing the values 't', 'e', 'a', 'm' and 's'.

  6. 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!!

Upvotes: 0

Views: 244

Answers (2)

Matthew Cox
Matthew Cox

Reputation: 13672

  1. It is true because alphas[i] = alphas[i+1] essentially will take element at position i and and replace it with the next element in the array at i + 1 (or another way to say it, its adjacent element).

  2. 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 kicks

  3. This will be true. the expression if (alphas[i] < first) is asking if the value stored in first is greater than the value in alphas[i] or inversely ... if alphas[i] is less than first. This is essentially performing a max/min algorithm because the final number in first will be the smallest value in the alphas array. In this case, the letters will be evaluated based upon their ascii values.

  4. 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.

  5. This is true, because it is taking the adjacent char and placing it in the current position, then the line alphas[alphas.length-1]=temp puts 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 }

  6. The last one is false. Like I mentioned in question 3, it is essentially performing a min search. the character u has a greater valued ascii value than any other letter in the sequence. a is the lowest letter in the set.

Great job on making an attempt.

Upvotes: 1

necromancer
necromancer

Reputation: 24641

1 is true (a[i+1] is to the right of a[i], so a[i] = a[i+1] copies the value on the right to the current)

2 is true because the loop condition is evaluated 7 times -- 6 times as true causing the body to be executed 6 times, and then 1 time as false to break the loop.

3 sounds true, a value like 'b' is considered to "come before" a value like 'd' (comparing numeric ascii codes)

4 is true because the condition 4 < (5 - 1) is false

5 is true as you think

6 is false - should return 'a'

please skip the cuddle!

Upvotes: 0

Related Questions