Topic Test - University of Texas at Austin



Multiple Choice. Pick the best response to each question.

|For questions 1 – 3 consider the following code segment: |

| |

|int[][] mat = new int[10][5]; |

| |

|1. How many rows are there in the 2D array that mat refers to? |

|A. 10 |B. 5 |C. 9 |D. 0 |E. None of these |

| |

|2. How many columns are there in the 2D array that mat refers to? |

|A. 10 |B. 5 |C. 4 |D. 0 |E. None of these |

| |

|3. How many ints can be stored in the 2D array that mat refers to? |

|A. 10 |B. 5 |C. 50 |D. 0 |E. None of these |

| |

|4. What is the output of the following code segment? |

| |

|int[][] mat = new int[4][6]; |

|System.out.println( mat[1][2] ); |

|A. Syntax |B. An ArrayIndexOutOfBoundsException |C. 0 |D. unknown |E. None of these |

|Error |occurs | |output | |

| |

|5. What is the output of the following code segment? |

| |

|int[][] table = new String[3][10]; |

|System.out.println( table[0][0].length() ); |

|A. 0 |B. null |C. Syntax Error |D. A NullPointerException occurs |E. None of these |

| |

|6. What is the output of the following code segment? |

| |

|int[][] mat = {{1, 3, 5, 7}, |

|{0, 0, 3, 0}, |

|{5, 6, 10, 12}}; |

|System.out.println( mat[2][3] ); |

| |

|A. 3 |B. 5 |C. 6 |D. 10 |E. None of these |

|For questions 7 - 10 consider the following method: |

| |

|public int[][] process(int rows, int cols) |

|{ int[][] mat = new int[rows][cols]; |

|for(int r = 0; r < mat.length; r++) |

|{ for(int c = 0; c < mat[0].length; c++) |

|{ if( c > r ) |

|mat[r][c] = 2; |

|else |

|mat[r][c] = 1; |

|} |

|} |

|} |

| |

|7. Consider these potential preconditions for method process |

| |

|I. row > 0 |

|II. col > 0 |

|III. row > col |

| |

|Which of the above are the best preconditions for method process? |

|A. I only |B. II only |C. III only |D. I and II only |E. I, II, and III |

| |

|8. What would be returned by the call process(3, 2) ? |

|A. {{1, 2}, |B. {{2, 1}, |C. {{2, 2}, |D. {{1, 2, 2}, |E. None of these |

|{1, 1}, |{2, 2}, |{1, 2}, |{1, 1, 2}} | |

|{1, 1}} |{2, 2}} |{1, 1}} | | |

| |

|9. What would occur if one of the arguments to process is less than 0? |

|A. Syntax error |B. Logic error |C. Runtime error |D. the method returns null |E. None of these |

| |

|10. In method process how many times is the boolean expression (c > r) evaluated? |

|A. c |B. r |C. c + r |D. r - c |E. None of these |

|For questions 11 - 14 consider the following method: |

| |

|//pre: mat != null, mat is a rectangular matrix |

|public int process2(int[][] mat) |

|{ int result = -1; |

|int y = 0; |

|while( result == -1 && y < mat.length) |

|{ int x = 0; |

|int count = 0; |

|while( x < mat[0].length ) |

|{ if( mat[y][x] == 0 ) |

|count++; |

|x++; |

|} |

|if( count == mat[0].length ) |

|result = y; |

|y++; |

|} |

|return result; |

|} |

| |

|11. What is the output of the following code segment? |

| |

|int[][] mat = {{1, 0, 0}, |

|{0, 0, 1}}; |

|System.out.println( process2(mat) ); |

|A. 1 |B. 0 |C. -1 |D. 2 |E. None of these |

| |

|12. What best describes what method process2 does? |

| |

|I. Returns the smallest row index for a row that contains all 0s. |

|II. Returns the index of the row that contains the most 0s. |

|III. Returns the number of 0s in the matrix. |

|IV. Returns –1 if the matrix dos not have a row that contains all 0s. |

|A. I only |B. II only |C. III only |D. IV only |E. I and IV only |

| |

|13. In method process2 what is the minimum number of times the boolean expression |

|( mat[y][x] == 0 ) is evaluated assuming the preconditions are met? |

|A. mat[0].length |B. mat.length |C. mat[0].length * |D. 0 |E. None of |

| | |mat.length | |these |

|14. As written the inner while loop of method process2 does more than is required. Which boolean expression could replace ( x < |

|mat[0].length ) so that method process2 works the same, but the amount of work is minimized? |

| |

|I. ( count == x && x < mat[0].length ) |

|II. ( count == x ) |

|III. ( count != mat[0].length && x < mat[0].length ) |

|A. I. only |B. II only |C. III only |D. I or III only |E. None of these |

| |

|15. Consider the following code segment: |

| |

|int[][] mat = new int[2][3]; |

|for(int r = 0; r < mat.length; r++) |

|{ for(int c = 0; c < mat[0].length; c++) |

|{ mat[r][c] = r * c; |

|} |

|} |

| |

|What are the contents of mat after the segment completes? |

|A. {{0, 0, 0}, |B. {{0, 0, 0}, |C. {{0, 1, 2}, |D. {{0, 0, 0}, |E. None |

|{0, 0, 0}} |{1, 1, 1}} |{0, 1, 2}} |{0, 1, 2}} |of these |

| |

|16. Consider the following code segment: |

| |

|int[][] mat = new mat[3][3]; |

|for(int i = 0; i < mat.length; r++) |

|{ mat[i][i] = 1; |

|} |

| |

|What are the contents of mat after the segment completes? |

|A. {{1, 0, 0}, |B. {{1, 0, 0}, |C. {{1, 0, 0}, |D. {{1, 0, 0}, |E. None |

|{0, 0, 0}, |{0, 0, 0}, |{0, 0, 0}, |{0, 0, 0}, |of these |

|{0, 0, 1}} |{0, 0, 1}} |{0, 0, 1}} |{0, 0, 1}} | |

| |

|17. What is the output of the following code segment? |

| |

|int[][] mat = {{1, 3, 5}, |

|{2, 7, 8}, |

|{9, 4, 6}}; |

|for(int i = 0; i < mat.length; i++) |

|{ System.out.print( mat[ mat.length – i - 1][i] ); |

|} |

| |

|A. 176 |B. 975 |C. 671 |D. 579 |E. None of these |

| |

|18. What is the output of the following code segment? |

| |

|int[][] mat = {{1, 3}, |

|{2, 7}, |

|{9, 4}}; |

|for(int i = 0; i < mat.length; i++) |

|{ System.out.print( mat[ mat.length – i - 1][i] ); |

|} |

|A. 17 |B. 97 |C. Syntax error |D. An ArrayIndexOutOfBoundsException |E. None of these |

| | | |occurs | |

|For questions 19 – 23 consider the following portions of the Marine Biology Case Study. Only portions of classes are shown. [MBCS] |

| |

|public class BoundedEnv extends SquareEnvironment |

|{ private Locatable[][] theGrid; // grid representing the environment |

|private int objectCount; // # of objects in current environment |

| |

|// Constructs an empty BoundedEnv object with the given dimensions. |

|public BoundedEnv(int rows, int cols) |

|{ // Construct and initialize inherited attributes. |

|super(); |

| |

|theGrid = new Locatable[rows][cols]; |

|objectCount = 0; |

|} |

| |

|public int numRows() |

|{ return theGrid.length; } |

| |

|public int numCols() |

|{ return theGrid[0].length; } |

| |

|public int numObjects() |

|{ return objectCount; } |

| |

|/** pre: none |

|* Returns all the objects in this environment. |

|* @return an array of all the environment objects |

|**/ |

|public Locatable[] allObjects() |

|{ |

|Locatable[] theObjects = new Locatable[numObjects()]; |

|int tempObjectCount = 0; |

| |

|// Look at all grid locations. |

|for ( int r = 0; r < numRows(); r++ ) |

|{ for ( int c = 0; c < numCols(); c++ ) |

|{ // If there's an object at this location, put it in the array. |

|Locatable obj = theGrid[r][c]; |

| |

|if ( obj != null ) |

|{ theObjects[tempObjectCount] = obj; |

|tempObjectCount++; |

|} |

|} |

|} |

|return theObjects; |

|} |

| |

| |

| |

|19. [MBCS] What do elements in theGrid equal to null represent? |

|A. the location of objects in the|B. empty cells in the |C. non existent cells in the |D. edge cells in the |E. None of these |

|environment |environment |environment |environment | |

| |

|20. [MBCS] Assume theGrid is a 20 by 20 matrix. Also assume there is a Fish object at location theGrid[1][8] known as f1 and a Fish|

|object at location theGrid[7][3] known as f2. What is known about the indices of f1 and f2 in the resulting array? |

|A. nothing |B. they are equal |C. the index of f1 is less than |D. the index of f2 is less than |E. None of these |

| | |the index of f2 |the index of f1 | |

| |

|21. [MBCS] What does the following quantity represent? |

| |

|( theGrid.length * theGrid[0].length ) – numObjects() |

|A. the number of cells in|B. the number of Fish in |C. the number of empty |D. the number of non Fish|E. None of these |

|the environment |the environment |cells in the environment |objects in the | |

| | | |environment | |

| |

|22. [MBCS] The boolean expressions in the for loops in method allObjects are |

|( r < numRows() ) and ( c < numCols() ). What is the reason for calling methods numRows and numCols instead of simply using |

|theGrid.length and theGrid[0].length ? |

| |

|I. To make the class harder to understand. |

|II. To make the class more efficient. |

|III. To make the class easier to maintain or change. |

|A. I only |B. II only |C. III only |D. I and II only |E. II and III only |

| |

|23. [MBCS] Consider the description of the allObjects method. If the for loops in the method are swapped how must the description |

|of the returned value be changed? |

| |

|// Look at all grid locations. |

|for ( int c = 0; r < numCols(); c++ ) |

|{ for ( int r = 0; r < numRows(); r++ ) |

|{ // rest of code |

|A. No changes |B. State the objects are not |C. State the objects are |D. State there may be null |E. None of these |

|necessary |in sorted order |in sorted order |objects in the array | |

|24. Consider the following method: |

| |

|// pre: table != null, table.length > 0, table[0].length > 0 |

|// table is a rectangular matrix |

|public int process3(String[][] table) |

|{ int result = -1; |

|int temp; |

|for(int r = 0; r < table.length; r++) |

|{ for(int c = 0; c < table[0].length; c++) |

|{ temp = table[r][c].length(); |

|if( temp > result ) |

|result = temp; |

|} |

|} |

|return result; |

|} |

| |

|What does method process3 return? |

|A. the String with the |B. the String with the |C. the length of the shortest |D. the length of the longest |E. None of |

|minimum length in table |maximum length in table |String in table |String in table |these |

| |

|25. Consider the following method: |

| |

|// pre: mat != null, mat.length > 1, mat[0].length > 1 |

|// mat is a rectangular matrix |

|public int process4(int[][] mat) |

|{ int temp = mat[0][0]; |

|mat[0][0] = mat[1][1]; |

|mat[1][1] = tmp; |

|} |

| |

|Assume process4 is called with the following code: |

| |

|int[][] mat = {{5, 3}, |

|{1, 2}}; |

|process4(mat); |

| |

|What are the contents of mat after the call to process4? |

|A. {{0, 0}, |B. {{5, 3}, |C. {{2, 3}, |D. {{5, 2} |E. None of these |

|{0, 0}} |{1, 2}} |{1, 5}} |{1, 5}} | |

| |

|26. [Review] What is the output of the following code segment? |

| |

|int x = 3; |

|int y = 3; |

|x *= x + y |

|System.out.println(x); |

| |

|A. 6 |B. 18 |C. 27 |D. Syntax error |E. None of these |

|27. [Review] Consider the following method: |

| |

|public void review(int a, int b) |

|{ int c = a; |

|a = b; |

|b = c; |

|} |

| |

|What is the output of the following code segment? |

| |

|int x = 3; |

|int y = 2; |

|review(x, y) |

|System.out.println(x + " " + y); |

|A. 3 2 |B. 2 3 |C. 3 3 |D. 2 2 |E. None of these |

| |

|28. [Review] Consider the following method: |

| |

|// pre: list != null, 0 ................
................

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

Google Online Preview   Download