java题(8)
Question: 31
Given:
11. Float pi = new Float(3.14f);
12. if (pi > 3) {
13. System.out.print("pi is bigger than 3. ");
14. }
15. else {
16. System.out.print("pi is not bigger than 3. ");
17. }
18. finally {
19. System.out.println("Have a nice day.");
20. }
What is the result?
A. Compilation fails.
B. pi is bigger than 3.
C. An exception occurs at runtime.
D. pi is bigger than 3. Have a nice day.
E. pi is not bigger than 3. Have a nice day.
Answer: A
Finally不能这样单独使用。
Question: 32
Given:
11. String test = "This is a test";
12. String[] tokens = test.split("\s");
13. System.out.println(tokens.length);
What is the result?
A. 0
B. 1
C. 4
D. Compilation fails.
E. An exception is thrown at runtime.
Answer: D
应该是\\s,否则识别不了为\s,识别出来的是s
Question: 33
Given:
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.
Answer: A
以下是基本数据的参数查找优先级
boolean参数查找优先级:boolean,Boolean
short参数查找优先级:short,int,Short
byte参数查找优先级:byte,short,int,long,Byte,float,double
char参数查找优先级:char,int,long,float,double,Character
int参数查找优先级:int,long,float,double,Integer
long参数查找优先级:long,float,double,Long
float参数查找优先级:float,double,Float
double参数查找优先级:double,Double
17行是基本类型short,对应的先查找short,没有short,查找int,发现有,它就不会查找到Short
20行,由上可得查找为Long。
Question: 34
Given:
12. System.out.format("Pi is approximately %d.", Math.PI);
What is the result?
A. Compilation fails.
B. Pi is approximately 3.
C. Pi is approximately 3.141593.
D. An exception is thrown at runtime.
Answer: D
如果要问为什么不是A,我也不会回答。。。。
Question: 35
Given:
33. Date d = new Date(0);
34. String ds = "December 15, 2004";
35. // insert code here
36. try {
37. d = df.parse(ds);
38. }
39. catch(ParseException e) {
40. System.out.println("Unable to parse " + ds);
41. }
42. // insert code here too
What creates the appropriate DateFormat object and adds a day to the Date object?
A. 35. DateFormat df = DateFormat.getDateFormat();
42. d.setTime( (60 * 60 * 24) + d.getTime());
B. 35. DateFormat df = DateFormat.getDateInstance();
42. d.setTime( (1000 * 60 * 60 * 24) + d.getTime());
C. 35. DateFormat df = DateFormat.getDateFormat();
42. d.setLocalTime( (1000*60*60*24) + d.getLocalTime());
D. 35. DateFormat df = DateFormat.getDateInstance();
42. d.setLocalTime( (60 * 60 * 24) + d.getLocalTime());
Answer: B
getLocalTime()查api是没有的说,api应该没撒谎,至少java8是没有的。
Getdateformat()也是这个状况。。。。