Knowee
Questions
Features
Study Tools

Given1. public class Program {2. public static void main(String[] args) {3. long [][] l2d;4. long [] l1d = {1,2,3};5. Object o = l1d;6. l2d = new long[3][3];7. l2d[0][0] = (long[])o;8. }9. }    What is the result?*Compilation fails due to an error on line 5.Compilation fails due to an error on line 6.Compilation succeeds and the code runs without exception.Compilation fails due to an error on line 7.Compilation succeeds and an exception is thrown at runtime.

Question

Given1. public class Program {2. public static void main(String[] args) {3. long [][] l2d;4. long [] l1d = {1,2,3};5. Object o = l1d;6. l2d = new long[3][3];7. l2d[0][0] = (long[])o;8. }9. }    What is the result?*Compilation fails due to an error on line 5.Compilation fails due to an error on line 6.Compilation succeeds and the code runs without exception.Compilation fails due to an error on line 7.Compilation succeeds and an exception is thrown at runtime.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The compilation fails due to an error on line 7. The reason is that you are trying to cast an object of type long[] to long, which is not allowed in Java. The correct way to assign the array would be l2d[0] = (long[])o; This would assign the entire 1D array to the first row of the 2D array.

Similar Questions

Consider the following program.public class Main {     public static void main(String args[]) {     String[] names = new String[2];     names[0] = "Alice";     names[1] = "Bob";     names[2] = "Charlie";     System.out.println(names[2]);     }}Which of the following is an output of the above program? a. Charlie b. Bob c. Alice d. Compilation Error

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

Given1. public class Program{2. public static void main(String[] args) {3.            4. for(int j = 0,k=5; j < k; k--) ;5. for(int j = 0; j++ < 3;) ;6. for(int i = 0; i < 5; i++, System.out.print(i + ".go ")) ;7. 8. }9. }What will be the result?*Compilation fails due to multiple errors.Compilation fails due to an error on line 6.0.go 1.go 2.go 3.go 4.go 5.goAn exception is thrown at runtime.1.go 2.go 3.go 4.go 5.go

The following code contains one compilation error, find it?public class Test { Test() { } // line 1 static void Test() { this(); } // line 2 public static void main(String[] args) { // line 3 Test(); // line 4 }}

Given1. class Program{2. public static void main(String args[]){3.  int x = (int)args[0];4.  System.out.print(x);5.  }6. }What is true?*Compilation fails due to multiple errorsIf we use command line invocation, java Program 10, the output will be 10.If we use command line invocation, java Program abc, An ClassCastException will be thrown.If we use command line invocation, java Ex, a ArrayIndexOutOfBoundsException will be thrown.Compilation fails due to error on line 3.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.