SCJP(Sun Certified Java Programmer) Exercise 1

SCJP考题1

http://blog.csdn.net/wangjun_pfc/article/category/341137

分类: SCJP考题 2007-10-13 14:45 518人阅读 评论(0) 收藏 举报
Section(部分,节):Language Fundamentals(基本原则)
Problem[1]
A method is …
[Select One]
[1]an implementation of an abstraction.
[2]an attribute defining the property of
a particular abstraction
[3]a category of objects.
[4]an operation defining the behavior
for a particular abstraction.
[5]a blueprint(蓝图,设计图) for making operations.

Section:Language Fundamentals
Problem[2]
An object is …
[Select One]
[1]what classes are instantiated(例示) from.
[2]an instance of a class.
[3]a blueprint for creating concrete realization of abstractions.
[4]a reference to an attribute.
[5]a variable.

Section:Declarations and Access Control
Problem[3]
Which line contains a constructor in
this class definition?
public class Counter
{
// (1)
int current, step;
public Counter(int startValue, int
stepValue) { // (2)
set(startValue);
setStepValue(stepValue);
}
public int get() { return current; }
// (3)
public void set(int value) { current

value; } // (4)

public void setStepValue(int
stepValue) { step = stepValue; } //
(5)
}
[Select One]
[1]Code marked with (1) is a constructor.
[2]Code marked with (2) is a constructor.
[3]Code marked with (3) is a constructor.
[4]Code marked with (4) is a constructor.
[5]Code marked with (5) is a constructor.

Section:Language Fundamentals
Problem[4]
Given that Thing is a class, how many
objects and reference variables are
created by the following code?
Thing item, stuff;
item = new Object();
Thing entity = new Object();
[Select All]
[1]One object is created.
[2]Two objects are created.
[3]Three objects are created.
[4]One reference variable is created.
[5]Two reference variables are created.
[6]Three reference variables are created.

Section:Language Fundamentals
Problem[5]
An instance member …
[Select One]
[1]is also called a static member。
[2]is always a variable.
[3]is never a method.
[4]belongs to a single instance, not to
the class as a whole.
[5]always represents(描述) an operation.

Section:Language Fundamentals
Problem[6]
How do objects pass messages in Java?
[Select One]
[1]They pass messages by modifying each
other's member variables.
[2]They pass messages by modifying the
static member variables of each other's
classes.
[3]They pass messages by calling each
other's instance member methods.
[4]They pass messages by calling static
member methods of each other's classes.

Section:Declarations and Access Control
Problem[7]
Given the following code, which
statements are true?
class A {
int value1;
}
class B extends A {
int value2;
}
[Select All]
[1]Class A extends class B.
[2]Class B is the superclass of class A.
[3]Class A inherits from class B.
[4]Class B is a subclass of class A.
[5]Objects of class A have a member
variable named value2.

Section:Language Fundamentals
Problem[8]
If this source code is contained in a
file called SmallProg.java, what
command should be used to compile it
using the JDK?
public class SmallProg {
public static void main(String
args[]) { System.out.println("Good
luck!"); }
}
[Select One]
[1]java SmallProg
[2]javac SmallProg
[3]java SmallProg.java
[4]javac SmallProg.java
[5]java SmallProg main

Section:Declarations and Access Control
Problem[9]
Given the following class, which
statements can be inserted at position
1 without causing the code to fail
compilation?
public class Q6db8 {
int a;
int b = 0;
static int c;
public void m() {
int d;
int e = 0;
// Position 1
}
}
[Select All]
[1]a++;
[2]b++;
[3]c++;
[4]d++;
[5]e++;

Section:Operators and Assignments(分配)
Problem[10]
Which statements are true concerning the
effect of the » and »> operators?
[Select All]
[1]For non-negative(正的,非负的) values of the left
Operand操作数, the » and »> operators will
have the same effect.
[2]The result of (-1 » 1) is 0.
[3]The result of (-1 »> 1) is -1.
[4]The value returned by »> will never
be negative as long as the value of the
right operand is equal to or greater than
1.
[5]When using the » operator, the
Leftmost bit of the bit representation(表示法)
of the resulting value will always be the
same bit value as the leftmost bit of the
bit representation of the left operand.

Section:Flow Control and Exception
Handling
Problem[11]
What is wrong with the following code?
class MyException extends Exception {}
public class Qb4ab {
public void foo() {
try {
bar();
} finally {
baz();
} catch (MyException e) {}
}
public void bar() throws
MyException {
throw new MyException();
}
public void baz() throws
RuntimeException {
throw new RuntimeException();
}
}
[Select All]
[1]Since the method foo() does not catch
the exception generated by the method
baz(), it must declare the
RuntimeException in its throws clause.
[2]A try block cannot be followed by both
a catch and a finally block.
[3]An empty catch block is not allowed.
[4]A catch block cannot follow a finally
block.
[5]A finally block must always follow
one or more catch blocks.

Section:The java.lang package
Problem[12]
What will be written to the standard
output when the following program is run?
public class Qd803 {
public static void main(String
args[]) {
String word = "restructure";
System.out.println(word.substring(2,
3));
}
}
[Select One]
[1]est
[2]es
[3]str
[4]st
[5]s

Section:Threads
Problem[13]
Given that a static method doIt() in a
class Work represents work to be done,
what block of code will succeed in
starting a new thread that will do the
work?
CODE BLOCK A:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread(r);
t.start();
CODE BLOCK B:
Thread t = new Thread() {
public void start() {
Work.doIt();
}
};
t.start();
CODE BLOCK C:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
r.start();
CODE BLOCK D:
Thread t = new Thread(new Work());
t.start();
CODE BLOCK E:
Runnable t = new Runnable() {
public void run() {
Work.doIt();
}
};
t.run();
[Select All]
[1]Code block A.
[2]Code block B.
[3]Code block C.
[4]Code block D.
[5]Code block E.

Section:The java.awt package - Layout
Problem[14]
Write a line of code that declares a
variable named layout of type
LayoutManager and initializes it with a
new object, which when used with a
container can lay out components in a
rectangular(矩形的) grid(格子) of equal-sized
rectangles(长方形), 3 components wide and 2
components high.
[Fill in Blank]
[Possible Solutions]
[1]LayoutManager layout = new
GridLayout(2, 3);
[2]LayoutManager layout = new
GridLayout(2, 3)

Section:Declarations and Access Control
Problem[15]
Declarations and Access Control
public class Q275d {
static int a;
int b;
public Q275d() {
int c;
c = a;
a++;
b += c;
}
public static void main(String
args[]) {
new Q275d();
}
}
[Select One]
[1]The code will fail to compile, since
the constructor is trying to access
static members.
[2]The code will fail to compile, since
the constructor is trying to use static
member variable a before it has been
initialized.
[3]The code w ill fail to compile, since
the constructor is trying to use member
variable b before it has been
initialized.
[4]The code will fail to compile, since
the constructor is trying to use local
variable c before it has been
initialized.
[5]The code will compile and run without
any problems.

Section:Operators and Assignments
Problem[16]
What will be written to the standard
output when the following program is run?
public class Q63e3 {
public static void main(String
args[]) {
System.out.println(9 ^ 2);
}
}
[Select One]
[1]81
[2]7
[3]11
[4]0
[5]false

Section:The java.awt package - Layout
Problem[17]
Which statements are true concerning(关于) the
default layout manager for containers in
the java.awt package?
[Select All] flowLayout:Applet Panel BorderLayout:JApplet JFrame Frame
[1]Objects instantiated from Panel do
not have a default layout manager.
[2]Objects instantiated from Panel have
FlowLayout as default layout manager.
[3]Objects instantiated from Applet
have BorderLayout as default layout
manager.
[4]Objects instantiated from Dialog
have BorderLayout as default layout
manager.
[5]Objects instantiated from Window
have the same default layout manager as
instances of Applet.

Section:Language Fundamentals
Problem[18]
Which declarations will allow a class to
be started as a standalone program?
[Select All]
[1]public void main(String args[])
[2]public void static main(String
args[])
[3]public static main(String[] argv)
[4]final public static void main(String
[] array)
[5]public static void main(String
args[])

Section:Threads
Problem[19]
Under which circumstances(环境) will a thread
stop?
[Select All]
[1]The method waitforId() in class
MediaTracker is called.
[2]The run() method that the thread is
executing ends.
[3]The call to the start() method of the
Thread object returns.
[4]The suspend() method is called on the
Thread object.
[5]The wait() method is called on the
Thread object.

Section:The java.util package
Problem[20]
When creating a class that associates(联系) a
set of keys with a set of values, which
of these interfaces is most applicable(可适用的)?
[Select One]
[1]Collection
[2]Set
[3]SortedSet
[4]Map

Section:Language Fundamentals
Problem[21]
What does the value returned by the
method getID() found in class
java.awt.AWTEvent uniquely(独特的,无一的) identify(识别,鉴定)?
[Select One]
[1]The particular event instance.
[2]The source of the event.
[3]The set of events that were triggered(触发的)
by the same action.
[4]The type of event.
[5]The type of component from which the
event originated.

Section:Overloading Overriding Runtime
Type and Object Orientation(方向,方位)
Problem[22]
What will be written to the standard
output when the following program is run?
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i += v; i=1
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String
args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
[Select One]
[1]9
[2]18
[3]20
[4]21
[5]22

Section:Declarations and Access Control
Problem[23]
Which lines of code are valid
declarations of a native method when
occurring within the declaration of the
following class?
public class Qf575 {
// insert declaration of a native
method here
}
[Select All]
[1]native public void
setTemperature(int kelvin);
[2]private native void
setTemperature(int kelvin);
[3]protected int native
getTemperature();
[4]public abstract native void
setTemperature(int kelvin);
[5]native int setTemperature(int kelvin)
{}

Section:The java.awt package - Layout
Problem[24]
How does the weighty property of the
GridBagConstraints objects used in grid
bag layout affect the layout of the
components?
[Select One]
[1]It affects which grid cell the
components end up in.
[2]It affects how the extra vertical(垂直的)
space is distributed.
[3]It affects the alignment of each
component.
[4]It affects whether the components
completely fill their allotted display
area vertically.

Section:Overloading Overriding Runtime
Type and Object Orientation
Problem[25]
Which statements can be inserted at the
Indicated(指出的) position in the following code
to make the program write 1 on the
standard output when run?
public class Q4a39 {
int a = 1;
int b = 1;
int c = 1;
class Inner {
int a = 2;
int get() {
int c = 3;
// insert statement here
return c;
}
}
Q4a39() {
Inner i = new Inner();
System.out.println(i.get());
}
public static void main(String
args[]) {
new Q4a39();
}
}
[Select All]
[1]c = b;
[2]c = this.a;
[3]c = this.b;
[4]c = Q4a39.this.a;
[5]c = c;

Section:Garbage Collection(碎片收集)
Problem[26]
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)
}
}
[Select One]
[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).

Section:The java.lang package
Problem[27]
Which methods from the String and
StringBuffer classes modify(修改) the object
on which they are called?
[Select All]
[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.

Section:Overloading Overriding Runtime
Type and Object Orientation
Problem[28]
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
}
}
[Select All]
[1]x = y;
[2]z = x;
[3]y = (B) x;
[4]z = (C) y;
[5]y = (A) y;
运行时异常而不是编译时异常

Section:Language Fundamentals
Problem[29]
Which of these are keywords in Java?
[Select All]
[1]default
[2]NULL null
[3]String string
[4]throws
[5]long

Section:Declarations and Access Control
Problem[30]
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(强迫)?
[Select One]
[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.
Package不是关键字

Section:Declarations and Access Control
Problem[31]
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} };
[Select 2]
[1]Code fragment A.
[2]Code fragment B.
[3]Code fragment C.
[4]Code fragment D.
[5]Code fragment E.

Section:Operators and Assignments
Problem[32]
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);

}
}
[Select One]
[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.

Section:Operators and Assignments
Problem[33]
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' };
[Select All]
[1](a == "Hello") 小写和大写
[2](a == b)
[3](a == c)
[4]a.equals(b)
[5]a.equals(d) 不是同一个类型

Section:Overloading Overriding Runtime
Type and Object Orientation(定位,方位,方向)
Problem[34]
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) {}
}
[Select All]
[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.

Section:The java.util package
Problem[35]
Given two collection objects referenced(参考的,引用的)
by col1 and col2, which of these
statements are true?
[Select All]
[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.

Section:Overloading Overriding Runtime
Type and Object Orientation
Problem[36]
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;
}
[Select 2]
[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.
子类是父类——继承
一个类中有另一个类对象

Section:Declarations and Access Control
Problem[37]
Which statements concerning the value of
a member variable are true, when no
explicit assignments have been made?
[Select All]
[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.

Section:Garbage Collection
Problem[38]
Which statements describe guaranteed(保证,担保)
behavior of the garbage collection and
finalization mechanisms?
[Select All]
[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(运算法则).

Section:Language Fundamentals
Problem[39]
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)
{}
}
[Select All]
[1]Code fragment A.
[2]Code fragment B.
[3]Code fragment C.
[4]Code fragment D.
[5]Code fragment E.

Section:The java.util package
Problem[40]
Which of these statements concerning the
collection interfaces are true?
[Select All]
[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 a lso
defined in Collection.
[5]Map extends Collection.

Section:Threads
Problem[41]
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).
[Fill in Blank]
[Possible Solutions]
[1]wait

Section:Overloading Overriding Runtime
Type and Object Orientation

Problem[42]
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 {}
[Select All]
[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)

Section:The java.lang package
Problem[43]
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; -2
i *= (int) Math.ceil(d); 4
i *= (int) Math.abs(d);
System.out.println(i);
}
}
[Select One]
[1]12
[2]18
[3]8
[4]12
[5]27

Section:Operators and Assignments
Problem[44]
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();
}
}
[Select One]
[1]0 0 0
[2]0 0 1
[3]0 1 0
[4]1 0 0
[5]1 0 1

Section:The java.awt package - Painting
Problem[45]
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?
[Select All]
[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.

Section:The java.awt package - Layout
Problem[46]
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);
}
}
[Select All]
[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);

Section:The java.io package
Problem[47]
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 {
FilterWriter fw =
FilterWriter(new FileWriter("file"),
"UTF8");
fw.write(msg);
fw.close();
}
IMPLEMENTATION E:
public void write(String msg)
throws IOException {
OutputStreamWriter osw = new
OutputStreamWriter(
new OutputStream(new
File("file")), "UTF8"
);
osw.write(msg);
osw.close();
}
[Select All]
[1]Implementation A.
[2]Implementation B.
[3]Implementation C.
[4]Implementation D.
[5]Implementation E.

Section:Language Fundamentals
Problem[48]
Which are valid identifiers(标识符)?
[Select All]
[1]_class
[2]$value$
[3]zer@
[4]?ngstr?m
[5]2much

Section:Flow Control and Exception
Handling
Problem[49]
What will be the result of attempting to
compile and run the following program?
public class Q28fd {
public static void main(String
args[]) {
int counter = 0;
l1:
for (int i=10; i<0; i) {
l2:
int j = 0;
while (j < 10) {
if (j > i) break l2;
if (i == j) {
counter++;
continue l1;
}
}
counter
;
}
System.out.println(counter);
}
}
[Select One]
[1]The program will fail to compile.
[2]The program will not terminate
normally.
[3]The program will write 10 to the
standard output.
[4]The program will write 0 to the
standard output.
[5]The program will write 9 to the
standard output.

Section:Overloading Overriding Runtime
Type and Object Orientation
Problem[50]
Given the following interface
definition, which definitions are valid?
interface I {
void setValue(int val);
int getValue();
}
DEFINITION A:
(a)class A extends I {
int value;
void setValue(int val) { value =
val; }
int getValue() { return value; }
}
DEFINITION B:
(b)
interface B extends I {
void increment();
}
DEFINITION C:
(c)abstract class C implements I {
int getValue() { return 0; }
abstract void increment();
}
DEFINITION D:
(d)
interface D implements I {
void increment();
}
DEFINITION E:
(e)—class E implements I {
int value;
public void setValue(int val)
{ value = val; }
}
[Select All]
[1]Definition A.
[2]Definition B.
[3]Definition C.
[4]Definition D.
[5]Definition E.

Section:Threads
Problem[51]
Which statements concerning the methods
notify() and notifyAll() are true?
[Select All]
[1]Instances of class Thread have a
method called notify().
[2]A call to the method notify() will
wake the thread that currently owns the
monitor of the object.
[3]The method notify() is synchronized.
[4]The method notifyAll() is defined in
class Thread.
[5]When there is more than one thread
waiting to obtain the monitor of an
object, there is no way to be sure which
thread will be notified by the notify()
method.

Section:Overloading Overriding Runtime
Type and Object Orientation
Problem[52]
Which statements concerning the
correlation between the inner and outer
instances of non-static inner classes
are true?
[Select All]
[1]Member variables of the outer
instance are always accessible to inner
instances, regardless of their
accessibility modifiers.
[2]Member variables of the outer
instance can never be referred to using
only the variable name within the inner
instance.
[3]More than one inner instance can be
associated with the same outer instance.
[4]All variables from the outer instance
that should be accessible in the inner
instance must be declared final.
[5]A class t hat is declared final cannot
have any inner classes.

Section:Operators and Assignments
Problem[53]
What will be the result of attempting to
compile and run the following code?
public class Q6b0c {
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);
}
}
[Select One]
[1]The code will fail to compile.
[2]0 will be written to the standard
output.
[3]1 will be written to the standard
output.
[4]2 will be written to the standard
output.
[5]3 will be written to the standard
output.

Section:Operators and Assignments
Problem[54]
Which operators will always evaluate all
the operands?
[Select All]
[1]||
[2]+
[3]&&
[4]? :
[5]%

Section:Flow Control and Exception
Handling
Problem[55]
Which statements concerning the switch
construct are true?
[Select All]
[1]All switch statements must have a
default label.
[2]There must be exactly one label for
each code segment in a switch statement.
[3]The keyword continue can never occur
within the body of a switch statement.
[4]No case label may follow a default
label within a single switch statement.
[5]A character literal can be used as a
value for a case label.

Section:Language Fundamentals
Problem[56]
Which modifiers and return types would
be valid in the declaration of a working
main() method for a Java standalone
application?
[Select All]
[1]private
[2]final
[3]static
[4]int
[5]abstract

Section:The java.awt package - Layout
Problem[57]
What will be the appearance of an applet
with the following init() method?
public void init() {
add(new Button("hello"));
}
[Select One]
[1]Nothing appears in the applet.
[2]A button will cover the whole area of
the applet.
[3]A button will appear in the top left
corner of the applet.
[4]A button will appear, centered in the
top region of the applet.
[5]A button will appear in the center of
the applet.

Section:Language Fundamentals
Problem[58]
Which statements concerning the event
model of the AWT are true?
[Select All]
[1]At most one listener of each type can
be registered with a component.
[2]Mouse motion listeners can be
registered on a List instance.
[3]There exists a class named
ContainerEvent in package
java.awt.event.
[4]There exists a class named
MouseMotionEvent in package
java.awt.event.
[5]There exists a class named
ActionAdapter in package java.awt.event.

Section:The java.io package
Problem[59]
Which statements are true, given the
code new FileOutputStream("data", true)
for creating an object of class
FileOutputStream?
[Select All]
[1]FileOutputStream has no constructors
matching the given arguments.
[2]An IOExeception will be thrown if a
file named "data" already exists.
[3]An IOExeception will be thrown if a
file named "data" does not already exist.
[4]If a file named "data" exists, its
contents will be reset and overwritten.
[5]If a file named "data" exists, output
will be appended to its current contents.

Section:Overloading Overriding Runtime
Type and Object Orientation
Problem[60]
Given the following code, write a line
of code that, when inserted at the
indicated location, will make the
overriding method in Extension invoke
the overridden method in class Base on
the current object.
class Base {
public void print() {
System.out.println("base");
}
}
class Extention extends Base {
public void print() {
System.out.println("extension");
// insert line of
implementation here
}
}
public class Q294d {
public static void main(String
args[]) {
Extention ext = new Extention();
ext.print();
}
}
Fill in a single line of implementation.
[Fill in Blank]
[Possible Solutions]
[1]super.print()
[2]super.print();

Section:The java.io package
Problem[61]
Given that file is a reference to a File
object that represents a directory,
which code fragments will succeed in
obtaining a list of the entries in the
directory?
[Select All]
[1]Vector filelist = ((Directory)
file).getList();
[2]String[] filelist = file.directory();
[3]Enumeration filelist =
file.contents();
[4]String[] filelist = file.list();
[5]Vector filelist = (new
Directory(file)).files();

Section:The java.lang package
Problem[62]
What will be written to the standard
output when the following program is run?
public class Q03e4 {
public static void main(String
args[]) {
String space = " ";
String composite = space +
"hello" + space + space;
composite.concat("world");
String trimmed =
composite.trim();
System.out.println(trimmed.length());
}
}
[Select One]
[1]5
[2]6
[3]7
[4]12
[5]13

Section:Threads
Problem[63]
Given the following code, which
statements concerning the objects
referenced through the member variables
i, j and k are true, given that any thread
may call the methods a, b and c at any
time?
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v—; }
}
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}
[Select All]
[1]i.v is guaranteed always to be 0 or
1.
[2]j.v is guaranteed always to be 0 or
1.
[3]k.v is guaranteed always to be 0 or
1
[4]j.v will always be greater than or
equal to k.v at any give time.
[5]k.v will always be greater than or
equal to j.v at any give time.

Section:Operators and Assignments
Problem[64]
Which statements concerning casting and
conversion are true?
[Select All]
[1]Conversion from int to long does not
need a cast.
[2]Conversion from byte to short does
not need a cast.
[3]Conversion from float to long does
not need a cast.
[4]Conversion from short to char does
not need a cast.
[5]Conversion from boolean to int using
a cast is not possible.

Section:Operators and Assignments
Problem[65]
Given the following code, which method
declarations, when inserted at the indicated(指出)
position, will not cause the program to fail
compilation?
public class Qdd1f {
public long sum(long a, long b) { return a

b; }

// insert new method declaration here
}
[Select All]
[1]public int sum(int a, int b) { return a + b; }
[2]public int sum(long a, long b) { return 0; }
[3]abstract int sum();
[4]private long sum(long a, long b) { return a

b; }

[5]public long sum(long a, int b) { return a +
b; }

Section:Operators and Assignments
Problem[66]
The 8859-1 character code for the uppercase
letter A is 65. Which of these code fragments
declare and initialize a variable of type char
with this value?
[Select All]
[1]char ch = 65;
[2]char ch = '/65';
[3]char ch = '/0041';
[4]char ch = 'A';
[5]char ch = "A";

SCJP考题2

分类: SCJP考题 2007-10-13 14:49 361人阅读 评论(0) 收藏 举报

1.What will happen when you try to compile and run the following program?
class Test {
int milesperGallon;
int index;
Test(int mpg){
milesperGallon=mpg;
index=0;
}
Test(){}
public static void main(String[] args){
int index;
Test c=new Test(25);
if(args.length>0)
if(args[index].equals("Hiway"))
milesperGallon*=2;
System.out.println("mpg:"+milesperGallon);
}
}
Select the one right answer.
A. The code compiles and displays"mpg:50"if the command-line argument is "Hiway". If the command-line argument is not"Hiway", the code displays"mpg:25".
B. The code compiles and displays"mpg:50" if the command-line argument is"Hiway". If the command-line argument is not "Hiway", the code throws an ArrayIndesOutOfBoundsException.
C. The code does not compile because the automatic variable named index has not been initialized.
D. The code does not compile because milesPerGallon has not been initialized
E. The code does not compile because the no-args constructor is not written correctly.
正确答案:C
注意两个index的位置不同
2.What will happen when you compile and run this program:
class Test{
pubilc static void main(String[]args){
int length=100;
int[] d=new int[length]; 100个数组元素
for(int index=0;index<length;index++)
System.out.println(d[index]);
}
}
Select the one right answer.
A. The code will not compile because the int[] array is not declared correctly.
B. The code will compile but will throw an IndexArrayOutOfBoundsException when it runs and nothing will appear in standard output.
C. The code will display the numbers 0 through 99 in the standard output, and then throw an IndexOutOfBoundsException.
D. The code will compile but the println() method will throw a NoSuchMethodException.
E. The code will work fine and display 100 zeroes in the standard output.
正确答案:E

3.What is the result of attempting to compile and run the following class?
class Test{
public static void main(Srring[]args){
int[] seeds = new int[3];
for(int i=0;i<seeds.length;i++)
System.out.println(i);
}
}
Select all valid answers.
A. 0
B. 1
C. 2
D. 3
E.the program does not compile because the seeds array is not initialized
正确答案:A、B、C

4. How you can use the escape notation u to set the variable c, declared as a char, to the Unicode character whose value is hex 0X30A0?
Fill in the blank.
正确答案:C=/u30a0;

5.Which label name(s) are illegal?
Select all invalid answers.
A. here;
B. there;
C. this;
D. that;
E. 2to1odds;
正确答案:C、E

6.What will happen when you attempt to compile and run the following program by passing the Test class to the Java interpreter(解释程序)?
class Test{
public static void main(){
System.out println("hello");
}
}
Select the one right answer.
A. The program does not compile because main() is not defined correctly.
B. The program compiles but when you try to run the interpreter complains that it cannot find the main() method it meeds to run.
C. The program compiles but you cannot run it because the class is not declared as public.
D. The program compiles and runs without an error but does not display anything in the standard output.
E.The program compiles and displays "hello" in the standard output when you run it.
正确答案:B

7.Which statements about garbage collection are true?
Select all valid answers.
A. You can directly free the memory allocated by an object.
B. You can directly run the garbage collector whenever you want to.
C. The garbage collector informs(通知) your object when it is about to be garbage collected.
D. The garbage collector reclaims(收回) an object's memory as soon as it becomes a candidate(侯选人) for garbage collection.
E. The garbage collector runs in low-memory situations.
正确答案:B、C、E
精解:本题考察的重点是垃圾回收站的运行机制。在Java中,由垃圾回收站负责回收废弃对象占用的内存资源,程序员从不需要明确释放掉对象,尽管可以将一个引用变量设置为null,程序员可以通过用Runtime或System 类的 ge()方法来运行垃圾回收站,如果系统中可用内存过低,系统也会自动运行垃圾回收站释放掉废弃对象占用的内存资源供其他对象使用。

8.How many bits are used to maintain a char data type?
Fill in the blank.
16

9.What is the syntax for specifying the size of an array in Java?
Fill in the blank.
正确答案:String [] args = new String[15];

10.What output is produced by the following program?
class Test{
public static void main(String args[]){
long size=10;
int[]array=new int[size];
size=20;
System.out.println(array.length);
}
}
Select the one right answer.
A. A compiler error.
B. A runtime error.
C. 10
D. 20
正确答案:A
精解:本题考察的重点是数组大小的指定。
Java 语言中规定用来指定数组长度的数值类型只能是字节型、短整型或整型,而不能是长整型。

11. What output is produced by the following program"
class Test{
public static void main(String args[]){
char size=10;
int[] array=new int[size];
size=20;
System.out.printiln(array.length);
}
}
Select the one right answer.
A. A comoiler error.
B. A runtime error.
C. 10
D. 20
正确答案:C

12. True or false? Element values in Java arrays are automatically initialized when the arrays is constructed using the new operator.
Fill in the blank.
正确答案:True

13. When an array of char elements is constructed, the elements are automatically initialized to which of the following values:
A. '0'
B. '/u0000'
正确答案:B

14. What output is produced by the following program?
class Test{
public static void main(String args[]){
char[] a1={'u/0030','/u0031'};
System.out.println(a1[1]);
}
}
Select the one right answer.
A. A compiler error
B. A runtime error
C. 30
D. 31
E. 0
F. 1
正确答案: F

15. What output is produced by the following program? (Note the placeholder comma in the boldface portion.)注意黑字体的一部分(占位符逗号)
class Test{
public static void main(String args[]){
char[]a1={'u/0030', ,'/u0031'};
Sysem.out.println(a1[0]);
}
}
Select the one right aswer.
A. A compiler error
B. A runtime error
C. 0
正确答案:A

16. What is the name of the property of an array object that always contains the size of the array and how may it be used?
Fill in the blank
正确答案:length

17. True or false? All Java applications and applets require a main() method.
Fill in the blank.
正确答案:False

18. Show six different ways to express the decimal value 28 as an integral(完整的) literal.(文字的,照字面上的)
Fill in the blank.
正确答案:28,034, 0x1c, 0x1C, 0X1c, 0X1C

19. True false? By default, an integral literal is a 64 bit value
Fill in the blank.
正确答案:False

20. There are seven characters, at least one of which must be included to indicate a floating point literal. What are they?
Fill in the blank.
正确答案:'E','e', 'F', 'f', 'D', 'd', '.'

21. Show how to represent(表现) a string literal.
Fill in the blank.
正确答案:"This is a String literal."

22. A Java array is an ordered collection of three kinds of things. What are they?
Fill in the blank.
正确答案:primitives. object references, references to other arrays.

23. True or false? All elements in a Java array must be of the same type.
Fill in the blank.
正确答案:True

24. There are two different formats that can be used to declare a reference variable capable of containing a reference to a single-dimensional array. What are they?
Fill in the blank.
正确答案:datatype[] arrayName; datatype arrayNane[];

25. True or false? The size of a Java array can be specified using a variable or a literal(文字).
Fill in the blank.
正确答案:True

26. Consider the following program, which initializes the instance variable named mylntVar to a value of 10 when the instance variable is initialized.
What value will be displayed by this program?
class Test{
int myIntVar=10;
Test(){
myIntVar=20;
}
public static void main(String args[]){
System.out.println(new Test().myIntVar);
}
}
Fill in the blank.
正确答案:20

27. What is the syntax for the main() method?
Fill in the blank.
正确答案: public static void main(String[] args){}
或者: public static void main(String args[]){}

28. What output is produced by the following program? Note that the main() method is not declared public.
class Test{
static void main(String args[]){
System.out.println("OK");
}
}
Select the one right answer.
A. A compiler error.
B. A runtime error.
C. OK
正确答案: C

29. What output is produced by the following program? Note that the main() method is not declared static.
class Test{
public void main(String args[]){
System.out.println("OK");
}
}
Select the one right answer.
A. A compiler error.
B. A runtime error.
C. OK
正确答案: B

30. What is the purpose of the reference to the array of String references in the argument list of the main() method?
Fill in the blank.
正确答案: Thansfer of command-line arguments

31. True or false? All member variables and all local variables are automatically initialized in Java.
Fill in the blank.
正确答案: False

32. Member Variables are automatically initialized to default values in Java. What are those default values?
Fill in the blank.
正确答案: null, 0, 0.0, false, '/u0000'

33. True or false? In Java primitives(原始的) are passed by value and objects are passed by reference.
Fill in the blank.
正确答案: False
数组

34. True or false? Because all parameters are passed by value in Java methods in Java are incapable of (不能)modifying the values stored in original(原始的) objects passed into a method.
Fill in the blank. 静态的??
正确答案: False

35. Given the code in the following simple program, which of the following will be displayed by the program?
class Test{
String myString="1";
public static void main(String args[]){
Test myObj=new Test();
myObj.stringModifier(myObj.myString);
System.out.println(""+myObj.myString);
}
void stringModifier(String theString){
theString=theString+"2";
System.out.print(theString);
}
}
Select the one right answer..
A. 12 1
B. 12 12
正确答案: A

SCJP考题3

分类: SCJP考题 2007-10-13 14:53 398人阅读 评论(0) 收藏 举报
443. Which asignments are legal?
Select all valid answers.
A. long test=012;
B. float f=-412;
C. int other=(int)true;
D. double d=0x12345678;
E. short s=10;
正确答案: A、B、D、E

444. What is the result of executing the following code:
class Test{
public static void main(String args[]){
Test t=new Test();
t.test(1.0,2L,3);
}
void test(double a,double b,short c){
System.out.println("1");
}
void test(float a,byte b,byte c){
System.out.println("2");
}
void test(double a, double b, double c){
System.out.println("3");
}
void test(int a,long b,int e){
System.out.println("4");
}
void test(long a, long b, long c){
System.out.println("5");
}
}
Select the one right answer.
A. 1
B. 2
C. 3
D. 4
E. 5
正确答案: C

445. Given these class definitions:
class Superclass{}
class Subclass1 extends Superclass{}
And these objects:
Superclass a=new Superclass();
Suberclass1 b=new Suberclass1();
Which of the following explains the result of the statement:
a=b;
Select the one right amswer.
A. Illegal(不合规定的) at compile time
B. Legal at compile time but possibly illegal at runtime
C. Definitely(明确地) legal at runtime
正确答案: C

446. Given these class definitions:
class Superclass{}
class Subclass1 extends Superclass{}
class Subclass2 extends Superclass{}
And these objects:
Superclass a=new Superclass();
Subclass1 b=new Subclass1();
Subclass2 c=new Subclass2();
Which of the following explains the result of the statement:
b=(Subclass1)c;
Select the one right amswer.
A. Illegal at compile time
B. Legal at compile time but possibly illegal at runtime
C. Definitely legal at runtime
正确答案: A

447. Given these class definitions:
class Superclass{}
class Subclass1 extends Superclass{}
And these objects:
Supetclass a=new Superclass();
Suberclass1 b=new Suberclass1();
Which of the following explains the result of the statement:
b=a;
Select the one right amswer.
A. Illegal at compile time
B. Legal at compile time but possibly illegal at runtime
C. Definitely legal at runtime
正确答案: A

448. Given these class definitions:
class Superclass{}
class Subclass1 extends Superclass{}
And these objects:
Supetclass a=new Superclass();
Subclass1 b=new Subclass1();
Which of the following explains the result of the statement:
b=(Subclass1)a;
Select the one right amswer.
A. Illegal at compile time
B. Legal at compile time but possibly illegal at runtime
C. The code will display the numbers 0 through 99 in the standard output, and then throw an IndexOutOfBoundsException
正确答案: B

449. True or false? The default for a floating-point literal(文字的) without an F or a D is a 32-bit float.
Fill in the blank.
正确答案: False

450. What output is produced by the following program?
class Test{
public static void main(String args[]){
byte x;
short y=128; byte -128~~~~~127 short -32768~~~~~32767
x=(byte)y;
System.out.println(x + " " +y);
}
}
Select the one right amswer.
A. A compiler error
B. A runtime error
C. 128 128
D. -128 128
正确答案: D

451. What output is produced by the following program?
class Test{
public static void main(String args[]){
byte x;
short y=128;
x=y;
System.out.println(x + " " +)y;
}
}
Select the one right answer.
A. A compiler error
B. A runtime error
C. 128 128
D. -128 128
正确答案: A

452. What output is produced by the following program?
class Test{
public static void main(String args[]){
char x=65;
char y;
y=(char)(2*x);
System.out.println(y);
}
}
Select the one right amswer.
A. A compiler error
B. A runtime error
C. 130
D. ?(symbol(符号) for non-printable character)
正确答案: D

453. What output is produced by the following program?
class Test{
public static void main(String args[]){
byte x=-65;
byte y=2;
byte z=(byte)(x*y);
System.out.println(z);
}
}
Select the one right amswer.
A. A compiler error
B. A runtime error
C. -130
D. None of the above
正确答案: D

454. What output is produced by the following program?
class Test{
public static void main(String args[]){
int x;
double y=-10.9;
x=(int)y;
System.out.println(x+" ");
y=10.9;
x=(int)y;
System.out.println(x);
}
}
Select the one right amswer.
A. -11 10
B. -11 11
C. -10 10
D. –10 11
正确答案: C

455. What output is produced by the following program?
class Test{
public static void main(String args[]){
try{
byte x=64;
byte y=64;
byte z=(byte)(x + y);
System.out.println(z);
}catch(Exception e){
System.out.println("Exception");
}
}
}
Select the one right amswer.
A. A compiler error
B. Exception
C. 128
D. -128
正确答案: D

456.What output is produced by the following program?
class Test{
public static void main(String args[]){
try{
byte x=64;
byte y=64;
System.out.println(x + y);
}catch(Exception e){
System.out.println("Exception");
}
}
}
Select the one right amswer.
A. A compiler error
B. Exception
C. 128
D. -128
正确答案: C

457. What output is produced by the following program?
class Test{
public static void main(String args[]){
try{
byte x=5;
byte y=x«2;
System.out.println(y);
}catch(Exception e){
System.out.println("Exception");
}
}
}
Select the one right answer.
A. A compiler error
B. Exception
C. 5
D. 10
E. 15
F. 20
G. 25
正确答案: A

458. What output is produced by the following program?
class Test{
public static void main(String args[]){
try{
byte x=5;
byte y=(byte)(x«2);
System.out.println(y);
}catch(Exception e){
System.out.println("Exception");
}
}
}
Select the one right answer.
A. A compiler error
B. Exception
C. 5
D. 10
E. 15
F. 20
G. 25
正确答案: F

459. What output is produced by the following program?
class Test{
public static void main(String args[]){
try{
byte x=32;
byte y=(byte)(x«2);
System.out.println(y);
}catch(Exception e){
System.out.println("Exception");
}
}
}
Select the one right amswer.
A. A compiler error
B. Exception
C. 32
D. 64
E. 128
F. 256
G. None of the above -128
正确答案: G

460. What output is produced by the following program?
class Test{
public static void main(String args[]){
try{
byte x=-32;
byte y=(byte)(x»>2);
byte z=(byte)(x + y);
System.out.println(y);
}catch(Exception e){
System.out.println("Exception");
}
}
}
Select the one right amswer.
A. A compiler error
B. Exception
C. -4
D. -8
E. -16
F. -32
G. None of the above
正确答案: D

461.Which of the following assignments are legal?
A. floal a=2.0
B. double b=2.0
C. int c=2
D. long d=2
正确答案: B、C、D

462. The Person, Student and Teacher are class names. These classes have the following inheritance relation as shown below:

Person
Student Teacher

There is the following expression in a Java source file:
Person p=new Student();
Which one of the following statements are true?
A. The expression is legal
B. The expression is illegal
C. Some error will correct but it will be wrong when running
正确答案: A

463. The person, Student and Teacher are class names. These classes have the following inheritance relation as shown below:

Person
Student Teacher

In Java source file a specific method has an argument. In order to handle all these classes in this method which type of argument of this method should be used?
A. Person
B. Student
C. Tescher
D. Object
E. None of them can be used' Person
正确答案: A、D

464. Analyze these two consecutive lines of code:
float f=3.2;
int i=f;
Select all valid answer.
A. this code would not compile
B. this code would compile and i would be set to 3
C. the second line could compile if it were written instead as: int i =(byte)f;
D. the first line could compile if it were written instead as: float f=3.2F;
正确答案: A、C、D

465. What is the final value of temp in this sequence?
long temp=(int)3.9;
temp%=2;
Select the one right amswer.
A. 0
B. 1
C. 2
D. 3
E. 4
正确答案: B

466. Which of the following lines will compile without warning or error?
A. float f=1.3;
B. char c="a";
C. byte b=257;
D. boolean b=null;
E. int i=10;
正确答案: E

467. Which of the following lines of code will compile without error?
A. int i=0;
if(i){
System.out.println("Hello");
}
B. boolean b=true;
boolean b2=true;
if(b==b2){
System.out.println("So true");
}
C. int i=1;
int j=2;
if(i==1 || j==2)
System.out.println("OK");
D. int I=1;
int j=2;
if(i==1 &| j==2)
System.out.println("OK");

正确答案: B、C

468. What will be the result when you attempt to compile this program?
public class Test{
public static void main(String args[]){
int iRand;
iRand=Math.random();
System.out.println(iRand);
}
}
Select the one right answer.
A. Compile time error referring to a cast problem
B. A random number between 1 and 10
C. A random number between 0 and 1
D. A compile time error about random being an unrecognized method
正确答案: A

469. Which of the following will compile correctly?
A. short myshort = 99s;
B. String name = 'Excellent tutorial Mr Green';
C. char c = 17c;
D. int z=015;
正确答案: D

470. Given the following code:
class Base {}
public class Test extends Base{
static boolean b1 = false;
static int i=-1;
static double d=10.1;
public static void main(String argv[]){
Test m=new Test();
Base b=new Base();
//Here
}
}
Which of the following, If inserted at the comment //Here will allow the code to compile and run without error?
A. b=m;
B. m=b;
C. d=i;
D. b1=i;
正确答案: A、C

471. Given the following classes which of the following will compile without error?
interface IFace{}
class CFace implements IFace{}
class Base{}
public class Test extends Base{
public static void main(String argv[]){
Test ob=new Test();
Base b=new Base();
Object o1=new Object();
IFace o2=new CFace();
}
}
Select the one right answer.
A. o1=o2;
B. b=ob;
C. ob=b;
D. o1=b;
正确答案: A、B、D

472. Which of the following are legal statements?
A. float f=1/3;
B. int i=1/3;
C. float f=1.01;
D. double d=999d;
正确答案: A、B、D

473. Which of the following are valid statements?
A. System.out.println(1+1);
B. int i=2+'2';
C. byte b=255;
正确答案: A、B

474. Given the following code:
class Base{}
class Test extends Base{
public String getFields(){
String name="Test";
return name;
}
}
public class DoTest{
public static void main(String argv[]){
Base a=new Test();
Here
}
}
What code placed after the comment
Here will result in calling the getFields method resulting in the output of the string "Test"?
A. System.out.println(a.getFields());
B. System.out.println(a.name);
C. System.out.println((Base) a.getFields());
D. System.out.println(((Test) a).getFields());
正确答案: D

475. What is the result when you compile and run the following code?
byte Byte=10;
byte Double=12;
byte Integer=Byte*Double;
Select the one right answer.
A. 120
B. Compile time error while declaring variables
C. Compile time error while multiplication
D. None of the above
正确答案: C

SCJP考题4

http://blog.csdn.net/wangjun_pfc/article/details/1823165
分类: SCJP考题 2007-10-13 15:00 481人阅读 评论(0) 收藏 举报
500. Examine the following code which includes an inner class:
public final class Test implements A{
class Inner{
void test(){
if (!Test.this.flag);{
sample();
}
}
private boolean flag = false;
}
publi void sample(){
System.out.println("Sample");
}
public Test(){
(new Inner()).test();
}
public static void main(String args[]){
new Test();
}
}
What is the result?
A. Print out "Sample"
B. Program produces no output but terminates correctly.
C. Program does not terminate.
D. The program will not compile
正确答案: A

501. Given the following incomplete method:
1) public void method(){
2)
3) if(someTestFails()){
4)
5) }
6)
7) }
You want to make this method throw an IOException if, and only if, the method someTestFails() returns a value of true.
A. Add at line 2:IOException e;
B. Add at line 4:throw e;
C. Add at line 4:throw new IOException();
D. Add at line 6:throw new IOException();
E. Modify the method declaration to indicate that an object of type Exception might be thrown.
正确答案: C、E

502. Which exception might wait() throw?
Fill in the blank.
正确答案: "InterruptedException" or "IllegalMonitorException"

503. Given this code snippet:
try{
tryThis();
return:
}catch(IOException x1){
System.out.println("exception 1");
return;
}catch(Exception x2){
System.out.println("exception 2");
return;
} finally{
System.out.println("finally");
}
What will appear in the standard output if tryThis() throws a NumberFormatException?
Select the one right answer.
A. Nothing
B. "exception 1",followed by "finally"
C. "exception 2",followed by "finally"
D. "exception 1"
E. "exception 2"
正确答案: C

504. What letters get written to the standard output with the following code?
class Test{
public static void main(String[] args){
try {
method();
} catch(Exception e){}
}
static void method(){
try {
wrench();
System.out.println("a");
} catch (ArithmeticExeption e){
System.out.println("b");
}finally{
System.out.println("c");
}
Syatem.out.println("d");
}
static void wrench(){
throw new NullPointerException();
}
}
Select all valid answer.
A. "a"
B. "b"
C. "c"
D. "d"
E. none of there
正确答案: C

505. consider the following program:
public class Test{
public static void main(String args[]){
boolean a=false;
if(a=true)
System.out.println("Hello");
else
System.out.println("Goodbye");
}
}
What is the result? Select the most appropriate answer.
A. Program produces no output but terminates correctly.
B. Program does not terminate
C. Prints out "Hello"
D. Prints out "Goodbye"
正确答案: C

506. Given the following code:
public void fun(){
int i;
try{
i=System.in.read();
System.out.println("Location 1");
} catch(IOException e){
System.out.println("Location 2");
} finally{
System.out.println("Location 3");
}
System.out.println("Location 4");
}
If an IOException occurs, what will be printed?
A. Location 1
B. Location 2
C. Location 3
D. Location 4
正确答案: B、C、D

507. If the method func() is allowed to throw out the IOException, which declaration of this method can used?
A. public int func(int i)
B. public int func(int i) throw IOException
C. public int func(int i) throw Exception
D. public int func(int i) throws IOException
E. public int func(int i) throws Exception
正确答案: D、E

508. the following code:
class Test{
public static void main(String args[]){
try{
return;
}
catch(Exception e){
System.out.println("Exception");
}
finally{
System.out.println("Finally");
}
}
}
What will be the out put?
A. Compilation Error viod main Can't return any value.
B. Prints Exception.
C. Prints Exception Finally.
D. Prints Finally
正确答案: D

509. Give incompleted method:
1) {
2) if(unsafe())
3) {do something…}
4) else if (safe())
5) {
do the other…}
6) }
The method unsafe() will throw an IOException, which completes the method of declaration when added at line one?
A. public IOException methodName()
B. public void methodName()
C. public void methodName() throw IOException
D. public void methodName() throws IOException
E. public void methodName() throws Exception
正确答案: D、E

510. Give the following method:
public void example(){
try{
unsafe();
System.out.println("Test 1");
}catch(SafeException e){
System.out.println("Test 2");
}finally{
System.out.println("Test 3");
}
System.out.println("Test 4");
}
Which will display if method unsafe() run normally?
A. Test 1
B. Test 2
C. Test 3
D. Test 4
正确答案: A、C、D

511. What appears in the standard output if the method named problem() in the code below throws an instance of class Exception when the method named trythis() is invoked?
public void trythis(){
try{
System.out.println("1");
problem();
}catch(RuntimeException x){
System.out.println("2");
return;
}catch(Exception x){
System.out.println("3");
return;
}finally{
System.out.println("4");
}
System.out.println("5");
}
Select all valid answers.
A. "1"
B. "2"
C. "3"
D. "4"
E. "5"
正确答案: A、C、D

512. What deyword must appear in a method declaration(followed by the name of the exception )when that method might cause an exception to be thrown and that method does not handle the exception?
Fill in the blank
正确答案: throws

513. Analyze the following code.
void looper(){
int x=0;
one:
while(x<10){
two: 非法标号
System.out.println(++x);
if(x > 3)
break two;
}
}
Select all valid answer.
A. This code compiles
B. This code does not compile
C. This code writes the number 0 to the standard output
D. The numbers 1 and 2 to the standard outout
E. The numbers 3 and to the standard outout
F. The numbers 4 to the standard outout
G. The numbers 5 through 9 to the standard outout
H. The numbers 10 to the standard outout
正确答案: B

514. What appears in the standard output when the method named testing is invoked?
void testing(){
one:
two:
for(int i=0;i < 3;i++){
three:
for(int j = 10;j < 30; j+=10){
System.out.println(i+j);
if(i > 2)
continue one;
}
}
}
Select all valid answers.
A. 10 and 20
B. 11 and 21
C. 12 and 22
D. 13 and 23
E. 30,31,32,33
正确答案: A、B、C

515. What is written to the standard output as the result of executing the following statements?
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
Object obj1 = (Object)b1;
Object obj2 = (Object)b2;
if(obj1==obj2)
if (obj1.equals(obj2))
System.out.println("a");
else
System.out.println("b");
else
if(obj1.equals(obj2))
System.out.println("c");
else
System.out.println("d");
Select the one right answer.
A. a
B. b
C. c
D. d
正确答案: C

516. Which of the following statements about try, catch, and finally are true?
Select all valid answers.
A. A try block must always be followed by a catch block
B. A try block can be followed either by a catch block or a finally block, or both
C. A catch block must always be associated with a try block
D. A finally can never stand on its own (that is, without being associated with try block)
E. None of these are true
正确答案: B、C、D

517. What will be printed out if you attempt to compile and run the following code?
int i=1;
switch(i){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one")
Case 2:
System.out.ptintln("two")
default:
System.out.ptintln("default");
}
Select the one right answer.
A. one
B. one,default
C. one,two,default
D. default
正确答案: C

518. What will be printed out if you attempt to compile and run the following code?
int i=9;
switch(i){
default:
System.out.println("default");
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
Select the one right answer.
A. default
B. default, zero
C. error default clause not defined
D. no output displayed
正确答案: B

519. What will be output by the following code?
public class Test{
public static void main(String argv[]){
int i;
int j;
outer:
for (i=1; i < 3;i++){
if(j==2)continue outer;
System.out.println("Value for i="+i+"Value for j="+j);
}
}
}
Select the one right answer.
A. Value for i=1 value for j=1
B. Value for i=2 value for j=1
C. Value for i=2 value for j=2
D.编译错误
正确答案:D
没初使化

520.What code placed after the comment For loop
would populate the elements of the array ia[] with values of the variable i?
public class Test{
public static void main(String argv[]){
Test 1=new Test();
1.amethod();
}
public void anethod(){
int ia[]=new int[4];
Start For loop
{
ia[i]=i;
System.out.println(ia[i]);
}
}
}
Select the one right answer.
A. for(int i=0;i<ia.length()-1;i++)
B. for(int i=0;i<ia.length();i++)
C. for(int i=1;i<4.i++)
D. for(int i=0;i<ia.length;i++)
正确答案: D

521. Give the following code:
import java io.*;
public class Test{
public static void main(String argv[]){
Test t=new Test();
t.amethod();
}
public void amethod(){
try{
ioCall();
}catch(IOException ioe){}
}
}
What code would be most likely for the body of the ioCall method?
A. public void ioCall () throws IOException{
DataInputStream din=new DataInputStream(System.in);
din.readChar();
}
B. public void ioCall() throw IOException{
DataInputStream din=new DataInputStream(System.in);
din.readChar();
}
C. public void ioCall (){
DataInputStream din=new DataInputStream(System.in);
din.readChar();
}
D. public void ioCall IOException(){
DataInputStream din=new DataInputStream(System.in);
din.readChar();
}
正确答案: A

522. What will happen when you attempt to compile and run the following code?
public class MySwitch{
public static void main(String argv[]){
MySwitch ms=new MySwitch();
ms.anethod();
}
public void anethod(){
int k=10;
switch(k){
//Put thedefault at the botton, not here
defauly:
System.out.println("This is thedefault output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}
Select the one reght answer.
A. None of these options.
B. Compile time error target of switch must be an integral type.
C. Compile and run with output "This is the default output".
D. Compile and run with output of the single line "ten".
正确答案: D

523. What will happen when you attempt to compile and run the following code?
public class Test{
static public long i=10;
public static void main(String argv[]){
switch(i){
default:
System.out.println("no value given");
case 1:
System.out.println("one");
case 10:
System.out.println("ten");
case 5):
System.out.println("five");
}
}
}
Select the one right answer.
A. Compile time error
B. Output of "ten"followed by "five"
C. Output of "ten"
D. Compilation and run time error because of location of default
正确答案: A

524. Given the following code:
import java io*;
public class Test{
public static void main(String argv[]){
Test P=new Test();
p.fliton();
}
public int fliton(){
try{
FileInputStream din=new FileInputStream("Ttst.java");
din.read();
}catch(IOException ioe){
System.out.println("flytwick");
return 99;
}finally{
System.out.println("fliton");
}
return -1;
}
}
Assuming the Test.java is available to be read which of the following statements are true if you try to compile and run the program?
A. The program will run and output only "flytwick".
B. The program will run and output only "fliton".
C. The program will run and output both "fliton"and"flytwick".
D. The error will occur at compile time because the method fliton attempts to return two values.
正确答案: B

525. The thread run() method has the following code, what is the result when the thread runs?
try{
sleep(200); 不会扔出一个IOException
System.out.println("Printing from thread run()method");
}catch(IOException ie){}
Selet the one right answer.
A. Compile time error
B. Prints on the console Printing from thread run() method
C. At line 2 the thread will be stop running and resumes after 200 milliseconds and prints Printing from thread run() method
D. At line 2 the thread will be stop running and resumes exactly 200 milliseconds elapsed
正确答案: A

526. Please select the correct answer from the following?
public class Test{
static void throwMethod() throws Exception{
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public statle void main(String args[]){
try{
throwMethod();
}catch(illegalAccessException e){
System.out.println("Caught"+e);
}
}
}
Select the one right answer.
A. Compilation error
B. Runtime error
C. Compile successfully, nothing is printed
D. inside throwMethod. followed by caught:java.lang.IllegalAccessException:demo
正确答案: d

527. What is the output when you execute the following code?
int i=100;
switch(i){
case 100:System.out.println(i);
case 200:System.out.println(i);
case 300:System.out.println(i);
}
Select the one right answer.
A. Nothing is printed
B. Compile time error
C. The values 100,100,100 printed
D. Only 100 is Printed
正确答案: C

528. How can you change the break statement below so that it breaks out if the inner and middle loops and continues with the next iteration of the outer loop?
outer: for(int x=0;x < 3; x++){
middle: for(int y=0;y<3;y++){
if (y==1){
break;
}
}
}
Select the one right answer.
A. continue middle;
B. break middle;
C. break outer;
D. continue
正确答案: B

529. What is the result when you compile and run the following code?
public class Test{
static void throwMethod(){
System.out.println("Inside throw Method.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]){
try{
throwMethod();
}
catch(IllegallAccessException e){
System.out.Println("Caught"+e);
}
}
}
Select the one right answer.
A. Compilation error
B. Runtime error
C. Compile successfully, nothing is printed
D. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption:demo
正确答案: A

530. Given the following code:
if(x > 0){
System.out.println("first");}
else
if(x > -3){
System.out.println("second");}
else{
System.out.println("third");}
Which range of x value would print the string "second"?
A. x>0
B. x>-3
C. x<=-3
D. x<=0 & x>-3
正确答案: D

531. Given the following code:
public void test(){
try {
oneMethod();
System.out.println("condition 1");
} catch(ArrayIndexOutOfBoundsException e){
System.out.println("condition 2");
}
catch(Exception e){
System.out.println("condition 3");
}finally{
System.out.println("finally");
}
}
Which will display if oneMethod run normally?
A. condition 1
B. condition 2
C. condition 3
D. finally
正确答案: A、D

532. Given the following code:
public class Test{
void printValue(int m){
do {
System.out.println("The value is "+m);
}
While(—m > 10)
}
public static void main(String arg[]){
int i=10
Test t=newTest();
t.printValue(i);
}
}
Which will be output?
A. The value is 8
B. The value is 9
C. The value is 10
D. The value is 11
正确答案: C

533. Given the following code fragment:
1) switch(m){
2) case 0:System.out.println("case 0");
3) case 1:System.out.println("case 1");break;
4) case 2:
5) defarlt:System.out.println("default");
6) }
Which value of m would cause "default" to be the output?
A. 0
B. 1
C. 2
D. 3
正确答案: C、D

534. Given the incompleted method:
//line 1
{
success = connect();
if (success==-1){
throw new TimedOutException();
}
}
TimedOutException is not a RuntimeException. Which can complete the method of declaration when added at line 1?
A. public void method()
B. public void method() throws Exception
C. public void method() throws TimedOutException
D. public void method() throw TimedOutException
E. public throw TimedOutException viod method()
正确答案: B、C

535. Which of the following are acceptable to the Java compiler.
Select all correct avswers.
A. if(2==3)System.out.println("Hi");
B. if(2=3)System.out.println("Hi");
C. if(true)System.out.println("Hi");
D. if(2!=3)System.out.println("Hi");
E. if(aString.equals("hello"))System.out.println("Hi");
正确答案:A、C、D、E

536. Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is the correct way for a method to indicate that it expects the caller to handle that exception:
Select the most appropriate answer.
A. throw Exception
B. throws Exception
C. new Exception
D. Don't need to specify anything
正确答案:B

537. What is the result of executing the following code, using the parameters 4 and 0:
public void divide(int a, int b){
try{
int c=a/b;
}catch(Exception e){
System.out.ptint("Exception");
}finally{
System.out.println("Finally");
}
Select the most appropriate answer.
A. Prints out: Exception Finally
B. Prints out: Finally
C. Prints out: Exception
D. No output
正确答案:A

538. Given the following code what is the effect of a being 5:
public class Test{
public void add(int a){
loop:for(int i= 1; i < 3; i++){
for(int j = 1;j < 3; j++){
if(a==5){
break loop;
}
System.out.println(i*j);
}
}
}
}
Select the most appropriate answer.
A. Generate a runtime error
B. Throw an ArrayIndexOutOfBoundsException
C. Print the values: 1,2,3,4
D. Produces no output
正确答案:D

539. What is the result of executing the following code when the value of x is 2?
switch(x){
case 1:
System.out.println(1);
case 2:
case 3:
System.out.println(3);
case 4:
System.out.println(4);
}
Select the most appropriate answer.
A. Nothing is printed out
B. The value 3 is printed out
C. The values 3 and 4 are printed out
D. The values 1,3 and 4 are printed out
正确答案:C

540. What is the result of executing the following fragment of code:
boolean flag=false;
if(flag=true){
System.out.println("true");
}else{
System.out.println("false");
}
Select the most appropriate(适合的) answer.
A. true is printed to standard out.
B. false is printed to standard out.
C. An exception is raised
D. Nothing happens
正确答案:A

541. Examine the following code:
public class Test{
public static void main(String args[]){
int total=0; 循环无法执行
for(int i=0,j=10;total>30;++i,—j){
System.out.println("i="+i+":j="+j);
total+=(i+j);
}
System.out.println("Total"+total);
}
}
Select the most appropriate answer.
A. Produce a runtime error
B. Produce a compile time error
C. Print out "Total 0"
D. Gemerate the following as output:
正确答案:C

542. Given the following code:
class Test{
public static void main(String args[]){
System.out.println(hai());
}
static int hai(){
try{
return 1;
}
catch(Exception e){
System.out.println("Exception");
}
finally{
System.out.println("Finally");
Return 2;
}
}
}
What will be the out put?
A. Compilation Error void main Can't return any value.
B. Prints Exception.
C. Prints Exception Finally.
D. Prints Finally and 1.
E. Prints Finally and 2.
正确答案:E

543. Given the following code:
public static void main(String args[]){
try{
System.out.println("1");
}catch(RuntimeException x){
System.out.println("2");
return:
}catch(Exception x){
System.out.println("3");
return;
}finally{
System.out.println("4");
}
System.out.println("5");
}
If there is no Exception in the try block, what will be the output? Select all that apply.
A. 1
B. 2
C. 3
D. 4
E. 5
正确答案:A、D、E

544. Given the following code:
class Test{
public static void main(String args[]){
try{
mymethod();
}catch(Exception e){
System.out.println("Exception");
}
}
}
How will you declare mymethod() so that it will throw a Exception? Select one?
A. In method declaration give throws Exception and inside the method give throw Exception.
B. In method declaration give throw Exception and inside the method give throws Exception.
C. In method declaration give throw Exception and inside the method give throw Exception.
D. In method declaration give throws Exception and inside the method give throws Exception.
正确答案:A

545. Given the following code:
class Test{
public static void main(String args[]){
try{
//ProtocoIException
}catch(IOException e){
System.out.println("I");
}catch(IOException e){
System.out.println("E");
}finally{
System.out.println("F");
}
}
}
If a ProtocoIException occurs in try block. what will be the output? Select all.
A. I
B. E
C. F
D. Compilation Error says that there is no matching catch block
正确答案:A、C

546. Given the following code:
public static void main(String args[]){
int a=10;
int b=20;
if(a=b)
System.out.println("Not Equal");
else
System.out.println("Equal");
}
What will be the output?
A. Equal
B. Not Equal
C. Compilation Error
D. An Exception will occurs
正确答案:C

547. Given the following code:
public static void main(String args[]){
int t=0;
while(1){
if(t++<10)
break;
}
}
What will be the value of t after the while loop?
A. 11
B. 9
C. 10
D. Compilation Error
E. An Exception will occcurs
正确答案:D

548. If a method throws an ProtocoIException(subclass of IOException) which of the following can be used in declaring that method?
A. public void method() throws Exception
B. public void method() throw Exception
C. public throws IOException void method()
D. public void method() throws IOException
E. public void method() throws ProtocoIException
正确答案:A、D、E

549. What is the output when following code is run?
1. class Test{
2. public static void main(String args[]){
3. int i=0,j=5;
4. st: for(;;i++){
5. for(;;j—){
6. if(i>j)
7. break st;
8. }
9. }
10. System.out.println("i="+i"j="+);
11. }
12.}
Select the one right answer.
A. i=0 j=-1
B. i=1 j=0
C. i=0 j-1
D. i=1 j=-1
E. Compiler error at line 4.
正确答案:A

550. What will be the output when following code is run?
1. class Test{
2. static void foo() throws Exception{
3. throw new Exception();
4. }
5. pubilc static void main(String args[]){
6. try{
7. foo();
8. }catch(Exception e){
9. System.exit(0);
10. }
11. finally{
12. System.out.println("Infinally");
13. }
14. }
15. }
Select the one right answer.
A. Compiler error.
B. Code will compile fine and when run prints Infinally.
C. Nothing will be printed.
D. Runtime exeption.
正确答案:B

SCJP考题5

第八周测试 61题
http://blog.csdn.net/wangjun_pfc/article/details/1823171

600. Your chief Software designer has shown you a sketch of the new Computer parts system she is about to create. At the top of the hierarchy is a Class called Computer and under this are two child classes. One is called LinuxPC and one is called WindowsPC.
The main difference between the two is that one runs the Linux operating System and the other runs the Windows System(of course another difference is that one needs constant re-booting and the other runs reliably), Under the WindowsPC are top Sub classes one called Server and one Called Workstation. How might you appraise your designers work?
A. Give the go ahead for further design using the current scheme
B. Ask for a re-design of the hierarchy with changing the Operating System to a field rather than Class type
C. Ask for the option of WindowsPC to be removed as it will soon be obsolete
D. Change the hierarchy to remove the need for the superfluous Computer Class
正确答案:B

601. Which of the following statements are true?
A. An inner class may be defined as static
B. There are No circumstances where an inner class may be defined as private
C. An anonymous class may have only one constructor
D. An inner class may extend another class
正确答案:A、D

602. Given the following class definition, which of the following methods could be legally placed after the comment?
public classRid{
public void amethod(int i, String s){}
//Here
}
Select the one right answer.
A. public void amethod(String s, int i){}
B. public int amethod(int i, String s){}
C. public void amethod(int i, String mystring s){}
D. public void Amethod(int i, String s){}
正确答案: A、D

603. Given the following class definition which of the following can be legally placed after the comment line?
class Base{
public Base(int i){}
}
public class MyOver extends Base{
public static void main(String arg[]){
MyOver m=new MyOver(10);
}
MyOver(int i){
super(i);
}
MyOver(String s,int i){
this(i);
//Here
}
}
Select the one right answer.
A. MyOver m=new MyOver();
B. super(
正确答案: D
C. this("Hello",10);
D. Base b=new Base(10);
正确答案: D

604. Given the following class definition which of the following statements would be legal after the comment?
class InOut{
String s=new String("Between");
public void amethod(final int iArgs){
int iam;
class iam;
class Bicycle{
public void sayHello(){
Here
}
End of bicycle class
}
}//End of amethod
public void another(){
int iOther;
}
}
Selec the one right answer.
A. System.out.println(s);
B. System.out.println(Other);
C. System.out.println(iam);
D. System.out.println(Args);
正确答案: A、D

605. Which of the following statements are true?
A. An interface can only contain method and not variables
B. Interfaces cannot have constructors
C. A class may extend only one other class and implement only one interface
D. Interfaces are the Java approach to addressing its lack of multiple inheritance,but reqrire inplementing classes the functionality of the Interfaces
正确答案: B、D

606. W@hat will gappen when you attempt to compile and run the following code?
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}
Select the one right answer.
A. Compile time error Base is a keyword
B Compile and no output at runtime
C. Output of Base
D. Runtime error Base has no valid constructor
正确答案: B

607. Which of the following statements are true?
A. Comstructors cannot have a visibility modifier
B. Comstructors can be marked public and protected,but not private
C. Comstructors can only have a prinitive return type
D. Comstructors are not inherited
正确答案: D

608. What will happtn when you attempt tp compile and run the following code?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Severn extends Base{
public static void main(String argv[]){
Severn s=new Severn();
}
void Severn(){
System.out.printl("Severn");
}
}
Select the one right answer.
A. Compilation and output of the string "Severn"at runtime
B Compile time error
C. Compilation and no output at runtime
D. Compilation and putput of the string"Base"
正确答案: B

609. Which of the following statements are true?
A. The default constructor has a return type of void
B. The default constructor takes a return type of void
C. The default constructor takes no parameters
D. The default constructor is not created if the class has any constructors of its own
正确答案: C、D

610. Which of the following statements statemints are true?
A. All of the variables in an interface are implicitly static
B. All of the variables in an interface are implicitly final
C. All of the methods in an interface are implicitly sbstract
D. A method in an interface can access class level variables
正确答案: A、B、C

611. Which of the following statements are true?
A. A method in an interface must not have a body
B. A class may extend one other class plus at most one interface
C. A class may extend at most one other class plus implement many interfaces
D. An class accesses an interface via the diyword uses
正确答案: A、C

612. You are given a class hierarchy with an instance of the class Dog.The class Dog is a child of mammal and the class Mammal is a child of thd class Vertebrate.The class Vertebrate has a method called move which prints out the string "move".The class mammal onerrides this method and prints out the string "walks".The class Dog overrides this method and prints out the string"walks on paws".Given an instance of the class Dog.how can you access the ancestor method move in Vertebrate so it prints out the string"move".
A. d.super().supeer().move();
B. d.parent().parent().move();
C. d.move();
D. none of the above;
正确答案: D

613. Which of the following most closely describes the process of overriding?
A. A class with the same name replaces the functionality of a class defined earlier in the hierarchy
B. A method with the same name completely replaces the functionality of a method earlier in the hierarchy
C. A method wigh the same name but different parameters gives multiple uses for the same method name
D. A calss is prevented from accessing methods in its immediate ancestor
正确答案: B

614. Given the following class definition:
public dalss Upton{
public static void amethod(int i){
}
oublic void amethod(int i){
}
//Here
}
Which of the following would be legal to place after the comment?
A. public int amethod(int z){}
B. publi int amethod(int i,int j){returm 99;}
C. orotected void amethod(long 1){}
D. private void anothermethod(){}
正确答案: B、C、D

615. Given the following class definition:
public class Droitwich{
classone {
private class two{
public void main(){
System.out.println("two");
}
}
}
}
Which of the following statements are true?ar
A. The code will not compile because the classes are nested to more than one level
B. The code will not compile because class two is marked as private
C. The code will not compile and output the string two at runtime
D. The code will not compile without error
正确答案: D

616. Which of the following statements are true?
A. constructors cannot be overloaded
B. constructors cannot be overloaden
C. a constructor can return a primitive or an object trference
D. constructor code executes from the current class up the hierarchy to the ancestor class
正确答案: B

617. Which of the following statements are true?
A. A method cannot be orerloaded tp be less public in a child class
B. To be overridden a method must have the same name and parameter types
C. To be overridden a method nust have the same name,parameter and return types
D. An overridden method nust ahve the same name,parameter names and parameter types
正确答案: C

618. What will happen when you attempt to compile and run the following code?
class Base{
Base(){
System.out.println("Base");
}
}
public class Checket extends Base{
public static void main(String argv[]){
hecket c=new Checket();
srper();
}
Checket(){
System.out.println("Checket");
}
}
Select the one right answer.
A. Compile time error
B. Checket followed by Checket
C. Base followed by Checket
D. runtime error
正确答案: A

619. Which of the following statements are true?
A. Static methods cannot be overriden to be non static
B. Static methods cannot be declared as private
C. Private methods cannot be overloaded
D. An overloaded method cannot throw exceptions not checked in the nase class
正确答案: A

620. What is the result when you compile and run the following code?
class Top{
static void myTop(){
System.out.println("Testing myTop method in Topclass");
}
}
public class Down extends Top{
void myTop(){
void myTop(){
System.out.println("Testing myTop method in Down class");
}
public static viod main(String[]args){
Top t=new Down();
t.myTop();
}
}
Select the one right answer.
A. Compile Time error
B. Runtime errorPrints Testing myTop method in Top class on the console
C. Prints Testing myTo[ method in Top class on the console
D. Prints Testing myTop method in Down class on the screen
正确答案: A

621. At what ooint the following code shows compile time error?
class A{
A(){
System.out.println("Class A constructor");
}
}
class B extends A{
B(){
System.out.println("Class B constructor");
}
}
public class C extends A{
C(){
System.out.println("Class C constructor");
}
public static void main(String args[]){
A a=new A();Line 1
A a1=new B();
Line 2
A a2=new C();Line 3
B b=new C();
Line 4
}
}
Select the one right answer.
A. A a=new A();Line 1
B. A a1=new B();
Line 2
C. A a2=new C();Line 3
D. A b=new C();
Line 4
正确答案: D

622. Select all correct answers about what is the definition of an interface.
A. It is a blue print
B. A new data type
C. Nothing but a class definition
D. To provide multiple inheritance
正确答案: A、B、D

623. What is the result from the following code when you run?
import java,io.*;
calss A {
A()throws Exception{
System.out.println("execrting class A constructor");
throw mew IOException();
}
}
public class B extends A{
B(){
System.out.println("Execrting class B constructor");
}
public static void main(String args[]){
try{
A a=new B();
}catch(Exception){
System.out.println(c.getMessage());
}
}
}
Select the one right answer.
A. Executing class A constructor
B. Executing class B constrRuntime errorA a2=new C();//Line 3
C. Runtime error
D. Compile time error
正确答案: D

624. What is the result from the following code when you run?
import java.io.*;
class A{
A(){
System.out.println("Executing class A construclor");
}
A(int a)throws Exception{
System.out.println("Executing class A constructor");
throw new IOException();
}
}
public class B extends A{
B(){
System.out.println("Executing class B constructor");
}
public static vild main(String args[]){
try{A a=new B();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
}
Select the one right answer.
A. Executing class A constructor followed by Executing class B constructor
B. No output
C. Runtime error
D. Compile time error
正确答案: A

625. If you save and compile the following code.it gives compile time error.How do you correct the compile time error?
public class OuterClass{
finsl String s="Iam outer class member variable";
public void Method(){
String s1="Iam inner class variable";
class InnerClass{
Public viod innerMethod(){
int xyz=20;
System.out.println(s);
System.out.println("Integer value is"+ xyz);
System.out.println(s1);//Illegal.compiler error
}
}
}
}
Select the one right answer.
A. By making s1 as static variable
B. By making s1 as public variable
C. By making s1 as final variable
D. By making InnerClass as static
正确答案: C

626. What is the reason using $ in inner class representation?
A. Because the inner classes are defined inside of amy class
B. Due to the reason thaat inner classes can be defined inside any method
C. This is convention adopted by Sun,to insure that there ix no ambiguity between packages and inner classes.
D. Because if use getClass(),getName() will gives you the error
正确答案: C

627. What will be printed when you execute the code?
class A{
A(){
System.out.println("Class A Constructor");
}
}
public class B exlends A{
B(){
System.out.println("Class B Constructor");
}
public statie void main(String args[]){
B b=new B();
}
}
Select the one right answer.
A. Class A Constructow followed by Class B Constructor
B. Class B Constructow followed by Class B Constructor
C. Compile time error
D. Run time error
正确答案: A

628. Given the piece of code,select the correct to replace at the comment line.
class A{
A(int i){
}
}
public class B extends A{
B(){
//xxxxx
}
public static void main(String args[]){
B b=new B();
}
}
Select the one right answer.
A. super(100);
B. this(100);
C. super();
D. this();
正确答案: A

629. Examine the code below and select the best answer.
class OuterOne{
static classInerOne{}
}
A. we can't make instantce of innerclass outside class OuterOne
B. InnerOne in=new Inner()' will make instance of inner class in another class
C. OuterOne.InnerOne in=new OuterOne.Inner()
D. InnerOne in=new OuterOne().new Inner()'will make instance of inner class in another class
正确答案: C

630. Which of the statements are true?
A. Overridden methods have the asme method name and signature
B. Overridded methods have the asme method name and signature
C. Overridden methods have the asme method name and different signature
D. Overridded methods have the asme method name and different signature
正确答案: A、D

631. What is the result of compiling the following code?
import java.io.*;
classMyExp {
void MyMethod() throws IOException,EOFException{
…………
}
}
class MyExp1 extends MyExp{
void MyMethod(){
…………
}
}
public class MyExp2 extends MyExp1{
voik MyMethod() throws IOException {
…………
}
}
Select the one right answer.
A. Compile time error
B. No compile time error
C. Run-Time error
D. MyMethod() cannot declare IOExceptio in MyExp2 class
正确答案: A、D

632. Select the correct anonymous inner class declaration?
A. new Outer.new Inner
B. new Inner() {}
C. new Inner()
D. Outwe.new Inner()
正确答案: B

633. Which of the following statements are true?
A. An anonymous class cannot have any constructors.
B. An anonymous class can only be created within the body of a method.
C. An anontmous class can only access stati fields of the enclosing class.
D. An anontmous class instantiated and declared in the same place.
正确答案: A、D

634. How can you implement encapsulation?
A. By making methods private and variable private.
B. By making methods are public and variables as private.
C. Make all variable are public and access them using methods.
D. Making all methods and variables as protected.
正确答案: B

635. Given the following class definition,which of the following methods could be legally placed after the comment?
public class Test{
public void anethod(int i,String s){}
//Here
}
Select the one right answer.
A. public void anethod(String s,int i){}
B. public int amethod(int i,String mystring){}
C. public void amethod(int i,String mystring){}
D. public void Amethod(int i,String s){}
正确答案: A、D

636. Given the following class definition,which of the following can be legally placed after the comment line?
class Base{
public Base(int i){
}
}
public class Derived extends Base{
public static void main(String arg[]){
Derived d=new Derived(10);
}
Derived(int i){
super(i);
}
Derived(String s,int i){
this(i);
//Here
}
}
Select the one right answer.
A. Derived d=new Derived();
B. super();
C. this("Hello",10);
D. Base b=new Base(10);
正确答案: D

637. Given the following code fragment:
1) public void create(){
2) Vector mVect;
3) myVect=new Vector();
4)}
Which og the following statements are true?
A. The declaration on line 2 does not allocate memory space for the variable myVect.
B. The declaration on line 2 allocates memory space for a reference to a Vector object.
C. The statement on line 2 creates an object of class Vctor.
D. The statement on line 3 creates an object of class Vctor.
E. The statement on line 3 allocates memory space for an object of class Vector.
正确答案: A、D、E

638. Which statements about inheritance are true?
A. In Java programming language only allows single inheritance.
B. In Java programming language allows a class to implement only one interface.
C. In Java programming language a class cannot extend a class and implement a interface together.
D. In Java programming language single inheritance makes code more reliable.
正确答案:A、D

639. Given the following code:
1) class Person{
2) public void printValue(inti int j){/*…*/}
3) public void printValue(inti i){/*…*/}
4) }
5) puvlic class Teacher extends Person{
6) public void prinValue(){/*…*/}
7) public void prinValue(int i){/*…*/}
8) public static void main(String args[]){}
9) person t=new Teacher();
10)t.trintValue(10);
11)}
12)}
Which method will the statement on line 10 call?
A. on line 2
B. on line 3
C. on line 6
D. on line 7
正确答案:D

640. Given the uncompleted code of a class:
class Person {
String name,department;
int age;
public Person(String n){
name=n; }
public Person(String n, int a){
name=n;
age=a;
}
public Person(String n, String d,int a){
doing the same as two arguments version of constructor
including assignment name=n,age=a
department=d;
}
}
Which expression can be added at the "doing the same as…" part of the constructor?
A. person(n,a);
B. this(Person(n,a));
C. this(n,a);
D. this(name,age).
正确答案:C

641. Given the following code:
public class Parent {
public int addValue(int a,int b){
int s;
s=a+b;
return s;
}
}
class Child extends Parent{
}
Which methods can be added into class Child?
A. int addValue(int a,int b){// do something…}
B. public void addValue(){do something…}
C. public int addValue(int a){
do something…}
D. public int addValue(int a,int b)throws MyException {//do something…}
正确答案:B、C

642. Given the following code:
public class Parent {
int change(){…}
}
class Child extends Parent{
}
Which methods can be added into class Child?
A. public int change(){}
B. int chang(int i){}
C. private int change(){}
D. abstract int chang(){}
正确答案:A、B

643. Given the following code:
class Parent {
String one,two;
public Parent(String a,String b){
one = a;
two = b;
}
public void print(){
System.out.println(one);
}
}
public class Child extends Parent{
public Child(String a,String b){
super(a,b);
}
public void print(){
System.out.println(one+"to"+two);
}
public static void main(String arg[]){
Parent p=new Parent("south","north");
Patent t=new Child("east","west");
p.print();
t.print();
}
}
Which of the following is correct?
A. Cause error during compilation.
B. south
east
C. south to north
east to west
D. sorth to north
east
E. south
east to west
正确答案:E

644. Given the following code:
class Person{
String name.department;
public void printValue(){
System.out.println("name is "+name);
System.out.println("department is"+depqrtment);
}
}
public classnTeacher extendsb Person{
int salary;
public class Teacher extends Person{
int salary;
public void prinValue(){
doing the same as in the parent method printValue()
including print the value of name and department.
System.out.println("salary is"+salary);
}
}
Which expression can be added at the "doing the same as…"part of the method printValue()?
A. printValue();
B. this.printValue();
C. person.printValue();
D. super.printValue();
正确答案:D

645. Given the following code:
1) public class Test{
2) int m,n;
3) public Test(){}
4) public Test(int a){m=a;}
5) public static void main(String arg[]){
6) Test t1,t2;}
7) int j,k;
8) j=0;k=0;
9) t1=new Testz();
10)t2=new Test(j,k);
11)}
12)}
Which line would cause one error during compilation?
A. line 3
B. line 5
C. line 6
D. line 10
正确答案:D

646. In the code if you compile as "java hello4.java" following files will be generated.
1 import java applet.Applet;
2 import java.awt*;
3 import java.awt.event.*;
4 public class hello4 extends Applet {
5 public void int(){
6 add(new myButton("BBB"));
7 }
8 public void paint(Graphics screen){
9 }
10 class myButton extends Button{
11 myButton(String label){
12 super(label);
13 }
14 public String paramString(){
15 return srper.parmString();
16 }
17 }
18 public static void main(String[] args){
19 Frame myFrame=new Frame(
20 "hello4");
21 myFrame.setSize(300,100);
22 Applet myApplet=new hello4();
23 Butten b=new Button("My Button");
24 myApplet.add(b);
25 b setLabel(b getLabel()+"New");
26 myButton b1=new myaButton("PARAMBUTTON");
27 System.out.println(b1.paramString());
28 myFrame.set(myApplet);
29 myFrame.addVisible(true);
30 myFrame.addWindowListener(new WindowAdapter()){
31 public void windowClosing(WindowEvent e){
32 System.exit(0);}});
33}
34 }
End hello4 class.
Select the one reght answer.
A. hello4.class,myButton.class,hello4.lass
B. hello4.class,hello4$myButton.class,hello4$1.class
C. hello4.class.hello4$myButton.class
正确答案:B

647. The following code will give:
1: class Test
2: {
3: void show()
4: {
5: System.out.println("non-static method in Test");
6: }
7: }
8: public class Q3 extends Test
9: {
10:static void show()
11:{
12:System.out.println("Overridden non-static method in Q3");
13:}
14:
15:public static void main(String[]args)
16:{
17:Q3 a=new Q3();
18:}
19:}
Select the one right answer
A. Compilation error at line 3.
B. Compilation error at line 10.
C. No compilation error,but runtime exception at line 3.
D. No compilation error,but runtime exception at line 10.
正确答案:B

648. What will happen if you compile/run the following code?
1: public class Q11
2: {
3: static String str1="main method with String[] args";
4: static String str2="main method with int[] args";
5:
6: public static void main(String[] args)
7: {
8: System.out.println(str1);
9: }
10:
11:public static void main(int[] args)
12:{
13:System.out.println(str2);
14:}
15:}
Select the one right answer.
A. Duplicate method main(),compilation error at line 6;
B. Duplicate method main(),compilation error at line 11;
C. Prints"main method with main String[]args".
D. Prints"main method with main int[]args".
正确答案:C

649. What is the output of the following code?
1: class Test
2: {
3: Test(int i)
4: {
5: System.out.println("Test("+i+")");
6: }
7: }
8:
9: public class Q12
10:{
11:static Test t1=new Test(1);
12:
13:Test t2=new Test(2);
14:
15:static Test t3=new Test(3);
16:
17:public static void main(String[] args)
18:{
19:Q12Q=new Q12();
20:}
21:}
Select the one right answer.
A. Test(1)
Test(2)
Test(3)
B. Test(3)
Test(2)
Test(1)
C. Test(2)
Test(1)
Test(3)
D. Test(1)
Test(3)
Test(2)
正确答案:D

650. What will happen if you compile/run the following code?
1: public class Test
2: {
3: int maxExements;
4:
5: void Test()
6: {
7: maxElements=100;
8: System.out.println(maxElements);
9: }
10:
11:Test(int i)
12:{
13:maxElements=i;
14:System.out.println(maxElements);
15:}
16:
17:public static void main(String[] args)
18:{
19:Test a=new Test();
20:Test b=new Test(999);
21:}
22:}
Select the one right answer.
A. prints 100 and 999.
B. prints 999 and 100.
C. Compilation error at lie 3, variable maxElements was not initialized.
D. Compillation error at line 19.
正确答案:D

651. Which two demonstrate a "has a"relationship?(Choose two)?
A. public interface Person { }
public class Employee extends person{ }
B. public interface Shape { }
public interface Rectandle extends Shape{ }
C. public interface Colorable{ }
public class Shape implements Colorable{ }
D. public class Species{ }
public class Animal{private Species species;}
E. interface Component{}
class Container implements Component{
private Component[]children;
}
正确答案:D、E

652. What is displayed when the following code is executed?
class parent{
private void method1(){
System.out.println("Parent's method1()");
}
public void method2(){
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent{
Public void method(){
System.out.println("Child's method1()");
}
public static void main(String args[]){
Parent p=new Child();
p.method2();
}
}
Select the one right avswer.
A. Compile-time error
B. Run-time error
C. Prints:Patent's method2() Parent's method1()
D. Prints:Patent's method2() Child's method1()
正确答案:C

653. What is displayed when the following code is executed?
class Paten{
void method1(){
System.out.println("Parent's method1()");
}
public void method2(){
System.out.println("Parent's method2()");
method1();
}
}
class Child extends Parent{
public void method1(){
System.out.println("Child's method1()");
}
public static void main(String args[]){
Parent p=new Child();
p.method2();
}
}
Select the one right answer.
A. Compile-time error
B. Run-time error
C. Prints:Parent's method2() Parent's method1()
D. Prints:Parent's method2() Child's method1()
正确答案:D

654. What will be the result of compiling the following code?
public class Test{
static int age;
public static void main(String args[]){
age=age+1;
System.out.println("The age is"+age);
}
}
Select the most appropriate answer.
A. Compiles and runs with no output
B. Compiles and runs printing out The age is 1
C. Compiles but generates a runtime error
D. Does not compile
E. Compiles but generates a compile time error
正确答案:B

655. What is the correct declaration of an abstract method that is intended to be public?
Select the most appropriate answer.
A. public abstract void add();
B. public abstract void add() {}
C. public abstract add() {}
D. public virtual add();
正确答案:A

656. Under what situations do you obtain a default constructor?
Select the most appropriate answer.
A. When you define any class;
B. When the class has no other constructors;
C. When you define at least one constructor;
正确答案:B

657. Given the following code:
public the most appropriate answer.
A. public void Test() {…}
B. public Test(){…}
C. public static Test(){…}
D. public static void Test(){…}
正确答案:B

658. Which of the following is a legal return type of a method overloading the following method?
public void add(int a){…}
Select the most appropriate answer.
A. void
B. int
C. Can be anything
正确答案: C

659. Which of the following statemints is correct for a method which is overriding the following method?
public void add(int a){…}
Select the most appropriate answer.
A. the overriding method must return void
B. the overriding method must return int
C. the overriding method can return whatever it likes
正确答案: A

660. Given the following classes defined in separate files?
class Vehicte {
public void dreve(){
System.out.priantln("Vebile.drive");
}
}
class Car extends Vahicle{
public void drive(){
System.out.println("Car.drive")
}
}
public class Test{
public static void main(String args[]){
Vehicle v;
Car c;
v=new Vehicle();
c=new Car();
v.drive();
c.drive();
v=c;
v.drive();
v.drivez();
}
}
What will be the effect of compiling and running this class Test?
Select the most appropriate answer.
A. Generates a Compiler error on the statement v=c;
B. Generates runtime error on the statement v=c;
C. Prints out:
Vehicle:drive
Car:drive
D. Prints out:
Vehicte:drive
Car:drive
Vehicle:drive
正确答案: C

SCJP考题6

分类: SCJP考题 2007-10-13 15:07 743人阅读 评论(0) 收藏 举报
930. What String instance method would return true when invoked like this?
a.method(b);
Where a="GROUNDhog" and b="groundHOG"?
Select the one right answer.
A. equals()
B. toLowerCase()
C. toUpperCase()
D. equalsIgnoreCase()
E. none of the above
正确答案: D

931. At the end of these two lines of code:
String s="hypertext";
String t=s.substring(2,5);
What does the object reference t contain? Select the one right answer.
A. "yper"
B. "ype"
C. "pert"
D. "per"
E. "perte"
正确答案: D

932. After these two lines of code:
String s="Dolly";
String t=s.concat("Hello");
What characters will the object reference t contain? Select the one right answer.
A. "Hello, Dolly"
B. "Dolly Hello"
C. "Hello"
D. "Dolly"
E. none of the above
正确答案: B

933. What is the result of invoking main() for the classes D and E?
class D {
public static void main(String[] args){
String s1=new String("hello");
String s2=new String("hello");
if(s1.equals(s2))
System.out.println("equal");
else
System.out.println("not equal")
}
}
class E {
public static void main(String[] args){
StringBuffer s1=new StringBuffer("hello");
StringBuffer s2=new StringBuffer("hello");
if(sb1.equals(sb2))
System.out.println("equal");
else
System.out.println("not equal");
}
}
Select the one right answer.
A. D: equal; E: equal
B. D: not equal; E:not equal
C. D: equal; E: not equal
D. D: not equal; E: not equal
E. nothing appears in the standard output for either class
正确答案: C

934. String s="Example String"; Which operation is legal?
A. s»>=3;
B. int i=s.length();
C. s[3]="x";
D. String short_s=s.trim();
E. String t="root"+s;
正确答案: B D E

935. Which contains objects without ordering, duplication, or any particular lookup/retrieval mechanism?
A. Map
B. Set
C. List
D. Collection
E. Enumeration
正确答案: B

936. How can you use the String method indexOf() to determine which position the letter 'C' is in given this String?
String s="ABCDE";
Write a complete statement in your answer but you do not have to assign the letter your retrieve to another variable.
Fill in the blank.
正确答案: s.indexOf('C');

937. What will the result be for the following block of code when it is executed?
int i=3;
int j=0;
float k=3.2F;
long m=-3;
if(Math.ceil(i)<Math.floor(k))
if(Math.abs(i)==m)
System.out.println(i);
else
System.out.println(j);
else
System.out.println(Math.abs(m)+1);
Select the one right answer.
A. 3
B. 0
C. -3
D. 4
E. none of these
正确答案: D

938. Which of the following will output -4.0 ?
A. System.out.println(Math.floor(-4.7));
B. System.out.println(Math.round(-4.7));
C. System.out.println(Math.ceil(-4.7));
D. System.out.println(Math.min(-4.7));
正确答案: C

939. What will happen if you attempt to compile and run the following code?
Integer ten=new Integer(10);
Long nine=new Long(9);
System.out.println(ten+nine);
int i=1;
System.out.println(i+ten);
Select the one right answer.
A. 19 followed by 20
B. 19 followed by 11
C. Error:Can't onvert java lang Integer
D. 10 followed by 1
正确答案: C

940. If you run the code below, what gets printed out?
String s=new String("Bicycle");
int iBegin=1;
char iEnd=3;
System.out.println(s.substring(Begin, iEnd));
Select the one right answer.
A. Bic
B. ic
C. icy
D. error: no method matching substring(int,char)
正确答案: B

941. If you wanted to find out where the position of the letter v (ie return 2) in the string s containing "Java", which of the following could you rse?
A. mid(2,s);
B. charAt(2);
C. s.indexOf('V');
D. indexOf(s,'V');
正确答案: C

942. Given the following code, what test would you need to put in place of the comment line?
To result in an output of
place test here
public class Test{
public static void main(String argv[]){
Test e=new Test();
}
Test(){
String s="Java";
String s2="Java";
place test here{
System.out.println("Equal");
}else{
System.out.println("Not equal");
}
}
}
Select the one right answer.
A. if(s==s2)
B. if(s.equals(s2))
C. if(s.equalsIgnoreCase(s2))
D. if(s.noCaseMtch(s2))
正确答案: C

943. You need to create a class that will store a unique object element. You do not need to sort these elements but they must be unique.
What interface might be most suitable to meet this need?
A. Set
B. List
C. Map
D. Vector
正确答案: A

944. Which of the following will successfully create an instance of the Vector class and add an element?
A. Vetor v=new Vector(99);
v[1]=99;
B. Vector v=new Vector();
v.addElement(99);
C. Vector v=new Vector(100);
v.addElement("99");
正确答案: D

945. Which most closely matches a description of a java Map?
A. A vector of arrays for a 2D geographic representation.
B. A class for containing unique array elements.
C. A class for containing unique vector elements.
D. An interface that ensures that implementing classes cannot contain duplicate keys.
正确答案: D

946. How does the set collection deal with duplicate elements?
A. An exception is thrown if you attempt to add an element with a duplicate value.
B. The add method returns false if you attempt to add an element with a duplicate value.
C. A set may contain elements that return duplicate values from a call to the equal's method.
D. Duplicate values will cause an error at compile time.
正确答案: B

947. What will happen when you attempt to compile and run the following code?
public class Test{
public static void main(String argv[]){
Test s=new Test();
}
private Test(){
String s="Marcus";
String s2=new String("Marcus");
if(s==s2){
System.out.println("we have a match");
}else{
System.out.println("Not equal");
}
}
}
Select the one right answer.
A. Compile time error caused by private constructor
B. Output of "we have a match"
C. Output of "Not equal"
D. Compile time error by attempting to compare strings using==
正确答案: C

948. What will be output by the following line?
System.out.println(Math.floor(-2.1));
A. -2
B. 2.0
C. -3
D. -3.0
正确答案: D

949. Which of the following statements are true?
A. At the root of the collection hierarchy is a class called Collection.
B. The collection interface contains a method called enumerator.
C. The interator method returns an instance of the Vector class.
D. The set interface is designed for unique elements.
正确答案: D

950. Which of the following methods are members of the Vector class and allow you to input a new element?
A. addElement
B. insert
C. append
D. addItem
正确答案: A

951. Which of the following are valid statements?
A. public class MyCalc extends Math
B. Math.max(s);
C. Math.round(9.99,1);
D. Math.mod(4,10);
E. None of these
正确答案: E

952. Given the following declaration?
Integer i=new Integer(99);
How can you now set the value of i to 10 ?
A. i=10;
B. i.setValue(10);
C. i.parseInt(10);
D. none of the above
正确答案: D

953. Given the following class?
public class Test{
public static void main(String argv[]){
int i=0;
//Here
}
}
Which of the following lines if placed after the comment? //Here will print out 0.
A. System.out.println(i++);
B. System.out.println(i+'0');
C. System.out.println(i);
D. System.out.println(i—);
正确答案: A C D

954. Which of the following are examples of immutable classes, select all correct answer(s) ?
A. String
B. StringBuffer
C. Double
D. Integer
正确答案: A C D

955. Which of the following statements would return false? if given the following statements.
String s=new String("New year");
String s1=new String("new Year");
Select the one right answer.
A. s==s1
B. s.equals(s1);
C. s=s1;
D. None of the above
正确答案: A B

956. What is the result when you compile and run the following code?
public class Test{
public static void main(String args[]){
int x=0;
for (int i=; i<10; i++){
x=new Math(i);
System.out.println(x);
}
}
}
Select the one right answer.
A. Prints 0 to 9 in sequence
B. No output
C. Runtime error
D. Compile time error
正确答案: D

957. Which of the following casses will throw "NumberFormatException"?
A. Double
B. Boolean
C. Integer
D. Byte
正确答案: A C D

958. What is the result when you compile and run the following code?
import java.util.*;
public class MyVector{
public Vector myVector(){
Vector v=new Vector();
return v.addElement("Adding element to vector");
}
public static void main(String[] args){
Test mv=new Test();
System.out.println(mv.myVector());
}
}
Select the one right answer.
A. Prints Adding element to vector
B. Compile time error
C. Runtime error
D. Compiles and runs successfully
正确答案: B

959. What are possible ways to implement linkedList class?
A. As a HashMap
B. As a Queue
C. As a TreeSet
D. As a Stack
正确答案: B D

960. What is the result when you try to compile and run the following ode?
public class Test {
public static void main(String args[]){
String s="HelloWorld";
if ((s !=null)&& (s.length()>6))
System.out.println("The value of s is "+s);
}
}
Select the one right answer.
A. Compile time error
B. Runtime error
C. No output is printed
D. The value of s is HelloWorld will be printed on the screen
正确答案: D

961. Which of the following statements are true about the fragment below?
import java.lang.Math;
public class Test{
pubilc static void main(String args[]){
Math m = new Math();
System.out.println(m.abs(2.6);
}
}
Select the one right answer.
A. Compiler fails at line 1
B. Compiler fails at line 3
C. Compiler fails at the time of Math class instantiation
D. Compiler succeeds.
正确答案: C

962. What will be the output of the following line?
public class Test{
public static void main(String args[]){
System.out.println(Math.floor(145.1));
System.out.println(Math.floor(-145.4));
}
}
Select the one right answer.
A. 145.0 followed by -145.0
B. 150.0 followed by -150.0
C. 145.1 followed by -145.4
正确答案: A

963. Which of the following implement clear notion of one item follows another(order)?
A. List
B. Set
C. Map
D. Vector
正确答案: A

964. Collection interface iterator method returns Iterator(lide Enumerator), through you can traverse a collection from start to finish and safely remove elements.
A. true
B. false
正确答案: A

965. Which of the following places no constraints on the type of elements, order of elements. or repetition of elements with in the collection?
A. List
B. Collection
C. Map
D. set
正确答案: B

966. Which of the following gives Stack and Queue functionality?
A. Map
B. Vector
C. List
D. Set
正确答案: C

967. Which are not Java primitive types?
A. short
B. Boolean
C. unit
D. float
正确答案: B C

968. What is the output of following if the return value is "the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument"(Assuming written inside main)
String s5="AMIT";
String s6="amit";
System.out.println(s5.ompareTo(s6));
System.out.println(s6.ompareTo(s5));
System.out.println(s6.ompareTo(s6));
Select the one right answer.
A. -32
32
0
B. 32
32
0
C. 32
-32
0
D. 0
0
0
正确答案: A

969. What is the output(Assuming written inside main)?
String s1=new String("amit");
String s2=new String("'m','i');
s1.concat("Poddar");
System.out.println(s1);
System.out.println((s1+s2).charAt(5));
Select the one right answer.
A. Compile error
B. amitPoddar
o
C. amitPoddar
i
D. amit
i
正确答案: D

970. What is the output(Assuming written inside main)?
String s1=new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit";
String s4="arit";
String s2=s1.replace('m','r');
System.out.println(s2==s3);
System.out.println(s3==s4);
Select the one right answer.
A. aret
amit
false
true
B. arit
arit
false
true
C. amil
amit
false
true
D. arit
amit
true
true
正确答案: A

971. Which one does not extend java.lang.Number?
A. Integer
B. Boolean
C. Character
D. Long
E. Short
正确答案: B C

972. Which one does not have a valueOf(String)method?
A. Integer
B. Boolean
C. Character
D. Long
E. Short
正确答案: C

973. What will be the output of following?
{
double d1=-o.5d;
System.out.println("Cell for d1 "+Math.ceil(d1));
System.out.println("Floor for d1 "+Math.floor(d1));
}
Select the one right answer.
A. Ceil for d1 0
Floor for d1 -10;
B. Ceil for d1 -1.0
Floor for d1 0.0;
C. Ceil for d1 0.0
Floor for d1 -1.0;
D. Ceil for d1 -0.0
Floor for d1 -1.0;
正确答案: D

974. What is the output of following?
{
float f4==-5.5f;
float f5==5.5f;
float f6==-5.49f;
float f7==5.49f;
System.out.println("Round f4 is"+Math round (f4));
System.out.println("Round f5 is"+Math round (f5));
System.out.println("Round f6 is"+Math round (f6));
System.out.println("Round f7 is"+Math round (f7));
}
Select the one right answer.
A. Round f4 is -6
Round f5 is 6
Round f6 is -5
Round f7 is 5
B. Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5
正确答案: B

975. Given Integer.MIN_VALUE=-2147483648; Integer.MAX_VALUE=2147483647?
What is the output of following?
{
float f4=Integer.MIN_VALUE;
float f5=Integer.MIN_VALUE;
float f7=-2147483655f;
System.out.println("Round f4 is "+Math.round(f4));
System.out.println("Round f5 is "+Math.round(f5));
System.out.println("Round f7 is "+Math.round(f7));
}
Select the one right answer.
A. Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -2147483648
B. Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -214748365
正确答案: A

976. What is the output of following?
1 Boolean b1 =new Boolean("TRUE");
2 Boolean b2 =new Boolean("true");
3 Boolean b3 =new Boolean("JUNK");
4 System.out.println(""+b1+b2+b3);
Select the one right answer.
A. Compiler error
B. RunTime error
C. truetruefalse
D. truetruetrue
正确答案: C

977. What is the output of following?
1 Boolean b1 = new Boolean("TRUE");
2 Boolean b2 = new Boolean("true");
3 Boolean b3 = new Boolean("JUNK");
4 System.out.println(b1+b2+b3);
Select the one right answer.
A. Compile time error
B. Run time error
C. truetruefalse
D. truetruetrue
正确答案: A

978. What is the output?
{
Float f1=new Float("4.4e99f");
Float f2=new Float("-4.4e99f");
Double d1=new Double("4.4e99");
System.out.println(f1);
System.out.println(f2);
System.out.println(d1);
}
Select the one right answer.
A. Runtime error
B. Infinity
-Infinity
4.4e99
C. Infinity
-Infinity
Infinity
D. 4.4E99
-4.4E99
4.4E99
正确答案: B

979. Which of the following wrapper classes cannot take a "String" in constructor?
A. Boolean
B. Integer
C. Long
D. Character
E. Byte
F. Short
正确答案: D

980. What is the output of the following code?
1: String str = "Welcome";
2:
3: str.concat("to java!");
4:
5: System.out.println(str);
Select the one right answer.
A. Strings are immutable, compilation error at line 3.
B. Strings are immutable, runtime exception at line 3.
C. Prints "Welcome".
D. Prints "Welcome to Java!".
正确答案: C

SCJP考题7

分类: SCJP考题 2007-10-13 15:09 591人阅读 评论(0) 收藏 举报
685. For an object to be a target for a Thread, that object must be of type:
Fill in the blank.
正确答案: Runnable

686. If you supply a target object when you create a new Thread, as in:
Thread t=new Thread(targetObject);
What test of instanceof does targetObject have to pass for this to be legal?
Select the one right answer.
A. targetObject instanceof Thread
B. targetObject instanceof Object
C. targetObject instanceof Applet
D. targetObject instanceof Runnable
E. targetObject instanceof String
正确答案: D

687. What appears in the standard output when you run the Dots class?
class Test impltements Runnable{
DotThread t;
public static vokd main(String[]args){
Test d=new Test();
d.t=new DotTgread();
}
public void init(){
t.start();
t=new DashThread().start();
}
}
class DotThread extends Thread{
public void run(){
for(int index=0;index<100;index++)
System.out.print(".");
class DashThread extends Thread{
public void run(){
for(int index=0;index<100;index++)
System.out.print("-");
}
}
Select the one right answer.
A. nothing
B. 100 dots(.)
C. 200 dots(.)
D. 100 dashes(-)
E. 100 dots(.)and 100 dashes(-)
正确答案: A

688. If a thread calls the wait() method, which methods can make it continue to run?
A. join()
B. resume()
C. notify()
D. notifyAll()
E. high priority thread is ready
正确答案: C D

689. Which method is used to define the execution body of a thread?
A. start()
B. init()
C. run()
D. main()
E. synchronized()
正确答案: C

690. Which of the following method can be used to define a new thread class?
A. implemint the Runnable interface
B. add a run() method in the class
C. create an instance of Thread
D. extend the Thread class
正确答案: A D

691. Which method you define as the starting point of new thread in a class from which new the thread can be excution?
A. public void start()
B. public void run()
C. public void int()
D. public static void main(String args[])
E. public void runnable()
正确答案: B

692. Which modifier should be applied to a method for the lock of object this to be obtained prior to execution any of the method body?
A. synchronized
B. abstract
C. final
D. static
E. public
正确答案: A

693. What might cause the current thread to stop executing?
A. An interrupted exception is thrown.
B. The thread execute a sleep() call.
C. The thread constructs a new thread
D. A thread of higher priority becomes ready.
E. The thread executes a read() call on InputStream
正确答案: A B D E

694. Which statements are true about threads?
A. A thread can be created only by subclassing java.lang.Thread.
B. Invoking the suspend() method stops a thread so that it cannot be restarted.
C. The java interpreter's natural exit occurs when no non-daemon threads remain alive.
D. Uncoordinated changes to shared data by multiple threads may result in the data being read, or left, in an inconsistemt state.
正确答案: C D

695. What statements are true concerning the method notify() that is used in conjunction with wait()?
Select all valid answers.
A. if there is more than one thread waiting on a condition, only the thread that has been waiting the longest is notified
B. if there is more than one thread waiting on a condition, there is no way to predict which thread will be notified
C. notify() is defined in the Thread class
D. it is not strictly necessary to own the lock for the object you invoke notify() for
E. notify() should only be invoked from within a while loop
正确答案: B

696. Given the following class:
class Test {
public int startHere=1;
public int endHere=100;
public startic void main(String[] args){
new Test().go();
}
void go(){
//A
Thread 1=new Thread(a);
t.start();
}
}
What block of code can you replace at line A above so that this program will count from startHere to endHere?
Select all valid answers.
A. Runnable a = new Runnable(){
public void run(){
for (int i=starHere;i<=endHere;i++){
System.out.println(i);
}
}
};
B. implements Runnable{/
public void run(){
for(int i=starHere;i<=endHere;i++){
System.out.println(i);
}
}
};
C. Thread a=new Thread(){
public void run(){
for(int i=starHere;i<=endHere;i++)|
System.out.prinln(i);
}
}
};
正确答案: A C

697. Which will contain the body of the thread?
A. run()
B. start()
C. stop()
D. main()
E. init()
正确答案: A

698. Given the following class:
class Test {
public statie void main(String[]args){
Thread t=new Thread(new CounteBehavior());
t.star();
}
}
Which of the following is a valid definition of CounterBehavior that would make Counter's main() method count from 1 to 100,countingonce per second?
Select the one right answer.
A. //This class is an inner class to Counter;
class CounterBehavior{
for(int i=1;i<=100;i++);
try{
System.out.println(i);
Thread.sleep(1000);
} catch(InterruptedException x){}
}
}
B. //This class is an inner class to Counter;
class CounterBehavior implements Runnable{
public void run(){
for(int i=1; i<=100;i++);
try{
System.out.println(i);
Thread.sleep(1000);
} catch(InterruptedException x){}
}
}
C. //This class is an inner class to Counter;
static class CounterBehavior implements Runnable{
public void run(){
for(int i=1;i<=100;i++){;
try{
System.out.println(i);
Thread.sleep(1000);
}
}catch(InterruptedException x){}
}
}
正确答案: B

699. What must be true for the RunHandler class so that instances of RunHandler can be used as written in the code below?
class Test {
public static void main(String[]args){
Thread t=new Thread(new RunHandlr());
t.start();
}
}
Select all valid answers.
A. RunHandler must impement the java.lang.Runnable interface.
B. RunHandler must extend the Thread class.
C. RunHandler must provide a run() method declared as public and returning void.
D. RunHandler must provide an init()method.
正确答案: A C

700. What can you write at the commentA in the code below so that this program writes the word "running" to the standard output?
class Test implements Runnable{
public static void main(String[] args){
Test rt=new Test();
Thread t=new Thread(rt);
A
}
public void run(){
System.out.println("running");
}
void go(){
start(1);
}
void start(int i){
}
}
Select all volid answers.
A. System.out.println("running");
B. rt.start();
C. rt.go();
D. rt.start(1);
正确答案: A

701. What will happen when you attempt to compile and run the following code?
class Test implements Runnable{
int i=0;
publi int run(){
while(true){
i++;
System.out.println("i="+i);
}//End while
return 1;
}
}
Select all valid answers.
A. It will compile and the run method will print out the increasing value of i.
B. It will compile and calling start will print out the increasing value of i.
C. The code will cause an error at compile time.
D. Compilation will cause an error because while cannot take a parameter of true.
正确答案: C

702. What will happen when you attempt to compile and run the following cods?
public class Test extends Thread{
public static void main(String argv[]){
Test b=new Test();
b.run();
}
public void start(){
for(int i=0;u<10; i++){
System.out.println("Value of i="+i);
}
}
}
Select the one right answer.
A. A compile time error indicating that no run method is defined for the Thread class.
B. A run time error indicating that no run method is defined for the Thread class.
C. Clean compile and at run time the values 0 To 9 are printed out.
D. Clean compile but no output at runtime.
正确答案: D

703. What can cause a thread to stop executing?
A. The program exits via a call to System.exit(0);
B. Amother thread is given a higher priority
C. A call to the thread's stop method.
D. A call to the halt method of the Thread class
正确答案: A B C

704. Under what circumstances might you use the yield method of the Thread class?
A. To call from the currently running thread to allow another thread of the same or higher priority to run.
B. To call on a waiting thread to allow it to run
C. To allow a thread of higher priority to run
D. To call from the currently running thread with a parameter designating which thread should be allowed to run
正确答案: A

705. Which of the following statements about threading are true?
A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements Runnable.
B. You can obtain a mutually exclusive lock on any object.
C. A thread can obtain a mutually exclusive lock on a synchronized method of an object.
D. Thread scheduling algorithms are platform dependent.
正确答案: B C D

706. Which of the following are methods of the Thread class?
A. yield()
B. sleep(long msec)
C. go()
D. stop()
正确答案: A B D

707. Which of the following best describes the use of the synchronized keyword?
A. Allows two process to run in parallel but to communicate with each other
B. Ensures only one thread at a time may access a method or object
C. Ensures that two or more processes will start and end at the same time
D. Ensures that two or more Threads will start and end at the same time
正确答案: B

708. Which of the following are methods of the Runnable interface?
A. run
B. start
C. yield
D. stop
正确答案: A

709. What will happen when you attempt to compile and run the following code?
public calss Test extends Thread{
public static void main(String argv[]){
Test b=new Test();
b.start();
}
public void run(){
System.out.println("Running");
}
}
Select the one right answer.
A. Compilation and run but no output
B. Compilation and run with the output "Running"
C. Compile time error with complaint of no Thread targer
D. Compile time error with complaint of no access to Thread package
正确答案: B

710. Given a reference called t to a class which extends Thread. which of the following will cause it to give up cycles to allow another thread to execute
A. t.yield();
B. yield();
C. yield(100);//Or sone other suitable amount in milliseconds
D. yield(t);
正确答案: A B

711. Select all the exceptions thrown by wait() method of an Object class, which you can replace in the place of xxx legally?
class Test implements Runnable{
public void run(){
System.out.println("Execrting run() method");
myTest();
}
public synchronized void myTest(){
try{
wait(-1000);
System.out.println("Execrting the myTest() method");
}XXX
}
}
public class MyTest{
public static void main(String args[]){
Test t=new Test();
Thread th=new Thread(t);
th.start();
}
}
Select the one right answer.
A. catch (InterruptedException ie){}
B. catch (IllegalArgumentException i1){}
C. catch (IllegalMonitorStateException i1){}
D. Only catch (InterruotedException e){} exception
正确答案: A B C

712. There are 20 threads are waiting in the waiting pool with same priority. how can you invokes 15th thread from the wailing pool?
A. By calling resume() method
B. By calling interrupt() method
C. Calling call() method
D. By calling notify(15)method on the thread instance
E. None of the above
正确答案: E

713. Please select all correct statements from the following.
A. toString() method is defined in Object class.
B. toString() method is defined in Class class.
C. wait(), notify(), notifyAll() methods are defined in Object class and used for Thread communication.
D. toString() method provides string representation of an Object state.
正确答案: A C D

714. What does the following code do?
public class Test implements Runnable{
public void run (String s){
System.out.println("Executing Runnable Interface Thread");
}
public static void main(String args[]){
Test rt=new Test();
Thread t=new Thread(rt);
t.start();
}
}
Select the one right answer.
A. The compiler error
B. The runtime error
C. Comliles and prints "Execrting Runnable Interface Thread" on the screen
D. Comliles and does not print any thing on the screen
正确答案: A

715. Which statements are true?
A. Threads start() method makes it eligible to run
B. Thread dies after the run() returns
C. A dead Thread can be started again.
D. A stop() method kills the currently running Thread
正确答案: A B D

716. Which keyword should be used to enable interaction with the lock of an object?
The flag allows exclusive access to that object.
A. transient
B. synchronized
C. serialize
D. static
正确答案: B

717. Which statements about thread are true?
A. Once a thread is created, it can start running immediately.
B. To use the start() method makes a thread runnable, but it does not necessarily start immediately.
C. When a thread stops running because of pre-emptive, it is placed at the front end of the runnable queue.
D. A thread may cease tp be ready for a variety of reasons.
正确答案: B D

718. The method resrme() is responsible for resuming which thread's execution?
A. The thread which is stopped by calling method stop()
B. The thread which is stopped by calling method steep()
C. The thread which is stopped by calling method wait()
D. The thread which is stopped by calling method suspend()
正确答案: D

719. What will happen if you compile/run this code?
1: public class Test extends Thread
2: {
3: public void run()
4: {
5: System.out.println("Before start method");
6: this.stop();
7: System.out.println("After stop method");
8: }
9:
10: public static void main(String[] args)
11: {
12: Test a=new Test();
13: a.start();
14:}
15:}
Select the one right answer.
A. Compilation error at line 7.
B. Runtime exception at line 7.
C. Prints"Before start method" and "After stop method".
D. Prints"Before start method" only.
正确答案: D

720. Under which conditions will a currently executing thread stop?
A. When an interrupted exception occurs
B. When a thread of higher priority is ready(becones runnable)
C. When the thread creates a new thread
D. When the stop() method is called.
正确答案: A B D

721. What happens when you try to compile and run the following application?
public class Test{
public static void main(String[] args){
new Test();
}
Test(){
Test alias1=this;
Test alias2=this;
synchronized(alias1){
try{
slias2.wait();
System.out.println("DONE WAITING");
}
Catch(InterruptedException e){
System.out.println("INTERR UPTED");
}
catch(Exception e){
System.out.println("OTHER EXCEPTION");
}
finally{
System.out.println("FINALLY");
}
}
System.out.println("ALL DONE");
}
}
Choose all correct options.
A. The application compiles but doesn't print anything.
B. The application compiles and print"DONE WAITING"
C. The application compiles and print "FINALLY"
D. The application compiles and print "ALL DONE"
E. The application compiles and print "INTERRUPTED"
正确答案: A

722. What is the name of the interface that can be used to define a class that can execute within its own thread?
Selecs the most appropriate answer.
A. Runnable
B. Run
C. Threadable
D. Thread
E. Executable
正确答案: A

723. Which is the name of the method used to schedule a thread for execution?
Select the most appropriate answer.
A. inpt();
B. start();
C. run();
D. resume();
E. sleep();
正确答案: B

724. Which method may cause a thread to stop executing?
Select all correct answers.
A. sleep();
B. stop();
C. yield();
D. wait();
E. notify();
F. notifyAll();
G. synchronized()
正确答案: A B C D

725. What is the effect of issuing a wait() method on an object?
Select the most appropriate answer
A. If a notify() method has already been sent to that object then it has no effect.
B. The object issuing the call to wait() will halt until another object sends a notify() or nutifyAll() method.
C. An exception will be raised.
D. The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.
正确答案: B

726. What is the result of compiling and executing the following Java class:
public class Test extends Thread{
public void run(){
System.out.println("In run");
suspend();
resume();
System.out.println("Leaving run");
}
public static void main(Syring args[]){
(new Test()).start();
}
}
Select the most appropriate answer.
A. Compilation will fail in the method main.
B. Compilation will fail in the method run.
C. A warning will be generated for method run.
D. The string" In run" will be printed to standard out.
E. Both strings will be printed tp standard out.
F. Nothing will happen.
正确答案: D

727. What will happen if you compile/run this code?
1: public class Test implements Runnable
2: {
3: public void run(String s)
4: {
5: System.out.println("Before start Thread:"+s);
6:
7: System.out.println("After stop of Thread:"+a);
8: }
9:
10:public static void main(String[] args)
11:{
12:Test a=new Test();
13:Thread t=new Thread(a);
14:t.start();}
15:}
Select the one right answer.
A. Compilation error
B. Runtime exception
C. Compilation error at line 14
D. Prints "Before start of Thread" After Start of Thread
正确答案: A

728. What is the name of the method used to start a thread execution?
A. init();
B. start();
C. run();
D. resume();
正确答案: B

729. Which will contain the body of the thread?
A. run()
B. start()
C. stop()
D. main()
E. init()
正确答案: A

730. Which method is used to construct and execute the thread?
A. execte()
B. run()
C. build()
D. start()
正确答案: D

731. Which of the following will not directly cause a thread to stop?
A. notify()
B. wait()
C. notifyAll()
D. sleep()
正确答案: A C

732. Which of the following statements are true?
A. all the threads created in a class will come to an end at the same tine.
B. you can stop a thread indefinitely if you wish to.
C. you can start a thread only by extending the Thread class.
D. multiple threads accessing same variable will lead to producing junk(not the exact word )data.
E. JVM exits after the main() thread is exited even if there might be some threads running.
正确答案: B D

SCJP考题8

分类: SCJP考题 2007-10-13 15:19 432人阅读 评论(1) 收藏 举报
880. If you run the following code on a on a PC from line directory c:/ source:
import java.io.*;
class Test {
public static void main(String[] args) throws Exception{
File file = new File("Ran.test");
System.out.println(file.getAbsolutePath());
}
}
What do you expect the output to be?
Select the one right answer.
A. Ran.test
B. source/Ran.test
C. c:/source/Ran.test
D. c:/source
E. null
正确答案: C

881. What happens if the file "Ran.test" does not yet exist and you attempt to compile and run the following code?
import java.io.*;
class Test {
public static void main(String[] args) throws IOException{
RandomAcessFile out=
new RandomAccessFile("Ran.test", "rw");
out.writeBytes("Ninotchka");
}
}
Select the one right answer.
A. The code does not compile because RandomAccessFile is not created correctly.
B. The code does not compile because RE. andomAccessFile does not implement the writeBytes() method.
C. The code compiles and runs but throws an IOExeption because "Ran.test" does not yet exist.
D. The code compiles and runs but nothing appears in the file"Ran.test" that it creates.
E. The code compiles and runs and "Ninotchka" appears in the file "Ran.test" that it creates.
正确答案: E

882. What modes are legal for creating a new RandomAccessFile object?
A. "w"
B. "r"
C. "x"
D. "rw"
E. "xrw"
正确答案: B D

883. To invoke read() from an InputStream subclass, you must handle what type of exception?
Fill in the blank
正确答案: "IOExeption" or "java.io.IOException"

884. What does the following code do?
File f=new File("hello.test");
FileOutputStream out=new FileOutputStream(f);
Select the one right answer.
A. Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.
B. Create a new file named "hello.test" if it does not yet exist. The file is not opened.
C. Open a file named "hello.test" so that you can write to it and read from it, but does not create the file if it does not yet exist.
D. Open a file named "hello.test" so that you can write to it but cannot read from it.
E. Create an object that you an now use tp create and open the file named "hello.test," and write to and read from the file.
正确答案: A

885. Given this code:
import java.io.*;
class Write{
public static void main(String[] args)throws Exception{
File file=new File("temp.test");
FileOutputStream stream=
new FileOutputStream(file);
//write integers here…
}
}
How can you replace the comment at the end of main() with code that will write the integers 0 through 9?
A. DataOutputStream filter=new DataOutputStream(stream);
for(int i=0; i<10; i++)
filter.writeInt(i);
B. for(int i=0; i<10; i++)
file.writeInt(i);
C. for(int i=0; i<10; i++)
stream.writeInt(i);
D. DataOutputStream filter=new DataOutputStream(stream);
for(int i=0; i<10; i++)
filter.write(i);
E. for(int i=0; i<10; i++)
stream.write(i);
正确答案: A

886. Which of the following classes can handle the Unicode?
A. InputStreamReader
B. BufferedReader
C. Writer
D. PipedInputStream
正确答案: A B C

887. Which classes are used to implement TCP/IP client and servers?
A. ServerSocket
B. Server
C. Socket
D. DatagramPacket
E. DatagramSocket
正确答案: A C

888. Which is possible parameter that can be passed into the constructor of a FilterOutputStream?
A. File
B. PrintStream
C. FileOutputStream
D. FileInputStream
E. RandomAccessFile
正确答案: C

889. A socket object has been created and connected to a standard internet service on a remote network server. Which construction give the most suitable means for reading ASCII data online at a time from the socket?
A. InputStream in =s.getInputStream();
B. DataInputStream in =new DataInputstream(s.getInputStream());
C. ByteArrayInputStream in=new ByteArrayInputStream(s.getInputStream());
D. BufferedReader in =new BufferedReader(new InputStreamReader(s.getInputStream()),"8859-1");
正确答案: D

890. FilterOutoutStream is the parent class for BufferedOutputStream. DataOutputStream and PrintStream. Which classes is valid argument for the constructor of a FilterOutputStream?
A. InputStream
B. OutputStream
C. File
D. RandomAcessFile
E. StreamTokenizer
正确答案: B

891. You have an 8-bit file using the character set defined by ISO 8859-8. You are writing an application to display this file in a TextArea. The local encoding is already set to 8859-8. How can you write a chunk of code to read the first line from this file?
You have three variables accessible to you, myfile is the name of the file you want to read, stream is an InputStream object associated with this file, s is a String object.
Select all valid answers.
A. InputStreamReader reader=new InputStreamReader(stream,"8859-8");
BufferedReader buffer=new BufferedReader(stream);
s=buffer.readLine();
B. InputStreamReader reader=new InputStreamReader(stream);
BufferedReader buffer=new BufferedReader(stream);
s=buffer.readLine();
C. InputStreamReader reader=new BufferedReader(reader);
s=buffer.readLine();
D. InputStreamReader reader=new InputStreamReader(myfile);
BufferedReader buffer=new BufferedReader(reader);
s=buffer.readLine();
E.FileReader reader=new FileReader (myfile);
BufferedReader buffer=new BufferedReader(reader);
s=buffer.readLine();
正确答案: A B E

892. The ISO ode for the language you are interested in is 8859-5.Assume you have a stream in a variable named mystream that's associated with a file generated in this 8-bit character set. if the default conversion to Unicode in your environment is for the encoding 8859-1, how can you create a new instance of InputStreamReader that will perform the inversion from ISO 8859-5 to Unicode, automatically?(Start by writing the keyword new and do not add a semicolon at the end.)
Fill in the bland.
正确答案: new InputStreamRedaer(mystreeam,"8859-5")

893. What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory?
import java.io.*;
public class Test{
public static void main(String argv[]){
Test m=new Test();
System.out.println(m.amethod());
}
public int amethod(){
try{
FileInputStream dis=
new FileInput Stream("Hello.txt");
}catch(FileNotFoundExeption fne){
System.out.println("No such file found");
return-1;
}catch(IOException ioe){
} finally{
System.out.println("Doing finally");
}
return 0;
}
}
Select all valid answers.
A. No such file found
B. No such file found,-1
C. No such file found, Doing finally,-1
D. 0
正确答案: C

894. How would you go about opening an image file called MyPicture.jpg?
A. Graphics.getGraphics("MyPicture.jpg");
B. Image inage=Tooldit.getDefaultToolkit().getImage("MyPicture.jpg");
C. Graphics.openImage("MyPicture");
D. Image m=new Image("MyPicture");
正确答案: B

895. How can you change the current working directory using an instance of the File class called FileName?
A. FileName.chdir("DirName")
B. FileName.cd("DirName")
C. FileName.cwd("DirName")
D. The File class does not support directly changing the current directory.
正确答案: D

896. Which of the following can you perform using the File class?
A. Change the current directory
B. Return the name of the parent directory
C. Delete a file
D. Find if a file contains text or binary information
正确答案: B C

897. Which of the following will compile without error?
A. File f=new File("/","autoexec.bat");
B. DataInputStream d=new DataInputStream(System.in);
C. OutputStreamWriter o=new OutputStreamWriter(System.out);
D. RandomAccessFile r=new RandomAcessFile("OutFile");
正确答案: A B C

898. Which of the following are true statements?
A. The RandomAccessFile class allows you to move directly to any point a file.
B. The creation of a named instance of the File class creates a matching file in the underlying operating system only when the close method is called.
正确答案: A

899. Assuming any exception handing has been set up, which of the following will create an instance of the RandomAccessFile class?
A. RandomAccessFile raf=new RandomAccessFile("myfile.txt","rw");
B. RandomAccessFile raf=new RandomAccessFile(new DataInputStream());
C. RandomAccessFile raf=new RandomAccessFile("myfile.txt");
D. RandomAccessFile raf=new RandomAccessFile(new file("myfile.txt"));
正确答案: A

900. You need to read in the lines of a large text file containing tens of megabytes of data. Which of the following would be most suitable for reading in such a file?
A. new FileInputStream("file.name")
B. new InputStreamReader(new FileInputStream("file, name"))
C. new BufferedReader(new InputStreamReader(new FileInputStream("file name")));
D. new RandomAccessFile raf=new RandomAccessFile("myfile.txt","+rw");
正确答案: C

901. There are two computers are connected to internet, one computer is trying to open a socket connections to read the home page of another computer, what are the possible exceptions thrown while connection and reading InputStream?
A. IOException
B. MalformedURLException
C. NetworkException
D. ConnectException
正确答案: A B

902. What line of a given program will throw FileNotFoundException?
import java.io.*;
public class Test{
public static void main(String args[]){
try{
FileReader fileReader=
new FileReader("MyFile.java");
BufferedReader bufferedReader=
new BufferedReader(fileReader);
String strString;
fileReader.close();
while((strString=bufferedReader.readLine())!=null{
System.out.printIn(strString);
}
}catch(IOExeption ie){
System.out.println(ie.getMessage());
}
}
Select the one right answer.
A. This program never throws FileNotFoundException.
B. The line fileReader.close() throws FileNotFoundException.
C. At instantiation of FileReader object.
D. While constructing the BuffereReader object.
正确答案: C

903. When the following program will throw an IOException?
import java.io.*;
class Test{
public static void main(String args[]){
try{
String strString=
"Now is the time to take Sun Centification";
char buffer[]=new char[strString.length()];
strString.getChars(0,strString.length(),buffer,0);
FileWriter f=new FileWriter("myFile1.txt");
FileWriterf1=f;
f1.close();
for(int i=0; i<buffer.length;i+=2){
f.write(buffer[i]);
}
f.close();
FileWriter f2=new FileWriter("MyFile2.txt");
f2.writer(buffer);
f2.close();
} catch(IOException ie){
System.out.println(ie.getMessage());
}
}
}
Select the one right answer.
A. This program never throws IOException.
B. The line f1.close(0 throws IOException.
C. While writing to the stream f.write(buffer[i])throws an IOExcpetion.
D. While constructing the FileWriter object.
正确答案: C

904. Which line of the program could be throwing an exception, if the program is as listed below. Assume that "MyFile2.txt" is a read only file?
import java.io.*;
class FileWrite{
public static void main(String args[]){
try{
String strString=
"Updating the critical data section"
char buffer[]=new char[strString.length()];
strString.getChars(
0,strString.length(),buffer,0);
FileWriter f=new FileWriter("MyFile1.txt");
FileWriter f1=f;
for(int i=0; i<buffer.length; i+=2){
f1.write(buffer[i]);
}
f1.close();
FileWriter f2=new FileWriter("MyFile2.txt");
f2.write(buffer);
f2.close();
}atch (IOException ie){
System.out.println(ie.getMessage());
}
}
}
Select the one right answer from the following.
A. This program never throws IOException.
B. The line f1.close() tjrpwsIOException.
C. While writing to the stream f1.write(buffer[i])throws an IOException.
D. Which constructing the FileWriter f2=new FileWriter("MyFile2.txt");
正确答案: D

905. Select all the correct answers about File Class?
A. A File class can be used to create files and directories.
B. A File class has a method mkdir() to create directories.
C. A File class has a method mkdirs() to create directory and its parent directories.
D. A File cannot be used to create directories.
正确答案: A B C

906. Using File class, you can navigate the different directories and list all the files in those directories.
A. True
B. False
正确答案: A

907. Select all the constructor definitions of "FileOutputStream".
A. FileOutputStream(FileDescriptor fd)
B. FileOutputStream(String fileName, boolean append)
C. FileOutputStream(RandomAccessFile raFile)
D. FileOutputStream(String dirName, String filename)
正确答案: A B

908. If you run the following code on a PC from the directory c:/source:
import java.io.*;
class Test{
public static void main(String[] args) throws Exception{
File file=new File("Ran.test");
System.out.println(file.getAbsolutePath());
}
}
What do you expect the output to be? Select the one right answer.
A. Ran.test
B. source/Ran.test
C. c:/source/Ran.test
D. c:/source
E. null
正确答案: C

909. Which of the following used to read and write to network sockets, which are super classes of Low level streams?
A. InputStream
B. StreamReaders
C. OutputStream
D. Writers
E. Readers
F. Streams
正确答案: A C

910. Low Level Streams read input as bytes and writes as bytes, then select the correct declarations of Streams.
A. FileInputStream FIS=new FileInoutStream("test.txt")
B. File file=new File("test.txt");
C. File file=new File("c:");
File file1=new File(file,"test.txt");
FileOutputStream FOS=new FileOutputStream(file1);
D. FileInputStream FIS=new FileInputStream("c:
","test.txt")
正确答案: A B C

911. Choose all valid forms of the argument list for the FileOutputStream constructor shown below:
A. FileOutputStream(FileDescriptor fd)
B. FileOutputStream(String n, boolean a)
C. FileOutputStream(boolean a)
D. FileOutputStream()
E. FileOutputStream(File f)
正确答案: A B E

912. What is the class that has "mode" argument such as "r" or "rw" is required in the constructor?
A. DataInputStream
B. InputStream
C. RandomAccessFile
D. File
正确答案: C

913. What is the output displayed by the following code?
import java.io.*;
class Test{
public class Test{
public static void main(String args[]) throws IOException{
RandomAccessFile file=
new RandomAccessFile("test.txt","rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeInt(7890);
file.writeIntLong(1000000);
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
Select the correct answer.
A. 123456
B. 7890
C. 1000000
D. .0001
正确答案: B

914. Which is not a method of the class inputStream?
A. int read(byte[])
B. void flush()
C. void close()
D. int available()
正确答案: B

915. Which class is not subclass of FilterInputStream?
A. DataInputStream
B. BufferedInputStream
C. PushbackInputStream
D. FileInputStream
正确答案: D

916. Which classes can be used as the argument of the constructor of the class FileInputStream?
A. InputStream
B. File
C. FileOutputStream
D. String
正确答案: B D

917. Which classes can be used as the argument of the constructor of the class FilterInputStream?
A. FilterOutputStream
B. File
C. InputStream
D. RandomAccessFile
正确答案: C

918. True or False: Readers have methods that can read and return floats and doubles.
A. Ture
B. Flase
正确答案: B

919. Which of the following correctly illustrate how an InputStreamReader can be created?
Select all correct answers.
A. new InputStreamReader(new FileInputStream("data"));
B. new InputStreamReader(new FileReader("data"));
C. new InputStreamReader(new FileBufferedReader("data"));
D. new InputStreamReader("data");
E. new InputStreamReader(System.in);
正确答案: A E

920. Which of the following is a legal way to construct a RandomAcessFile?
Select the most appropriate answer.
A. RandomAccessFile("data","r");
B. RandomAccessFile("r","data");
C. RandomAccessFile("data","read");
D. RandomAccessFile("read","data");
正确答案: A

921. Which Method Returns the parent part of the pathname of this File object, or null if the name has no parent part?
A. getParent()
B. getParentDir()
C. getParentDirectory()
D. parentDirectory()
正确答案: A

922. Which of the following is the correct form of constructor for PrintWriter?
A. LineNumberWriter
B. PrintWriter
C. PrintStream
D. PrintOutoutReader
正确答案: B

923. Which of the following is the correct form of constructor for PrintWriter?
A new PrintWriter(new OutputStreamReader(new File("./hai.txt");
B. new PrintWiter(new File("./hai.txt");
C. new PrintWiter(new OutputStream());
正确答案: C

924. Suppose this file exists in your directory. Which of the following can be used to write a line of text to the end of this file?
File f=new File("aa.txt");
Select the most appropriate answer from following.
A. RandomAccessFile f1=new RandomAccessFile(f,"r");
B. RandomAccessFile f1=new RandomAccessFile(f,"a");
C. RandomAccessFile f1=new RandomAccessFile(f,"rw");
D. RandomAccessFile f1=new RandomAccessFile(f,"w");
E. RandomAccessFile f1=new RandomAccessFile(f,"wr");
正确答案: C