Programming in Visual Basic



Computer Programming II Instructor: Greg Shaw

COP 3337

Study Guide for Midterm Exam

1. Be able to understand and write code that performs simple operations on ArrayLists

2. Be able to understand and write code that performs simple operations on one- and two-dimensional arrays

3. Explain class cohesion, dependency and coupling, and method side effects

4. Understand the meaning/effect of the assignment operator when applied to object variables

5. Explain the difference between the “==” operator and the equals method

6. Explain the difference between instance variables and static class variables

7. Be able to use interfaces and the classes that implement them, including

• upcasting from a class type to the interface type

• calling interface methods polymorphically

• downcasting from the interface type back to the native class type

• calling methods declared in the implementing classes but not present in the interface

8. Understand polymorphic method calls and the benefits of polymorphism, and be able to define terms like “early binding” and “late binding” and explain how they are used to implement polymorphism

9. Be able to “play computer” or “hand trace” segments of code

S A M P L E Q U E S T I O N S

1. Which of the following is true regarding a class and interface types?

a. You cannot convert from a class type to an interface type

b. You can implicitly convert from a class type to any interface type that the class implements.

c. You can implicitly convert down from the interface type to the type of the implementing class

d. None of the above

e. Both b and c are correct

2. What will be the contents of array values after execution of the following code?

int[] values = { 4, 5, 6, 7 };

for (int i = 1; i < values.length; i++)

{

values[i] = values[i – 1] ;

}

a. 4 4 5 6 b. 4 9 15 22 c. 9 11 13 7 d. 4 4 4 4

3. What happens when the following code segment is executed?

int[] somearray = new int[6];

for (int i = 1; i < 6; i++)

{

somearray[i] = i + 1;

}

a. The for loop initializes all the elements of the array.

b. The for loop initializes all the elements except the first element.

c. The for loop initializes all the elements except the last element.

d. The for loop initializes all the elements except the first and last elements.

e. An ArrayIndexOutOfBoundsException is thrown

4. What is printed by this code segment:

int[][] numarray = { { 3, 2, 3 },

{ 0, 0, 0 } } ;

System.out.print( numarray[0][0] + "" + numarray[1][0] ) ;

a. 00

b. 31

c. 30

d. 03

e. 32

5. Consider the following 2-dimensional array. Which expression gives the number of elements in the third row?

int[][] counts = { { 0, 0, 1 },

{ 0, 1, 1, 2 },

{ 0, 0, 1, 4, 5 },

{ 0, 2 } } ;

a. counts[2].size()

b. counts.length

c. counts[2].length

d. counts[3].size()

e. counts[3].length

6. Assume that ArrayList nums initially contains the following Integer values: 0 0 4 2 5 0 3 0

What will ArrayList nums contain after execution of this code segment?

int k = 0 ;

while ( k < nums.size() )

{

if ( nums.get(k) == 0 )

{

nums.remove(k);

}

k++ ;

}

(A) 0 0 4 2 5 0 3 0

(B) 4 2 5 3

(C) 4 2 5 3 0

(D) 3 5 2 4 0 0 0 0

(E) 0 4 2 5 3

7. Method search may or may not work correctly. If it does not, find the bug!

HINT: Read the documentation; then make up a list with 3 or 4 names and play computer!

/**

* Returns the index (position) of a given name in an ArrayList

* @param names the ArrayList to be searched

* @param target the name to be found

* @return the index of target in names, or -1 if target is not found

*/

public int search(ArrayListnames, String target)

{

boolean found = false ;

int index = names.size() ;

while ( !found && index >= 0 )

{

String currentName = names.get(index) ;

if ( currentName.equals(target) )

{

found = true ;

}

else

{

index-- ;

}

}

if (found)

return index ;

else

return -1 ;

}

a. the method works correctly

b. the method throws an IndexOutOfBoundsException

c. target will not be found if it is in the first element of the list

d. if target is found, the index returned is "off by one"

e. the method will search only one element of the list

Answers

1. b

2. d

3. b

4. c

5. c

6. e (Hint: What happens when an object is removed from an ArrayList?)

7. b

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download