Java 代写 |计算机CS代写

ICS45J Programming in Java


1. Which of these statements about constructors is false?

A. A constructor has no return type

B. Its name must be the same as the class in which it is defined

C. There must be exactly one constructor defined for a class

D. Constructors are almost always declared as public

E. They can appear anywhere in the class where it is legal to declare a method

2. When does Java know an object is no longer needed? And what happens to an unneeded object's storage?

A. The programmer tells Java when an object is no longer needed by calling dispose() on it; the object's memory is released back to the memory pool.

B. If there are no references to the object, Java knows the object is no longer needed and automatically returns its memory to the memory pool.

C. If there are no references to the object, Java marks it as no longer needed; the memory stays in use until the programmer explicitly returns it to the memory pool.

D. Objects, once constructed, stay active until the program terminates, so though the programmer may know an object is it no longer needed, Java does not know this; objects' memory is returned to the memory pool when the program terminates.

E. Objects, once constructed, stay active until the method in which they were constructed terminates, so though the programmer may know an object is no longer needed, Java does not know this; objects' memory is returned to the memory pool when the method terminates.

6. What is the value of i when System.out.print(i) is executed?

A. 6

B. 5

C. 4

D. 3

E. unpredictable, since i is local to the loop

7. What is the value of sum when System.out.print(sum) is executed?

A. 6

B. 10

C. 15

D. 21

E. unpredictable, since sum is dependent upon a variable local to the loop

8. The fragment executes more times through the loop than is necessary. What change to the fragment can be made that removes the unneeded passes through the loop and doesn’t change the values of i and sum that are printed out?

A. initialize sum to 1, rather than 0

B. initialize i to 1, rather than 0

C. make the while loop boolean (i <= 4), instead of (i < 5)

D. make the while loop boolean (i < 4), instead of (i < 5)

E. place the statement i++; before sum = sum + i; (rather than after it)

11. According to Jacobson, when is it good practice to use a break statement?

A. As appropriate to break out of a switch statement

B. As needed to break out of a loop

C. As needed to break out of a deeply nested series of if statements

D. Both A and B.

E. Both A and C.

14. Suppose you are writing the definition of MyClass (line (1) above). Which of the following function signatures (headers) is correct?

A. public MyClass D. public void MyClass() B. public MyClass() E. public MyClass(void) C. public void MyClass

19. Which aspects of the fragment (of those listed below) violate good style standards (as defined in this course)?

I. having the { after do (on the same line)—the { should be on its own line directly underneath do

II. The constants –9, -99, 0 and 100 should have been named, and the names should have been used in the fragment

III. Using notDone == true as the test for the loop—there is a cleaner expression that can be used

IV. The user is not told the sentinel value

A. I and II only

B. III and IV only

C. I, II and III only

D. II, III and IV only

E. I through IV

20. Which statement best describes the intended purpose of this fragment?

A. to compute the minimum of a group of user-entered (integer) scores

B. to compute the maximum of a group of user-entered (integer) scores

C. to inform the user when s/he enters a score that is not between 0 and 100

D. to compute the maximum of the entered scores that are between 0 and 100

E. to compute the minimum of the entered scores that are between 0 and 100

21. Which of the following statements about Java arrays and ArrayLists are true?

I. Arrays are similar to objects, but technically are not true objects.

II. Once an ArrayList’s size is set, it cannot be changed without reconstructing it.

III. Arrays can directly hold primitive types as well as object references.

IV. Array indexing begins at 0, but ArrayList indexing begins at 1.

A. I and III only

B. I, II and III only

C. II and III only

D. I, II and IV only

E. I, II, III and IV

22. Consider a program written using an array A of size 50. Now suppose you want to change the program so that A is an ArrayList. What changes must or should be made to the program? Your goal is to have a correctly functioning program that follows good programming practice, while minimizing changes to the code.

I. A must be constructed (using an appropriate call to the ArrayList class constructor) before any “work” with the ArrayList is attempted.

II. A call to the ArrayList’s destructor should be added, to destroy A when it is no longer needed.

III. All references to elements of A via use of an index (e.g., A[I]) must be replaced with calls to appropriate ArrayList methods.

IV. If the array stores a primitive type, the values stored in the array must be changed, either explicitly or implicitly via auto boxing, to objects.

A. I and II only 

B. I, III and IV only 

C. II and III only
D. I and IV only
E. I, II, III and IV

23. Does Java do boundary checking of arrays during program execution?

A. Yes, always

B. Yes, but only if a project file setting is made to enable such checking

C. Yes, but it is limited to single-dimension arrays

D. Yes, but only if the array is a member of a class

E. No

29. Suppose you wanted to use a function to initialize three (already declared) variables that are not fields of a class. Further suppose you are doing this in a language that allows passing parameters either by value or by reference. To initialize the variables,

A. you should pass them to the function by reference and set the values in the function; the variables will be initialized when the function returns.

B. you should pass them to the function by value and set the values in the function; the variables will be initialized when the function returns.

C. you should pass them to the function by value, set the values in the function, then return these values via a return statement.

D. define these variables so they are global to the function, then set their values within the function; that is the preferred approach.

E. you should adopt another approach: none of the above methods is a reasonable approach.

32. Suppose you have a class MyClass and want to easily replace the contents of one object, target, with the contents of another object of MyClass, source. Which of the following statements would correctly create the copy?

A. target = source;

B. target.clone(source);

C. target = source.clone();

D. target = source.equals();

E. target = (MyClass) source.clone();

33. To enable exception handling on a block of code, one

A. encloses it in a try block

B. encloses it in a throwable block

C. labels it, then inserts that label into the Java exception handler list

D. must place the code into its own method and mark the method throwable

E. enables a trap for that code

34. In Java, what happens if code is written that could throw a checked exception in a method that has no throws clause for that exception, and there is no catch block defined in the method to handle that particular exception class?

A. If an accessible catch block exists for one of the exception’s ancestor classes, then the program compiles and runs.

B. If there is no catch block that can handle the exception, the code will not compile.

C. If the exception is thrown, and there is no catch block that can handle the exception, the program halts.

D. If the exception is thrown, and there is no catch block that can handle the exception, the program continues, but its results are unpredictable

E. Both A and B, taken together, fully describe what occurs.

咨询 Alpha 小助手,获取更多课业帮助