ExamCompetition Forum Question Papers Ask A Question Mock Test Learn & Earn Sign Up Login Menu



26 vote

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]); }}

Asked on by | Votes 26 | Views: 1324 | Tags: computer science     | java programming     | array     | Add Bounty

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]); }}

A).  The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.

B).  The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};

C).  The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};

D).  The program compiles and runs fine and the output


Share on Facebook      Share on Whatsapp       Share on Twitter




1 answers

30 vote
Answered by on | Votes 30 |

 The program compiles and runs fine and the output



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};

Join Telegram Group




Answer This Question

Name:
Email:
Answer :
Sum of (1+4)
Submit:

Other Questions

1. What is the result of compiling and running the following code?public class Test{ public static void main(String[] args){ int[] a = new int[0]; System.out.print(a.length); }}

2. What is output of the following code:public class Test{ public static void main(String[] args){ int[] x = {120, 200, 016 }; for(int i = 0; i < x.length; i++) System.out.print(x[i] + " "); }}

3. Analyze the following code and choose the correct answer.int[] arr = new int[5];arr = new int[6];

4. Determine output:public class Test{ public static void main(String[] args){ int[] x = {1, 2, 3, 4}; int[] y = x; x = new int[2]; for(int i = 0; i < x.length; i++) System.out.print(y[i] + " "); }}

5. 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

6. Which will legally declare, construct, and initialize an array?

7. What will be the output?public class Test{ public static void main(String[] args){ int[] x = new int[3]; System.out.println("x[0] is " + x[0]); }}

8. When you pass an array to a method, the method receives ________ .

9. 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]); }}

10. What is the output of the following code?public class Test{ public static void main(String args[]){ double[] myList = {1, 5, 5, 5, 5, 1}; double max = myList[0]; int indexOfMax = 0; for(int i = 1; i < myList.length; i++){ if(myList[i] > max){ max = myList[i]; indexOfMax = i; } }...