Review Questions

graphics/rq_icon.gif

5.5

What will be the result of attempting to compile and run the following code?

class MyClass {
    public static void main(String[] args) {
        boolean b = false;
        int i = 1;
        do {
            i++;
            b = ! b;
        } while (b);
        System.out.println(i);
    }
}

Select the one correct answer.

  1. The code will fail to compile, since b is an invalid conditional expression for the do-while statement.

  2. The code will fail to compile, since the assignment b = ! b is not allowed.

  3. The code will compile without error and will print 1 when run.

  4. The code will compile without error and will print 2 when run.

  5. The code will compile without error and will print 3 when run.

5.6

What will be the output when running the following program?

public class MyClass {
    public static void main(String[] args) {
        int i=0;
        int j;
        for (j=0; j<10; ++j) { i++; }
        System.out.println(i + " " + j);
    }
}

Select the two correct answers.

  1. The first number printed will be 9.

  2. The first number printed will be 10.

  3. The first number printed will be 11.

  4. The second number printed will be 9.

  5. The second number printed will be 10.

  6. The second number printed will be 11.

5.7

Which one of these for statements is valid?

Select the one correct answer.

  1. int j=10; for (int i=0, j+=90; i<j; i++) { j--; }

  2. for (int i=10; i=0; i--) {}

  3. for (int i=0, j=100; i<j; i++, --j) {;}

  4. int i, j; for (j=100; i<j; j--) { i += 2; }

  5. int i=100; for ((i>0); i--) {}

5.8

What will be the result of attempting to compile and run the following program?

class MyClass {
    public static void main(String[] args) {
        int i = 0;
        for (   ; i<10; i++) ;      // (1)
        for (i=0;     ; i++) break; // (2)
        for (i=0; i<10;    ) i++;   // (3)
        for (   ;     ;    ) ;      // (4)
    }
}

Select the one correct answer.

  1. The code will fail to compile, since the for statement (1) is missing the expression in the first section.

  2. The code will fail to compile, since the for statement (2) is missing the expression in the middle section.

  3. The code will fail to compile, since the for statement (3) is missing the expression in the last section.

  4. The code will fail to compile, since the for statement (4) is invalid.

  5. The code will compile without error, and the program will run and terminate without any output.

  6. The code will compile without error, but will never terminate when run.

5.9

Which statements are valid when occurring on their own?>

Select the three correct answers.

  1. while () break;

  2. do { break; } while (true);

  3. if (true) { break; }

  4. switch (1) { default: break; }

  5. for (;true;) break;

5.10

Given the following code fragment, which of the following lines will be a part of the output?

outer:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        if (i == j) {
            continue outer;
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Select the two correct answers.

  1. i=1, j=0

  2. i=0, j=1

  3. i=1, j=2

  4. i=2, j=1

  5. i=2, j=2

  6. i=3, j=3

  7. i=3, j=2

5.11

What will be the result of attempting to compile and run the following code?

class MyClass {
    public static void main(String[] args) {
        for (int i = 0; i<10; i++) {
            switch(i) {
                case 0:
                    System.out.println(i);
            }
            if (i) {
                System.out.println(i);
            }
        }
    }
}

Select the one correct answer.

  1. The code will fail to compile, owing to an illegal switch expression in the switch statement.

  2. The code will fail to compile, owing to an illegal conditional expression in the if statement.

  3. The code will compile without error and will print the numbers 0 through 10 when run.

  4. The code will compile without error and will print the number 0 when run.

  5. The code will compile without error and will print the number 0 twice when run.

  6. The code will compile without error and will print the numbers 1 through 10 when run.

5.12

Which of the following implementations of a max() method will correctly return the largest value?

// (1)
int max(int x, int y) {
    return (if (x > y) { x; } else { y; });
}
// (2)
int max(int x, int y) {
    return (if (x > y) { return x; } else { return y; });
}
// (3)
int max(int x, int y) {
    switch (x < y) {
        case true:
            return y;
        default:
            return x;
    };
}
// (4)
int max(int x, int y) {
    if (x>y) return x;
    return y;
}

Select the one correct answer.

  1. Implementation labeled (1).

  2. Implementation labeled (2).

  3. Implementation labeled (3).

  4. Implementation labeled (4).

5.13

Given the following code, which statement is true?

class MyClass {
    public static void main(String[] args) {
        int k=0;
        int l=0;
        for (int i=0; i <= 3; i++) {
            k++;
            if (i == 2) break;
            l++;
        }
        System.out.println(k + ", " + l);
    }
}

Select the one correct answer.

  1. The program will fail to compile.

  2. The program will print 3, 3 when run.

  3. The program will print 4, 3 when run if break is replaced by continue.

  4. The program will fail to compile if break is replaced by return.

  5. The program will fail to compile if break is simply removed.

5.14

Which statements are true?

Select the two correct answers.

  1. {{}} is a valid statement block.

  2. { continue; } is a valid statement block.

  3. block: { break block; } is a valid statement block.

  4. block: { continue block; } is a valid statement block.

  5. The break statement can only be used in a loop (while, do-while or for) or a switch statement.



     
    ASPTreeView.com
     
    Evaluation has ЗУїЙМexpired.
    Info...