Will thumbs up if answered all, Greatly appreciated, thank you! 26. (3 x 2 marks
ID: 3899723 • Letter: W
Question
Will thumbs up if answered all,Greatly appreciated, thank you!
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) doubletl values -new doublet31 double result-Math.sqrttvalees) b) publia static int getMax (int s, int b) if( a > b ) return a; else System.out.println (b)a c) Diet dice -e Die151 dice [01.roll int val -dice 01.aLdeUpO 27. (2 x3 marks) What is the output of the following code snippets? Pay attention to the bold lines public static void main(Stringll az5) double x a. System.out printin() System.out.printin(x) public atatic vold maltiply (int x) Systen.out.printin() b. pablic statie void main(String11 args) System.out printin(x[01) System.out.printin([01) public atatie void multiply(int x) for (int 1- x.lengtha 1+) nt-3,5,7 x(-*110 System.out printin(x[o1
Explanation / Answer
26. b
either you return the values or you print the greatest element.. the error in that program is you did the both. the corrected answer is:
import java.util.*;
class Gfg
{
public static int getMax(int a, int b)
{
if(a>b)
return a;
else
return b;
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.print("the greatest element is");
int answer=getMax(a,b);
System.out.println(answer);
}
}
input:
10
20
output:
the greatest element is: 20
26.a
either u can do like this:
import java.util.*;
class Gfg
{
public static void main(String args[]){
double [] values={16,25,36};
double result=Math.sqrt(values[0]);
System.out.println(result);
}
}
output: 4.0
or u can do like this
import java.util.*;
class Gfg
{
public static void main(String args[]){
double [] values={16,25,36};
for(int i=0;i<3;i++)
{
double result=Math.sqrt(values[i]);
System.out.println(result);
}
}
}
output:
4.0
5.0
6.0
27.a
import java.util.*;
class Gfg
{
public static void multiply(double x)
{
x=x*10;
System.out.println(x);
}
public static void main(String args[]){
double x=5.0;
System.out.println(x);
multiply(x);
}
}
output: 5.0
50.0
27.b
import java.util.*;
class Gfg
{
public static void multiply(int x[])
{
for(int i=0;i<3;i++)
{
x[i]=x[i]*10;
System.out.println(x[i]);
}
}
public static void main(String args[]){
int [] x={3,5,7};
for(int i=0;i<3;i++)
{
System.out.println(x[i]);
}
multiply(x);
}
}
output;
3
5
7
30
50
70
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.