Java练习题1(有答案)(7)

2019-08-31 09:26

} } B:

public class Test { public Test() { java.util.Date date = new java.util.Date(); } } 1、 A. 2、 B.

3、 Neither 答案 1 2 第 103题 You should add the static keyword in the place of ? in Line ________ in the following code: 1 public class Test {

2 private int age;

3

4 public ? int square(int n) { 5 return n * n; 6 } 7

8 public ? int getAge() { 9 } 10}

1、 in line 4 2、 in line 8

3、 in both line 4 and line 8

4、 none 答案 1 第 104题 Analyze the following code. public class Test { public static void main(String[] args) { int n = 2; xMethod(n); System.out.println(\ } void xMethod(int n) { n++; } }

1、 The code has a compile error because xMethod does not return a value.

2、 The code has a compile error because xMethod is not declared static. 3、 The code prints n is 1.

4、 The code prints n is 2.

5、 The code prints n is 3. 答案 2 第 105题 What is the printout of the second println statement in the main method? public class Foo { int i; static int s;

public static void main(String[] args) { Foo f1 = new Foo();

System.out.println(\ Foo f2 = new Foo();

System.out.println(\ Foo f3 = new Foo();

System.out.println(\ } public Foo() { i++; s++; } }

1、 f2.i is 1 f2.s is 1 2、 f2.i is 1 f2.s is 2 3、 f2.i is 2 f2.s is 2

4、 f2.i is 2 f2.s is 1 答案 2 第 106题 What is the printout of the third println statement in the main method? public class Foo { int i; static int s;

public static void main(String[] args) { Foo f1 = new Foo();

System.out.println(\ Foo f2 = new Foo();

System.out.println(\ Foo f3 = new Foo();

System.out.println(\ } public Foo() { i++; s++; } }

1、 f3.i is 1 f3.s is 1 2、 f3.i is 1 f3.s is 2 3、 f3.i is 1 f3.s is 3 4、 f3.i is 3 f3.s is 1

5、 f3.i is 3 f3.s is 3 答案 3 第 107题 Analyze the following code: public class Test {

public static void main(String args[]) { NClass nc = new NClass(); nc.t = nc.t++; } } class NClass { int t;

private NClass() { } }

1、 The program has a compilation error because the NClass class has a private constructor.

2、 The program does not compile because the parameter list of the main method is wrong.

3、 The program compiles, but has a runtime error because t has no initial value.

4、 The program compiles and runs fine. 答案 1 第 108题 Analyze the following code:

public class Test { private int t; public static void main(String[] args) { int x; System.out.println(t); } }

1、 The variable t is not initialized and therefore causes errors. 2、 The variable t is private and therefore cannot be accessed in the main method.

3、 t is non-static and it cannot be referenced in a static context in the main method.

4、 The variable x is not initialized and therefore causes errors. 5、 The program compiles and runs fine. 答案 3 第 109题 Analyze the following code and choose the best answer: public

class

Foo

{ private int x;

public static void main(String[] args) { Foo foo = new Foo(); System.out.println(foo.x); } }

1、 Since x is private, it cannot be accessed from an object foo.

2、 Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code.

3、 Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code.

4、 You cannot create a self-referenced object; that is, foo is created inside the class Foo. 答案 3 第 110题 When invoking a method with an object argument, ___________ is passed. 1、 the contents of the object 2、 a copy of the object 3、 the reference of the object

4、 the object is copied, then the reference of the copied object 答案 3 第 111题 What is the value of myCount.count displayed? public class Test {

public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times); System.out.println(

\ System.out.println(\ } public static void increment(Count c, int times) { c.count++; times++; } } class Count { int count; Count(int c) { count = c; } Count() { count = 1; } } 1、 101 2、 100

3、 99 4、 98 答案 1 第 112题 What is the value of times displayed? public class Test {

public static void main(String[] args) { Count myCount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(myCount, times);


Java练习题1(有答案)(7).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017-2018学年度第一学期小学教研工作计划及打算

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: