Menu
Topics Index
...
`

Love Pure Impure2251


If you need explanation Read this topic

If you need Answer Take test on this topic

Love Pure Impure2251
What will be the output of the following program?
public class LovePureImpure {
    static {
        System.out.println("Love is neither pure nor impure");
    }
    public LovePureImpure() {
        System.out.println("Love is purely impure and impurely pure");
    }
    public static void main(String[] args) {
        new LovePureImpure();
        Pure pure = null;
        Impure impure = new Impure();
        pure = new Pure();
    }
}
class Pure {
    static {
        System.out.println("I am pure Love");
    }
    public Pure() {
        System.out.println("I am impure Love disguised as pure Love");
    }
}
class Impure {
    public Impure() {
        System.out.println("I am impure Love");
    }
}

A. Love is neither pure nor impure
I am pure Love
I am impure Love disguised as pure Love
I am impure Love
I am impure Love disguised as pure Love
B. Love is neither pure nor impure
I am impure Love
I am pure Love
I am impure Love disguised as pure Love
C. Love is neither pure nor impure
Love is purely impure and impurely pure
I am impure Love
I am pure Love
I am impure Love disguised as pure Love
D. Some other output or Compilation Error or Runtime Error
Topic: Creating Static Methods In Java Using Static Keyword

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

A

Posted by Sudhakar Dhs    2014-10-22 03:51:01


Ans is C..
here we have three classes

LovePureImpure and Pure and Impure

static Block:

static block wil execute when the class loads first time.it wil execute only once..class loading means (Accessing static variable or calling static method or creating instance or class.forName("ClassNam"))

in the example
main method(static) in  LovePureImpure class wil call so that static block in that class wil get executed("Love is neiher Pure nor impure")
in main method we are creating instnce for the this class so that constructor wil invoked and printed as "Love is purely impure and impurely pure"

next we are creating refernce for the class Pure creating reference didnt cal anything next we are creating instance for the impure class so that constrcotr wil cal and prints "I am impure Love"

here we are creating the object for the Pure class so that static block in Pure class wil get printed "I am pure Love" next constrcutor wil Invoked

Posted by Uday Kumar    2014-10-22 05:22:13


ans=c
because firstly static method will be called before main class ececution i.e. "Love is neither pure nor impure".

after that class  LovePureImpure will be called which contains " Love is purely impure and impurely pure"

after that class impure executed thatcontains " I am impure Love"

after ececution of impure method , class pure will be called which contain "I am pure Love  " will be executed...

and after again pure class will be executed and " I am impure Love disguised as pure Love"....

Posted by Shubham Bansal    2014-10-22 05:23:21


Ans is C.
In above Example concept of Static block is used.If you have executable statements in the static block, JVM will automatically execute these statements when the class is loaded into JVM.

Execution Order:

----LovePureImpure is First Class that  gets loaded and static block gets executed which prints  --> "Love is neither pure nor impure"

---Object of LovePureImpure class is created which calls it's constructor which in turn prints -> "Love is purely impure and impurely pure."

---Reference of pure class is created.

---Object of Impure class is created which calls its constructor which prints -> "I am Impure Love"

---Reference of pure class is allocated memory which executes static block i.e. " I am pure Love " gets printed.

---After that constructor of Pure Class is created which prints "I am Impure Love disguised as pure Love."

Posted by Mânïshå Mùlchåndânï    2014-10-22 08:25:21


C.

Posted by Tahir Abbas    2014-10-22 18:12:03


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Vikas Tiyagi. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-23 01:18:12


© meritcampus 2019

All Rights Reserved.

Open In App