55
56
65
66
第1题:
public class Something {
public static void main(String[] args) {
Other o = new Other();
new Something().addOne(o);
}
public void addOne(final Other o) {
o.i++;
}
}
class Other {
public int i;
}
和上面的很相似,都是关于final 的问题,这有错吗?
正确。在addOne method 中,参数o 被修饰成final。如果在addOne method 里我们修
改了o 的reference
(比如: o = new Other();),那么如同上例这题也是错的。但这里修改的是o 的member vairable
(成员变量),而o 的reference 并没有改变。
第2题:
下面程序执行的结果是【 】
include<iostream>
using namespace std;
class A{
public:
static int x;
A(inty){cout<<x+y;}
};
int A::x=2;
void main(){
A a(5);
}
第3题:
下列程序的输出结果是 interface Inter{ public final static int A=100; } class My implements Inter{ public static void main (String args[ ]) {System.out.println(A) ; }
A.100
B.0
C.A
D.程序有错误
第4题:
以下程序调试结果为:
public class Test {
int m=5;
public void some(int x) {
m=x;
}
public static void main(String args []) {
new Demo().some(7);
}
}
class Demo extends Test {
int m=8;
public void some(int x) {
super.some(x);
System.out.println(m);
}
}
A.5
B.8
C.7
D.无任何输出
E.编译错误
第5题:
请完善程序(程序文件名:Java_3.java)并进行调试。请在下画线处填入正确内容,然后删除下画线。请勿删除注释行和其他已有的语句内容。
[题目要求]
生成下面左边图形界面,单击图中的New按钮,弹出如右图所示的对话框。
源程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Java_3 {
public static void main(String[] args) {
MulticastFrame. frame=new MulticastFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MulticastFrame. extends JFrame. {
public MulticastFrame() {
setTitle("MulticastTest");
setSize(WIDTH,HEIGHT);
MulticastPanel panel=new MulticastPanel();
Container contentPane=getContentPane();
contentPane.add( (1) );
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
class MulticastPanel extends JPanel }
public MulticastPanel() {
JButton newButton=new JButton("New");
add(newButton);
ActionListener newListener=new ActionListener() {
public void actionPerformed(ActionEvent event) {
makeNewFrame();
}
};
newButton.addActionListener(newListener);
closeAllButton=new JButton("Close all");
add(closeAllButton);
}
private void makeNewFrame() {
final BlankFrame. frame=new BlankFrame();
frame.show();
ActionListener closeAllListener=new ActionListener() {
public void actionPerformed(ActionEvent event) {
frame. (2) (); //使窗口隐藏或消除
}
};
closeAllButton.addActionListener( (3) );
}
private JButton closeAllButton;
}
Class BlankFrame. extends JFrame. {
public BlankFrame() {
(4) ++;
setTitle("Frame"+counter);
setSize(WIDTH,HEIGHT);
setLocation(SPACING*counter,SPACING*counter);
}
public static final int WIDTH=200;
public static final int HEIGHT=150;
public static final int SPACING=30;
private static int counter=0;
}
第6题:
interface A{
int x = 0;
}
class B{
int x =1;
}
class C extends B implements A {
public void pX(){
System.out.println(x);
}
public static void main(String[] args) {
new C().pX();
}
}
错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的
x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。
对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可
以通过A.x 来明确。
第7题:
下面程序段的输出结果为 package test; public class A { int x=20; static int y=6; public static void main(String args[]) { Class B b=new Class B(); b.go(10); System.out.println(”x=”+b.x); } } class Class B { int x; void go(int y) { ClassA a=new ClassA(); x=a.y; } }
A.x=10
B.x=20
C.x=6
D.编译不通过
第8题:
下列程序段中,正确的是______。 ①class MvClass { int var = 100; static int getVar() { return var; } } ②public class MyClass { final int date; void MyClass (int d) { date = d; } } ③public class MyMain { public static void main(String args[]) { System.out.println(Myclass1.date); } } class MyClass1 { int data = 10; } ④class IamAbstract { final int f; double d; abstrct void method(); }
A.②④
B.①③
C.②
D.以上都不对
第9题:
下面的例子中
using System;
class A
{
public static int X;
static A(){
X=B.Y+1;
}
}
class B
{
public static int Y=A.X+1;
static B(){}
static void Main(){
Console.WriteLine("X={0},Y={1}",A.X,B.Y);
}
}
产生的输出结果是什么?
第10题:
Which two code fragments correctly create and initialize a static array of int elements?()