What is the result?

Given:

public class A { //Line 1

private void a() {}; //Line 2

class B { //Line 3

private void b(){{ //Line 4

a();{ //Line 5

}{ //Line 6

}{ //Line 7

public static void main(String[] args) {{ //Line 8

B b = new A().new B();{ //Line 9

b();{ //Line 10

}{ //Line 11

}{ //Line 12

What is the result?

Given:

public class A { //Line 1

private void a() {}; //Line 2

class B { //Line 3

private void b(){{ //Line 4

a();{ //Line 5

}{ //Line 6

}{ //Line 7

public static void main(String[] args) {{ //Line 8

B b = new A().new B();{ //Line 9

b();{ //Line 10

}{ //Line 11

}{ //Line 12

What is the result?

A.
Compilation fails at line 9

B.
Compilation fails at line 10

C.
Compilation fails at line 5

D.
Compilation fails at line 3

E.
Compilation succeeds



Leave a Reply 6

Your email address will not be published. Required fields are marked *


Francesco

Francesco

The correct answer is B. The b() must be called through b.b().

a() can be normally called as the internal class as an implicit reference to the enclosing class

Elaine

Elaine

Don’t understand the question at all.

Serg

Serg

Compilation fails. It have much error.

class A { //Line 1

private void a() {}; //Line 2

class B { //Line 3

private void b(){ //Line 4

a();{ //Line 5

}} //Line 6

} //Line 7

public static void main(String[] args) { //Line 8

B b = (new A()).new B(); //Line 9

b(); //Line 10

}

}

Have Error in line 9 and 10

A.B b = (new A()).new B(); //Line 9

b.b(); //Line 10

WASABI

WASABI

The correct answer is B.
public class A {
private void a() {}

class B {
private void b(){a();}

}
public static void main(String[] args) {
B b = new A().new B();
b.b();
}

}
original statement is wrong?
why Unnecessary{?

Luiz

Luiz

Correct answer is B, if the code is:

public class A { // Line 1

private void a() {}; // Line 2

class B { // Line 3

private void b() { // Line 4

a(); // Line 5

} // Line 6

} // Line 7

public static void main(String[] args) { // Line 8

B b = new A().new B(); // Line 9

b(); // Line 10

} // Line 11

} // Line 12

Michael

Michael

if Line 10 change to b.b(), the answer would be ‘E’.