Making a method abstract means the method: A. can be overridden by child class B
ID: 3760032 • Letter: M
Question
Making a method abstract means the method:
A.can be overridden by child class
B.cannot have arguments
C.must have overloaded method
Assume a public method genderateOutput is an abstract method is class Product, and this method returns a string value and has a parameter in the type of string. Please write abstract method. Do no write code to define the whole Product class.
Given the following program, Test, whether method m must be defined as static and why?
If the program is executed, what is the output of the program?
Public class Test
{
Public static void main (String arg[])
{
Int n[] = {-1, 1};
Int sum = n[0] +n{1};
M(n, sum);
System.out.println(“n[0] = “ + n[0]);
System.out.println(“n[1] = “ + n[1]);
System.out.println(“sum = “ + sum);
}
Public static void m(int k[], int sum)
{
For (int i = 0; i < k.lengh; i++)
{
k[i] = k[i] +1
sum = sum + k[i]
}
}
}
Explanation / Answer
There are some mistakes in your program if you rectify then you will get the following output
static is used because static values cannot be changed so by default we used static
public class Test
{
public static void main (String arg[])
{
int n[] = {-1, 1};
int sum = n[0] +n[1];
m(n, sum);
System.out.println("n[0] = " + n[0]);
System.out.println("n[1] =" +n[1]);
System.out.println("sum =" +sum);
}
public static void m(int k[], int sum)
{
for (int i = 0; i < k.length; i++)
{
k[i] = k[i] +1;
sum = sum + k[i];
}
}
}
Output
java Test
n[0]=0
n[1]=2
sum=0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.