您的位置:首页 > 编程语言 > Java开发

4个小题测测你的java基础如何

2006-04-16 01:18 441 查看
下面来四个小题,不用上机看你是否都能答对..


预测控制台的输出

题1:

class A
{
 static
 {
  System.out.println("static a"); 
 }
 {
   System.out.println("just a"); 
 }
  public A(){System.out.println("instruct a");} 
}
public class Test extends A
{
  static
  {
    System.out.println("static b"); 
  }
  {
    System.out.println("just b"); 
  }
  public Test()
  {
    System.out.println("instruct b"); 
  }
  public static void main(String []args)
  {
    A a=new Test();
  } 
}

题2:

class A
{
 public String str="haha";
 public static int b=1;
  private int a;
  public void setA(int a)
  {
   this.a=a;
  }
  public int getA()
  {
    return a; 
  }
}
public class Test
{
  public static void main(String [] args)
 {
   Test test=new Test();
   A a=new A();
   test.changeValue(a.b,a,a.str);
   System.out.println(""+a.b+a.getA()+a.str);
   a.b=2;
   A b=new A();
   System.out.println(""+b.b+A.b);
 } 
 public void changeValue(int a ,A b,String str)
 {
   a=2;
   b.setA(2); 
   str="lala";
 }
}

题3:

class A
{
  public static int a=1;
  public static void  a()
  {
   System.out.println("a"); 
  } 
}
public class Test
{
  public static void main(String [] args) throws Exception
  {
    new Test();
  }
  public  Test() throws Exception
  {
   synchronized(this)
    {
     try
     {
      A a=null;
      a.a();
      this.wait();
      System.out.println(a.a+"ok done");
     }catch(java.lang.NullPointerException e)
     {
       System.out.println("null pointer");
     } catch(Exception e)
     {
       System.out.println("other exception"); 
     }
     finally
     {
       System.out.println("exit"); 
     }  
    }
  }
}

题4:

class A
{
  static
  {
   System.out.println("hehe"); 
  }
  {
   System.out.println("haha"); 
  }
  public A(){System.out.println("a");}; 
  public static void show()
  {
    System.out.println("show in A"); 
  }
  public void test()
  {
     System.out.println("test in A"); 
  }
}
class Test extends A
{
 public static void show()
 {
     System.out.println("show in Test"); 
 }
 public void test()
 {
   System.out.println("test in Test"); 
 }
 static
 {
   System.out.println("a ha");
   new A();
   new Test(); 
 }
 public static void main(String [] args)
 {
 
   Test test=new Test();
   ((A)test).show();
   ((A)test).test();
   test.show();
   test.test();
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息