Java基础思考题

以下代码先不要在机器上运行,你能答对几道?

 

1.*以下代码运行的结果是什么?
class Number{
 int i;
}

public class Assignment{
 public static void main(String[] args) {
  Number n1 = new Number();
  Number n2 = new Number();
  n1.i = 9;
  n2.i = 47;
  System.out.println ("n1.i="+n1.i+",n2.i="+n2.i);
  n1 = n2;
  n1.i = 50;
  System.out.println ("n1.i="+n1.i+",n2.i="+n2.i);  
 }
}

 

2.先看下列代码:
Integer n1 = new Integer(20);
Integer n2 = new Integer(20);
请问如果运行这行代码:System.out.println(n1==n2);打印出来的结果是true还是false?为什么?


3.请问以下程序运行的结果是什么?
public class Display{
 public static void main(String[] args) {
  int x = 2, y = 3, z = 4;
  String str = "x,y,z";
  System.out.println (str + x + y + z);
  System.out.println (x + y + z + str);
 }
}


4.请看下面一段关于构造函数的程序:
class Rock{
 public Rock() {
  System.out.println ("Creating Rock");
 }
}

public class SimpleConstructor{
 public static void main(String[] args) {
  for(int i=0; i<10; i++) {
   new Rock();
  }
 }
}
请问结果是什么?

 

5.请问下列程序的运行结果是什么?
class Letter{
 char c;
}

public class Pass {
 static void f(Letter y) {
  y.c = 'z';
 }
 public static void main(String[] args) {
  Letter x = new Letter();
  x.c = 'a';
  System.out.println ("之前x.c:"+x.c);
  f(x);
  System.out.println ("之后x.c:" + x.c);
 }
}

 

6.*请问以下代码的运行结果是什么?
   将new Rock(i)替换成new Rock( ),此时程序编译能够通过吗?为什么?
   如何修改能够让编译通过?
class Rock{
 public Rock(int i) {
  System.out.println ("Creating Rock " + i);
 }
}

public class SimpleConstructor{
 public static void main(String[] args) {
  for(int i=0; i<10; i++) {
   new Rock(i);
  }
 }
}

 

7.*请问以下代码的运行结果是什么?
class Tree{
 int height;
 public Tree() {
  System.out.println ("播下一颗种子");
  height = 0;
 }
 public Tree(int i) {
  System.out.println ("栽一棵"+i+"英尺高的树");
  height = i;
 }
 public void info() {
  System.out.println ("这棵树高"+ height + "英尺");
 }
 public void info(String str) {
  System.out.println (str + "树高"+ height + "英尺") ;
 }
}

public class OverLoading{
 public static void main(String[] args) {
  System.out.println ("***********调用无参的构造函数**********");
  Tree tree = new Tree();
  System.out.println ();//空一行
  
  for(int i = 0; i < 3; i++) {  
   System.out.println ("********调用重载的构造函数********");    
   Tree t = new Tree(i);
   t.info();
   t.info("调用重载的普通函数:");
   System.out.println ();
  }
 }
}

 

8.*失眠时数绵羊吧,请看下面这段代码:
class Sheep{
 int i;
 public Sheep increment( ) {
  i++;
  return this;
 }
 
 void print( ) {
  System.out.println ("i = " + i+"只");
 }
}

public class CountSheep{
 public static void main(String[] args) {
  Sheep s = new Sheep();
  System.out.print("绵羊数到了第");
  s.increment( ).increment( ).increment( ).print( );
 }
}
运行结果是什么?你是如何理解this的?


9.*请看下面这段代码:
class Flower{
 int petalCount = 0;
 String s = new String();
 
 Flower(int petals) {
  petalCount = petals;
  System.out.println ("调用了Flower(int petals)构造函数,petalCount="+ petalCount);
 }
 
 Flower(String str) {
  System.out.println ("调用了Flower(String str)构造函数,s="+ str);
  s = str;
 }
 
 Flower(String s, int petals) {
  this(petals);//使用this调用另一个构造函数,此句必须放在第一句,否则编译器报错
  this.s = s;//当方法中的局部变量与类成员同名时,利用this区别
 }
 
 Flower() {
  this("hi~~~", 47);
 }
 
 void print() {
  System.out.println ("petalCount=" + petalCount + "  s=" + s);
 }
}

public class CallConstructorWiththis{
 public static void main(String[] args) {
  Flower f = new Flower();
  f.print();
 }
}
(1)运行结果是什么?
(2)将构造函数Flower(String s, int petals) 中的this(petals)放到第二个语句的位置,如下:
  Flower(String s, int petals) {
   this.s = s;
   this(petals); 
  }
     试一试,可以吗?编译器不让编译通过的理由是什么?
(3)将构造函数Flower(String s, int petals) 修改:
       Flower(String s, int petals) {
   this(petals);
   this(s);
   this.s = s;//当方法中的局部变量与类成员同名时,利用this区别
  }
     这样做行吗?

 

10**.探索变量初始化
    下面代码的运行结果是什么?
class Tag {
 Tag(int marker) {
  System.out.println ("Tag(" + marker + ")");
 }
}

class Card {
 Tag t1 = new Tag(1);
 Card() {
  System.out.println ("Card()");
  t3 = new Tag(33);
 } 
 Tag t2 = new Tag(2);
 void f( ) {
  System.out.println ("f( )");
 }
 Tag t3  = new Tag(3);
}

public class OrderOfInitialization{
 public static void main(String[] args) {
  Card c = new Card( );
  c.f();
  Card c2 = new Card();
  c2.f();
 }
}

 

11.***探索静态成员初始化:
   下面的代码运行结果是什么?
class Bowl {
 Bowl(int marker) {
  System.out.println ("Bowl(" + marker + ")");
 }
 void f(int marker) {
  System.out.println ("f(" + marker + ")");
 }
}

class Table{
 static Bowl b1 = new Bowl(1);
 Table() {
  System.out.println ("Table()");
  b2.f(1);
 }
 void f2(int marker) {
  System.out.println ("f2(" + marker + ")");
 }
 static Bowl b2 = new Bowl(2);
}

class Cupboard {
 Bowl b3 = new Bowl(3);
 static Bowl b4 = new Bowl(4);
 Cupboard() {
  System.out.println ("Cupboard()");
  b4.f(2);
 }
 void f3(int marker) {
  System.out.println ("f3(" + marker + ")");
 }
 static Bowl b5 = new Bowl(5);
}

public class StaticInitialization {
 public static void main(String[] args) {
  System.out.println ("在main函数中");  
  new Cupboard();
  System.out.println ();
  new Cupboard();
  System.out.println ();
  t.f2(1);
  System.out.println ();
  c.f3(1);
 } 
 static Table t = new Table();
 static Cupboard c = new Cupboard();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值