Menu
Question Index
...

What will be the output of the following program?

public class KeepItClean {
    public static void main(String[] args) {
        Valuable.status();
        try(Valuable v = new Valuable(); Valuable v2 = new Valuable())
        {
            Valuable.status();
            v.open();
            Valuable.status();
            v2.open();
            Valuable.status();
        }
        Valuable.status();
    }
}
class Valuable implements AutoCloseable {
    static int numberOfValuables = 5;
    Valuable() {
        open();
    }
    public void open() {
        numberOfValuables--;
        System.out.print("O" + numberOfValuables + " ");
    }
    public void close() {
        numberOfValuables++;
        System.out.print("C" + numberOfValuables + " ");
    }
    static void status() {
        System.out.print("S" + numberOfValuables + " ");
    }
}


S5 S5 O4 S4 O3 S3 S3
S5 S5 O4 S4 O3 S3 C4 c5 S5
S5 O4 O3 S3 O2 S2 O1 S1 S1
S5 O4 O3 S3 O2 S2 O1 S1 C2 C3 S3
Some other output
Compilation Error or Runtime Error

Doubts

Problems

Topic: Static Keyword In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App