Static Methods QuizJ8 Home « Static Methods Quiz

Java Objects & Classes Quiz 13

The quiz below tests your knowledge of the material learnt in Objects & Classes - Lesson 13 - Static Methods.

Question 1 : What will be output from the following code snippet?

public class StatCalls {
    public static void main (String[] args) {
        int aSquare = squareNumber(5);
        System.out.println(aSquare);
    }
    int squareNumber(int number) {
        int square = number * number;
        return square;
    }
}
- The code snippet will not compile as a non-static method cannot be referenced from a static content.
Question 2 : Static methods run without any instance of a class being involved?
- Static methods DO run without any instance of a class being involved.
Question 3 : What will be output from the following code snippet?

public class StatCalls2 {
    public static void main (String[] args) {
        this.number = 6;
        int aSquare = StatCalls2.squareNumber(this.number);
        System.out.println(aSquare);
    }
    int squareNumber(int number) {
        int square = number * number;
        return square;
    }
}
- The code snippet will not compile as a non-static variable cannot be referenced from a static content.
Question 4 : You cannot access an instance variable from a static method?
- You CAN access an instance variable from a static method by using a local reference from within your static method to access non-static content.
Question 5 : What will be output from the following code snippet?

public class AccessNonStatic {
    int nonStatic = 28;

    public static void main (String[] args) {
        AccessNonStatic ans = new AccessNonStatic();
        System.out.println(ans.nonStatic); // Calling non-static
    }
}
- The code snippet will compile and output 28 as we are using a local reference from within a static method to access non-static content, which is fine.
Question 6 : We can use instantiation from within our static methods?
- We CAN use instantiation from within our static methods.
Quiz Progress Bar Please select an answer


What's Next?

The next quiz we investigate Java constants and how to use static initializer blocks.