Oracle
1Z0-808 · Question #21
Given the content of three files: A.java: public class A { public void a () {} int a; } B.java: public class B { private int doStuff() { private int x = 100; return x++; } } C.java: import…
The correct answer is A. Only the A.java file compiles successfully. Class B doesn't compile because we can't use access modifiers (private) inside methods. Class C doesn't compile because if the class is part of a package (p1), the package statement must be the first line in the source code file, before any import statements (java.io.*) that…
Java Class Design
Question
Given the content of three files:
A.java:
public class A {
public void a () {}
int a;
}
B.java:
public class B {
private int doStuff() {
private int x = 100;
return x++;
}
}
C.java:
import java.io.*;
package p1;
class A {
public public void main(String fileName) throws IOException {}
}
Which statement is true?
Options
- AOnly the A.java file compiles successfully.
- BOnly the B.java file compiles successfully.
- COnly the C.java file compiles successfully.
- DThe A.java and B.java files compile successfully.
- EThe B.java and C.java files compile successfully.
- FThe A.java and C.java files compile successfully.
How the community answered
(24 responses)- A79% (19)
- D4% (1)
- E8% (2)
- F8% (2)
Explanation
Class B doesn't compile because we can't use access modifiers (private) inside methods. Class C doesn't compile because if the class is part of a package (p1), the package statement must be the first line in the source code file, before any import statements (java.io.*) that may be present.
Topics
#compilation rules#package declaration#access modifiers#class structure
Community Discussion
No community discussion yet for this question.