After explaining in the previous article, let's see how many questions can you solve?
// What is the output result of the following code? Public class test1 {public static void changestr (string Str) {STR = "welcome";} public static void main (string [] ARGs) {string STR = "1234 "; changestr (STR); system. out. println (STR );}}
Question 2
Public class test {private int I = getvalue (); // 2nd rows private Int J = 10; int getvalue () {return J ;} public static void main (string [] ARGs) {system. out. print (new test (). i); // 9th rows}/** output: The second line of a compilation error B the ninth line of compilation error C output: 10D output: 0 What is the output result? */
Question 3
// What results will the following program print? Public class test {static Boolean Foo (char c) {system. out. print (c); Return true;} public static void main (string [] argv) {int I = 0; For (FOO ('A'); Foo ('B ') & (I <2); Foo ('C') {I ++; Foo ('D ');}}}
**************************************** ********************************* *********
Answer:Question 1: The result is 1234. As for why, I think the problem will be explained better if this is the case.
public class Test {public static void changeStr(String str) {str = "welcome";System.out.println("str of changeStr():"+str);}public static void main(String[] args) {String str = "1234";changeStr(str);System.out.println("str of main():"+str);}}
The output result is str of changestr (): Welcome, STR of main (): 1234. The scope of local variables must be considered.
**************************************** ***************************** ********************
Question 2: the answer is d, and the output result is 0. When you execute system. Out. Print (new test (). I,
Run New Test ().
- Java virtual machine starts building objects
- First, allocate a memory space for the object.
- Perform the zeroing operation on the memory space (that is, the value of 0 is assigned to all variables due to this zeroing operation)
- Start the initialization of static members and the initialization of static blocks-you don't need to talk about it here
- Initialize the member variables and execute them sequentially.
- Run private int I = getvalue ();
- Call the getvalue () method
- Execute the getvalue method to return 0. --------- please refer to 2nd
- Run private Int J = 10;
- Start executing the constructor
- Because you are executing code in the main function of test. The main function also belongs to the test class. Therefore, it can directly access the private variable I.
Therefore, the output result is 0.
**************************************** *************************** **************
Question 3: The output result is abdcbdcb. Is it a bit different from what you think? First, let's talk about the for loop. The for loop contains the initialization expression and Boolean expression) and step operations, when performing a for loop, first initialize and then test the Boolean expression. If true is obtained, the Code following the for statement is executed. After each cycle ends, a step is executed. Note that the step is left at the end. For this question, the for loop process is as follows:
- Execute Foo ("A") and Output
- Test the Boolean expression, execute Foo ("B"), and output B. In this case, Foo ("B") & I <2 is determined to be true. Continue to execute the statement after, foo ("D") is executed and Output D
- Execute one step. Foo ("C") is executed and C is output.
- Judge the Boolean expression again, execute Foo ("B"), and output B. At this time, Foo ("B") & I <2 does not judge whether the result is true. Continue to execute the statement after, foo ("D") is executed and Output
D
- Execute the step again. Foo ("C") is executed and C is output.
- Continue to judge the Boolean expression, execute Foo ("B"), and output B. In this case, Foo ("B") & I <2 is regarded as false. The loop ends, and subsequent statements are not executed.
So the final result is: abdcbdcb.