public class testmeth{static int i = 1;public static void main(String args[]){System.out.println(i+” , “);m(i);System.out.println(i);}public void m(int i){i += 2;}}
Question
public class testmeth{static int i = 1;public static void main(String args[]){System.out.println(i+” , “);m(i);System.out.println(i);}public void m(int i){i += 2;}}
Solution
The code you've provided is written in Java. It seems like you want to understand what this code does. However, there is a small mistake in the code. The method m(int i) should be static because it's called from a static context (the main method). Also, the quotation marks around the comma in the println statement are not standard. Here is the corrected code:
public class testmeth {
static int i = 1;
public static void main(String args[]) {
System.out.println(i + ", ");
m(i);
System.out.println(i);
}
public static void m(int i) {
i += 2;
}
}
Now, let's go through the code step by step:
-
The class
testmethis defined with a static integer variableiinitialized to 1. -
The
mainmethod is the entry point of the program. It first prints the value ofifollowed by a comma. At this point,iis 1, so it prints "1, ". -
Then, it calls the method
m(i), passingias an argument. -
Inside the
mmethod, the passed argumentiis incremented by 2. However, this change does not affect the static variableiin the class because Java is pass-by-value, meaning that it passes a copy of the variable, not the original one. -
After the
mmethod is finished, it goes back to themainmethod and prints the value ofiagain. Sinceiwas not changed by themmethod, it still prints "1".
So, the output of the program will be:
1,
1
Similar Questions
public class Test { public void m1(String arg1) { arg1="Test"; } public static void main(String[] args) { Test test1= new Test(); String s= "Hello"; test1.m1(s); System.out.println(s); }}
class Test { public static void main(String args[]) { System.out.println(fun()); } static int fun() { static int x= 0; return ++x; }}
Analyze the following code carefully. Please select the one that applies.public class Test { public static void main(String[] args) { A a = new A(); a.print(); }}class A { String s; A(String s) { this.s = s; }void print() { System.out.println(s); }}
public class Code { public static void main(String[] args) { method(null); } public static void method(Object o) { System.out.println("Object method"); } public static void method(String s) { System.out.println("String method"); }}
public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); }}which line is an example of an inappropriate use of assertions?Line 11Line 12Line 14Line 22
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.