Home > Articles

  • Print
  • + Share This
This chapter is from the book

review.jpg Review Questions

  • 3.13 What will the following program print when run?

    public class ParameterPass {
      public static void main(String[] args) {
        int i = 0;
        addTwo(i++);
        System.out.println(i);
      }
    
      static void addTwo(int i) {
        i += 2;
      }
    }

    Select the one correct answer.

    1. 0

    2. 1

    3. 2

    4. 3

  • 3.14 What will be the result of compiling and running the following program?

    public class Passing {
      public static void main(String[] args) {
        int a = 0; int b = 0;
        int[] bArr = new int[1]; bArr[0] = b;
    
        inc1(a); inc2(bArr);
    
        System.out.println("a=" + a + " b=" + b + " bArr[0]=" + bArr[0]);
      }
    
      public static void inc1(int x) { x++; }
    
      public static void inc2(int[] x) { x[0]++; }
    }

    Select the one correct answer.

    1. The code will fail to compile, since x[0]++; is not a legal statement.

    2. The code will compile and will print a=1 b=1 bArr[0]=1 at runtime.

    3. The code will compile and will print a=0 b=1 bArr[0]=1 at runtime.

    4. The code will compile and will print a=0 b=0 bArr[0]=1 at runtime.

    5. The code will compile and will print a=0 b=0 bArr[0]=0 at runtime.

  • 3.15 Which statements, when inserted at (1), will result in a compile-time error?

    public class ParameterUse {
      static void main(String[] args) {
        int a = 0;
        final int b = 1;
        int[] c = { 2 };
        final int[] d = { 3 };
        useArgs(a, b, c, d);
      }
    
      static void useArgs(final int a, int b, final int[] c, int[] d) {
        // (1) INSERT STATEMENT HERE.
      }
    }

    Select the two correct answers.

    1. a++;

    2. b++;

    3. b = a;

    4. c[0]++;

    5. d[0]++;

    6. c = d;

  • 3.16 Which of the following method declarations are valid declarations?

    Select the three correct answers.

    1. void compute(int... is) { }

    2. void compute(int is...) { }

    3. void compute(int... is, int i, String... ss) { }

    4. void compute(String... ds) { }

    5. void compute(String... ss, int len) { }

    6. void compute(char[] ca, int... is) { }

  • 3.17 Given the following code:

    public class RQ810A40 {
      static void print(Object... obj) {
        System.out.println("Object...: " + obj[0]);
      }
      public static void main(String[] args) {
        // (1) INSERT METHOD CALL HERE.
      }
    }

    Which method call, when inserted at (1), will not result in the following output from the program:

    Object...: 9

    Select the one correct answer.

    1. print("9", "1", "1");

    2. print(9, 1, 1);

    3. print(new int[] {9, 1, 1});

    4. print(new Integer[] {9, 1, 1});

    5. print(new String[] {"9", "1", "1"});

    6. print(new Object[] {"9", "1", "1"});

    7. None of the above.

  • + Share This
  • 🔖 Save To Your Account