Arrays start at index 0, so nums[0]=1, nums[1]=2, nums[2]=3, nums[3]=4.
Step 2: Identify the printed element
The code prints nums[2], which is the third element, value 3.
Final Answer:
3 -> Option C
Quick Check:
Index 2 value = 3 [OK]
Hint: Remember array index starts at 0 in Java [OK]
Common Mistakes:
Confusing index 2 with the second element
Off-by-one errors in indexing
Misreading array initialization
4. Identify the error in the following Java array declaration:
int arr[5];
medium
A. Array size cannot be specified in declaration without initialization
B. Missing semicolon at the end
C. Array must be declared as int arr = new int[5];
D. Array size must be a variable, not a number
Solution
Step 1: Understand Java array declaration rules
In Java, you cannot specify the size inside square brackets during declaration without initialization.
Step 2: Analyze the given code
int arr[5]; tries to declare an array with size 5 but does not initialize it, which is invalid syntax.
Final Answer:
Array size cannot be specified in declaration without initialization -> Option A
Quick Check:
Size only with new keyword = correct [OK]
Hint: Specify size only when using new keyword [OK]
Common Mistakes:
Trying to declare size in brackets without new
Confusing Java with C/C++ array syntax
Forgetting to initialize array after declaration
5. How can you declare and initialize a String array with the values "apple", "banana", and "cherry" in one line in Java?
hard
A. String[] fruits = new String{"apple", "banana", "cherry"};
B. String[] fruits = new String[]{"apple", "banana", "cherry"};
C. String fruits[] = new String[3]{"apple", "banana", "cherry"};
D. String fruits = {"apple", "banana", "cherry"};
Solution
Step 1: Understand array initialization with values
Java allows two ways to initialize arrays with values: shorthand without new keyword only at declaration, or with new keyword and braces.
Step 2: Check each option
String[] fruits = new String[]{"apple", "banana", "cherry"}; is correct. Other options misuse the new keyword by missing brackets after the type, specifying size with initializer list (invalid syntax), or missing array declaration brackets.
Final Answer:
String[] fruits = new String[]{"apple", "banana", "cherry"}; -> Option B
Quick Check:
Use new Type[]{values} for inline initialization [OK]
Hint: Use new Type[]{values} or shorthand only at declaration [OK]
Common Mistakes:
Mixing shorthand and new keyword incorrectly
Missing square brackets in declaration
Assigning array values without brackets
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.