10 tricky Java interview questions, see how many can you answer correctly?

I saw a few interview questions shared by foreigners last night, which was quite interesting. Let's take a look at the problems in two installments. What kind of problems are difficult for foreigners? If it is you, can you get all the answers right?

If you are interested in the original text, you can also view it through this link: https://levelup.gitconnected.com/tricky-java-interview-questions-cfc546fd03ab

Q1: What is the output of the following Java code?

public class Test {
  
  public static void main(String[] args) {
    method(null);
  }

  public static void method(Object o) {
    System.out.println("Object method");
  }
  
  public static void method(String s) {
    System.out.println("String method");
  }
}

Answer :

"String method" will be printed. First of all, null is not an object in Java. In Java, we can assign null to any reference type. And a string in Java is an object of Java.lang.String class. Here, the Java compiler will choose the most specific parameter type to call the overloaded method. The String type is more specific than Object, so method(String s)methods are called .

Q2: What is the output of the following Java code?

public class Test{
  public static void main(String[] args){
    Integer num1 = 100;
    Integer num2 = 100;
    if(num1 == num2){
      System.out.println("num1 == num2");
    }
    else{
      System.out.println("num1 != num2");
    }
  }
}

Answer :

Will print "num1 == num2". Whenever using "==" to compare two different object references, the value is always "false". But here, due to the Integer cache, num1 and num2 are automatically boxed. Therefore num1 == num2 returns "true". At the same time, integer caching will only occur for values ​​between -128 and 127.

Q3: How does garbage collection prevent Java applications from running out of memory?

Answer :

The Java garbage collector cannot prevent Java applications from running out of memory. It simply clears unused memory when the object is out of scope and is no longer needed. Therefore, it is not guaranteed to prevent Java applications from running out of memory!

Q4: Is Java "pass by reference" or "pass by value"

Answer :

Java is always "pass by value". However, when we pass the value of an object, we pass a reference to it because the variable stores the object reference, not the object itself. But this is not "pass by reference", which may confuse beginners.

Q5: How many String objects are created by the following code

public class Test{
  public static void main(String[] args){
    String s = new String("Hello World");
  }
}

Answer :

Created 2 String objects. One in the heap memory and one in the string constant pool in the method area.

Q6: What is the output of the following code

public class Test{
  public static void main(String[] arr){
    System.out.println(0.1*3 == 0.3);
    System.out.println(0.1*2 == 0.2);
  }
}

Answer :

The first sentence print outputs false, and the second sentence print outputs true. Because of the error rounding of floating-point numbers, only powers of 2 can be accurately represented by simple binary notation. Numbers that do not correspond to the power of 2 must be rounded to fit a limited number of bits. Therefore, 0.1*3 is not equal to 0.3.

Q7: Is it possible to rewrite or overload static methods in Java?

Answer :

It is possible to overload static Java methods, but it is impossible to override them. You can write another static method with the same name in the subclass, but it will not override the superclass method. It is called method hiding in Java.

Q8: What is the most reliable way to test whether two double values ​​are equal?

Answer :

Double.compare(d1, d2) == 0

Q9: If a try or catch block executes a return statement, will the finally block be executed?

Answer :

Will execute. The only way to prevent finally block execution is to use System.exit().

Q10: What is the output of the following code

public class Test{ 
  public static void main(String[] args){ 
   System.out.println("main method");
  } 
  public static void main(String args){ 
   System.out.println("Overloaded main method");
  } 
}

Answer :

Will print "main method". There will be no errors or exceptions, because the main method can be overloaded in Java. It must be called from within the main method to execute like other methods.

Okay, after sharing the 10 questions, how many answers did you get right?

DD's self-developed Shanghai brand proxy auction business, click directly

Recommended in the past

Several regret medicines after Git submits the code

Why do most IOC containers use ApplicationContext instead of BeanFactory

JIRA, Confluence and other products will stop selling localized versions in February next year, which will affect nearly 90% of customers in China!

Use Intellij IDEA to create temporary files, the kind that Git can't track

The first domestic Bitcoin ransomware producer was arrested, but the process was a bit funny...

TIOBE announced the November list: Python is unstoppable, surpassing Java!

Scan and follow me

Study together, progress together

Book donation every week, continuous benefits

In-depth content

Recommend to join

Guess you like

Origin blog.csdn.net/j3T9Z7H/article/details/109684871