Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Which lines in the following code will cause a compilation


Download 1.52 Mb.
Pdf ko'rish
bet43/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   39   40   41   42   43   44   45   46   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Which lines in the following code will cause a compilation
error?
public interface MyInterface {
private static int FIELD1 = 7; //Line 1
int FIELD2 = 8;//Line 2
public final int FIELD3; //Line 3
public static int FIELD4 = 8; //Line 4
}
Answer:


110.
In the above code, Lines 1 and 3 will cause a compilation error.
All the fields declared in an interface need to be public static and
final. If the public static and final keywords are not specified, the
field is implicitly made public static final. Line 1 explicitly
specifies the private keyword and so causes a compilation
error. Line 2 does not have the public static final
keywords specified. However, this does not cause a compilation
error, Java implicitly makes the field public, static and
final. Line 3 specifies the public and final keyword but
does not initialize the field. A final field needs to be initialized.
So, Line 3 causes a compilation error as well. Line 4 specifies the
public and static keywords and initializes the field. It does not
specify the final keyword. However, this again does not cause a
compilation error, the field is implicitly treated as final.
Is the code snippet below valid? Explain.
public abstract class MyAbstractClass {
private abstract void doSomething();
}
Answer:
The above code is invalid and causes a compilation error for the
doSomething method. This is because the doSomething ()
method is both abstract and private. An abstract method
cannot be made private. A private method is not accessible
in the sub–class. An abstract method needs to be accessible
in the sub–class in order for the sub–class to provide
an implementation for the method. A sub–class will not be able to
provide an implementation for an abstract private method.




Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling