6. TestPoly.java程序如下:
1. public class TestPoly {
2. public static void main(String [] args ){
3. Parent p = new Child();
4. }
5. }
6.
7. class Parent {
8. public Parent() {
9. super();
10. System.out.println("instantiate a parent");
11. }
12. }
13.
14. class Child extends Parent {
15. public Child() {
16. System.out.println("instantiate a child");
17. super();
18. }
19. }
指出此程序的错误之处?(Choose one.)
A. 第3行将子对象赋值给一个父对象引用
B. 第9行,Parent类无父类,故不能调用super()方法
C. 第17行,super()方法调用应置于System.out.println(“instantiate a child”)语句之前
D. 此程序可以正常运行,无错误
答案:C
解析://构造函数使用的时候前面用this或者是super。
构造函数特征:1具有与类相同的名称2没有返回值3不能在方法中用return语句返回值。
7. MyThread.java程序如下:
1. public class MyThread extends Thread {
2.
3. public static void main(String [] args) {
4. MyThread t = new MyThread();
5. t.run();
6. }
7.
8. public void run() {
9. for(int i=1;i<3;++i) {
10. System.out.print(i + "..");
11. }
12. }
13. }
编译并运行,则?(Choose one.)
A. 第4行错误,无法编译
B. 第5行错误,无法编译.
C. 可以正常编译,并输出:1..2..
D. 可以正常编译,并输出:1..2..3..
答案:C
8. AbstractTest.java程序如下:
1. public class AbstractTest {
2. public static void main(String[] args) {
3. new B();
4. }
5. }
6. abstract class A {
7. public A(){
8. System.out.println("hello");
9. }
10. }
11. class B extends A {
12. }
编译并运行,则?(Choose one)
A. 可以正常编译,并输出”hello”
B. 第6行编译有错
C. 第7行编译有错
D. 第11行编译有错
答案:A
9. InterfaceTest.java程序如下:
1. public class InterfaceTest {
2. public static void main(String[] args) {
3. new B() {
4. public void x(){
5. System.out.println("test");
6. }
7. };
8. }
9. }
10. interface A {
11. void x();
12. }
13. interface B extends A {
14. }
编译并运行,则?(Choose one.)
A. 正常编译,并输出”test”
B. 正常编译,不输出任何作息。
C. 第13行编译出错
D. 第3至7行编译出错
答案:B
10. SuperThis.java程序如下:
1. public class SuperThis {
2. public static void main(String[] args) {
3. new B();
4. }
5. }
6. class A {
7. public A(){
8. System.out.print ("A");
9. }
10. }
11. class B extends A {
12. public B(Object obj){
13. System.out.print ("B");
14. }
15. public B(){
16. super();
17. this(new Object());
18. }
19. }
编译程序并运行,则?(Choose one)
A. 正常编译,输出AB
B. 正常编译,无任何输出信息
C. 第16行编译错误,其应置于this()方法之后
D. 第17行编译错误,因为super()方法与this()方法不能同置于一个构造器内。
答案:D
1. public class TestPoly {
2. public static void main(String [] args ){
3. Parent p = new Child();
4. }
5. }
6.
7. class Parent {
8. public Parent() {
9. super();
10. System.out.println("instantiate a parent");
11. }
12. }
13.
14. class Child extends Parent {
15. public Child() {
16. System.out.println("instantiate a child");
17. super();
18. }
19. }
指出此程序的错误之处?(Choose one.)
A. 第3行将子对象赋值给一个父对象引用
B. 第9行,Parent类无父类,故不能调用super()方法
C. 第17行,super()方法调用应置于System.out.println(“instantiate a child”)语句之前
D. 此程序可以正常运行,无错误
答案:C
解析://构造函数使用的时候前面用this或者是super。
构造函数特征:1具有与类相同的名称2没有返回值3不能在方法中用return语句返回值。
7. MyThread.java程序如下:
1. public class MyThread extends Thread {
2.
3. public static void main(String [] args) {
4. MyThread t = new MyThread();
5. t.run();
6. }
7.
8. public void run() {
9. for(int i=1;i<3;++i) {
10. System.out.print(i + "..");
11. }
12. }
13. }
编译并运行,则?(Choose one.)
A. 第4行错误,无法编译
B. 第5行错误,无法编译.
C. 可以正常编译,并输出:1..2..
D. 可以正常编译,并输出:1..2..3..
答案:C
8. AbstractTest.java程序如下:
1. public class AbstractTest {
2. public static void main(String[] args) {
3. new B();
4. }
5. }
6. abstract class A {
7. public A(){
8. System.out.println("hello");
9. }
10. }
11. class B extends A {
12. }
编译并运行,则?(Choose one)
A. 可以正常编译,并输出”hello”
B. 第6行编译有错
C. 第7行编译有错
D. 第11行编译有错
答案:A
9. InterfaceTest.java程序如下:
1. public class InterfaceTest {
2. public static void main(String[] args) {
3. new B() {
4. public void x(){
5. System.out.println("test");
6. }
7. };
8. }
9. }
10. interface A {
11. void x();
12. }
13. interface B extends A {
14. }
编译并运行,则?(Choose one.)
A. 正常编译,并输出”test”
B. 正常编译,不输出任何作息。
C. 第13行编译出错
D. 第3至7行编译出错
答案:B
10. SuperThis.java程序如下:
1. public class SuperThis {
2. public static void main(String[] args) {
3. new B();
4. }
5. }
6. class A {
7. public A(){
8. System.out.print ("A");
9. }
10. }
11. class B extends A {
12. public B(Object obj){
13. System.out.print ("B");
14. }
15. public B(){
16. super();
17. this(new Object());
18. }
19. }
编译程序并运行,则?(Choose one)
A. 正常编译,输出AB
B. 正常编译,无任何输出信息
C. 第16行编译错误,其应置于this()方法之后
D. 第17行编译错误,因为super()方法与this()方法不能同置于一个构造器内。
答案:D