O que o seguinte código imprimirá?public class Test { public static void main(String[] args) { int y = 2; if (y < 3) { if (y == 2) { System.out.println("A"); } else { System.out.println("B"); } } else { System.out.println("C"); } }
Question
O que o seguinte código imprimirá?public class Test { public static void main(String[] args) { int y = 2; if (y < 3) { if (y == 2) { System.out.println("A"); } else { System.out.println("B"); } } else { System.out.println("C"); } }
Solution
O código imprimirá "A".
Aqui está o passo a passo:
- A variável
yé inicializada com o valor 2. - A primeira condição
if (y < 3)é verdadeira porque 2 é menor que 3. - Então, o programa entra no bloco de código dentro dessa condição.
- Dentro desse bloco, há outra condição
if (y == 2). Esta também é verdadeira porqueyé igual a 2. - Portanto, o programa entra no bloco de código dentro dessa condição e imprime "A".
- As outras partes do código (
else { System.out.println("B"); }eelse { System.out.println("C"); }) não são executadas porque suas condições correspondentes não são verdadeiras.
Similar Questions
Considere o trecho de código a seguir:int x = 10;int y = 3;int result = (x % 4) * (y % 2);System.out.println(result);Qual será a saída do código acima?
Qual é o valor impresso na execução do código abaixo ? public static void main(String[] args) throws Exception { int numero1 = 1; String numero2 = "2"; System.out.println(numero1+numero2); }
the correct answerWhat will be the output of the following Java program? class CT { public static void main(String args[]) { try { int X, Y; Y = 0; X = 5 / Y; System.out.print("A"); } catch(ArithmeticException e) { System.out.print("B"); } finally { System.out.print("C"); } } }
public class Test2 { public static int x; public static int foo(int y) { return y * 2; } public static void main(String [] args) { int z = 5; assert z > 0; /* Line 11 */ assert z > 2: foo(z); /* Line 12 */ if ( z < 7 ) assert z > 4; /* Line 14 */ switch (z) { case 4: System.out.println("4 "); case 5: System.out.println("5 "); default: assert z < 10; } if ( z < 10 ) assert z > 4: z++; /* Line 22 */ System.out.println(z); }}which line is an example of an inappropriate use of assertions?Line 11Line 12Line 14Line 22
rrect answerWhat will be the output of the following Java code? class Name { public static void main(String args[]) { try { int ct1 = 0; int ct2 = 5; int ct3 = ct2 / ct1; System.out.print("Hello"); } catch(Exception e) { System.out.print("CodeTantra"); } } }
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.