26. (3 x 2 marks) The following code snippets have either a syntax or logic erro
ID: 3573376 • Letter: 2
Question
26. (3 x 2 marks) The following code snippets have either a syntax or logic error. Find and correct the error(s) (you do not need to rewrite all of the code, just make the corrections clear). There is only one error per question and it is in the code that is shown to you.
a) double[] values = new double[3]; values[0] = 1; values[1] = 5; values[2] = 19; double result = Math.sqrt(values);
b) public static int getMax(int a, int b){ if( a > b ) return a; else System.out.println(b); }
c) Die[] dice = new Die[5]; dice[0].roll(); int val = dice[0].sideUp();
27. (2 x 3 marks) What is the output of the following code snippets? Pay attention to the bold lines.
a. public static void main(String[] args){ double x = 5.0; System.out.println(x); multiply(x); System.out.println(x); } public static void multiply(int x){ x = x * 10; System.out.println(x); }
b. public static void main(String[] args){ int[] x = {3,5,7}; System.out.println(x[0]); multiply(x); System.out.println(x[0]); } public static void multiply(int[] x){ for(int i = 0; i
Explanation / Answer
26 a)we cannot pass array as argument to sqrt method.just we can pass single value like values[0] like that.
if want square roor of all values you can write for loop.
26b)public static int getMax(int a,int b)
{
if(a>b)
return a;
else
return b;
}
26c)we are calling method it return some value that means you have to pass arguments to that object.
27a)shows error.incompatible types of 'x'.x is declared as double but passing to method as integer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.