latest articles

Recent Posts Widget

Arrays (QCM 1 Answered)


01.Correct Option is : C
Explanation:
The correct syntax to access any element within an array is to use the square brackets - [ ]. Thus, to access the first element in an array, you would use array[0]. For a multi dimensional array, to reach an individual item, you need to specify index for each dimension. For example, since matrix is a two dimensional array, matrix is an array of array and matrix[0] will give you the first array of the arrays. matrix[0][0] will give you the first element of the first array of the arrays.
02.Correct Options are : A B E
Explanation:
There is a subtle difference between: int[] i; and int i[]; although in both the
cases, i is an array of integers.
The difference is if you declare multiple variables in the same statement such as:
int[] i, j; and int i[], j;, j is not of the same type in the two cases.
In the first case, j is an array of integers while in the second case, j is just an integer.
Therefore, in this question:
array1 is an array of int
array2, array3, array4, and array5 are arrays of int arrays
Therefore, option 1, 2 and 5 are valid.
03.Correct Option is : C
Explanation:
arr[0][1][2] => [0] = { { "a", "b" , "c"}, { "d", "e", null } }, [1] = { "d", "e", null }
and [2] = null.
So it will print null.
04.Correct Option is : A
Explanation:
In Java, array numbering starts from 0. So in this case, twoD is an array containing 3 other arrays.
twoD[0] is { 1, 2, 3} , twoD[1] is { 4, 5, 6, 7}, and twoD[2] is { 8, 9, 10}.
Thus, twoD[1].length is 4 and twoD[1][2] is the third element in { 4, 5, 6, 7}, which is 6.
In Java, arrays are just like regular Objects and arrays of different types have different class names. For example, the class name of an int[] is [I and the class name for int[][] is [[I.

For array classes, the isArray() method of a Class returns true. For example,
twoD.getClass().isArray() will return true.
Back to Questions
05.Correct Option is : C

Explanation:
java and classname are not part of the args array. So tom gets "111", dick gets "222" and harry gets "333".
06.Correct Option is : D

Explanation:
The statement iA[i] = i = 30 ; will be processed as follows:
iA[i] = i = 30; => iA[0] = i = 30 ; => i = 30; iA[0] = i ; => iA[0] = 30 ;
Here is what JLS says on this:
1 Evaluate Left-Hand Operand First
2 Evaluate Operands before Operation
3 Evaluation Respects Parentheses and Precedence
4 Argument Lists are Evaluated Left-to-Right
For Arrays: First, the dimension expressions are evaluated, left-to-right. If any of the expression evaluations completes abruptly, the expressions to the right of it are not
evaluated.
07.Correct Option is : B

Explanation:
Arrays are proper objects (i.e. iArr instanceof Object returns true) and Object references are passed by value (so effectively, it seems as though objects are being passed by reference).
So the value of reference of iArr is passed to the method incr(int[] i); This method changes the actual value of the int element at 0.
08.Correct Option is : E

Explanation:
In an array creation expression, there may be one or more dimension expressions, each within brackets. Each dimension expression is fully evaluated before any part of any dimension expression to its right. The first dimension is calculated as 4 before the second dimension expression sets 'i' to 3.
Note that if evaluation of a dimension expression completes abruptly, no part of any dimension expression to its right will appear to have been evaluated.
09.Correct Option is : A

Explanation:
All the arrays are initialized to contain the default values of their type. This means,
int[] iA = new int[10]; will contain 10 integers with a value of 0.
Object[] oA = new Object[10]; will contain 10 object references pointing to null.
boolean[] bA = new boolean[10] will contain 10 booleans of value false.
So, as bA[0] is false, the if condition fails and str remains 111.
10.Correct Option is : D

Explanation:
First, "a" and "b" are appended to an empty list. Next, "c" is added between "1" and "2".
Then a new list s2 is created using the sublist view allowing access to elements from index 1 to index 1(exclusive) (i.e. no elements ).
Now, s2 is added to s1.
So s1 remains :a, c, b
11.Correct Option is : C

Explanation:
It will throw a NullPointerException for ia[1][0] because ia[1] is null.
Note that null is not same as having less number of elements in an array than expected.
If you try to access ia[2][0], it would have thrown ArrayIndexOutOfBoundsException because the length of ia is only 2 and so ia[2] tries to access an element out of that range. ia[2] is not null, it simply does not exist.
12.Correct Option is : C

Explanation:
FYI, All types of arrays are objects. i.e. intArr instanceof Object is true.
13.Correct Option is : C

Explanation:
In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.
In the expression a[(a=b)[3]], the expression a is fully evaluated before the expression (a=b)[3]; this means that the original value of a is fetched and remembered while the expression (a=b)[3] is evaluated. This array referenced by the original value of a is then subscripted by a value that is element 3 of another array (possibly the same array) that was referenced by b and is now also referenced by a. So, it is actually a[0] = 1.
Note that if evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.
14.Correct Option is : E

Explanation:
Elements of Arrays of primitive types are initialized to their default value ( i.e. 0 for integral types, 0.0 for float/double and false for boolean)
Elements of Arrays of non-primitive types are initialized to null.
15.Correct Options are : C E

Explanation:
The line 1 will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference.
The cast in line 2 is needed because a superclass reference is assigned to a subclass reference variable. And this works at runtime because the object referenced to by a is actually of an array of B.
Now, the cast at line 3 tells the compiler not to worry, that I'm a good programmer and I know what I am doing and the object referenced by the super class reference (a1) will actually be of class B at run time. So there is no compile time error. But at run time, this fails because the actual object is not an array of B but is an array of A.
16.Correct Options are : B D

Explanation:
There are no commas separating the strings.
Back to Questions
17.Correct Option is : E
Explanation:
A. An array can dynamically grow in size. Arrays cannot grow in size once created. ArrayLists can do that.
B. Arrays can be created only for primitive types. You can have arrays for objects also. For example:
Object[] objArray = new Object[4];
String[] arrayOfStrings = { "a", "b" };
C. Every array has a built in property named 'size' which tells you the number of elements in the array. It is named length and not size. ArrayList has a method named size() that returns the number of elements in the ArrayList.
String[] sa = { "a", "b" };
int k = sa.length; //k will be assigned a value of 2.
ArrayList al = new ArrayList();
int k = al.size(); //k will be assigned a value of 0.
D. Every array has in implicit method named 'length' which tells you the number of elements in the array.
E. Element indexing starts at 0.
18.Correct Option is : C

Explanation:
Following are the default values that instance variables are initialized with if not
initialized explicitly:
types (byte, short, char, int, long, float, double) to 0 ( or 0.0 ).
All Object types to null.
boolean to false.
19.Correct Options are : A C

Explanation:
Note that an array of integers IS an Object :
Object obj = new int[]{ 1, 2, 3 }; // is valid.
But it is not an Array of objects.
Object[] o = new int[10]; // is not valid.
Difference between the placement of square brackets:
int[] i, j; //here i and j are both array of integers.
int i[], j; //here only i is an array of integers. j is just an integer.
20.Correct Option is : E

Explanation:
1 and 3 declare a two dimensional array alright but they create the array of size 2, 3. And cA[2][3] means we need an array of size 3, 4 because the numbering starts from 0.
4 : You cannot put array size information on LHS.
5 : This is a one dimensional array and that too of strings. Note that a java String is not equivalent to 1 dimensional array of chars.
This leaves us with only one choice 2.
21.Correct Options are : B E

Explanation:
If you explicitly specify the members then you can't give the size. So option 2 is wrong.
The size of the array is never given during the declaration of an array reference. So option 5 is wrong.
The size of an array is always associated with the array instance, not the array reference. 
Back to Questions
22.Correct Option is : C

Explanation:
Even though the line x[0] = 10; will throw
java.lang.ArrayIndexOutOfBoundsException, JVM will wrap it and rethrow
java.lang.ExceptionInInitializerError.
23.Correct Option is : C

Explanation:
Note that whenever you create an array all of its elements are automatically given defaults values. Numeric types are initialized to 0, objects are initialized to null, and booleans to false.
So if you have, float[ ] f = new float[3]; f[0], f[1] and f[2] will all be 0.0. if you have Object[ ] o = new String[3]; o[0], o[1] and o[2] will all be null.
In this case, b1[0] and b1[1] are false. whereas b2[0] and b2[1] are true and false.
So the answer is false and true.
24.Correct Option is : A

Explanation:
If the array reference expression produces null instead of a reference to an array, then a NullPointerException is thrown at runtime, but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.
This means, first index = 2 will be executed, which assigns 2 to index. After that null[2] is executed, which throws a NullPointerException. But this exception is caught by the catch  block, which prints nothing. So it seems like NullPointerException is not thrown but it actually is.
In other words, the embedded assignment of 2 to index occurs before the check for
array reference produced by getArray().
In an array access, the expression to the left of the brackets appears to be fully
evaluated before any part of the expression within the brackets is evaluated. Note that
if evaluation of the expression to the left of the brackets completes abruptly, no part of
the expression within the brackets will appear to have been evaluated.
25.Correct Option is : A

Explanation:
Example: When a Java program is run without any program arguments, the String[]
args argument to main() gets an array of length Zero.
26.Correct Option is : B

Explanation:
When you create an array of Objects ( here, Strings) all the elements are initialized to
null. So in the line 3, null is assigned to myStr.
Note that. empty string is "" ( String str = ""; ) and is not same as null.
27.Correct Options are : B C D E
A. String[] sA = new String[1] { "aaa"};
Array size cannot be given here as the array is being initialized in the declaration.
28.Correct Options are : A B D E
Explanation
A. int[ ] a[ ] = new int [5][4] ;
This will create an array of length 5. Each element of this array will be an array of 4 ints.
B. int a[ ][ ] = new int [5][4] ; 
This will create an array of length 5. Each element of this array will be an array of 4 ints.
C. int a[ ][ ] = new int [ ][4] ;
The statement int[ ][4] will not compile, because the dimensions must be created from left to right.
D. int[ ] a[ ] = new int[4][ ] ;
This will create an array of length 4. Each element of this array will be null. But you can assign an array of ints of any length to any of the elements. For example:
a[0] = new int[10];//valid
a[1] = new int[4];//valid
a[2] = new int[]; //invalid because you must specify the length
a[3] = new Object[] //invalid because a[3] can only refer to an array of ints.
This shows that while creating a one dimensional array, the length must be specified but while creating  multidimensional arrays, the length of the last dimension can be left unspecified. Further, the length of multiple higher dimensions after the first one can also be left unspecified if none of the dimensions are specified after it. So for example, a[][][][] = new int[4][3][3][5]; is same as a[][][][] = new int[4][][][]; (Note that the first dimension must be specified.)
Thus, multidimensional arrays do not have to be symmetrical. 
E. int[ ][ ] a = new int[5][4] ;
This will create an array of length 5. Each element of this array will be an array of 4 ints.
 The [] notation can be placed both before and after the variable name in an array declaration.
int[] ia, ba; // here ia and ba both are int arrays.
int ia[], ba; //here only ia is int array and ba is an int.
Multidimensional arrays are created by creating arrays that can contain references to other arrays .
29.Correct Options are : A C D
Explanation
A. saveObject( new ArrayList() );
Because an ArrayList is a List.
B. Collection c = new ArrayList(); saveObject( c );
saveObject() cannot accept c because c is declared of type Collection, which is a superclass of List, but the saveObject() method expects a List.
C. List l = new ArrayList(); saveObject(l);
D. saveObject(null);
In this case prevObj will be set to null.
E. saveObject(0); //The argument is the number zero and not the letter o 0 is an int, which means it is a primitive. So it will be boxed into an Integer object when you pass it to a method that expects an Object. However, Integer cannot be passed to a method that expects a List. Therefore, this option is not valid. Had the method bean saveObject(Object obj), it would have been valid because an Integer is an Object.
Back to Questions
30.Correct Options are : A B E
 Explanation:
A. ArrayList extends java.util.AbstractList.
ArrayList is a subclass of AbstractList.
java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractList<E>
- java.util.ArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
B. It allows you to access its elements in random order.
This is true because you can directly access any element using get(index)
method. (This is unlike a LinkedList, in which you have to go through all the
elements occuring before Nth element before you can access the Nth element.)
C. You must specify the class of objects you want to store in ArrayList when you
declare a variable of type ArrayList.
This is not true because you can still use non-generic form. For example, instead
of using
ArrayList<String> listOfStrings;
you can use:
ArrayList listOfStrings;
Of course, if you use non generic version, you will lose the compile time type
checking.
D. ArrayList does not implement RandomAccess.
It does.
RandomAccess is a marker interface used by List implementations to indicate
that they support fast (generally constant time) random access. The primary
purpose of this interface is to allow generic algorithms to alter their behavior to
provide good performance when applied to either random or sequential access
lists.
E. You can sort its elements using Collections.sort() method.
An ArrayList is a List so you can use it where ever a List is required. This
include Collections methods such as sort, reverse, and shuffle.
31.Correct Options are : B C E

Explanation:
A. Standard JDK provides no subclasses of ArrayList.
It does.
Direct Known Subclasses:
AttributeList, RoleList, RoleUnresolvedList
B. You cannot store primitives in an ArrayList.
This is true because only Objects can be stored in it.
C. It allows constant time access to all its elements.
This is true because it implements java.util.RandomAccess interface, which is a
marker interface that signifies that you can directly access any element of this
collection. This also implies that it takes the same amount of time to access any
element.
(Compare that with a LinkedList, which takes more time to access the last
element than the first element.)
D. ArrayList cannot resize dynamically if you add more number of elements than its
capacity.
It does resize dynamically. Compare that to an array, which cannot be resized
once created.
E. An ArrayList is backed by an array.
This is true. The elements are actually stored in an array and that is why is it
called an ArrayList.
(The expression "backed by an array" means that the implementation of
ArrayList actually uses an array to store elements.)
Explanation:
ArrayList is a subclass of AbstractList.
java.lang.Object
- java.util.AbstractCollection<E>
- java.util.AbstractList<E>
- java.util.ArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
Direct Known Subclasses:
AttributeList, RoleList, RoleUnresolvedList
32.Correct Options are : B E
Explanation:
A. daaa[0] = d;
daaa[0] should be a 2 dimensional array because daaa is a 3 dimensional
array.
B. daaa[0] = daa;
C. daaa[0] = daa[0];
daaa[0] should be a 2 dimensional array while daa[0] is a one dimensional
array.
D. daa[1][1] = d;
daa[1][1] will cause an ArrayIndexOutofBoundsException because daa's
length is only 1 and the indexing starts from 0. To access the first element, you
should use daa[0][0].
E. daa = daaa[0]
Back to Questions
33.Correct Option is : A
Explanation:
 
A. You do not have to worry about the size of the ArrayList while inserting elements. An ArrayList resized dynamically at run time as per the situation. An array cannot be resized once created. This reduces the amount of boiler plate code that is required to do the same task using an array.
B. It consumes less memory space. Because of additional internal data structure and pointers, it actually consumes a little more memory than an array.
C. You do not have to worry about thread safety. An ArrayList, just like an array is not thread safe. If you have multiple threads trying to add and remove elements from an ArrayList, you have to write additional code to ensure thread safety.
D. It allows you to write type safe code. Since ArrayList is a generics enabled class, it helps you write type safe code. For example, if you have:
ArrayList<String> al = new ArrayList<>();
al.add(new Integer(10)); will not compile because the compiler knows that al can only contain Strings.
However, this is not an advantage over an array because arrays are also type safe. For example, if you have:
String[] sa = new String[10];
you cannot do sa[0] = new Integer(10); either. But you can do Object[] oa = sa; and oa[0] = new Integer(10); This
will compile fine but will fail at runtime. This is a hole in the type safety provided by arrays.
Back to Questions
34.Correct Option is : C
Explanation:
 
A. It will not compile.
B. It will throw a NullPointerException at run time.
C. It will throw an IndexOutOfBoundsException at run time. size() method of ArrayList returns the number of elements. Here, it returns 2. Since numbering in ArrayList starts with 0. al.get(2) will cause an IndexOutOfBoundsException to be thrown because only 0 and 1 are valid indexes for a list of size 2.
D. 222

Aucun commentaire:

Enregistrer un commentaire