Question Type: Complete the Code
Question Body: Given the task of initializing an array with the names "armaan", "ganesh", "daksh", and "srinath" in Java, complete the code snippet below to correctly instantiate and initialize the array in a single line.
String[] names = _______________________________;
Correct Response:
String[] names = {"armaan", "ganesh", "daksh", "srinath"};
Explanation of the Correct Answer: To declare and initialize an array in a single line in Java, you use the curly braces {} with each element of the array separated by commas. This method provides a concise way to create an array with predefined elements. Here, the array names is properly declared as a String array and initialized with the values "armaan", "ganesh", "daksh", and "srinath".
Tip: Remember, in Java, arrays are fixed in size once created, and the syntax {} for initialization can only be used at the point of declaration.
Difficulty: Easy
Question Type: Multiple Choice
Question Body: Which of the following is the correct way to set a single value in the previously created names array to "aditya" at index 2 (considering indices start at 0)?
A. names.set(2, "aditya");
B. names[2] = "aditya";
C. Array.set(names, 2, "aditya");
D. names.put(2, "aditya");
Correct Response: B. names[2] = "aditya";
Explanation of the Correct Answer: In Java, individual elements of an array can be accessed and modified using the square bracket notation, where the index of the element you wish to change is specified inside the brackets, followed by the assignment operator = and the new value. Option B correctly shows how to assign the value "aditya" to the element at index 2 of the names array.
Tip: Option C is incorrect because Array.set is a method used in reflection and not for direct array manipulation in most common scenarios.
Difficulty: Easy
Question Type: Find the Error
Question Body: Examine the following Java code snippet. What change needs to be made to correctly instantiate and populate an array named students?
String[] students = new String[4];
students = {"armaan", "ganesh", "daksh", "srinath"};
Correct Response: The code snippet is attempting to reinitialize the students array with a new set of elements in a manner that is not allowed in Java. To fix this, you can either directly declare and initialize the array in one line without first declaring its size, or use individual assignment statements for each index.
Explanation of the Correct Answer: The correct way to initialize an array with specific values after declaration is by assigning values to each element individually or by using an array initializer at the moment of declaration. The attempt to reassign the array with {} syntax on a separate line from its declaration is invalid. Thus, the corrected code could either look like this:
String[] students = {"armaan", "ganesh", "daksh", "srinath"};
Or, if initializing after declaration:
String[] students = new String[4];
students[0] = "armaan";
students[1] = "ganesh";
students[2] = "daksh";
students[3] = "srinath";
Tip: Remember, {} initializer syntax can only be used at the point of variable declaration.
Difficulty: Medium