The course is part of this learning path
In this course, we'll learn about Arrays in Java.
Learning Objectives
- What is an Array?
- Declaration and Initialization
- Sorting Arrays
- Searching Arrays
- Multi-Dimensional Arrays
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
Prerequisites
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there, in this video we'll look at sample questions on Array and their solutions. If you're ready, let's start. In this question, an array of type int is defined and the element in index 2 of this array is wanted to be printed to the console. We are asked what the output will be. Pause the video here and try to solve it yourself. We'll do the solution together on the next slide. Yes, if you selected option E, you have given the correct answer, because this code won't compile. Let's see why this code cannot compile. Notice that in line 4, after the new int statement, after the equal sign, both the size of the array is specified and the array is initialized, but this syntax is incorrect and causes a compiler error because it cannot define dimension expressions when an array initializer is provided. Therefore, we get a compiler error. So, the answer is option C.
Let's move on to the next question. Yes, in this question, an array of type string is created and this array is initialized. We're asked what code we should write in the missing part to print the third element of this array to the console. Pause the video here and try to answer it yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you selected option B. Now let's answer the question together. The third element of the array is wanted to be printed to the console. Therefore, let's first detect the third element of the array. If you notice, the question does not say element in the third index, it says third element. So, we want to look at which is the third element in the array. The first element of the array is Volvo, the second element is BMW, the third element is Ford, and the fourth element is Mazda. In other words, as the third element, we must print the Ford element on the console.
In order to print the Ford element to the console, we need to use the index number of this element. So, the index number of the Ford element is two. Therefore, in order to print the Ford element to the console, we must write cars[2] in the parentheses in the print method. So, the answer is option B. Actually, the reason I made this explanation was so that you can easily distinguish the concept of index number and order of the element in array, because you can mark this question directly. If you remember, the index number was one less than the elements order in the array. So, the index number of the first element is 0, the second element is 1, and the third element is 2. Therefore, you can directly select option B. So, for this question, it doesn't matter which third element is, the important thing is, which code
the third element can be accessed. Now let's move on to the next question. This question is about getting the size of arrays. An array is given in line 4 and the size of this array is wanted to be printed to the console. For this, we're asked which codes should be written in parentheses in the print method. If you remember, we were getting the length of the arrays with the length property. But the point to note here is that the length expression is not a method, so there shouldn't be any parentheses at the end of the length expression. Therefore, the answer is option C. Let's move on to the next question. Yes, this question is about sorting arrays.
An array named myString has been created and passed as a parameter to the sort method of the arrays class. Then the elements of the array are printed to the console. We are asked what the output will be. Here, stop the video and try to solve it yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you chose option E. Now let's look at why there is option E. Notice that the array type is string, but there are also numeric expressions. That's why you have to be careful while sorting. First of all, I should say that if the elements in a string array consists of textual and numeric expressions, the numeric expressions are ordered before the text expressions. Because a character-based control is made in the ordering of string arrays, the decimal value of the numbers in the ASCII table is smaller than the decimal value of the letters.
Also, uppercase letters have smaller decimal values than lowercase letters. Therefore, when sorting an array in this way, first the numeric elements, then the elements starting with a capital letter, and finally elements starting with a lowercase letter are sorted. So, the first three elements after the sort operation will consist of elements 15, 9, and 109. The fourth element will be Dog, because it starts with a capital letter. The last element will also be cat, because it starts with a lowercase letter. So, the last elements will be Dog and cat respectively. Now let's look at the order of the first three elements. When comparing the numbers 15, 9, and 109 by character, we first look at the first digits from the left. So, we need to compare the numbers 1, 9, and 1. Since 9 is greater than 1, the largest element in the order of these three elements will be 9. Now, let's look at 15 and 109. The first digits from the left are the same.
When we look at the second digit from the left, 0 is less than 5. So, the smallest elements will be 109. As a result, when the array myString is sorted using the sort method, the elements will be 109, 15, 9, Dog, and cat respectively. So, the answer is option E. Let's move on to the next question. This question is about using the binary search method. An int array named ages is created and this array is initialized. Then the binary search method of the arrays class is used. The question is, what will be the output of this code? Pause the video here and try to figure it out yourself. I'll be giving the answer on the next slide. Yes, your answer is correct if you selected option B. Now let's solve the question together. When using the binary search method, the first thing we had to look at was whether the given array was sorted.
If you notice, the elements of the ages array are sorted, therefore the result is predictable. The second thing we need to look at now is whether the element being queried is an element of the array. Notice that 6 is an element of this array. Do not confuse this here. The second parameter of the binary search method is the element to be queried, not the index number. The index number is given to us as the output. Do not forget this. As a result, the array is a sorted array and 6 is an element of this array. So, the output will be the index number of 6. Notice that 6 is the fourth element of this array and its index number is 3. So, the answer is option B.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.