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} /* ...
随机推荐
- ubuntu18.04安装mongoDB 4.0
STEP 1: 在终端输入GPK码 $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334B ...
- Java EE JSP内置对象及表达式语言
一.JSP内置对象 JSP根据Servlet API规范提供了一些内置对象,开发者不用事先声明就可使用标准变量来访问这些对象. JSP提供了9种内置对象: (一).request 简述: JSP编程中 ...
- Laxcus大数据操作系统单机集群版
Laxcus大数据管理系统是我们Laxcus大数据实验室历时5年,全体系全功能设计研发的大数据产品,目前的最新版本是2.1版本.从三年前的1.0版本开始,Laxcus大数据系统投入到多个大数据和云计算 ...
- Debian 9 + Windows 10 双系统安装体验
很久之前就想在自己的电脑上也装个 Debian 玩玩了,最近正好有时间折腾,就踩了踩坑在笔记本上装了玩玩~ UEFI + GPT 解决启动相关的麻烦配置 如果在支持 UEFI 的电脑上安装 Debia ...
- redis 常用命令 结合php
这篇文章主要介绍了30个php操作redis常用方法代码例子,本文其实不止30个方法,可以操作string类型.list类型和set类型的数据,需要的朋友可以参考下 redis的操作很多的,以 ...
- Polycarp and Letters(set首战!)
Description Polycarp loves lowercase letters and dislikes uppercase ones. Once he got a string s con ...
- TCP系列50—拥塞控制—13、Eifel探测下的拥塞撤销
一.概述 我们之前在SACK关闭场景下的拥塞撤销那篇文章中提到过Eifel探测算法(Eifel Detection Algorithm),最早在介绍DSACK和FRTO的时候我们就有提到过Eifel探 ...
- (三)MySQL终极篇
1.索引 详细介绍:http://www.cnblogs.com/57rongjielong/p/8039452.html 索引是对数据库表中一个或多个列的值进行排序的结构.索引是经过某种算法优化过的 ...
- ubuntu 手动apache记录
1.下载apache tar -xvzf httpd.xx 解压 2.下载安装pcre Download PCRE from PCRE.org 解压,进入文件夹中 ./configure --pre ...
- Laravel 5.4 数据库迁移一次之后就不起作用!
https://segmentfault.com/q/1010000010806351 我在命令行中生成了一个新的迁移脚本: 当我执行命令:php artisan migrate 时 显示迁移成功,并 ...