📄 scjp试题-scjpmockexam1.txt
字号:
3) c = this.b;
4) c = Q4a39.this.a;
5) c = c;
Q26
Which is the earliest line in the following code after which the object created on the line marked (0) will be a candidate for being garbage collected, assuming no compiler optimizations are done?
public class Q76a9 {
static String f() {
String a = "hello";
String b = "bye"; // (0)
String c = b + "!"; // (1)
String d = b;
b = a; // (2)
d = a; // (3)
return c; // (4)
}
public static void main(String args[]) {
String msg = f();
System.out.println(msg); // (5)
}
}
1) The line marked (1).
2) The line marked (2).
3) The line marked (3).
4) The line marked (4).
5) The line marked (5).
Q27
Which methods from the String and StringBuffer classes modify the object on which they are called?
1) The charAt() method of the String class.
2) The toUpperCase() method of the String class.
3) The replace() method of the String class.
4) The reverse() method of the StringBuffer class.
5) The length() method of the StringBuffer class.
Q28
Which statements, when inserted at the indicated position in the following code, will cause a runtime exception when attempting to run the program?
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String args[]) {
A x = new A();
B y = new B();
C z = new C();
// insert statement here
}
}
1) x = y;
2) z = x;
3) y = (B) x;
4) z = (C) y;
5) y = (A) y;
Q29
Which of these are keywords in Java?
1) default
2) NULL
3) String
4) throws
5) long
Q30
It is desirable that a certain method within a certain class can only be accessed by classes that are defined within the same package as the class of the method. How can such restrictions be enforced?
1) Mark the method with the keyword public.
2) Mark the method with the keyword protected.
3) Mark the method with the keyword private.
4) Mark the method with the keyword package.
5) Do not mark the method with any accessibility modifiers.
Q31
Which code fragments will succeed in initializing a two-dimensional array named tab with a size that will cause the expression tab[3][2] to access a valid element?
CODE FRAGMENT A:
int[][] tab = {
{ 0, 0, 0 },
{ 0, 0, 0 }
};
CODE FRAGMENT B:
int tab[][] = new int[4][];
for (int i=0; i
CODE FRAGMENT C:
int tab[][] = {
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
CODE FRAGMENT D:
int tab[3][2];
CODE FRAGMENT E:
int[] tab[] = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
1) Code fragment A.
2) Code fragment B.
3) Code fragment C.
4) Code fragment D.
5) Code fragment E.
Q32
What will be the result of attempting to run the following program?
public class Qaa75 {
public static void main(String args[]) {
String[][][] arr = {
{ {}, null },
{ { "1", "2" }, { "1", null, "3" } },
{},
{ { "1", null } }
};
System.out.println(arr.length + arr[1][2].length);
}
}
1) The program will terminate with an ArrayIndexOutOfBoundsException.
2) The program will terminate with a NullPointerException.
3) 4 will be written to standard output.
4) 6 will be written to standard output.
5) 7 will be written to standard output.
Q33
Which expressions will evaluate to true if preceded by the following code?
String a = "hello";
String b = new String(a);
String c = a;
char[] d = { 'h', 'e', 'l', 'l', 'o' };
1) (a == "Hello")
2) (a == b)
3) (a == c)
4) a.equals(b)
5) a.equals(d)
Q34
Which statements concerning the following code are true?
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
1) The code will fail to compile.
2) The constructor in A that takes an int as an argument will never be called as a result of constructing an object of class B or C.
3) Class C has three constructors.
4) Objects of class B cannot be constructed.
5) At most one of the constructors of each class is called as a result of constructing an object of class C.
Q35
Given two collection objects referenced by col1 and col2, which of these statements are true?
1) The operation col1.retainAll(col2) will not modify the col1 object.
2) The operation col1.removeAll(col2) will not modify the col2 object.
3) The operation col1.addAll(col2) will return a new collection object,
containing elements from both col1 and col2.
4) The operation col1.containsAll(Col2) will not modify the col1 object.
Q36
Which statements concerning the relationships between the following classes are true?
class Foo {
int num;
Baz comp = new Baz();
}
class Bar {
boolean flag;
}
class Baz extends Foo {
Bar thing = new Bar();
double limit;
}
1) A Bar is a Baz.
2) A Foo has a Bar.
3) A Baz is a Foo.
4) A Foo is a Baz.
5) A Baz has a Bar.
Q37
Which statements concerning the value of a member variable are true, when no explicit assignments have been made?
1) The value of an int is undetermined.
2) The value of all numeric types is zero.
3) The compiler may issue an error if the variable is used before it is initialized.
4) The value of a String variable is "" (empty string).
5) The value of all object variables is null.
Q38
Which statements describe guaranteed behavior of the garbage collection and finalization mechanisms?
1) Objects are deleted when they can no longer be accessed through any reference.
2) The finalize() method will eventually be called on every object.
3) The finalize() method will never be called more than once on an object.
4) An object will not be garbage collected as long as it is possible for an active part of the program to access it through a reference.
5) The garbage collector will use a mark and sweep algorithm.
Q39
Which code fragments will succeed in printing the last argument given on the command line to the standard output, and exit gracefully with no output if no arguments are given?
CODE FRAGMENT A:
public static void main(String args[]) {
if (args.length != 0)
System.out.println(args[args.length-1]);
}
CODE FRAGMENT B:
public static void main(String args[]) {
try { System.out.println(args[args.length]); }
catch (ArrayIndexOutOfBoundsException e) {}
}
CODE FRAGMENT C:
public static void main(String args[]) {
int ix = args.length;
String last = args[ix];
if (ix != 0) System.out.println(last);
}
CODE FRAGMENT D:
public static void main(String args[]) {
int ix = args.length-1;
if (ix > 0) System.out.println(args[ix]);
}
CODE FRAGMENT E:
public static void main(String args[]) {
try { System.out.println(args[args.length-1]); }
catch (NullPointerException e) {}
}
1) Code fragment A.
2) Code fragment B.
3) Code fragment C.
4) Code fragment D.
5) Code fragment E.
Q40
Which of these statements concerning the collection interfaces are true?
1) Set extends Collection.
2) All methods defined in Set are also defined in Collection.
3) List extends Collection.
4) All methods defined in List are also defined in Collection.
5) Map extends Collection.
Q41
What is the name of the method that threads can use to pause their execution until signalled to continue by another thread?
Fill in the name of the method (do not include a parameter list).
Q42
Given the following class definitions, which expression identifies whether the object referred to by obj was created by instantiating class B rather than classes A, C and D?
class A {}
class B extends A {}
class C extends B {}
class D extends A {}
1) obj instanceof B
2) obj instanceof A && ! (obj instanceof C)
3) obj instanceof B && ! (obj instanceof C)
4) obj instanceof C || obj instanceof D
5) (obj instanceof A) && ! (obj instanceof C) && ! (obj instanceof D)
Q43
What will be written to the standard output when the following program is run?
public class Q8499 {
public static void main(String args[]) {
double d = -2.9;
int i = (int) d;
i *= (int) Math.ceil(d);
i *= (int) Math.abs(d);
System.out.println(i);
}
}
1) 12
2) 18
3) 8
4) 12
5) 27
Q44
What will be written to the standard output when the following program is run?
public class Qcb90 {
int a;
int b;
public void f() {
a = 0;
b = 0;
int[] c = { 0 };
g(b, c);
System.out.println(a + " " + b + " " + c[0] + " ");
}
public void g(int b, int[] c) {
a = 1;
b = 1;
c[0] = 1;
}
public static void main(String args[]) {
Qcb90 obj = new Qcb90();
obj.f();
}
}
1) 0 0 0
2) 0 0 1
3) 0 1 0
4) 1 0 0
5) 1 0 1
Q45
Which statements concerning the effect of the statement gfx.drawRect(5, 5, 10, 10) are true, given that gfx is a reference to a valid Graphics object?
1) The rectangle drawn will have a total width of 5 pixels.
2) The rectangle drawn will have a total height of 6 pixels.
3) The rectangle drawn will have a total width of 10 pixels.
4) The rectangle drawn will have a total height of 11 pixels.
Q46
Given the following code, which code fragments, when inserted at the indicated location, will succeed in making the program display a button spanning the whole window area?
import java.awt.*;
public class Q1e65 {
public static void main(String args[]) {
Window win = new Frame();
Button but = new Button("button");
// insert code fragment here
win.setSize(200, 200);
win.setVisible(true);
}
}
1) win.setLayout(new BorderLayout()); win.add(but);
2) win.setLayout(new GridLayout(1, 1)); win.add(but);
3) win.setLayout(new BorderLayout()); win.add(but, BorderLayout.CENTER);
4) win.add(but);
5) win.setLayout(new FlowLayout()); win.add(but);
Q47
Which method implementations will write the given string to a file named "file", using UTF8 encoding?
IMPLEMENTATION A:
public void write(String msg) throws IOException {
FileWriter fw = new FileWriter(new File("file"));
fw.write(msg);
fw.close();
}
IMPLEMENTATION B:
public void write(String msg) throws IOException {
OutputStreamWriter osw =
new OutputStreamWriter(new FileOutputStream("file"), "UTF8");
osw.write(msg);
osw.close();
}
IMPLEMENTATION C:
public void write(String msg) throws IOException {
FileWriter fw = new FileWriter(new File("file"));
fw.setEncoding("UTF8");
fw.write(msg);
fw.close();
}
IMPLEMENTATION D:
public void write(String msg) throws IOException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -