How does join work? a. You write separator.join('a', 'b', 'c', 'd', ...) where 'a', 'b', 'c', 'd' can be replaced with other str
ings, but isn't in a list. b. The separator must be a single character, and you use list.join(separator). c. You use separator.join(a_list) where a_list is a list of strings. d. You write a_list.join(separator) where a_list is a list of strings, and separator is a string.
c. You use separator.join(a_list) where a_list is a list of strings.
Explanation:
The join() is an in-built string method which returns a string concatenated with the elements of an iterable. It concatenates each element of an iterable (such as list, string and tuple) to the string and returns the concatenated string.
The syntax of join() is:
string.join(iterable)
From the above syntax, the string usually mean a separator and the iterable will be a string or list or tuple.
The answer is C.
c. You use separator.join(a_list) where a_list is a list of strings.
It is not A because the iterable could be a string. It is not D because the separator is outside not in the bracket.
//This code is designed to create and populate an array with
//Ten Integer values
Scanner in = new Scanner(System.in);
//Create the array
int [] intArray = new int [10];
//populating the array
for(int i =0; i<=10; i++){
System.out.print("Enter the elements: ");
intArray[i]= in.nextInt();
}
//Print the Values in the array
System.out.println(Arrays.toString(intArray));
}
}
Explanation:
The above code looks like it will run successfully and give desired output.
But a bug has been introduced on line 9. This will lead to an exception called ArrayIndexOutOfBoundsException.
The reason for this is since the array is of length 10, creating a for loop from 0-10 will amount to 11 values, and the attempt to access index 10 for the eleventh element will be illegal
One way to fix this bug is to change the for statement to start at 1.