Java interview will encounter 56 face questions

Source: Internet
Author: User
Tags finally block instance method locale

1. Question: What happens if the main method is declared private?

Answer: Can compile normally, but will prompt "Main method is not public" when run.

2. Question: What is the difference between a reference and a pass in Java?

Answer: A pass-through refers to an address rather than the value itself, and a pass value is a copy of the passed value.

3. Question: What else to consider if you want to override the Equals method of an object?

Answer: Hashcode.

4. Question: How is Java's "write Once, run everywhere" implemented?

Answer: Java programs are compiled into a class file consisting of bytecode that can be run on any platform, so Java is platform independent.

5. Question: Explain the function of each keyword in the declaration of public static void main (String args[])

Answer: The Public:main method is the first method that is called when the Java program is run, so it must be visible to the Java environment. So the visibility is set to Pulic.

The Static:java platform calls this method without creating an instance of the class, so this method must be declared as static.

The Void:main method does not return a value.

String is the type of the command-line input parameter, and args refers to the string array that the command line passes into.

6. Question: = = difference from equals

Answer: = = Compare two objects in memory is not the same object, that is, in memory storage location consistent. The values stored by two string objects are the same, but may be stored in different places in memory.

= = Comparison is a reference and the Equals method compares the content. public boolean equals (object obj) This method is provided by object objects and can be overridden by subclasses. The default implementation returns true only if the object and itself are compared, which is equivalent to = =. String, BitSet, Date, and file all override the Equals method, and for two string objects, a value equal means that they contain the same sequence of characters. For a wrapper class of a basic type, a value equal means the value of the corresponding base type.

  1. public class EqualsTest {

  2.    public static void main(String[] args) {

  3.        String s1 = "abc";

  4.        String s2 = s1;

  5.        String s5 = "abc";

  6.        String s3 = new String("abc");

  7.        String s4 = new String("abc");

  8.        System.out.println(" == comparison:" + (s1 == s5));

  9.        System.out.println(" == comparison:" + (s1 == s2));

  10.        System.out.println("Using equals method:" + s1.equals(s2));

  11.        System.out.println(" == comparison:" + s3 == s4);

  12.        System.out.println("Using equals method : " + s3.equals(s4));

  13.    }

  14. }

Results:

    1. == comparison : true

    2. == comparison : true

    3. Using equals method : true

    4. false

    5. Using equals method :true

7. Question: What happens if the static modifier of the main method is removed?

Answer: The program compiles normally. The runtime throws a Nosuchmethoderror exception.

8. Question: Why is the Oracle Type4 driver known as a thin drive?

Answer: Oracle provides a type 4 JDBC driver, known as thin drive. This driver contains an implementation of a TCP/IP Net8 that is fully implemented by Oracle itself, so it is platform independent and can be downloaded by the browser at run time, without relying on any client Oracle implementations. The client connection string uses the TCP/IP address port instead of the tnsname of the database name.

9. Question: Describe the Finalize method

Answer: Final: constant declaration. Finally: handles the exception. Finalize: Helps with garbage collection.

The variables declared in the interface are final by default. The final class cannot inherit, that is, there is no child class. This is done for basic types of security considerations, such as String and Integer. This also allows the compiler to perform some optimizations that make it easier to secure threads. The final method cannot be overridden. The value of the final variable cannot be changed. The Finalize () method is called before an object is destroyed and recycled. Finally, typically used for exception handling, whether or not an exception is thrown. For example, closing a connection is usually done in a finally block.

10. Question: What is the Java API?

Answer: The Java API is a collection of a large number of software components that provide a number of useful features, such as GUI components.

11-20

11. Question: What is the GregorianCalendar class?

Answer: GregorianCalendar provides support for traditional western calendars.

12. Question: What is the ResourceBundle class?

Answer: ResourceBundle is used to store resources for the specified locale, and the application can load these resources according to the language context of the runtime, providing a presentation in different languages.

13. Question: Why is there no global variable in Java?

Answer: Global variables are globally visible and Java does not support globally visible variables because: global variables undermine the principle of referential transparency. A global variable causes a namespace conflict.

14. Question: How do I convert a string type to number type?

Answer: The valueof method of the integer class can turn a string into number. The following is a code example:

    1. String numString = “1000″;

    2. int id=Integer.valueOf(numString).intValue();

15. Question: What is the SimpleTimeZone class?

Answer: SimpleTimeZone provides Gregorian date support.

16. Question: What is the difference between the while loop and the Do loop?

Answer: The while structure determines whether the next iteration should continue at the beginning of the loop. Do/while structure at the end of the loop to determine whether the next iteration will continue. The do structure executes at least one loop body.

17. Question: What is the locale class?

Answer: The locale class is used to dynamically adjust the output of the program according to the locale.

18. Question: What is the principle of object-oriented programming?

Answer: There are three main points, polymorphism, inheritance and encapsulation.

19. Question: Introduction of the principles of succession

Answer: Inheritance enables an object to get the properties of another object. Using inheritance allows the functionality that has been tested to be reused and can be modified one at a time, with all inherited places in effect.

20. Question: What is implicit type conversion?

Answer: Implicit type conversion is simply a type assignment to another type, without explicitly telling the compiler that a conversion has occurred. Not all types support implicit type conversions.

code example:

    1. int i = 1000;

    2. long j = i; //Implicit casting

21-30

21. Question: Is sizeof a key word for Java?

Answer: No.

22. Question: What is the native method?

Answer: The native method is a non-Java code implementation method.

23. Question: In System.out.println (), System, out, println what are the respective?

Answer: System is a pre-defined final class provided by the systems, out is a PrintStream object, and println is an overloaded method inside an Out object.

24. Question: What is encapsulation, inheritance, and polymorphism?

Answer: In simple terms, polymorphism refers to multiple implementations of a name. Polymorphism allows an entity to implement different operations in a common way. The specific operation is determined by the actual implementation.

Polymorphism is represented in Java in three ways: Method overloading is overridden by the Java interface through the implementation of the inheritance method.

25. Question: What is an explicit type conversion?

The answer: Explicit type conversions are explicitly telling the compiler to convert objects.

code example:

    1. long i = 700.20;

    2. int j = (int) i; //Explicit casting

26. Question: What is a Java virtual machine?

Answer: A Java Virtual machine is a software system that can be ported to different hardware platforms.

27. Question: What is type down conversion?

The answer: A down conversion refers to a generic type that is converted to a specific type, down on the inheritance structure.

28. Question: What is the access modifier for Java?

Answer: The access permission modifier is a keyword that indicates the type of access permission for a class member. Use these keywords to qualify the program's methods or variable access permissions. They contain:

Public: All classes can access the protected: within the same package and all subclasses have access to private: Only the classes that belong to the default: the Attribution class and subclasses under the same package can access

29. Question: What is the parent class for all classes?

Answer: Object.

30. Question: What are the basic types of Java?

Answer: Byte,char, short, int, long, float, double, Boolean.

31-40

31. Question: What are the characteristics of static types?

Answer: A static variable is bound to a class, not an instance object of the class. Each instance object shares the same copy of a static variable. That is, a static variable of a class has only one copy, no matter how many objects it has. A class variable, or static variable, is declared by using static as the keyword. Class variables are often used as constants. Static variables are usually accessed through the class name. This variable is created when the program is run and is not destroyed until the end of the program. The scope and instance variables of a class variable are the same. It also has the same initial value as the member variable, and when the variable is not initialized, it has a default value depending on its data type. Similarly, a static method is a method that belongs to a class, not a class object, whose invocation does not work on the class object, nor does it need to create any class instances. The static method itself is final, because overrides only occur on the class instance, and the static method is bound to the class, not the object. The static method of the parent class is masked by the static method of the class, as long as the original method is not declared final. A non-static method cannot override a static method, that is, you cannot change a static method into an instance method in a subclass.

Non-static variables have a separate value on each instance of an object.

32. What is the difference between the question:& operator and the && operator?

Answer: When an & expression is evaluated, the two operands are evaluated,&& more like a shortcut to an operator. When an && expression evaluates, the first operand is evaluated first, and if it returns true, the second operand is evaluated. If the first operand is a value of Fale, the second operand is not evaluated.

33. Question: How does Java handle overflow and underflow of integral types?

Answer: Java stores the corresponding low order bytes in the calculation results into corresponding values based on the size of the type.

34. Question: What happens if public static void is written as static public void?

Answer: The program compiles and runs normally.

Question, what is the difference between declaring a variable and defining a variable?

Answer: Declaring a variable we only provide the type and name of the variable, and it is not initialized. The definition consists of declaring and initializing a two-stage string s; just a variable declaration, string s = new String ("Bob"); or string s = "Bob"; is a variable definition.

35. Question: What type of parameter delivery does Java support?

Answer: The Java parameters are all passed values. For an object, the passed value is a reference to the object, which means that the original reference and the copy referenced by the parameter are pointing to the same object.

36. Question: What is the principle of object encapsulation?

The answer: encapsulation is the binding of the data and the code of the operation data to a separate unit. This ensures the security of the data and prevents incorrect use of external code. Objects allow programs and data to be encapsulated to reduce potential interference. Another understanding of encapsulation is that it acts as a layer of data and code to prevent arbitrary access to the outer layers of code.

37. Question: How do you understand variables?

Answer: A variable is a named area of memory for the program to access. Variables are used to store data, and as the program executes, the stored data may change.

38. Question: What is a numeric promotion?

Answer: Numeric promotion refers to the conversion of data from a smaller data type into a larger data type for integer or floating-point operations. During numeric promotion, the Byte,char,short value is converted to the int type. The int type may also be promoted to long when needed. Long and float are likely to be converted into double types.

39. Question: What is the type conversion for Java?

Answer: Converting from one data type to another data type is called type conversion. Java has two types of conversions, one for explicit type conversions and one for implicit.

40. Question: Inside the parameters of the main method, what is the first parameter of the string array?

Answer: The array is empty and has no elements. Unlike C or C + +, the first element is a program name by default. If the command line does not provide any arguments, the string array in the main method is empty, but not null.

41-50

41. Question: How can I tell if an array is null or empty?

Answer: The value of the output array.length, if it is 0, indicates that the array is empty. If NULL, a null pointer exception is thrown.

42. Question: Can I allow multiple classes to have a main method in the program?

Answer: Yes. When the program runs, we specify the class name to run. The JVM will only look for the main method in the class you specify. Therefore, multiple classes have a Main method and there is no naming conflict problem.

43. Question: When does a static variable load? Compile-time or run-time? What is the time for static code blocks to load?

Answer: Static variables are created when the ClassLoader loads the class into the JVM, regardless of whether the object is created or not. When static variables are loaded, memory space is allocated. The code for a static code block executes only once when the class is first initialized. A class can have more than one static block of code, which is not a member of a class, has no return value, and cannot be called directly. Static code blocks cannot contain this or super, which is typically used to initialize static variables.

44. Question: Can a class have more than one main method?

Answer: Yes, but only one main method has the following signature:

public static void Main (string[] args) {}
Otherwise, the program will not compile. The compiler warns you that the main method already exists.

45. Question: How does the JVM work under a simple introduction?

The answer: The JVM is an abstract computer, just like a real computer, that compiles. java files into a. class file (the. class file is a bytecode file), and then uses its interpreter to load the byte code.

46. Question: If the value of two variables is exchanged in situ?

Answer: First assign two values to the first variable, then subtract the second variable with the resulting result, and assign the value to the second variable. Subtract the second variable from the first variable and assign the value to the first variable. The code is as follows:

    1. int a=5,b=10;a=a+b; b=a-b; a=a-b;

It can also be exchanged using XOR or operation. The first method may also cause an overflow. The Xor method is as follows: int a=5,b=10;

  1. a=a+b; b=a-b; a=a-b;

  2. int a = 5; int b = 10;

  3. a = a ^ b;

  4. b = a ^ b;

  5. a = a ^ b;

47. Question: What is the encapsulation of data?

Answer: One way to encapsulate data is to create set and get methods in a class to access the object's data variables. The variables are generally private, and the get and set methods are public. Encapsulation can also be used to validate data when it is stored, to calculate data, or to be used as introspection (for example, using JavaBean in struts). Encapsulating data and functionality in a separate structure is called data encapsulation. Encapsulation is the encapsulation of data and associated operations into a single unit, so that the associated methods can be used to access the data. Encapsulation provides data security, which is actually a way to hide data.

48. Question: What is the Reflection API? How is it implemented?

Answer: Reflection refers to the ability to view the state and characteristics of a class at run time and to manage it dynamically. These functions are provided through the reflection API of some built-in classes, such as Class,method,field, constructors, and so on. Example: Use the Java Reflection API's GetName method to get the class name.

49. Question: Does the JVM itself maintain the cache, is it an object assignment in the heap, or is the heap of the operating system managed by the JVM itself? Why?

Answer: Yes, the JVM itself manages the cache, which creates objects in the heap and then references those objects in the stack.

50. Question: What is virtual memory?

Answer: Virtual memory is also called extended memory, there is actually no real physical memory.

51-56

51. Question: Can the method be both static and synchronized?

Answer: Yes. If you do this, the JVM acquires a lock on the Java.lang.Class instance associated with the object. Doing so equals:

    1. synchronized(XYZ.class) {

    2. }

52. Question: What is the difference between string and StringTokenizer?

Answer: StringTokenizer is a tool class used to split strings.

    1. StringTokenizer st = new StringTokenizer(”Hello World”);

    2. while (st.hasMoreTokens()) {

    3.    System.out.println(st.nextToken());

    4. }

Output:

    1. Hello

    2. World

53. Question: What are the characteristics of transient variables?

Answer: The transient variable is not serialized. For example, when a class that implements the serializable interface is serialized to Objectstream, a variable of type transient is not written to the stream, and the value of the corresponding variable is null when deserializing back.

54. Question: Which containers use the border layout as their default layout?

Answer: Window, Frame, Dialog.

55. Question: How to understand what synchronization is?

Answer: Synchronization is used to control the access of shared resources across multiple threads to ensure that only one thread can access this resource at the same time. In a multithreaded program with non-synchronous protection, when a thread is modifying a shared variable, another thread may be using or updating its value. Synchronization avoids the generation of dirty data.

To synchronize a method:

    1. public synchronized void Method1 () {

    2. // Appropriate method-related code.

    3. }

To synchronize a block of code inside a method:

    1. public myFunction (){

    2.    synchronized (this) {

    3.            // Synchronized code here.

    4.         }

    5. }

56. Problem: Give an integer variable A, write two pieces of code, the first set of a bit3 is 1, the other bit does not change, how to implement it?
    1. /**

    2. * @author sanchan

    3. * @since 1.0

    4. */

    5. public class Test {

    6.    public static void main(String[] args) {

    7.        //给一整型变量a,写两段代码,第一个设置a的bit3 为1,其他bit不变,怎么实现呢?

    8.        int a = 8;

    9.        //打印二进制

    10.        System.out.println(Integer.toBinaryString(a));//输出  1000

    11.        System.out.println(Integer.toBinaryString(a | 4));//输出 1100

    12.    }

    13. }          原文链接:https://coderknock.com/blog/2016/04/08/Interview.html

Java interview will encounter 56 face questions

Related Article

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.