Arrays in Java are similar in syntax to arrays in other languages such as C/C++ and Visual Basic. However, Java removes the feature of C/C++ whereby you can bypass the [] style accessing of elements and get under the hood using pointers. This capability in C/C++ , although powerful, makes it easy to write buggy software. Because Java does not support this direct manipulation of pointers, this source of bugs is removed.An array is a type of object that contains values called elements. This gives you a convenient bag or holder for a group of values that can be moved around a program, and allows you to access and change values as you need them. To give a trivial example you could create an array of Strings, each one containing the names of members in a sports team. The array can be passed into methods that need to access the names of each team member. If a new member joins the team, one of the old names can be modified to become that of the new member. This is much more convenient than having an arbitrary number of individual variables such as player1, player2, player3 etc Unlike variables which are accessed by a name, elements are accessed by numbers starting from zero. Because of this you can "walk" through an array, accessing each element in turn.Arrays are very much like objects, they are created with the new keyword, and have the methods of the great grandparent Object class. Arrays may store primitives or references to objects.Every element of an array must be of the same type The type of the elements of an array is decided when the array is declared. If you need a way of storing a group of elements of different types, you can use the collection classes which are a new feature in the Java2 exam, and are discussed in section 10. You can store an array of object references, which you can access, extract and use like any other object reference.
Java
Topic: Declarations and Access Control
Explain Arrays in java?
Browse random answers:
Explain Private in java?
Explain protected in java?
Explain public in java?
Explain default in java?
Explain final in java?
Explain abstract in java?
Explain transient in java?
Explain volatile in java?
Explain Arrays in java?
Explain Declaration of Arrays without allocation?
Example of Arrays?
How arrays know their own size?
Explain Java vs Visual Basic Arrays?
Explain Combined declaration and initialization of Arrays?
How can you re-size an array in a single statement whilst keeping the original contents?1) Use the setSize method of the Array class2) Use Util.setSize(int iNewSize)3) use the size() operator4) None of the above
You want to find out the value of the last element of an array. You write the following code. What will happen when you compile and run it?public class MyAr{public static void main(String argv[]){ int[] i = new int[5]; System.out.println(i[5]); }}1) Compilation and output of 02) Compilation and output of null3) Compilation and runtime Exception4) Compile time error
You want to loop through an array and stop when you come to the last element. Being a good Java programmer, and forgetting everything you ever knew about C/C++ you know that arrays contain information about their size. Which of the following can you use?1)myarray.length();2)myarray.length;3)myarray.size4)myarray.size();
Your boss is so pleased that you have written HelloWorld he she has given you a raise. She now puts you on an assignment to create a game of TicTacToe (or noughts and crosses as it was when I were a wee boy). You decide you need a multi dimensioned array to do this. Which of the following will do the job?1) int i =new int[3][3];2) int[] i =new int[3][3];3) int[][] i =new int[3][3];4) int i[3][3]=new int[][];
You want to find a more elegant way to populate your array than looping through with a for statement. Which of the following will do this? 1) myArray{ [1]="One"; [2]="Two"; [3]="Three"; end with 2)String s[5]=new String[] {"Zero","One","Two","Three","Four"};3)String s[]=new String[] {"Zero","One","Two","Three","Four"};4)String s[]=new String[]={"Zero","One","Two","Three","Four"};
What will happen when you attempt to compile and run the following code?public class Ardec{ public static void main(String argv[]){ Ardec ad = new Ardec(); ad.amethod(); } public void amethod(){ int ia1[]= {1,2,3}; int[] ia2 = {1,2,3}; int ia3[] = new int[] {1,2,3}; System.out.print(ia3.length); }}1) Compile time error, ia3 is not created correctly2) Compile time error, arrays do not have a length field3) Compilation but no output4) Compilation and output of 3
Which of these array declarations and instantiations are not legal?Select all valid Answers:(a) int []a[] = new int[4][4];(b) int a[][] = new int[4][4];(c) int a[][] = new int[][4];(d) int []a[] = new int[4][];(e) int[][] a = new int[4][4];(f) int [ ] [ ] a = new int[4][4];
Which of these array declarations and initialization are not legal?Select all valid Answers:(a) int []i[] = {{1,2}, {1}, {}, {1,2,3}};(b) int i[]= new int[2]{1, 2};(c) int i[][] = new int[][]{{1,2,3}, {4,5,6}};(d) int i[][] = {{1, 2}, new int[2]};(e) int i[4] = {1, 2, 3, 4};
Which of these statements are true?Select all valid Answers:(a) All classes must define a constructor.(b) A constructor can be declared private.(c) A constructor can declare a return value.(d) A constructor must initialize all the member variables of a class.(e) A constructor can access the non-static members of a class
Which of these statements are true?Select all valid Answers:(a) Transient variables will not be saved during serialization.(b) Constructors can be declared abstract.(c) The initial state of an array object constructed with the statement int a[] = new int[10] will depend on whether the variable a[] is a local variable, or a member variable of a class.(d) Any subclass of a class with an abstract method must implement a method body for that method.(e) Only static methods can access static members.
Which of the following are Java modifiers?(a) public(b) private(c) friendly(d) transient(e) vagrant
Which of the following method will not give you any error when placed on line no.3?Select one correct answer.1 interface i22 {3 //Here4 }(a) static void abc();(b) abstract void abc();(c) native void abc();(d) synchronized void abc();(e) final void abc();(f) private void abc();(g) protected void abc();