1. Which of the following statements creates alpha, a two-dimensional array of 10 rows and 5 columns, wherein each component is of the type int?
i. int[][] alpha = new int[10][5];
ii. int[][] alpha; alpha = new int[10][]; for (int i = 0; i < 10; i++) alpha[i] = new int[5];
Question 1 options:
2. Consider the method total below.
public static int total (int result, int a, int b) { if (a == 0) { if (b == 0) { return result * 2; } return result / 2; } else { return result * 3; } } The assignment statement x = total (6, 0, 0); must result in
Question 2 options:
| 1) | x being assigned the value 8 | |
| 2) | x being assigned the value 4 | |
| 3) | x being assigned the value 5 | |
| 4) | x being assigned the value 12 | |
| 5) | x being assigned the value 10 | |
3. Consider the following declarations.
public class List { private static final int MAX_ITEMS = < some value >; private int[] items; private int numItems; // constructor(s) and other methods not shown //precondition: 0 <= numItems < MAX_LENGTH public int find (int num) { boolean found = false; int loc = 0; while (!found && loc < numItems) { if (items[loc] == num) { found = true; } loc++; } if (!found) { loc = -1; } return loc; } }
Which of the following is a correct postcondition for the find method?
Question 3 options:
| 1) | returns true if num is found in this list; false otherwise | |
| 2) | returns true if num is found in this list; void otherwise | |
| 3) | returns the number of items currently in this list | |
| 4) | returns -1 if num is not in list; index of num otherwise | |
| 5) | returns index of num if num is in this list; nothing otherwise | |
4. What is the output of the program shown below?
public class SomeClass { private int x, y; public SomeClass (int xValue, int yValue) { x = xValue; y = yValue; } public void m1() { x = 30; System.out.print((y + 1) + " "); } public void m2() { m1(); System.out.print(x + " "); } } public class Tester { public static void main (String[] args) { int x = 20; int y = 10; SomeClass z = new SomeClass(y, x); z.m2(); z.m1(); } }
Question 4 options:
5. What is output by the following code?
public class Swapper
{ private int a, b; public Swapper(int aValue, int bValue) { a = aValue; b = bValue; } public void swap () { a = b; b = a; } public void print () { System.out.println("a = " + a + ", and b = " + b); } } public class Tester { public static void main(String[] args) { Swapper swapObj = new Swapper(10, 20); swapObj.swap(); swapObj.print(); } }
Question 5 options: