Advanced cutting-edge technologies for cainiao (II)-self-testing of Basic Java knowledge

Source: Internet
Author: User
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 ().

  1. Java virtual machine starts building objects
  2. First, allocate a memory space for the object.
  3. Perform the zeroing operation on the memory space (that is, the value of 0 is assigned to all variables due to this zeroing operation)
  4. Start the initialization of static members and the initialization of static blocks-you don't need to talk about it here
  5. Initialize the member variables and execute them sequentially.
  6. Run private int I = getvalue ();
  7. Call the getvalue () method
  8. Execute the getvalue method to return 0. --------- please refer to 2nd
  9. Run private Int J = 10;
  10. Start executing the constructor
  11. 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:

  1. Execute Foo ("A") and Output
  2. 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
  3. Execute one step. Foo ("C") is executed and C is output.
  4. 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
  5. Execute the step again. Foo ("C") is executed and C is output.
  6. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.