Java interview questions for fresh graduates (with answers)

Pay attention, don’t get lost; continue to update Java related technologies and information! ! !
The content comes from the contributions of group friends! Thanks for the support!

Preface

Insert picture description here

Due to the impact of the epidemic this year, many fresh graduates have not yet found a job. For Java fresh graduates, there are those that must be met, and those are required exams. Today, I will take everyone Get up and take a look! Here are 100 essential exam questions. Because of the limited space, only some of them are listed. If you need all the interview questions from big companies, you can click here to get them for free . There are also excellent resume templates.

What are the characteristics of object-oriented?

  • Abstraction: Abstraction is the process of constructing a class by summarizing the common characteristics of a class of objects, including data abstraction and behavior abstraction. Abstraction only pays attention to the properties and behaviors of the object, and does not pay attention to the details of these behaviors.
  • Inheritance: Inheritance is the process of creating new classes by obtaining inheritance information from existing classes. The class that provides inheritance information is called the parent class (super tired, base class); the class that gets the inheritance information is called the subclass (derived class). Inheritance gives the changing software system a certain continuity. At the same time, inheritance is also an important means of encapsulating variable factors in a program.
  • Encapsulation: It is generally considered that encapsulation is to bind data and methods of operating data, and access to data can only be through a defined interface. The essence of object-oriented is to describe the real world as a series of completely autonomous and closed objects. The method we write in the class is a encapsulation of the implementation details; we write a class is the encapsulation of data and data operations. It can be said that encapsulation is to hide things that can be hidden together, and only provide the simplest programming interface to the outside world.
  • Polymorphism: Polymorphism allows objects of different subtypes to make different responses to the same message. Simply put, it calls the same method with the same object reference but does different things. Polymorphism is divided into compile-time polymorphism and runtime polymorphism. If the method of an object is a service provided by the object to the outside world, then the runtime polymorphism can be explained as: calling a different subclass object to replace the parent class object. Method overload implements compile-time polymorphism (also known as pre-binding), while method override implements runtime polymorphism (also known as post-binding). Run-time polymorphism is the essence of object-oriented. To achieve polymorphism, two things need to be done: 1 method rewriting (the subclass inherits the parent class and rewrites the existing or abstract methods in the parent class) 2 object modeling (Use the parent class to refer to the subclass object)

Access modifier permissions

Permissions are divided into: current category, same package, sub-category, other packages

Public is acceptable; protected other packages are not available; default is available under the same package; private is only available for yourself.

Is String a basic data type

Answer: No. There are 8 basic types in java: byte, short, int, long, float, double, char, boolean; except for primitive types and enumeration types, the rest are reference types .
Insert picture description here

float f=3.4

Error, the default is double, it needs to be forced, or f=3.4f;

int and integer

In order to treat basic data types as objects, Integer is a wrapper class.

Integer cache is -128 to 127. Therefore, the Integer objects in this range are the same, == is true. Others are false.
Click here to receive more Java interview materials for free

&和&&

The operators of the & link must be calculated. && is a short-circuit operation, that is, the calculation stops when there is an error in the previous expression.

Explain the usage of stack, heap, and static area in memory

Answer: Usually we define a variable of a basic data type, a reference to an object, and the on-site storage of function calls all use the stack space in the memory; and the objects created by the new keyword and the constructor are placed in the heap space; the program Literals such as 100, "hello" and constants written directly are placed in the static area. The stack space is the fastest to operate but the stack is small. Usually a large number of objects are placed in the heap space. In theory, the entire memory is not used by other processes, and even the virtual memory on the disk can be used as heap space.

String str = new String("hello");

Above, str is placed on the stack, the string object produced by new is placed on the stack, and the literal "hello" is placed in the static area.

Java6 began to use the "escape analysis" technology, which can put some local objects on the stack to improve object operation performance.

Can switch be used in byte, long, String?

Answer: Before java5, only: byte, short, char, int. Add enum after 5, and String after 7.

The most efficient way to calculate 2 times 8

Answer: 2<<3 (shifting 3 to the left is equivalent to multiplying by 2 to the power of 3, shifting 3 to the right is equivalent to dividing by 2 to the power of 3)

Supplement: When we rewrite the hashCode method for the written class, we may see the code as shown below. In fact, we don’t understand why such a multiplication operation is used to generate a hash code (hash code), and why this number It is a prime number. Why is the number 31 usually chosen? You can Baidu the answers to the first two questions yourself. Choose 31 because you can use shift and subtraction operations instead of multiplication to get better performance. Speaking of which you may have already thought of: 31

  • num is equivalent to (num << 5)-num. Shifting by 5 bits to the left is equivalent to multiplying by the 5th power of 2 and subtracting itself is equivalent to multiplying by 31. The current VM can automatically complete this optimization.
    Click here to receive more Java interview materials for free

Does array have a length() method, and does String have a length() method?

Insert picture description here

Answer: The array does not have a length() method, but has a length attribute. String has a length() method. The string in js is the length attribute.

Can constructor override?

Answer: The constructor cannot be inherited, so it cannot be overridden, but it can be overloaded.

Two objects have the same value (x.equals(y)==true), but they can have different hash codes. Is this sentence correct?

Answer: Not right. The hashcode of equals must be the same.

Can I inherit the String class

Answer: The String class is final and cannot be inherited. Inheriting String is a wrong behavior. Association (Has-A) and dependency (Use A) should be used instead of inheritance (Is-A).

When an object is passed as a parameter to a method, the method can change the properties of the object and return the changed result. So, is it passed by value or by reference?

Answer: It is value transfer. The method call of Java language only supports parameter value passing. When an object instance is passed to a method as a parameter, the value of the parameter is a reference to the object. The properties of the object can be changed in the process of being called, but changes to the object reference will not affect the caller. Click here to receive more Java interview materials for free

String

@Test
    public void str_c(){
        String a = "hehe";
        String b = "he"+"he";
        String c = new String("hehe");
        String d = new String("hehe");
 
    System.out.println(a==b);//true
    System.out.println(a==c);//false
    System.out.println(a==a.intern());//true
    System.out.println(c==d);//false
    }

The difference between Overload and Override. Can overloaded methods be distinguished based on the return type?

Answer: Method overloading and rewriting are both ways to achieve polymorphism. The difference is that the former implements polymorphism at compile time, while the latter implements polymorphism at runtime. Overloading occurs in a class. If a method with the same name has different parameter lists (different parameter types, different numbers of parameters, or both), it is considered overloaded; overriding occurs between the subclass and the parent class. The overriding requires that the overridden method of the subclass has the same return type as the overridden method of the parent class, which has better access than the overridden method of the parent class, and cannot declare more exceptions than the overridden method of the parent class. Exchange principle). Overloading has no special requirements for the return type.

Can a Chinese character be stored in a char variable? Why?

Answer: The char type can store a Chinese character, because the encoding used in Java is Unicode (do not choose any specific encoding, directly use the number of the character in the character set, this is the only way to unify), a char type occupies 2 characters Section (16 bits), so it’s no problem to put a Chinese.

What are the similarities and differences between abstract class and interface?

Answer: Neither abstract classes nor interfaces can be instantiated, but references to abstract classes and interface types can be defined. If a class inherits an abstract class or implements an interface, all abstract methods in it need to be implemented, otherwise the class still needs to be declared as an abstract class. Interfaces are more abstract than abstract classes, because abstract classes can define constructors, abstract methods and concrete methods, but interfaces cannot define constructors and all the methods in them are abstract methods. The members in the abstract class can be privae, default, protected, public, and the member variables in the interface are all public. Member variables can be defined in abstract classes, while the members defined in interfaces are actually constants. Classes with abstract methods must be declared as abstract classes, and abstract classes may not have abstract methods.

What is the difference between Static Nested Class and Inner Class?

Insert picture description here

Answer: Static Nested Class is an internal class that is declared as static. It can be instantiated without relying on external class instances. The usual inner class needs to be instantiated after the outer class is instantiated. Click here to receive more Java interview materials for free

Will there be a memory leak in Java? Please describe briefly.

Answer: Theoretically, because of the garbage collection mechanism (GC) in Java, there will be no memory leakage problem (this is also an important reason why Java is widely used in server-side programming); however, in actual development, there may be useless but reachable Objects, these objects cannot be reclaimed by the GC, so memory leaks can also occur. For example, the objects in Hibernate's Session (first level cache) are in a persistent state, and the garbage collector will not reclaim these objects. However, there may be useless garbage objects in these objects. If they are not closed or flushed in time, The first level cache may cause memory leaks. The code in the following example will also cause memory leaks.

  • The code above View Code implements a stack (FILO) structure. At first glance, there seems to be no obvious problem. It can even pass the various unit tests you write. However, the pop method has the problem of memory leakage. When we use the pop method to pop an object in the stack, the object will not be treated as garbage, even if the program using the stack no longer references these objects, because the stack is maintained internally Obsolete
    references to these objects . In languages ​​that support garbage collection, memory leaks are very hidden. Such memory leaks are actually unconscious object retention. If an object reference is unconsciously reserved, then the garbage collector will not process this object, nor will it process other objects referenced by the object, even if there are only a few such objects, it may cause many objects to be excluded In addition to garbage collection, it has a significant impact on performance. In extreme cases, Disk
    Paging (physical memory and virtual memory of hard disk exchange data) will be triggered , and even OutOfMemoryError will be caused.

to sum up

Insert picture description here

The golden nine is almost over, and the silver ten is coming soon. Every Java programmer who has just graduated can grasp this interview boom and apply for a job that suits you. Here I have also compiled some interview questions and interview materials from major companies. , And a good resume template. If you need it, you can click here to get it for free . I hope to help readers in need. I wish you all a smoother next job interview.
 Insert picture description here
Insert picture description here
If you need it, you can click here to get it for free

Guess you like

Origin blog.csdn.net/yueyunyin/article/details/108772694