SCJP考题2

转载 2007年10月13日 14:49:00

 

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

正确答案:ABC

 

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;

正确答案:CE

 

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.

正确答案:BCE

精解:本题考察的重点是垃圾回收站的运行机制。在Java中,由垃圾回收站负责回收废弃对象占用的内存资源,程序员从不需要明确释放掉对象,尽管可以将一个引用变量设置为null,程序员可以通过用RuntimeSystem 类的 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.

正确答案:28034, 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考题2
举报原因:
原因补充:

(最多只允许输入30个字)