Java Bronze 备考练习
20 题 · 单选 15 题 + 多选 5 题 · 面向 Oracle Certified Java Programmer Bronze SE(1Z0-818)
考点:变量与初始化、修饰符、运算符、控制流程、字符串、接口与多态、继承、异常。
练完这 20 题,可进入 Java Bronze 全真模拟卷(60 题 · 65 分钟 · 60 分及格)。
What is wrong with the following Java program?
以下这段 Java 程序有什么问题?
次の Java プログラムのどこに問題があるか。
class testClass{
public static void main(String[] args) {
int a;
System.out.println(a);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 B
Local variables get no default value in Java. The compiler rejects any attempt to read one before a value has been assigned. Instance and static fields do receive defaults (0, false, null), but 'a' here is declared inside main, so it stays uninitialised.
Java 的局部变量没有默认值,任何「先赋值再读取」之外的用法都会被编译器直接拒绝。实例字段和静态字段才有默认值(0、false、null),但这里的 a 声明在 main 里面,属于局部变量,所以必须显式赋值后才能使用。
Java のローカル変数には既定値がないため、値を代入する前に読み取ろうとするとコンパイルエラーになる。既定値(0・false・null)を持つのはインスタンス変数と static 変数だけ。ここで a は main の中のローカル変数なので初期化が必須。
Which of these are compile errors in the program above? (select all that apply)
以上程序中,哪些会导致编译错误?(多选)
このプログラムでコンパイルエラーになるものはどれか。(すべて選択)
public class xyz {
int a;
final int b;
int c = 4;
final String str = "hello world";
public void func() {
int d;
String localstring = "hello everyone";
str = localstring;
System.out.println(str);
System.out.println(d);
System.out.println(a);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 A,C,D
A final field or variable can only be assigned once: assigning to 'str' inside func() is illegal. A final field also has to be assigned either at its declaration or in every constructor — 'b' gets neither. Local variables have no default value, so printing 'd' is an error. 'a' is an instance field, so it defaults to 0 and printing it is perfectly legal.
final 变量只能赋值一次,所以在 func() 里再次给 str 赋值是非法的。final 字段必须在声明时或每个构造器里完成初始化,b 两者都没有。局部变量没有默认值,所以打印 d 也是错误。而 a 是实例字段,默认值为 0,打印它完全合法。
final 変数は一度しか代入できないため、func() 内での str への再代入は不正。final フィールドは宣言時か全コンストラクタで初期化が必要で、b はどちらも満たしていない。ローカル変数に既定値はないので d の出力もエラー。a はインスタンス変数で既定値 0 を持つため出力は問題なし。
What is wrong with the following Java program?
以下这段 Java 程序有什么问题?
次の Java プログラムのどこに問題があるか。
final class test1{
protected static int a;
}
class test2 extends test1{
public static void main(String[] args) {
System.out.println(a);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 B
A final class cannot be extended — that is exactly what the modifier is for. Everything else here is legal: a final class may hold static fields, and static fields default to 0 so 'a' need not be assigned explicitly.
被 final 修饰的类不能被继承,这正是它的用途。其余部分都合法:final 类里可以有静态字段,而静态字段有默认值 0,所以 a 不显式赋值也没问题。
final クラスは継承できない —— それがこの修飾子の役割。他はすべて合法。final クラスに static フィールドを置くことはでき、static フィールドは既定値 0 を持つので a の明示的な初期化も不要。
What default value does the variable 'a' receive?
变量 a 取得的默认值是多少?
変数 a が持つ既定値はどれか。
class testClass{
static int a;
public static void main(String[] args) {
System.out.println(a);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 A
Static and instance fields of numeric primitives default to 0, booleans to false, and object references to null. Defaults apply only to fields — local variables never receive one.
静态字段和实例字段中,数值型基本类型默认为 0,布尔型为 false,引用类型为 null。默认值只给字段,局部变量永远不会自动获得默认值。
static フィールドとインスタンスフィールドは、数値プリミティブが 0、boolean が false、参照型が null で初期化される。既定値が与えられるのはフィールドだけで、ローカル変数には与えられない。
Which statement CANNOT be inserted at position 1?
哪条语句不能插在第 1 处?
位置 1 に挿入できない文はどれか。
class abcd{
int a;
int b=0;
static int c;
public void m() {
int d;
int e=0;
// position 1
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 C
m() is an instance method, so a, b, c and e are all reachable: instance fields default to 0 (a, b), static fields default to 0 (c), and e was explicitly set to 0. Only 'd' is an uninitialised local variable, and reading it — which ++ does — is a compile error.
m() 是实例方法,因此 a、b、c、e 都可用:实例字段 a、b 默认为 0,静态字段 c 默认为 0,e 被显式赋值为 0。只有 d 是未初始化的局部变量,而 ++ 需要先读取它的值,所以编译报错。
m() はインスタンスメソッドなので a・b・c・e はすべて参照可能。インスタンスフィールド a, b は既定値 0、static フィールド c も 0、e は明示的に 0。d だけが未初期化のローカル変数で、++ は値の読み取りを伴うためコンパイルエラーになる。
Which statement is valid at position 1 inside this static main method?
在这个静态 main 方法的第 1 处,哪条语句合法?
この static な main メソッド内の位置 1 に入れてよい文はどれか。
public class abc {
int a;
int b=0;
static int c;
public static void main(String[] args) {
int d;
int e=0;
// position 1
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 C
main is static, so it cannot touch instance fields directly — a++ and b++ fail to compile (no object exists to read them from). 'd' is an uninitialised local variable. Only 'c', a static field, is reachable from a static context.
main 是静态方法,不能直接访问实例字段——没有对象可以承载它们,所以 a++ 和 b++ 都无法编译。d 是未初始化的局部变量。只有静态字段 c 可以在静态上下文中被访问。
main は static なのでインスタンスフィールドに直接触れられない —— a++ と b++ はコンパイルエラー(読み取る対象のインスタンスがない)。d は未初期化のローカル変数。static フィールド c だけが static コンテキストから参照できる。
What is the result of compiling and running this program?
编译并运行这段程序的结果是什么?
このプログラムをコンパイルして実行した結果はどうなるか。
class testClass{
public static void main(String[] args) {
int i=4;
float f = 4.3;
double d = 1.8;
int c =0;
if(i == f) c++;
if(((int)(f+d)) == ((int) f) + (int)d) c+= 2;
System.out.println(c);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 D
A decimal literal such as 4.3 is a double, and assigning a double to a float needs either a cast or an 'f' suffix — so 'float f = 4.3;' never compiles. Write 4.3f to make it legal; the loop conditions are red herrings you never reach.
像 4.3 这样的小数字面量默认是 double 类型,把 double 赋给 float 必须做强制转型或加 f 后缀,所以 float f = 4.3; 这一行就编译不过。写成 4.3f 才合法——后面的判断全是干扰,根本执行不到。
4.3 のような小数リテラルは double 型。double を float に代入するにはキャストか f サフィックスが必要なため、float f = 4.3; の時点でコンパイルできない。4.3f と書けば合法。後続の条件判定は到達しないので関係ない。
What happens when this program runs?
这段程序运行时会发生什么?
このプログラムを実行するとどうなるか。
interface inter{
void func();
}
class testClass implements inter{
public void func(){
System.out.println("hello world");
}
public static void main(String[] args) {
inter x = new inter();
x.func();
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 C
An interface declares behaviour without implementing it, so 'new inter()' is meaningless and rejected by the compiler. You must instantiate a concrete class and hold it in an interface-typed reference: 'inter x = new testClass();'.
接口只声明行为而不提供实现,因此 new inter() 没有意义,编译器会直接报错。正确做法是实例化某个具体类,再用接口类型的引用去持有它:inter x = new testClass();
インターフェースは実装を持たないため new inter() は意味を持たずコンパイラに拒否される。具象クラスをインスタンス化し、インターフェース型の参照で保持する必要がある:inter x = new testClass();
Which declaration can be inserted at position 1 so that 'func()' can be invoked without a compile error?
在第 1 处插入哪条声明,可以让 func() 被调用且不产生编译错误?
位置 1 に挿入すれば func() をコンパイルエラーなしで呼べる宣言はどれか。
interface inter{
void func();
}
class testClass implements inter{
public void func(){
System.out.println("hello world");
}
public static void main(String[] args) {
// position 1
x.func();
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 A
Assigning the implementing class to an interface reference is the standard polymorphic idiom. 'new inter()' is illegal either way; 'testClass x;' leaves the local variable uninitialised, so calling x.func() is also a compile error.
把实现类赋给接口类型的引用,正是标准的多态写法。new inter() 无论如何都非法;testClass x; 只是声明而未初始化局部变量,调用 x.func() 同样编译报错。
実装クラスをインターフェース型の参照に代入するのが標準的なポリモーフィズムの書き方。new inter() はどちらにしても不正。testClass x; は宣言だけでローカル変数が未初期化なので、x.func() の呼び出しもコンパイルエラー。
What is printed?
程序的输出是什么?
何が出力されるか。
public class xyz {
int a;
xyz(){
a=9;
}
xyz(int i){
a=i;
}
public static void main(String[] args) {
xyz obj1 = new xyz();
xyz obj2 = new xyz(10);
obj1=obj2;
System.out.println(obj1.a);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 C
Two objects are built: one with a=9, one with a=10. 'obj1=obj2' copies the reference, not the object — both variables now point at the same object whose a is 10. The object with a=9 becomes unreachable.
先后创建了两个对象,a 分别是 9 和 10。obj1=obj2 复制的是引用而不是对象本身,此后两个变量指向同一个对象,其 a 为 10。原来那个 a=9 的对象失去引用,再也访问不到。
a=9 と a=10 の 2 つのオブジェクトが作られる。obj1=obj2 は参照をコピーするだけで、以後 2 つの変数は同じオブジェクト(a=10)を指す。a=9 のオブジェクトは参照を失い到達不能になる。
What is wrong with this code?
这段代码有什么错误?
このコードのどこが間違っているか。
class abcd extends Exception{}
public class pqrs{
public void func1() {
try {
func2();
}
finally {
func3();
}
catch(abcd exp) {
}
}
public void func2() throws abcd{
throw new abcd();
}
public void func3() throws RuntimeException{
throw new RuntimeException();
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 D
The order after try must be catch blocks first, then the optional finally — so 'try ... finally ... catch' is illegal. Everything else here is fine: try may be followed by both catch and finally, an empty catch block is legal (if useless), and RuntimeException is unchecked so func1() need not declare it.
try 之后的顺序必须是先 catch、末尾才是可选的 finally,所以 try…finally…catch 这种写法非法。其余都没问题:try 可以同时配 catch 和 finally,空的 catch 块语法上合法(虽然没用),而 RuntimeException 是非受检异常,func1() 不需要用 throws 声明。
try の後はまず catch、最後に任意の finally という順序が必須のため、try … finally … catch は不正。他は問題なし。try に catch と finally を同時に付けることはでき、空の catch ブロックも文法上は合法。RuntimeException は非検査例外なので func1() に throws 宣言は不要。
What is written to standard output?
标准输出的内容是什么?
標準出力には何が出力されるか。
class testClass{
public static void main(String[] args) {
String str ="restructure";
System.out.println(str.substring(2, 3));
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 D
substring(begin, end) takes the begin index inclusive and the end index exclusive. For "restructure" the indices are r=0, e=1, s=2, so substring(2, 3) contains exactly the character at index 2 — the single letter s.
substring(begin, end) 的 begin 下标包含在内,end 下标不包含。字符串 restructure 的下标是 r=0、e=1、s=2,所以 substring(2, 3) 恰好只取下标 2 这一个字符,即字母 s。
substring(begin, end) は begin の添字を含み、end の添字を含まない。"restructure" は r=0, e=1, s=2 なので、substring(2, 3) は添字 2 の文字だけ、つまり s 1 文字になる。
Which statements about the >> and >>> operators are true? (select all that apply)
关于 >> 与 >>> 运算符,哪些说法正确?(多选)
>> 演算子と >>> 演算子について正しいものはどれか。(すべて選択)
int x = 5;
int a = x >> 1;
int b = x >>> 1;
int c = -1 >> 1;
int d = -1 >>> 1;- A
- B
- C
- D
解析 / Explanation
正确答案 A,C,D
>> keeps the sign bit when shifting (arithmetic shift), >>> fills with zero (logical shift). They agree as long as the sign bit is 0, which is why non-negative operands give identical results. For -1 the bit pattern is all ones, so -1 >> 1 is still -1 and -1 >>> 1 becomes the large positive value 2147483647 — certainly not 0.
>> 移位时保留符号位(算术移位),>>> 则补零(逻辑移位)。只要符号位是 0,两者结果就一致,这正是非负操作数结果相同的原因。-1 的位模式是全 1,所以 -1 >> 1 仍是 -1,而 -1 >>> 1 会变成很大的正数 2147483647,绝不是 0。
>> はシフト時に符号ビットを保持し(算術シフト)、>>> は 0 を埋める(論理シフト)。符号ビットが 0 なら結果は一致するため、非負のオペランドでは同じ結果になる。-1 のビット列は全ビット 1 なので、-1 >> 1 は -1 のままだが、-1 >>> 1 は大きな正の値 2147483647 になり、0 にはならない。
Which declarations of main() are valid entry points for an application? (select all that apply)
下列哪些 main() 声明可以作为程序的合法入口?(多选)
プログラムの正しいエントリーポイントとして有効な main() の宣言はどれか。(すべて選択)
// candidate entry points
A: public void main(String args[])
B: final public static void main(String [] array)
C: public static void main(String[] args)
D: void static public main(String args[])- A
- B
- C
- D
解析 / Explanation
正确答案 B
A valid entry point must be public, static, void and take a String array. Modifiers may appear in any order, so 'final public static void main' (B) is fine, as is the standard form (C). A lacks static, and in D the return type 'void' appears before the 'static' modifier — modifiers must all precede the return type, so it does not compile.
合法的入口必须是 public、static、void,并接收 String 数组参数。修饰符之间的顺序不限,所以 final public static void main(B)和标准写法(C)都合法。A 缺少 static;D 里返回类型 void 出现在 static 修饰符之前,而修饰符必须全部排在返回类型之前,因此无法编译。
有効なエントリーポイントは public・static・void で String 配列を受け取る必要がある。修飾子の順序は自由なので final public static void main(B)も標準形(C)も有効。A は static がない。D は戻り値型 void が static 修飾子より前にあり、修飾子は戻り値型より前に置く決まりなのでコンパイルできない。
What is the output (shown here without line breaks)?
程序的输出是什么(此处忽略换行)?
出力はどうなるか(改行は無視する)。
class testClass{
public static void main(String[] args) {
for(int i=12;i>0;i-=3)
System.out.println(i);
System.out.println("");
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 B
i starts at 12 and drops by 3 each pass while staying above 0: 12, 9, 6, 3, then i becomes 0 and the loop ends. Each value is printed on its own line, followed by one empty line — so the digits read 12963.
i 从 12 开始每次减 3,并保持大于 0:12、9、6、3,之后 i 变为 0 循环结束。每个值单独占一行,最后再打印一个空行,因此连起来读作 12963。
i は 12 から始まり 0 より大きい間 3 ずつ減る:12, 9, 6, 3。その後 i は 0 になりループ終了。各値は 1 行ずつ出力され、最後に空行が 1 つ続くので、数字を並べると 12963 になる。
What is the result of attempting to compile and run this code?
编译并运行这段代码的结果是什么?
このコードをコンパイルして実行した結果はどうなるか。
class testClass{
static int a;
int b;
public testClass() {
int c;
c=a;
a++;
b+=c;
System.out.println("c="+c +"a="+a +"b="+b);
}
public static void main(String[] args) {
new testClass();
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 D
Nothing here is an error. 'c' is assigned from 'a' on the line before it is read, and both fields already default to 0 — so the JVM prints c=0a=1b=0 instead of complaining.
这里没有任何错误。局部变量 c 在读取之前已经被 a 赋值,而两个字段也都默认为 0,所以 JVM 会正常输出 c=0a=1b=0,不会报错。
ここにエラーはない。c は読み取られる前の行で a から代入されており、2 つのフィールドも既定値 0 を持つ。そのため JVM はエラーにせず c=0a=1b=0 と出力する。
This program fails to compile with 'Unhandled exception type Exception'. Which changes fix it? (select all that apply)
程序因「Unhandled exception type Exception」无法编译。哪些修改可以修复?(多选)
このプログラムは Unhandled exception type Exception でコンパイルできない。修正方法として正しいものはどれか。(すべて選択)
public class xyz {
int a;
xyz() throws Exception{
a=9;
}
xyz(int i){
a=i;
}
public static void main(String[] args) {
xyz obj1 = new xyz();
xyz obj2 = new xyz(10);
obj1=obj2;
System.out.println(obj1.a);
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 A,B,D
A checked exception must either be caught in a try/catch or declared with throws, so wrapping the whole method or just the failing statement both work, as does declaring throws on main. Option C is not Java syntax at all — a throws clause belongs on a method declaration, never on an assignment statement.
受检异常必须在 try/catch 中被捕获,或者用 throws 声明。因此把整个方法包起来、只处理出错那句、或者给 main 加 throws 声明都能通过。而 C 根本不是 Java 语法——throws 子句只能出现在方法声明上,绝不能写在赋值语句之后。
検査例外は try/catch で捕捉するか throws で宣言する必要がある。そのためメソッド全体を囲む方法、問題の行だけを囲む方法、main に throws を付ける方法はいずれも有効。一方 C は Java の文法ではなく、throws 句はメソッド宣言に付くもので代入文には付けられない。
How should the method be declared to meet this requirement?
要让方法满足这个要求,应该如何声明?
この条件を満たすにはメソッドをどう宣言すべきか。
// The method must be reachable only from classes
// in the same package as the method's own class.- A
- B
- C
- D
解析 / Explanation
正确答案 D
Leaving off the access modifier gives package-private (default) access: visible inside the package, invisible outside it. public opens it everywhere, protected also admits subclasses in other packages, and private hides it even from the rest of its own package.
不写访问修饰符得到的是包级私有(默认)权限:同包内可见,包外不可见。public 会对所有地方开放,protected 还会额外允许其他包的子类访问,private 则连同一个包内的其他类都无法访问。
アクセス修飾子を書かないとパッケージプライベート(既定)アクセスになり、同一パッケージ内からのみ参照できる。public はどこからでも、protected は他パッケージのサブクラスからも許可し、private は同一パッケージの他クラスからも見えなくなる。
Which statements make the program print 1? (select all that apply)
哪些语句能让程序输出 1?(多选)
プログラムに 1 を出力させる文はどれか。(すべて選択)
class testClass{
int a=1;
int b=1;
int c=1;
class innerClassTest{
int a=2;
int get() {
int c=3;
// insert statement here
return c;
}
}
testClass(){
innerClassTest i = new innerClassTest();
System.out.println(i.get());
}
public static void main(String[] args) {
new testClass();
}
}- A
- B
- C
- D
解析 / Explanation
正确答案 A,C
Inside the inner class, the simple names resolve to the innermost declaration first: the local c=3, then innerClassTest.a=2, then the outer fields. 'b' only exists on the outer object, so 'c = b' picks up the outer b=1. 'this.a' inside refers to the inner instance's own a=2, which prints 2, and 'testClass.this.a' reaches the outer a=1.
在内部类里,简单名会从最内层向外解析:先是局部变量 c=3,然后是 innerClassTest.a=2,最后才是外层字段。b 只存在于外层对象上,所以 c = b 取到的是外层的 b=1。this.a 指向内部实例自己的 a=2(会打印 2),而 testClass.this.a 才能访问外层的 a=1。
内部クラス内では単純名は内側から解決される:まずローカル変数 c=3、次に innerClassTest.a=2、最後に外側のフィールド。b は外側のオブジェクトにしかないので c = b は外側の b=1 を指す。this.a は内部インスタンス自身の a=2(2 が出力される)、testClass.this.a で外側の a=1 に到達する。
Which conversions can be performed without an explicit cast? (select all that apply)
下列哪些转换不需要显式强制转型?(多选)
明示的なキャストなしで行える変換はどれか。(すべて選択)
int i = 10;
long l = ???; // from i
short s = 5;
char ch = ???; // from s
float f = 1.5f;
long n = ???; // from f- A
- B
- C
- D
解析 / Explanation
正确答案 A,B
Java allows implicit widening when no information is lost: int fits in long and byte fits in short, so both convert silently. Going the other way (float to long) may lose the fractional part and needs a cast; short and char are both 16-bit but neither is a subtype of the other, so that cast is required too.
Java 允许不丢失信息的隐式拓宽转换:int 装得进 long、byte 装得进 short,两者都能自动完成。反向的 float 转 long 会丢掉小数部分,必须显式转型;short 和 char 虽然都是 16 位,但互不兼容,转换也需要显式转型。
Java では情報が失われない拡大変換のみ暗黙的に行われる:int は long に、byte は short に収まるため自動変換される。逆方向の float から long は小数部が失われる可能性があり明示的キャストが必須。short と char はどちらも 16 ビットだが互いに包含関係がないため、この変換もキャストが必要。