10 questions
Is String a primitive data type?
Yes
No
Both
Which method to use to find length of an array?
length
size
long
count
int[] myArray = {11, 22, 33, 44, 55};
which code is correct to print length of above int-array?
System.out.println(int[].myArray.length());
System.out.println(myArray.length);
System.out.println(myArray.length());
System.out.println(int[].myArray.size());
String[] friends = {"John", "Mike", "Samantha", "Monica"};
which code is correct to print "Samantha" using array functionality?
System.out.println(friends[2]);
System.out.println(friends[3]);
System.out.println(friends["Samantha"]);
System.out.println(friends{2});
Select correct code to create an array of "double" datatype of length 5.
double arr = double[5];
double{} arr = new double{5};
double[5] arr = new double[];
double[] arr = new double[5];
What is the correct relation between length and last-index of array?
last-index is always 1 more than the length
last-index is 1 less than the length (but not always)
last-index is always 1 less than the length
last-index is always equal to the length
Consider the following code segment.
int[]arr={1, 2, 3, 4, 5, 6, 7};
for(int k=3; k<arr.length-1;k++) {
Arr[k]=arr[k+1];
}
Which of the following represents the contents of arr as a result of executing the code segment?
{1, 2, 3, 4, 5, 6,7}
{1, 2, 3, 5, 6,7}
{1, 2, 3, 5, 6, 7,7}
{1, 2, 3, 5, 6, 7,8}
{2, 3, 4, 5, 6, 7,7}
A two-dimensional array arr is to be created with the following contents.
boolean[][] arr = {{false, true,false},
{false, false, true}};
Which of the following code segments can be used to correctly create and initialize arr in another way?
I. boolean arr[][] = new boolean[2][3];
arr[0][1] =true;
arr[1][2] = true;
II. boolean arr[][] = newboolean[2][3];
arr[1][2] =true;
arr[2][3] = true;
III. boolean arr[][] = newboolean[3][2];
arr[0][1] =true;
arr[1][2] = true;
I only
II only
III only
I and II only
I and III only
Consider the following code segment.
int[] oldArray = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int[][] newArray = newint[3][3];
int row = 0; int col = 0;
for (int index = 0; index < oldArray.length; index++) { newArray[row][col] = oldArray[index];
row++;
if ((row % 3) == 0){ col++;
row = 0;
}
}
System.out.println(newArray[0][2]);
What is printed as a result of executing the code segment?
3
4
5
7
8
Consider the following code segment
int[][] arr = {{6, 2, 5, 7},{7, 6, 1, 2}};
for (int j = 0; j < arr.length;j++){
for (int k = 0; k < arr[0].length; k++){ if (arr[j][k] > j + k){
System.out.println("!");
}
}
}
How many times will "!" be printed when the code segment is executed?
5 times
6 times
2 times
3 times
8 times