浙大 java quiz,Java Quiz(1)

Java Quiz(一)

我准备发一些Java的小Quiz,这些题目看似比较简单,但如果一不小心,就会得出错误的结论,大家小心咯。

1634007772.gif

如果你有什么意见或者建议,请留言或者加我的QQ:472429029,验证信息写Java,javaeye什么的都行。

一。请问下面的代码的运行结果是?

/**

* created on 2009-4-13

* Author softkid

*/

package cn.softkid;

public class MulOrAdd {

public static void main(String[] args) {

int x=5,y=5;

x *= 5+2;

y = y*5+2;

System.out.println("Value of X is : "+x);

System.out.println("Value of Y is : "+y);

}

}

怎么样?简单吧!请留言给出你的结果吧!然后编译运行试试,看运行生成的结果是否和你想的一样?

二。请问下面的代码的运行结果是?

/**

* created on 2009-4-13

* Author softkid

*/

package cn.softkid;

public class MyDouble {

public static void main(String[] args) {

for(double d = 0.0; d != 1.0; d = d + 0.1){

System.out.println("Tesging...");

}

}

}

两道题目下来,你是否觉得我的题目很幼稚呢?如果有兴趣,请继续吧!

三。下面一个是关于循环的,请问运行的结果是?

/**

* created on 2009-4-13

* Author softkid

*/

package cn.softkid;

public class MyLoop {

public static void main(String[] args) {

for(int i=0;i<10;i++){

for(i=0;i<5;i++){

System.out.println("Testing");

}

}

}

}

这一个应该没什么问题?但如果被前面的搞迷糊了,那就不一定了!

四。接下来扯到Integer和String了,请问下面代码运行的结果是?

/**

* created on 2009-4-13

* Author softkid

*/

package cn.softkid;

public class Equality {

public static void main(String args[]) {

Integer first = 100;

Integer second = 100;

String str1 = "abc";

String str2 = "abc";

String str3 = new String("abc");

String str4 = new String("abc");

if (first == second)

System.out.println("first == second");

else

System.out.println("first != second");

if (str1 == str2)

System.out.println("str1 == str2");

else

System.out.println("str1 != str2");

if (str1 .equals(str3))

System.out.println("str1 equals str3");

else

System.out.println("str1 not equals str3");

if (str3 == str4)

System.out.println("str3 == str4");

else

System.out.println("str3 != str4");

}

}

这一次就到这里吧,下次再继续!

1 楼

wthwth

2009-04-13

1

x *= (5+2);

y = (y*5+2);

2

浮点数不可以用==比较

3

两个i是同一个变量

4

first == second  值不同

str1 == str2  地址相同,初始化时自动优化的?

str1 equals str3  值相同

str3 != str4 地址不同

2 楼

javacto

2009-04-14

1.

x* = (5+2)   结果是:35

y = y*5+2 = 27

2.

double 0.0~1.0  无穷数啊

3.

for(int i=0;i<10;i++){

for(i=0;i<5;i++){}

}   两个i是同一个变量,死循环

4.

first == second  指向相同

str1 == str2    "abc"被共用

str1 equals str3   值相同

str3!=str4         地址不同

3 楼

softkid

2009-04-14

wthwth 写道

str1 == str2  地址相同,初始化时自动优化的?

java内存有个常量池,执行String str1="abc";时,jvm先检查常量池中是否有字符串常量"abc",如果没有

则在常量池中生成之。再执行String str2="abc";时,jvm检查到了常量池中的"abc"字符串常量,然后将str2指向

该字符串常量。

4 楼

shaobin0604

2009-05-24

Value of X is : 35

Value of Y is : 27

死循环

死循环

first == second

str1 == str2

str1 equals str3

str3 != str4

5 楼

softkid

2009-05-24

shaobin0604 写道

Value of X is : 35

Value of Y is : 27

死循环

死循环

first == second

str1 == str2

str1 equals str3

str3 != str4

正解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值