Review Questions

graphics/rq_icon.gif

3.10

Which statements are true?

Select the three correct answers.

  1. The result of the expression (1 + 2 + "3") would be the string "33".

  2. The result of the expression ("1" + 2 + 3) would be the string "15".

  3. The result of the expression (4 + 1.0f) would be the float value 5.0f.

  4. The result of the expression (10/9) would be the int value 1.

  5. The result of the expression ('a' + 1) would be the char value 'b'.

3.11

What happens when you try to compile and run the following program?

public class Prog1 {
    public static void main(String[] args) {
        int k = 1;
        int i = ++k + k++ + + k;
        System.out.println(i);
    }
}

Select the one correct answer.

  1. The program will not compile. The compiler will complain about the expression ++k + k++ + + k.

  2. The program will compile and will print the value 3 when run.

  3. The program will compile and will print the value 4 when run.

  4. The program will compile and will print the value 7 when run.

  5. The program will compile and will print the value 8 when run.

3.12

Which is the first incorrect line that will cause a compile time error in the following program?

public class MyClass {
    public static void main(String[] args) {
        char c;
        int i;
        c = 'a'; // (1)
        i = c;   // (2)
        i++;     // (3)
        c = i;   // (4)
        c++;     // (5)
    }
}

Select the one correct answer.

  1. The line labeled (1)

  2. The line labeled (2)

  3. The line labeled (3)

  4. The line labeled (4)

  5. The line labeled (5)

  6. None of the lines are incorrect. The program will compile just fine.

3.13

What happens when you try to compile and run the following program?

public class Cast {
    public static void main(String[] args) {
        byte b = 128;
        int  i = b;
        System.out.println(i);
    }
}

Select the one correct answer.

  1. The compiler will refuse to compile it, since you cannot assign a byte to an int without a cast.

  2. The program will compile and will print 128 when run.

  3. The compiler will refuse to compile it, since 128 is outside the legal range of values for a byte.

  4. The program will compile, but will throw a ClassCastException when run.

  5. The program will compile and will print 255 when run.

3.14

What will the following program print when run?

public class EvaluationOrder {
    public static void main(String[] args) {
        int[] array = { 4, 8, 16 };
        int i=1;
        array[++i] = --i;
        System.out.println(array[0] + array[1] + array[2]);
    }
}

Select the one correct answer.

  1. 13

  2. 14

  3. 20

  4. 21

  5. 24



     
    ASPTreeView.com
     
    Evaluation has ёјЪЛјї¶ЛЗФЖexpired.
    Info...