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} /* ...
随机推荐
- Ubuntu 安装python后,安装python-dev
1.通常情况下: sudo apt install python-dev 或者 在 sudo apt install python 命令下安装应该也附带了 python-dev 上述 pyhthon ...
- Ubuntu用户设置文件说明
Ubuntu用户设置文件说明 Ubuntu作为Linux的一个发行版本,自然具有Linux系统的多用户特性.因为经常会使用和管理Ubuntu的用户,现将Ubuntu系统下的User的个性化配置整理如下 ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- Scrum立会报告+燃尽图(十月二十三日总第十四次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- php addslashes和stripslashes函数
addslashes — 使用反斜线引用字符串 stripslashes — 反引用一个引用字符串 Example #1 一个 addslashes() 例子 <?php$str = &qu ...
- Linux安装weblogic
一.软件安装 1. 安装前的准备工作 1.1 首先请确认您要安装的WebLogic版本所在的平台已通过了BEA的认证,完整的认证平台列表请参考 http://e-docs.bea.com/wls/ce ...
- 团队项目-BUG排查-ADT工程 To Android Studio 一整天的排查日记
4-22 10:44至4-23 0:45 ①打开Eclipse从Github上Clone MathsApp到本机,报错'Unable to resolve target'android-19' ②尝试 ...
- 博客搬家 --- CSDN
博客园好久没维护了,搬家吧 欢迎移步到新博客
- Spring 学习 3- AOP
什么是AOP aop就是纵向的编程,业务1和业务2都需要一个共同的操作,与其往每个业务中都添加同样的代码,不如写一遍代码,让两个业务共同使用这段代码. spring中面向切面编程用的是代理模式,它的实 ...