楼主: wangcunjiang

[精华] Java陷阱一箩筐----面试题集[zt]

[复制链接]
论坛徽章:
39
ITPUB元老
日期:2005-10-13 10:38:002012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB官方微博粉丝徽章
日期:2011-07-04 09:42:062011新春纪念徽章
日期:2011-02-18 11:43:352011新春纪念徽章
日期:2011-01-04 10:36:17ITPUB9周年纪念徽章
日期:2010-10-08 09:34:022010新春纪念徽章
日期:2010-03-01 11:07:27祖国60周年纪念徽章
日期:2009-10-09 08:28:002009新春纪念徽章
日期:2009-01-04 14:52:282008新春纪念徽章
日期:2008-02-13 12:43:03
21#
 楼主| 发表于 2004-7-23 23:37 | 只看该作者
第十,Math.round(11.5)等於多少? Math.round(-11.5)等於多少?
Math.round(11.5)返回(long)12,Math.round(-11.5)返回(long)-11;

使用道具 举报

回复
论坛徽章:
39
ITPUB元老
日期:2005-10-13 10:38:002012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB官方微博粉丝徽章
日期:2011-07-04 09:42:062011新春纪念徽章
日期:2011-02-18 11:43:352011新春纪念徽章
日期:2011-01-04 10:36:17ITPUB9周年纪念徽章
日期:2010-10-08 09:34:022010新春纪念徽章
日期:2010-03-01 11:07:27祖国60周年纪念徽章
日期:2009-10-09 08:28:002009新春纪念徽章
日期:2009-01-04 14:52:282008新春纪念徽章
日期:2008-02-13 12:43:03
22#
 楼主| 发表于 2004-7-23 23:55 | 只看该作者
第十一,short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
short s1 = 1; s1 = s1 + 1;有错,s1是short型,s1+1是int型,不能显式转化为short型。可修改为s1 =(short)(s1 + 1) 。
short s1 = 1; s1 += 1正确。不知道为什么

测试代码
  1. public class Test
  2. {
  3.         public static void main(String[] args){
  4.                 //--1
  5.                 /*
  6.                 short s1 = 1;
  7.                 s1 = s1 + 1;
  8.                 System.out.println(s1);
  9.                 */
  10.                 //--2
  11.                 short s1 = 1;
  12.                 s1 += 1;
  13.                 System.out.println(s1);
  14.         }
  15. }
复制代码

使用道具 举报

回复
论坛徽章:
39
ITPUB元老
日期:2005-10-13 10:38:002012新春纪念徽章
日期:2012-01-04 11:49:54ITPUB官方微博粉丝徽章
日期:2011-07-04 09:42:062011新春纪念徽章
日期:2011-02-18 11:43:352011新春纪念徽章
日期:2011-01-04 10:36:17ITPUB9周年纪念徽章
日期:2010-10-08 09:34:022010新春纪念徽章
日期:2010-03-01 11:07:27祖国60周年纪念徽章
日期:2009-10-09 08:28:002009新春纪念徽章
日期:2009-01-04 14:52:282008新春纪念徽章
日期:2008-02-13 12:43:03
23#
 楼主| 发表于 2004-7-24 00:00 | 只看该作者
第十二,sleep() 和 wait() 有什么区别? 搞线程的最爱
sleep()方法是使线程停止一段时间的方法。在sleep 时间间隔期满后,线程不一定立即恢复执行。这是因为在那个时刻,其它线程可能正在运行而且没有被调度为放弃执行,除非(a)“醒来”的线程具有更高的优先级
(b)正在运行的线程因为其它原因而阻塞。
wait()是线程交互时,如果线程对一个同步对象x 发出一个wait()调用,该线程会暂停执行,被调对象进入等待状态,直到被唤醒或等待时间到。

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
24#
发表于 2004-7-24 00:12 | 只看该作者
最初由 wangcunjiang 发布
[B]第四,&和&&的区别。
&是位运算符。&&是布尔逻辑运算符。

yining的说法(第一个会判断所有条件,第二个只要有一个条件不符合就会返回false),目前没有见过如此代码 [/B]


15.22.2 Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean, then the type of the bitwise operator expression is boolean.
For &, the result value is true if both operand values are true; otherwise, the result is false.

For ^, the result value is true if the operand values are different; otherwise, the result is false.

For |, the result value is false if both operand values are false; otherwise, the result is true.

这个是从Java Language Specification抄来的。bitwise operator当然有可能,不过和一个logical operator比较,有点不着边际,所以我觉得原来的用意应该是比较两种logical operator的区别。

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
25#
发表于 2004-7-24 00:23 | 只看该作者
最初由 wangcunjiang 发布
[B]第十一,short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
short s1 = 1; s1 = s1 + 1;有错,s1是short型,s1+1是int型,不能显式转化为short型。可修改为s1 =(short)(s1 + 1) 。
short s1 = 1; s1 += 1正确。不知道为什么

测试代码
  1. public class Test
  2. {
  3.         public static void main(String[] args){
  4.                 //--1
  5.                 /*
  6.                 short s1 = 1;
  7.                 s1 = s1 + 1;
  8.                 System.out.println(s1);
  9.                 */
  10.                 //--2
  11.                 short s1 = 1;
  12.                 s1 += 1;
  13.                 System.out.println(s1);
  14.         }
  15. }
复制代码
[/B]


对于java来说,s1 += 1由于1是整数,所以有一个narrowing conversion。只要在short的范围之内,就可以接受。但是对于s1= s1 +1来说,由于1是int,所以对于右边的s1有一个widening conversion,s1+1的结果是int,因此不能直接付值。马后炮的解说。

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
26#
发表于 2004-7-24 00:24 | 只看该作者
5.1.2 Widening Primitive Conversion
The following 19 specific conversions on primitive types are called the widening primitive conversions:

byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type and from float to double do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float to double in strictfp expressions also preserve the numeric value exactly; however, such conversions that are not strictfp may lose information about the overall magnitude of the converted value.
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

A widening conversion of a signed integer value to an integral type T simply sign-extends the two's-complement representation of the integer value to fill the wider format. A widening conversion of a character to an integral type T zero-extends the representation of the character value to fill the wider format.

Despite the fact that loss of precision may occur, widening conversions among primitive types never result in a run-time exception (§11).

Here is an example of a widening conversion that loses precision:

class Test {
        public static void main(String[] args) {
                int big = 1234567890;
                float approx = big;
                System.out.println(big - (int)approx);
        }
}

which prints:

-46

thus indicating that information was lost during the conversion from type int to type float because values of type float are not precise to nine significant digits.

5.1.3 Narrowing Primitive Conversions
The following 23 specific conversions on primitive types are called the narrowing primitive conversions:

byte to char
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
Narrowing conversions may lose information about the overall magnitude of a numeric value and may also lose precision.
A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

A narrowing conversion of a character to an integral type T likewise simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the resulting value to be a negative number, even though characters represent 16-bit unsigned integer values.

A narrowing conversion of a floating-point number to an integral type T takes two steps:


In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows:
If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0.
Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). Then there are two cases:
If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V.
Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V.
Otherwise, one of the following two cases must be true:
The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long.
The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long.
In the second step:
If T is int or long, the result of the conversion is the result of the first step.
If T is byte, char, or short, the result of the conversion is the result of a narrowing conversion to type T (§5.1.3) of the result of the first step.
The example:

class Test {
        public static void main(String[] args) {
                float fmin = Float.NEGATIVE_INFINITY;
                float fmax = Float.POSITIVE_INFINITY;
                System.out.println("long: " + (long)fmin +
                                                ".." + (long)fmax);
                System.out.println("int: " + (int)fmin +
                                                ".." + (int)fmax);
                System.out.println("short: " + (short)fmin +
                                                ".." + (short)fmax);
                System.out.println("char: " + (int)(char)fmin +
                                                ".." + (int)(char)fmax);
                System.out.println("byte: " + (byte)fmin +
                                                ".." + (byte)fmax);
        }
}

produces the output:

long: -9223372036854775808..9223372036854775807
int: -2147483648..2147483647
short: 0..-1
char: 0..65535
byte: 0..-1


The results for char, int, and long are unsurprising, producing the minimum and maximum representable values of the type.
The results for byte and short lose information about the sign and magnitude of the numeric values and also lose precision. The results can be understood by examining the low order bits of the minimum and maximum int. The minimum int is, in hexadecimal, 0x80000000, and the maximum int is 0x7fffffff. This explains the short results, which are the low 16 bits of these values, namely, 0x0000 and 0xffff; it explains the char results, which also are the low 16 bits of these values, namely, '\u0000' and '\uffff'; and it explains the byte results, which are the low 8 bits of these values, namely, 0x00 and 0xff.


Despite the fact that overflow, underflow, or other loss of information may occur, narrowing conversions among primitive types never result in a run-time exception (§11).
Here is a small test program that demonstrates a number of narrowing conversions that lose information:


class Test {
        public static void main(String[] args) {

                // A narrowing of int to short loses high bits:
                System.out.println("(short)0x12345678==0x" +
                                        Integer.toHexString((short)0x12345678));

                // A int value not fitting in byte changes sign and magnitude:
                System.out.println("(byte)255==" + (byte)255);

                // A float value too big to fit gives largest int value:
                System.out.println("(int)1e20f==" + (int)1e20f);

                // A NaN converted to int yields zero:
                System.out.println("(int)NaN==" + (int)Float.NaN);
               
                // A double value too large for float yields infinity:
                System.out.println("(float)-1e100==" + (float)-1e100);

                // A double value too small for float underflows to zero:
                System.out.println("(float)1e-50==" + (float)1e-50);
        }
}

This test program produces the following output:

(short)0x12345678==0x5678
(byte)255==-1
(int)1e20f==2147483647
(int)NaN==0
(float)-1e100==-Infinity
(float)1e-50==0.0

5.1.4 Widening Reference Conversions
The following conversions are called the widening reference conversions:

From any class type S to any class type T, provided that S is a subclass of T. (An important special case is that there is a widening conversion to the class type Object from any other class type.)
From any class type S to any interface type K, provided that S implements K.
From the null type to any class type, interface type, or array type.
From any interface type J to any interface type K, provided that J is a subinterface of K.
From any interface type to type Object.
From any array type to type Object.
From any array type to type Cloneable.
From any array type to type java.io.Serializable
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC.
Such conversions never require a special action at run time and therefore never throw an exception at run time. They consist simply in regarding a reference as having some other type in a manner that can be proved correct at compile time.
See §8 for the detailed specifications for classes, §9 for interfaces, and §10 for arrays.


5.1.5 Narrowing Reference Conversions
The following conversions are called the narrowing reference conversions:

From any class type S to any class type T, provided that S is a superclass of T. (An important special case is that there is a narrowing conversion from the class type Object to any other class type.)
From any class type S to any interface type K, provided that S is not final and does not implement K. (An important special case is that there is a narrowing conversion from the class type Object to any interface type.)
From type Object to any array type.
From type Object to any interface type.
From any interface type J to any class type T that is not final.
From any interface type J to any class type T that is final, provided that T implements J.
From any interface type J to any interface type K, provided that J is not a subinterface of K and there is no method name m such that J and K both contain a method named m with the same signature but different return types.
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a narrowing conversion from SC to TC.
Such conversions require a test at run time to find out whether the actual reference value is a legitimate value of the new type. If not, then a ClassCastException is thrown.

5.1.6 String Conversions
There is a string conversion to type String from every other type, including the null type.

5.1.7 Forbidden Conversions
There is no permitted conversion from any reference type to any primitive type.
Except for the string conversions, there is no permitted conversion from any primitive type to any reference type.
There is no permitted conversion from the null type to any primitive type.
There is no permitted conversion to the null type other than the identity conversion.
There is no permitted conversion to the type boolean other than the identity conversion.
There is no permitted conversion from the type boolean other than the identity conversion and string conversion.
There is no permitted conversion other than string conversion from class type S to a different class type T if S is not a subclass of T and T is not a subclass of S.
There is no permitted conversion from class type S to interface type K if S is final and does not implement K.
There is no permitted conversion from class type S to any array type if S is not Object.
There is no permitted conversion other than string conversion from interface type J to class type T if T is final and does not implement J.
There is no permitted conversion from interface type J to interface type K if J and K contain methods with the same signature but different return types.
There is no permitted conversion from any array type to any class type other than Object or String.
There is no permitted conversion from any array type to any interface type, except to the interface types java.io.Serializable and Cloneable, which are implemented by all arrays.
There is no permitted conversion from array type SC[] to array type TC[] if there is no permitted conversion other than a string conversion from SC to TC.

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
27#
发表于 2004-7-24 03:46 | 只看该作者
最初由 臧圩人 发布
[B]偶有一些不同意见:

第二:可以继承,不能实现接口。
第三十:java永远是传值调用。包括primitive类型和对象参数。
       详细说明请见《java核心技术:卷I》(《Core java》)第四章。Method parameters一节。 [/B]


我可以肯定java中primitive类型是传值,而Object类型是引用的。
对于inner class,现在忘光了。等我查一下SCJP的资料。

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
28#
发表于 2004-7-24 03:51 | 只看该作者
最初由 wangcunjiang 发布
[B]第三,Static Nested Class 和 Inner Class的不同,说得越多越好(面试题有的很笼统)。
Nested Class (一般是C++的说法),Inner Class (一般是JAVA的说法)。Java内部类与C++嵌套类最大的不同就在于是否有指向外部的引用上。
注: 静态内部类(Inner Class)意味着
1创建一个static内部类的对象,不需要一个外部类对象
2不能从一个static内部类的一个对象访问一个外部类对象 [/B]


SCJP的教材中说:it is common to speak of the static memebers as nested and the nested classes that are memebers of instances of the enclosing class as inner classes.

使用道具 举报

回复
论坛徽章:
55
生肖徽章:虎
日期:2006-09-06 21:14:232011新春纪念徽章
日期:2011-01-25 15:41:502011新春纪念徽章
日期:2011-01-25 15:42:152011新春纪念徽章
日期:2011-01-25 15:42:332011新春纪念徽章
日期:2011-01-25 15:42:56管理团队成员
日期:2011-05-07 01:45:082012新春纪念徽章
日期:2012-01-04 11:49:542012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:182012新春纪念徽章
日期:2012-02-13 15:11:18
29#
发表于 2004-7-24 03:55 | 只看该作者

  1. //: c08:Parcel6.java
  2. // A method that returns an anonymous inner class.

  3. public class Parcel6 {
  4.   public Contents cont() {
  5.     return new Contents() {
  6.       private int i = 11;
  7.       public int value() { return i; }
  8.     }; // Semicolon required in this case
  9.   }
  10.   public static void main(String[] args) {
  11.     Parcel6 p = new Parcel6();
  12.     Contents c = p.cont();
  13.   }
  14. } ///:~
复制代码


以上是Thinking in Java的例子,这个例子相当于:

  1. return new Contents() {
  2.   private int i = 11;
  3.   public int value() { return i; }
  4. };
复制代码


所以anonymous inner class不应该再允许继承了。因为anonymous本身已经隐含了继承。

使用道具 举报

回复
论坛徽章:
4
授权会员
日期:2005-10-30 17:05:33管理团队2006纪念徽章
日期:2006-04-16 22:44:452011新春纪念徽章
日期:2011-02-18 11:42:48ITPUB 11周年纪念徽章
日期:2012-10-09 18:03:32
30#
发表于 2004-7-24 08:11 | 只看该作者
第十一,short s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
short s1 = 1; s1 = s1 + 1;有错,s1是short型,s1+1是int型,不能显式转化为short型。可修改为s1 =(short)(s1 + 1) 。
short s1 = 1; s1 += 1正确。不知道为什么


Thanks for verification.
The following is from Java Specification that may give the explanantion why the second one is OK.

5.6.1 Unary Numeric Promotion
Some operators apply unary numeric promotion to a single operand, which must produce a value of a numeric type:

If the operand is of compile-time type byte, short, or char, unary numeric promotion promotes it to a value of type int by a widening conversion (§5.1.2).
Otherwise, a unary numeric operand remains as is and is not converted.
In either case, value set conversion (§5.1.8) is then applied.
Unary numeric promotion is performed on expressions in the following situations:


Each dimension expression in an array creation expression (§15.10)
The index expression in an array access expression (§15.13)
The operand of a unary plus operator + (§15.15.3)
The operand of a unary minus operator - (§15.15.4)
The operand of a bitwise complement operator ~ (§15.15.5)
Each operand, separately, of a shift operator >>, >>>, or << (§15.19); therefore a long shift distance (right operand) does not promote the value being shifted (left operand) to long

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表