Starting Out With Java: Early Objects 5th Ed. Find the Error 1. int[] collection
ID: 3726179 • Letter: S
Question
Starting Out With Java: Early Objects 5th Ed.
Find the Error
1. int[] collection = new int[-20];
2. int[] hours = 8, 12, 16;
3. int[] table = new int[10];
Scanner keyboard = new Scanner(System.in);
for (int x = 1; x <= 10; x++)
{
System.out.print("Enter the next value: ");
table[x] = keyboard.nextInt();
}
4. String[] names = { "George", "Susan" };
int totalLength = 0;
for (int i = 0; i < names.length(); i++)
totalLength += names[i].length;
5. String[] words = { "Hello", "Goodbye" };
System.out.println(words.toUpperCase())
Explanation / Answer
Ques 1. The size of the array can't be negative.
Ques 2. The array should be initialized within curly braces like
int[] hours = {8, 12, 16};
Ques 3. It will cause IndexOutOfBoundsException. The loop goes from 1 to 10.
When x becomes 10, error occurs.
Ques 4. The length of te array is calculated using
names.length
To get the String length, we use
totalLength += names[i].length();
Ques 5. The reference to the array is being used to call toUpperCase() function. The elements should be used to call the function
System.out.println(words[0].toUpperCase())
System.out.println(words[1].toUpperCase())
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.