Java Foundation written test topic record __java

Source: Internet
Author: User
record some of the good pen questions you've encountered 1,
The following Java program outputs the result to ___.
int i=0;
Integer j = new Integer (0);
System.out.println (i==j);
System.out.println (J.equals (i));

Answer: TRUE

parsing:
The subject is an automatic disassembly box of the exam (automatic unboxing of the JDK required 1.5)
1, the basic type and basic type of package for the "= =" operator comparison, the basic type of package will automatically split into a basic type after the comparison, so the integer (0) will automatically open the box for the type of int again to compare, obviously return true;
2, two integer types for "= =" comparison, if its value is 128 to 127, then return True, otherwise return false, this is related to the buffer object of integer.valueof (), do not repeat here.
3, two basic type of package for equals () comparison, first Equals () compares the type, if the type is the same, continue to compare the value, if the value is the same, return True
4, the basic type of encapsulation type call Equals (), but the parameter is the basic type, this time, the first will be automatic boxing, the basic type conversion to its package type, and then the comparison in 3.

int a=257;
Integer b=257;
Integer c=257;
Integer b2=57;
Integer c2=57;
System.out.println (a==b);
System.out.println (A.equals (b));  Compilation error, base type cannot call Equals ()
System.out.println (B.equals (257.0));
System.out.println (b==c);
System.out.println (B2==C2);

The result of the above code is therefore true, False, False, True 2,

The following program outputs the result is __

public class Test {public  
    static void Main (String [] args) {  
        System.out.println (new B (). GetValue ());  
    }  
    Static class a{  
        protected int value;  
        public A (int v) {  
            setValue (v);  
        }  
        public void SetValue (int value) {  
            this.value = value;  
        }  
        public int GetValue () {  
            try{  
                value++;  
                return value;  
            } catch (Exception e) {  
                System.out.println (e.tostring ());  
            } finally {  
                This.setvalue (value)  
                ; System.out.println (value);  
    }} Static Class B extends a{public  
        B () {  
            super (5);  
            SetValue (GetValue ()-3);  
        }  
        public void SetValue (int value) {  
            Super.setvalue (2 * value);}}}  
  

Answer: 22 34 17

parsing:
The most critical point is that super (5)//invokes the parent class constructor, where the SetValue (value) inside the constructor is called SetValue method 3 of the subclass, because the subclass has overrides.

Read the following procedure to choose which is the correct output result

Class helloa{public
Helloa ()
    {
        System.out.println ("I ' M A class");
    }
    Static
    {
    System.out.println ("Static A");
    }
public class Hellob extends helloa{public
    Hellob ()
    {
        System.out.println ("I ' M B class");
    }
    static{
        System.out.println ("Static B");
    }
    public static void Main (string[] args) {
        new Hellob ();
    }
}

Results:
Static a static B I ' M A Class I ' M B class

parsing:
Order of execution in this topic:

Static and static block assignments of the parent class (in declared order)

own static variables and static block assignments (in declared order)

Parent class member variable and block assignment (in declared order)

Parent class constructor Assignment

Self-member variables and block assignments (in declared order)

Self constructor Assignment
Class static code blocks are class-level, and execution precedence is higher than normal code blocks > constructors. The summary is that the static code block of the parent class (including static variable assignment) is executed preferentially, then executes its own static code block, executes the normal code block of the parent class, executes the constructor of the parent class, executes its own normal code block, and executes its own constructor. 4,

The correct description of the following code fragment is ()

byte B1=1,b2=2,b3,b6; 
Final byte b4=4,b5=6; 
B6=B4+B5; 
B3= (B1+B2); 
System.out.println (B3+B6);

A, Output results: 13
B, Statement: B6=B4+B5 compilation error
C, Statement: B3=B1+B2 compilation error
D, runtime throws an exception

Correct answer c.
parsing:
Why not choose B, because B4, B5 is final decorated, so in the operation, will not be upgraded to the int type, and B1 and B2 are upgraded to the int type at run time, and B3 is byte type, type does not match. 5,

What happens to the following code during compilation and operation

public class testdemo{
    private int count;
    public static void Main (string[] args) {
        testdemo test=new testdemo ();
        System.out.println (Test.count);
    }
     Testdemo (int a) {
         count=a;
    }
}

A, the compilation runs through, the output result is 88
B, compile-time error, the count variable defines a private variable
C, compile-time error, System.out.println method is invoked when test is not initialized
D, compile and execute without output results
Correct answer:A
parsing:
The outside of the private variable is inaccessible, and this external refers to the outside of the class, and the main method here is within the large class Testdemo, so it is accessible. Have the following code:

public class Demo2 {
    private int count;
    Public Demo2 (int a) {
        count = A;
    }

    public static void Main (string[] args) {

        Demo2 d = new Demo2 (a);
        System.out.println (d.count);/normal operation, no error

        p p = new P (the);
        System.out.println (P.count);//This row compiles an error, the private variable cannot access
    }

class p{
    private int count;
    Public P (int a) {
        count = A;
    }
}
6,

The following program outputs the results.

Class Testa {public

    void Printvalue () {
        System.out.println ("Test01");
    }

Class Testb extends testa{public
    void Printvalue () {
        System.out.println ("Testb");
    }

public class Test01 {public

    static void Main (string[] args) {
        Testb b = new Testb ();
        B.printvalue ();
        Testa a = (testa) b;//type conversion
        a.printvalue ();
    }

Output results:
Testb
Testb
parsing:
The key here is the type conversion line, because B itself is defined as the TESTB type, where it does not turn into a Testa type, and the output is still "TESTB"

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.