15-Way Very classic Java face questions with detailed answers _java

Source: Internet
Author: User
Tags static class volatile

The questions are as follows:

Reference Answer:

Import java.io.FileNotFoundException;
Import java.io.IOException;
Import java.util.ArrayList;
Import Java.util.concurrent.ExecutorService;

Import java.util.concurrent.Executors;
 /** * Created by YSC on 7/26/16.
 * * Public class Interview {private static void one () {String str1 = "Hello";
 String str2 = "he" +new string ("Llo");
 System.err.println (STR1==STR2); System.out.println ("1").
 False ");
 private static void Two () {int i = Integer.max_value;
 System.err.println ((i+1) <i); System.out.println ("2").
 There is an I, which makes (i+1) <i ");
 private static void Three () {SYSTEM.ERR.PRINTLN ("GC is not a Java thread, it is a native thread"); Thread.getallstacktraces (). Keyset (). ForEach (thread-> System.out.println (thread.getname () + "->" +
 Thread.isdaemon () + "" +thread.getpriority ()); System.out.println ("3").
 GC thread is a daemon thread ");
 private static volatile int count = 0;
 private static void Four () {Executorservice executorservice = Executors.newcachedthreadpool (); for (int j=0; j<10; j) {
  Executorservice.submit (()->{for (int i=0; i<1000000; i++) {count++;
 }
  });
 } System.out.println ("Count should be: +10000000+", actual is: "+count);" System.out.println ("4").
 Volatile cannot guarantee thread safety ");
 private static void Five () {arraylist<integer> list = new arraylist<> (20);
 List.add (1);
 SYSTEM.OUT.PRINTLN ("Debug code, not execute Grow method"); System.out.println ("5").
 List grow 0 times ");
 private static void Six () {System.out.println ("BufferedReader ' constructor only accepts a Reader instance"); System.out.println ("6"). New BufferedReader (new FileInputStream (\ "a.dat\"));
 Is wrong ");
  private static void Seven () {try{if (true) {throw new IOException ();
 }}catch (FileNotFoundException e) {System.out.print ("filenotfoundexception!");
 }catch (IOException e) {System.out.print ("ioexception!");
 }catch (Exception e) {System.out.print ("exception!"); } System.out.println ("\n7.
 Ioexception! ");} private static void Eight () {System.out.println ("String s; System.out.println (s);
 Error:variable s might not have been initialized\nrecompile with-xlint:unchecked for details. "); System.out.println ("8").
 Because string s is not initialized, the code cannot be compiled through ");
 private static void Nine () {System.out.println ("5" +2); System.out.println ("9").
 52 ");
 private static void ten () {int i = 2;
 int result = 0;
  switch (i) {case 1:result = result + I;
  Case 2:result = result + I * 2;
 Case 3:result = result + I * 3;
 } System.out.println ("result=" +result); System.out.println ("10").
 10 ");
 private static class null{public static void Hello () {System.out.println ("Hello");
  public static void Main (string[] args) {(null) null). Hello ();
  Null _null = (null) null;
 _null.hello ();
 } private static class stringexample1{string str = new String ("good");
 char[] ch = {' A ', ' B ', ' C '};
  public void Change (String str, char[] ch) {str = ' test ok ';
 Ch[0] = ' g '; public static void Main (string[] args) {StringExample1 ex = new STringExample1 ();
  Ex.change (Ex.str, ex.ch);
  System.out.print (ex.str+ "and");
  System.out.print (ex.ch);
 System.out.println ();
 The private static class stringexample2{public static void Change (String str) {str = ' welcome ';
  public static void Main (string[] args) {String str = "1234";
  Change (str);
 System.out.println (str);
  The private static class forloop{static Boolean foo (char c) {System.out.print (c);
 return true;
  public static void Main (string[] args) {int i=0;
  For (foo (' A '); foo (' B ') && (i<2); foo (' C ')) {i++;
  Foo (' D ');
 } System.out.println ();
 The private static class Helloa{public Helloa () {System.out.println ("Helloa");

 } {System.out.println ("I ' M A class");}
 static {System.out.println ("static A");
 } private static class Hellob extends helloa{public Hellob () {System.out.println ("Hellob");

 {System.out.println ("I ' M B class");}
 static {System.out.println ("static B"); } public static void main(string[] args)
  {System.out.println ("main start");
  New Hellob ();
  New Hellob ();
 System.out.println ("main End");
 } public static void Main (string[] args) {one ();
 Two ();
 Three ();
 Four ();
 Five ();
 Six ();
 Seven ();
 Eight ();
 Nine ();
 Ten ();
 Null.main (NULL);
 Stringexample1.main (NULL);
 Stringexample2.main (NULL);
 Forloop.main (NULL);
 Hellob.main (NULL);
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.