先发几个,后面还会有.1 =================================11. public String makinStrings() {
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
这个还勉强可以理解,看了答案以后,11. public class Yikes {
12.
13. public static void go(Long n) {System.out.println(”Long “);}
14. public static void go(Short n) {System.out.println(”Short “);}
15. public static void go(int n) {System.out.println(”int “);}
16. public static void main(String [] args) {
17. short y= 6;
18. long z= 7;
19. go(y);
20. go(z);
21. }
22. }
What is the result?
A. int Long
B. Short Long
C. Compilation fails.
D. An exception is thrown at runtime.
都编译了
第1个s = s.substring(2, 5);改变了成s="ed4";
第2个不明白 go(z)编译不过去
是SCJP吧 以前看见过楼上的楼上 你用的是1.5以前的JDK吧 这个事1.5的自动包装的新功能 long会自动包装成Long类
C
A
但对第二题的A 不明白为什么,传的是short类型 回打印出int??
第二题就不是很清楚了,类型转换和参数传递问题,可以做实验
后面两个不产生新的对象
第二题:选 C 编译不过去
go(Long n)这个里面的Long ,L要小写
go(Short n)同理,S也要小写
这样的话
结果是:C
short long
是SCJP吧 以前看见过楼上的楼上 你用的是1.5以前的JDK吧 这个事1.5的自动包装的新功能 long会自动包装成Long类
说的是我吧!我是1.4的jdk
12. String s = “Fred”;
13. s = s + “47”;
14. s = s.substring(2, 5);
15. s = s.toUpperCase();
16. return s.toString();
17. }
How many String objects will be created when this method is invoked?一开始没注意invoked这个词,以为答案是5...
"Fred","47"是常量,在编译期就会放入常量池,所以不包括在此题中..
s + “47”;
s.substring(2, 5);
s.toUpperCase();
会各自生成一个新对象,所以产生了3个
s.toString();返回的是本身,不会产生新对象
个人理解
11. public static void main(String[] args) {
12. try {
13. File directory = new File(”d”);
14. File file = new File(directory,”f”);
15. if(!file.exists()) {
16. file.createNewFile();
17. }
18. } catch (IOException e) {
19. e.printStackTrace
20. }
21. }
22. }
The current directory does NOT contain a directory named “d.”
Which three are true? (Choose three.)
A. Line 16 is never executed.
B. An exception is thrown at runtime.
C. Line 13 creates a File object named “d.”
D. Line 14 creates a File object named “f.’
E. Line 13 creates a directory named “d” in the file system.
F. Line 16 creates a directory named “d” and a file ‘f’ within it in the
file system.
G. Line 14 creates a file named ‘f’ inside of the directory named “d” in
the file system.
不过小弟的英语超级的菜,麻烦解释以下invoked的含义
你是用short y=6;编译器默认short为int型,所以输出为int。
------------
如果你把上面的赋值语句改为Short y=6;y作为Short类的实例,则会输出short。
“你是用short y=6;”
“如果你把上面的赋值语句改为Short y=6;”
有区别吗??晕
定义为“short y=6;”的时候,因为short不匹配方法中的Short,所以系统会自动扩展,首先扩展为基本类型Int,检测一次,存在这种方法,则返回相应值。
而定义为“Short y=6;”的时候,已经存在相应的方法,就不会进行扩展。
“short y=6;”与“Short y=6;”不是同一个东西。前一个是基本类型,后一个是对象
short -> int / long
short -> Short
但是,扩展只能做一次,也就是说,
short -> int -> Integer,由short转化为Integer对象类型,是不会发生的.
这个题目,不难,但是对于序列化的概念,算是个入门的了解,也与各位兄弟共享之.12. import java.io.*;
13. public class Forest implements Serializable {
14. private Tree tree = new Tree();
15. public static void main(String [] args) {
16. Forest f= new Forest();
17. try {
18. FileOutputStream fs = new FileOutputStream(”Forest.ser”);
19. ObjectOutputStream os = new ObjectOutputStream(fs);
20. os.writeObject(f); os.close();
21. } catch (Exception ex) { ex.printStackTrace(); }
22. } }
23.
24. class Tree { }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. An instance of Forest is serialized.
D. A instance of Forest and an instance of Tree are both serialized.
这个有点不明白了.
主要是不明白"Assuming that the serializeBanana2() and the deserializeBanana2()
methods will correctly use Java serialization and given"的含义,这样的两个函数,该怎么写呢??Assuming that the serializeBanana2() and the deserializeBanana2()
methods will correctly use Java serialization and given:
13. import java.io.*;
14. class Food {Food() { System.out.print(”1”); } }
15. class Fruit extends Food implements Serializable {
16. Fruit() { System.out.print(”2”); } }
17. public class Banana2 extends Fruit { int size = 42;
18. public static void main(String [] args) {
19. Banana2 b = new Banana2();
20. b.serializeBanana2(b); // assume correct serialization
21. b = b.deserializeBanana2(b); // assume correct
22. System.out.println(” restored “+ b.size + “ “); }
23. // more Banana2 methods
24. }
What is the result?
A. Compilation fails.
B. 1 restored 42
C. 12 restored 42
D. 121 restored 42
E. 1212 restored 42
F. An exception is thrown at runtime.
11. private int x;
12. public int getX() { return x; }
12.publicFoo(int x){this.x=x; }
13. private void writeObject( ObjectOutputStream s)
14. throws IOException {
15. // insert code here
16. }
17. }
Which code fragment, inserted at line 15, will allow Foo objects to be
correctly serialized and deserialized?
A. s.writeInt(x);
B. s.serialize(x);
C. s.writeObject(x);
D. s.defaultWriteObject();
try {
Thread.sleep(n);
} catch (Exception e) {
System.out.print("Error ");
}
} public static void main(String[] args) {
System.out.print("StartMain ");
new Thread1().start();
delay(1000);
Thread t2 = new Thread2();
t2.start();
delay(1000);
t2.interrupt();
delay(1000);
System.out.print("EndMain ");
} static class Thread1 extends Thread {
public void run() {
synchronized (resource) {
System.out.print("Startl ");
delay(6000);
System.out.print("End1 ");
}
}
} static class Thread2 extends Thread {
public void run() {
synchronized (resource) {
System.out.print("Start2 ");
delay(2000);
System.out.print("End2 ");
}
}
}
}