Pegadinha?

3 respostas
marciolx
class X {
  public static void main(String[] args) {
    int i = 0;
    while (i++ < args.length) {
      System.out.print(args[i]);
    }
  }
}

Assume the above main method is invoked using the following
command line.

java X A B C D E F

What is the result of attempting to compile and run the program using the specified command line?

a. Prints: XABCDEF
b. Prints: ABCDEF
c. Compile-time error
d. Run-time exception
e. None of the Above

3 Respostas

ricardolecheta

RuntimeException! devido ao i++, ele passa na condicao do while mas incrementa somente depois…

marciolx

acertou, a resposta é D, não importa a precedência dos operadores no while, mas o índice do array é sempre acessado quando o i já foi incrementado.

P

fica até chato depois q alguém já respondeu, mas eu tb tinha marcado D. :slight_smile:

Criado 18 de agosto de 2003
Ultima resposta 18 de ago. de 2003
Respostas 3
Participantes 3