Java Worksheet 2
Java Worksheet 2
III. Complete the following code to declare and initialize an array of characters with
values 'a', 'b', 'c' and print its length:
1. public class ArrayExample4 {
public static void main(String[] args) {
// Fill in the blanks
______ [] letters = { ___, ___, ___ };
2. Write the code to declare and initialize an array of doubles with values 1.1, 2.2, 3.3
and modify the second element:(array name : Numbers)
3. Write the code to declare an array of strings with 3 elements and assign values to
them.(Array name :Fruits)
IV. Predict the output of the following code:
1. public class PredictOutput3 {
public static void main(String[] args) {
char[] letters = {'a', 'b', 'c', 'd', 'e'};
for (int i = 0; i < letters.length; i += 2) {
System.out.print(letters[i] + " ");
}
}
}
2. public class PredictOutput2 {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
for (int i = fruits.length - 1; i >= 0; i--) {
System.out.print(fruits[i] + " ");
}
}
}