ExamVeda
Login
Home
41
What would be the result of attempting to compile and run the following code?
public class HelloWorld{
      public static void main(String[] args){
            double[] x = new double[]{1, 2, 3};
            System.out.println("Value is " + x[1]);
      }
}
Discuss
Answer & Solution
Answer: Option D
Solution:

new double[]{1, 2, 3} is correct. This is the syntax I have not covered in this edition, but will be covered in the future edition. In this question, double[] x = new double[]{1, 2, 3} is equivalent to double[] x = {1, 2, 3};

Array mcq solution image
42
Which will legally declare, construct, and initialize an array?
Discuss
Answer & Solution
Answer: Option D
Solution:

The only legal array declaration and assignment statement is Option D
Option A is wrong because it initializes an int array with String literals.
Option B is wrong because it uses something other than curly braces for the initialization.
Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array.

43
What will be the output of the program?
public class Test{
      public static void main(String [] args){
            String s1 = args[1];
            String s2 = args[2];
            String s3 = args[3];
            String s4 = args[4];
            System.out.print(" args[2] = " + s2);
      }
}

and the command-line invocation is C:Java> java Test 1 2 3 4
Discuss
Answer & Solution
Answer: Option D
Solution:

An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of the bounds. The exception thrown is the ArrayIndexOutOfBoundsException.

44
What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
Discuss
Answer & Solution
Answer: Option B
Solution:
Here's an explanation of the code:

1. The array a is initialized with values: {0, 2, 4, 1, 3}.
2. The for loop iterates over each element of the array using the variable i.
3. Inside the loop, the value of a[i] is updated using the expression a[(a[i] + 3) % a.length].
  (i) For i = 0, a[i] is 0. So, a[(0 + 3) % 5] becomes a[3 % 5] which is a[3] (value 1).
 (ii) For i = 1, a[i] is 2. So, a[(2 + 3) % 5] becomes a[5 % 5] which is a[0] (value 0).
 (iii) For i = 2, a[i] is 4. So, a[(4 + 3) % 5] becomes a[7 % 5] which is a[2] (value 4).
 (iv) For i = 3, a[i] is 1. So, a[(1 + 3) % 5] becomes a[4 % 5] which is a[4] (value 3).
 (v) For i = 4, a[i] is 3. So, a[(3 + 3) % 5] becomes a[6 % 5] which is a[1] (value 2).
4. After the loop finishes, the modified array a becomes {0, 1, 4, 3, 2}.
5. Finally, the code prints the value of a[1] using System.out.println(a[1]), which outputs 1.

Therefore, the output of the code is 1.
45
Predict the output:
public class Test{
public static void main(String... args){
            int[] index = new int[5];
            System.out.println(index instanceof Object);
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

An array is always an instance of Object.

46
Which of these operators is used to allocate memory to array variable in Java?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
47
Which of these is an incorrect array declaration?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
48
What will be the output of the following Java code?
int arr[] = new int [5];
System.out.print(arr);
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
49
What will be the output of the following Java code?
class array_output 
{
    public static void main(String args[]) 
    {
        int array_variable [] = new int[10];
  for (int i = 0; i < 10; ++i) 
        {
            array_variable[i] = i;
            System.out.print(array_variable[i] + " ");
            i++;
        }
    } 
}
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
50
What will be the output of the following Java code?
class evaluate 
{
    public static void main(String args[]) 
        {
      int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
      int n = 6;
            n = arr[arr[n] / 2];
      System.out.println(arr[n] / 2);
        } 
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board