What will the following code display?

class Quizful {
    private static int count = 0;
    private final int id = ++count;

    private void print() {
        System.out.println(id);
    }

    public void printOther(Quizful other) {
        other.print();
    }
    public static void main(String[] args) {
        Quizful one = new Quizful();
        Quizful two = new Quizful();
        two.printOther(one);
    }    
}
Explanation
Two objects of Quizful class will be created, first with field id = 1 and second with field id = 2. A reference to object 'one' will be passed to the printOther() method of object 'two'. Then print() of object 'one' will be called and 1 will be printed.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes