Java考试题
1. public class GC {
2. private Object o;
3. private voiddoSomethingElse(Object obj) { o = obj; }
4. public void doSomething() {
5. Object o = new Object();
6. doSomethingElse(o);
7. o = new Object();
8. doSomethingElse(null);
9. o = null;
10. }
11. }
When the doSomething method is called,after which line does the Object created in line 5 become available for garbagecollection?
A. Line 5 B. Line 6 C. Line 7 D. Line 8 E.Line 9
F. Line 10
Answer: D
用关键字new实例化一个对象A时,首先会调用父类的构造方法,然后再为A分配空间,完毕后就返回对象A的一个引用。 在这题中,第5行Object o = new Object();首先在堆栈中为Object类的一个实例分配空间,并且默认初始化为null,然后返回其引用并将其值赋给了 o o是存在堆栈中的。 假设这个实例是A 这样一来,o 便引用了A。 第6行:doSomethingElse(o) 调用此方法后,第5行中的o的值传递给了第2行中的o,所以第2行中的o和第5行的o引用了同一个对象实例,也就是都引用A 第7行:o=new OBject(); 这里又实例化类Object并返回了一个新的引用。局部变量o引用类Object的另一个实例 假设这个实例是B 现在呢,局部变量o引用A 全局变量o引用B 第8行:doSomethingElse(null) 这里把null传给了全局变量o B呢就没有被引用了,一个实例没有被引用,也就失去了存在的意义。所以在这里,B就可能会被垃圾回收机制回收,释放其在堆栈中所占用的空间。(可能会被回收,是因为垃圾回收机制是间隔一定时间后执行一次,系统才回收没有被引用的实例,在回收之前,而且程序在运行,那它是一直存在内存空间中的) 第9行和第8行一个道理,局部变量o也没引用A了
11. public static void test(Stringstr) {
12. int check = 4;
13. if (check = str.length()) {
14. System.out.print(str.charAt(check-= 1) +", ");
15. } else {
16. System.out.print(str.charAt(0)+ ", ");
17. }
18. } and the invocation:
21. test("four");
22. test("tee");
23. test("to");
What is the result?
A. r, t, t,
B. r, e, o,
C. Compilation fails.//第13行应该为==号
D. An exception is thrown atruntime.
Answer: C
QUESTION 35
Given:
31. class Foo {
32. public int a = 3;
33. public void addFive() { a += 5; System.out.print("f "); }
34. }
35. class Bar extends Foo {
36. public int a = 8;
37. public void addFive() { this.a += 5; System.out.print("b " ); }
38. }
Invoked with: Foo f = new Bar();
f.addFive();
System.out.println(f.a); //此处代码,相当于只打印了f的属性;
What is the result?
A. b 3
B. b 8
C. b 13
D. f 3
E. f 8
F. f 13
G. Compilation fails.
H. An exception is thrown at runtime.
属性是没有多态的只有方法有,在方法中调用的是Bar中的a
QUESTION 84Given:11. public class Commander {12. public static void main(String[] args) {13. String myProp = /* insert code here */14. System.out.println(myProp);15. }16. }and the command line: java -Dprop.custom=gobstopper Commander Which two, placed on line 13, willproduce the output gobstopper? (Choose two.)A. System.load("prop.custom");B. System.getenv("prop.custom");C. System.property("prop.custom");D. System.getProperty("prop.custom");E. System.getProperties().getProperty("prop.custom");Answer: DE
命令行参数 -D<propertyName>=value
在虚拟机的系统属性中设置属性名/值对,运行在此虚拟机之上的应用程序可用System.getProperty(“propertyName”)得到value的值。
如果value中有空格,则需要用双引号将该值括起来,如-Dname=”space string”。
该参数通常用于设置系统级全局变量值,如配置文件路径,应为该属性在程序中任何地方都可访问。
static Properties getProperties() 功能:Determines the current system properties.
String getProperty(String key) 功能:Searches for the property with the specified key in this property list.
QUESTION 85
Given:
3. public class Spock {
4. public static void main(String[] args) {
5. Long tail = 2000L;
6. Long distance = 1999L;
7. Long story = 1000L;
8. if((tail > distance) ^ ((story * 2) == tail))
9. System.out.print("1");
10. if((distance + 1 != tail) ^ ((story * 2) == distance))
11. System.out.print("2");
12. }
13. }
What is the result?
A. 1
B. 2
C. 12
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.
; i < 613; i++)
则输出是:[606, 608, 609, 610, 612] [608, 609, 610]
而按照以下更改:
- import java.util.TreeSet;
- public class Explorer3 {
- public static void main(String[] args) {
- TreeSet<Integer> s = new TreeSet<Integer>();
- TreeSet<Integer> subs = new TreeSet<Integer>();
- for(int i = 606; i < 613; i++)
- if(i%2 == 0) s.add(i);
- subs = (TreeSet)s.subSet(608, true, 611, true);
- s.add(609);
- System.out.println(s + " " + subs);
- }
- }
输出依然是:[606, 608, 609, 610, 612] [608, 609, 610]
可以说subs是s的一部分,谁更改都会影响到另一个。
QUESTION 100
Given:
11. // insert code here
12. private N min, max;
13. public N getMin() { return min; }
14. public N getMax() { return max; }
15. public void add(N added) {
16. if (min == null || added.doubleValue() < min.doubleValue())
17. min = added;
18. if (max == null || added.doubleValue() > max.doubleValue()) 19. max = added;
20. }
21. }
Which two, inserted at line 11, will allow the code to compile? (Choose two.)
A. public class MinMax<?> {
B. public class MinMax<? extends Number> {
C. public class MinMax<N extends Object> {
D. public class MinMax<N extends Number> {
E. public class MinMax<? extends Object> {
F. public class MinMax<N extends Integer> {
Answer: DF
Java考试题的更多相关文章
- Java考试题之十
QUESTION 230 Given: 10. class One { 11. public One foo() { return this; } 12. } 13. class Two extend ...
- Java考试题之九
QUESTION 177 Given: 1. class TestException extends Exception { } 2. class A { 3. public ...
- Java考试题之八
QUESTION 139 Giventhe following directory structure: bigProject |--source | |--Utils.java ||--classe ...
- Java考试题之六
QUESTION 134 Given:11. class Snoochy {12. Boochy booch;13. public Snoochy() { booch = new Boochy(thi ...
- Java考试题之五
QUESTION 102 Given: 23. Object [] myObjects = { 24. new Integer(12), 25. new String("foo") ...
- Java考试题之四
QUESTION 73 Given: 10: public class Hello { 11: String title; 12: int value; 13: public Hello() { 14 ...
- Java考试题之三
QUESTION 46Given:11. public class Test {12. public static void main(String [] args) {13. int x = 5;1 ...
- Java考试题之七
QUESTION 150 Click the Exhibit button. Given: ClassA a = new ClassA(); a.methodA(); What is the resu ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
随机推荐
- jenkins 入门教程
jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各种语言(比如:java, ...
- android 签名相关
查看keystorekeytool -list -v -keystore debug.keystoreapk签名不带别名 apksigner sign --ks debug.keystore test ...
- Vue 编程之路(一)——父子组件之间的数据传递
最近公司的一个项目中使用 Vue 2.0 + element UI 实现一个后台管理系统的前端部分,属于商城类型.其中部分页面是数据管理页,所以有很多可以复用的表格,故引入自定义组件.在这里分享一下开 ...
- Spring Cloud(三):服务提供与调用 Eureka【Finchley 版】
Spring Cloud(三):服务提供与调用 Eureka[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 上一篇文章我们介绍了 Eureka 服务 ...
- Python基础入门(模块和包)
1 模块 1.1 什么是模块 在 Python 中,一个 .py 文件就称之为一个模块(Module). 我们学习过函数,知道函数是实现一项或多项功能的一段程序 .其实模块就是函数功能的扩展.为什么这 ...
- Linux命令之tar命令
[root@linux ~]# tar [-cxtzjvfpPN] 文件与目录 .... 参数: -c :建立一个压缩文件的参数指令(create 的意思): -x :解开一个压缩文件的参数指令! - ...
- python—2.x中如何使用中文
python2.x 默认使用ASCII编码格式 python3.x 默认使用UTF-8编码格式 在python2.x文件的第一行增加一下代码,解释器会以utf-8编码来处理python文件. # *_ ...
- mysql 转换13位数字毫秒时间
MySQL毫秒值和日期转换,MYSQL内置函数FROM_UNIXTIME: select FROM_UNIXTIME(t.createDate/1000,'%Y-%m-%d %h:%i:%s') as ...
- xpath抓取的值有\r\n\t时,去掉的方法
解决办法: normalize-space() 例子: 原来的xpath为: user=selector.xpath('//*[@id="Con"]/tr[1]/th/text() ...
- multipart/form-data post 方法提交表单,后台获取不到数据
这个和servlet容器有关系,比如tomcat等. 1.get方式 get方式提交的话,表单项都保存在http header中,格式是 http://localhost:8080/hello.do? ...