LearnFrenzy
Sign in Take a free test

Constructors and the this Keyword · Predict the Output · Question 6

Static block versus instance block Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Java program?

Main.java
class Engine {
    static { System.out.print("S "); }
    { System.out.print("I "); }
    Engine() { System.out.print("C "); }
}

public class Main {
    public static void main(String[] args) {
        System.out.print("M ");
        new Engine();
        new Engine();
        System.out.println();
    }
}
  • A M S I C I C Correct
  • B S M I C I C
  • C M S I C S I C
  • D M I C I C S
Correct Answer: Option A

Engine is initialised when first used (the first new), so S prints after M, and only once.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links