Complete collection of Java interview questions (continuously updated, interview with Java Senior Development Engineer)

Keywords: Java Back-end Interview Programmer

13. Look at the program and write the results

class Fu{

	public int num = 10;

	public Fu(){

		System.out.println("fu");

	}

}



class Zi extends Fu{

	public int num = 20;

	public Zi(){

		System.out.println("zi");

	}

	public void show(){

		int num = 30;

		System.out.println(num);

		System.out.println(this.num);

		System.out.println(super.num);

	}

}



class Test1_Extends {

	public static void main(String[] args) {

		Zi z = new Zi();

		z.show();

	}

}

/*

	fu

	zi 

	30

	20

	10

*/



14. Look at the program and write the results

class ExtendsTest {

	public static void main(String[] args) {

		Zi z = new Zi();

	}

	

	/*

		1,jvm The main method is called, and main is put on the stack

		2,Encountered Zi z = new Zi(); Fu.class and Zi.class will be loaded into memory respectively,

		Then create the object. When Fu.class is loaded into memory, the static code block of the parent class will follow Fu.class

		Load together. When Zi.class is loaded into memory, the static code blocks of subclasses will be loaded together with Zi.class

		The first output, static code block Fu, and the second output, static code block Zi

		3,Take the construction method of Zi class, because java is hierarchically initialized, initialize the parent class first,

		The child class is initialized again, so the parent class construction is carried out first, but the parent class is found when the parent class construction is executed

		There is a construction code block, which takes precedence over the execution of the construction method

		The third output constructs the code block Fu and the fourth output constructs the method Fu

		4,Fu After class initialization, the subclass is initialized. The fifth output is the construction code block Zi and the construction method Zi

	*/

}

class Fu {

	static {

		System.out.println("Static code block Fu");		//1

	}



	{

		System.out.println("Construct code block Fu");		//3

	}



	public Fu() {

		System.out.println("Construction method Fu");		//4

	}

}



class Zi extends Fu {

	static {

		System.out.println("Static code block Zi");		//2

	}



	{

		System.out.println("Construct code block Zi");		//5

	}



	public Zi() {

		System.out.println("Construction method Zi");		//6

	}

}



Zi z = new Zi(); Please execute the results.



15. Method rewritten interview questions

  • What is the difference between Override and Overload? Can Overload change the return value type?

  • overload can change the return value type, just look at the parameter list

  • Method overwrite: as like as two peas in the parent class, the subclass has the same method declaration. Related to the return value type, the return value is consistent (or child parent class)

  • Method overloading: methods with the same method name and different parameter lists in the same class. It is independent of the return value type.

  • When subclass objects call methods:

    • Find the child class itself first, and then the parent class.

16.final keyword interview questions?

A: Modify local variables

B: Initialization timing

/*

A:

Basic type: value cannot be changed.

Reference type: address value cannot be changed. The properties in the object can be changed.

B:

Explicit initialization

Before the object is constructed

*/

17. If an abstract class has no abstract methods, can it be defined as an abstract class? If so, what's the point?

  • sure

  • There is only one purpose to do this, that is, to prevent other classes from creating objects of this class and hand them over to subclasses

18. What keywords cannot abstract coexist with

static final private

abstract and static

cover abstract The modified method has no method body

cover static Class name can be used for modification.call,But the class name.There is no point in calling abstract methods

abstract and final

cover abstract Decorated methods force subclass overrides

cover final Decorated do not let subclasses override,So they are contradictory

abstract and private

cover abstract The modification is to let subclasses see and force overrides

cover private Modifier does not allow subclasses to access,So they are contradictory



19. Look at the program and write the results

A: See if there is a problem with the following program. If not, tell the result

class Fu {

	public void show() {

		System.out.println("fu show");

	}

}



class Zi extends Fu {

	public void show() {

		System.out.println("zi show");

	}



	public void method() {

		System.out.println("zi method");

	}

}



class Test1Demo {

	public static void main(String[] args) {

		Fu f = new Zi();

		f.method();

		f.show();

	}

}



//Compilation error. The compiler looks to the left, but there is no method() method in the Fu class.

B: See if there is a problem with the following program. If not, tell the result

class A {

	public void show() {

		show2();

	}

	public void show2() {

		System.out.println("I");

	}

}

class B extends A {

	public void show2() {

		System.out.println("love");

	}

}

class C extends B {

	public void show() {

		super.show();

	}

	public void show2() {

		System.out.println("you");

	}

}

public class Test2DuoTai {

	public static void main(String[] args) {

		A a = new B();

		a.show();

		

		B b = new C();

		b.show();

	}

}



//Love

//You

20. Is there a sequential relationship between package, import and class

package On the first line of valid code, import Put it in the middle, class Last of the three.



21. Requirements: use known variables to output 30, 20 and 10 on the console.

class Outer {

	public int num = 10;

	class Inner {

		public int num = 20;

		public void show() {

			int num = 30;

			System.out.println(?);	//num

			System.out.println(??);	//this.num

			System.out.println(???);//Outer.this.num

		}

	}

}



class InnerClassTest {

	public static void main(String[] args) {

		Outer.Inner oi = new Outer().new Inner();

		oi.show();

	}	

}



22. Complete the code as required

interface Inter { void show(); }

class Outer { //Complement code}

class OuterDemo {

	public static void main(String[] args) {

		  Outer.method().show();

	  }

}



"HelloWorld" is required to be output on the console

//The code content needs to be supplemented

public static Inter method() {

	return new Inter() {

		public void show() {

			System.out.println("HelloWorld");

		}

	};	

}



23. Difference between = = sign and equals method

Same point: all are for comparison, and all returned are Boolean types.

difference:

(1)==:

*It is a comparison operation symbol, which can compare both basic data types and reference data types,

Basic data types compare values, and reference data types compare address values

(2)equals:

*The equals method is a method that can only compare reference data types. All objects inherit the methods in the Object class,

If the equals method in the Object class is not overridden, there is no difference between the equals method and the reference data type,

The overridden equals method compares the properties in the object.

24. Judge whether s1 and s2 defined as String are equal

* String s1 = "abc";

* String s2 = "abc";

* System.out.println(s1 == s2); //true					

* System.out.println(s1.equals(s2)); //true

//If there is no string object in the constant pool, you can create one. If there is one, you can use it directly



25. The following sentence creates several objects in memory?

* String s1 = new String("abc");

//Create several objects

//Create two objects, one in the constant pool and one in heap memory



26. Judge whether s1 and s2 defined as String are equal

* String s1 = new String("abc");	//The address value of the heap memory object is recorded		

* String s2 = "abc";				//What is recorded is the address value in the constant pool

* System.out.println(s1 == s2); ?		//false	

* System.out.println(s1.equals(s2)); ?	//true



27. Judge whether s1 and s2 defined as String are equal

* String s1 = "a" + "b" + "c";	

* String s2 = "abc";			

* System.out.println(s1 == s2); ?		//True, there is a constant optimization mechanism in Java

* System.out.println(s1.equals(s2)); ?	//true



28. Judge whether s1 and s2 defined as String are equal

* String s1 = "ab";

* String s2 = "abc";

* String s3 = s1 + "c";

* System.out.println(s3 == s2);			//false

* System.out.println(s3.equals(s2)); ?	//true



29. What is the difference between null and ''?

'' is not only a String constant, but also an object of String class. Since it is an object, of course, you can call the methods in String class

Null is a null constant and cannot call any method. Otherwise, a null pointer exception will occur. Null constants can assign values to any reference data type

30. Differences between string, StringBuffer and StringBuilder

(1)String: is an immutable character sequence

(2)StringBuffer: is a variable character sequence. jdk1.0 is thread safe and inefficient

(3) StringBuilder: is a variable character sequence. jdk1.5 version, is thread unsafe, high efficiency

31. Interview questions for integer

Look at the program and write the results

Integer i1 = new Integer(97);

Integer i2 = new Integer(97);

System.out.println(i1 == i2);        //false

System.out.println(i1.equals(i2));    //true

System.out.println("-----------");



Integer i3 = new Integer(197);

Integer i4 = new Integer(197);

System.out.println(i3 == i4);        //false

System.out.println(i3.equals(i4));    //true

System.out.println("-----------");



Integer i5 = 97;

Integer i6 = 97;

System.out.println(i5 == i6);        //true

System.out.println(i5.equals(i6));    //true

System.out.println("-----------");



Integer i7 = 197;                    

Integer i8 = 197;                    

System.out.println(i7 == i8);        //false

System.out.println(i7.equals(i8));    //true



-128 to 127 are the value range of byte. Within this value range, automatic packing is performed

Instead of creating a new object, it is obtained from the constant pool. If the byte value is exceeded

The scope recreates the object

32. Note: String and StringBuffer are passed as parameters respectively

* A:Formal parameter problem

    * String Pass as parameter

    * StringBuffer Pass as parameter 

    The value of the basic data type is passed without changing its value. The value of the reference data type is passed and its value is changed.

    String Class is a reference data type, but when it is passed as a parameter, it is the same as the basic data type.



33. New features of JDK1.5:

  Generic, enumeration, enhancement for,Automatic disassembly box, static import, variable parameters



34. Causes and solutions of concurrent modification exceptions

Cause: when traversing a set with an iterator, using the set changes the structure of the set

Solution: (1) use ordinary for loop

(2) Using the list iterator ListIterator

35. Differences between HashMap and Hashtable

 * common ground: 

     The bottom layer is a hash algorithm, which is a double column set

 * HashMap:

       Unsafe threads and high efficiency, JDK1.2 edition

       Can store null Key sum null value

 * Hashtable:

       Thread safe, inefficient, JDK1.0 edition

       Cannot store null key  



36. Differences between collections and Collections:

Collection It is the top-level interface of a single column set, which defines the common contents of a single column set.

Collections It is a tool class for operating collections, which provides some common collection functions such as searching and sorting.



37. Difference between final, finally and finalize

  • final

    Modifier class, cannot be inherited

Posted by JimF on Wed, 24 Nov 2021 02:13:13 -0800