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} /* ...
随机推荐
- sql server 批量备份数据库
很多时候,我们都需要将数据库进行备份,当服务器上数据库较多时,不可能一个数据库创建一个定时任务进行备份,这时,就需要进行批量的数据库备份操作,好了,废话不多说,具体实现语句如下: --开启文件夹权限 ...
- vsftpd安装配置虚拟用户
原文发表于cu:2016-03-11 参考文档: FTP原理:http://vbird.dic.ksu.edu.tw/linux_server/0410vsftpd_1.php FTP配置:http: ...
- ab命令做压测测试
1. 背景:互联网发达的今天,大大小小的网站如雨后春笋,不断出现,但是想要做出一个网站很简单,但是想要做好一个网站,非常非常难,首先:网站做好之后的功能怎么样这都是次要的,主要的是你的网站能承受怎么样 ...
- nagios监控安装esxi的服务器(宿主机)
首先,该博文大部分内容来自网络,少部分是自己监控过程中遇到的问题.如果有侵权,请联系告知!!! 现在互联网公司,有能力的都是自己研发监控系统,要么就是zabbix或者小米的监控,还都二次开发等等,可能 ...
- text-align与vertical-align属性的区别
1.text-align属性设置元素在水平方向(x轴)的位置 text-align:left://文本居左 text-align:center://文本居中 text-align:right: //文 ...
- Python Requests库入门——应用实例-京东商品页面爬取+模拟浏览器爬取信息
京东商品页面爬取 选择了一款荣耀手机的页面(给华为打广告了,荣耀play真心不错) import requests url = "https://item.jd.com/7479912.ht ...
- 项目进行ing
1.我们的看板 2.立行会议 (1)照片 (2)时间:每天20:00 (3)地点:学校研发中心会议室 3.看板进展: 已有6个任务被移到Check Out栏中,详细情况如下: 梁植淋:构建项目架构,封 ...
- 探路者 Alpha阶段中间产物
版本控制 git地址:https://git.coding.net/clairewyd/toReadSnake.git 贪吃蛇(单词版)软件功能说明书 1 开发背景 “贪吃蛇”这个游戏对于80, ...
- 20135208JAVA第二次试验
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:贺邦 学号:20135208 成绩: 指导教师:娄嘉鹏 ...
- 20162328蔡文琛 week06 大二
20162328 2017-2018-1 <程序设计与数据结构>第6周学习总结 教材学习内容总结 队列元素按FIFO的方式处理----最先进入的元素最先离开. 队列是保存重复编码k值得一种 ...