Check for missing vacancies 2 (Small interview questions), check for missing vacancies 2 interview questions

Source: Internet
Author: User

Check for missing vacancies 2 (Small interview questions), check for missing vacancies 2 interview questions

// Interview public class Main {class demo {} public static void main (String [] args) {new demo (); // error, because the main function is static, class demo must be added with static/static. this} public void show () {new demo () cannot be used. // this} is omitted before }}


1. Combination of interview polymorphism and anonymous internal classes


Q: Why does the following code fail to be compiled? (It is said that many people are suspended)

Class in {void set () {Object obj = new Object () // a subclass Object of the anonymous internal class, and converts it to Object {public void show () {System. out. print ("sdf") ;}}; obj. show (); // The upward transformation restricts the unique methods of subclass objects, so compilation fails.} public class Main {public static void main (String [] args) {new in (). set ();}}

2. What does the following code print?

Class Fu {public Fu () {show ();} void show () {System. out. println ("Haha") ;}} class Zi extends Fu {int num = 9; Zi () {System. out. println ("sub-constructor... "+ num);} void show () {System. out. println ("shiow... "+ num) ;}} public class Main {public static void main (String [] args) {new Zi ();}}

Show... 0
Sub-constructor... 9

Why?

Zi ()
{
In fact, super () is omitted. When super is called, the show method in the Fu constructor is executed, while the show method is in the subclass. Therefore, the show method of the subclass is overwritten, And the num initialization of the subclass is 0 by default, so print show first... 0, then num can initialize 9 visually, and then print the sub-constructor... 9
}


3. What is printed below?

class Zi {int num = 9;{System.out.println("shiow..."+num);}Zi(){System.out.println("shiow2..."+num);}}public class Main{public static void main(String[] args){new Zi();}}



Shiow... 9
Show2. .. 9


4. What is printed below?

Class Fu {Fu () {System. out. println ("fu ++"); // 3 show ();} void show () {System. out. print ("hehe") ;}} class Zi extends Fu {int num = 9; {System. out. println ("shiow... "+ num); num = 10;} Zi () {super (); // 2 // visual initialization 5 // construct a code block to initialize 6System. out. println ("shiow2... "+ num); // 7} void show () // 4 {System. out. println ("zi show... "+ num) ;}} public class Main {public static void main (String [] args) {new Zi (); // 1: num = 0 }}

Fu ++
Zi show... 0
Shiow... 9
Shiow2. .. 10

Combined with the above three and four examples:

When new Zi () is used, num is the default initialization 0. Now, execute the constructor of the Zi class, super executes the constructor of the parent class, and then execute the show, at this time, num is still 0, and num is displayed as 9, then code block Initialization is executed, and then show of the Zi () constructor


Code comments are marked as execution order



Interview Questions

First, be sure to show confidence
2. Speak Out loud, but not loud.
Third, do more questions before the exam
Fourth, if the examiner asks you how you think about yourself, you need to say more about the advantages and disadvantages, but you must always say that you must be objective.
Fifth, say hello when you go in. The teacher asks you to sit down and say thank you. Say goodbye when you leave.

Self-introduction, your learning situation,

2 C interview questions

Void bit_set (unsigned char * p_data, unsigned char position, bool flag)
{
Int mask_open [] = {0x80, 0x40, 0x20, 0x10, 0x08,0x04,0x02,0x01 };
Int mask_close [] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe };
If (1 = flag)
* P_data = (* p_data) | mask_open [8-position];
If (0 = flag)
* P_data = (* p_data) | mask_close [8-position];
}

Void RightLoopMove (char * pStr, unsigned short steps)
{
Char * str_tmp = (char *) malloc (sizeof (* pStr ));
* Str_tmp = (* pStr );
Int n = sizeof (* pStr );
For (int I = 0; I <n-1; I ++)
{
If (I <steps)
PStr [steps-i-1] = str_tmp [n-steps-1 + I];
Else
PStr [I] = str_tmp [I-steps];
}
}

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.