Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Easy
Question 53
What will be the result of compiling and executing Test class?
- package com.udayan.oca;
- public class Test {
- public static void print() {
- System.out.println("static method");
- }
- public static void main(String[] args) {
- Test obj = null;
- obj.print();
- }
- }
-
A
Compilation error
-
B
None of the other options
-
C
NullPointerException is thrown
-
D
static method
Reveal correct answer
Correct answer: D
Explanation
print() is static method of class Test. So correct syntax to call method print() is Test.print();
but static methods can also be invoked using reference variable: obj.print(); Warning is displayed in this case.
Even though obj has null value, we don't get NullPointerException as objects are not needed to call static methods.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
