Menu
Topics Index
...
`

class W method c output


If you need explanation Read this topic

If you need Answer Take test on this topic

class W method c output
What will be the output of the following program?
class W
{
    static int c = 0;

    public static void main(String[] args)
    {
        W w1 = c();
        W w2 = c(w1);
        W w3 = c(w2);
        W w4 = c(w3);
    }

    private W()
    {
        System.out.println("c = " + c);
    }

    static W c()
    {
        return c++ <= 0 ? new W() : null;
    }

    static W c(W w)
    {
        return w.c++ == 1 ? new W() : null;
    }
}

A. c = 1
c = 2
B. c = 1
c = 2
c = 3
C. c = 1
c = 2
c = 3
c = 4
D. Compilation Error
E. NullPointerException
Topic: Static Keyword In Java

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.

Ans is A..here we have the Static Concepts..we have static variables which are common to the all instances can access using the class name also and we can invoke the static methods using the class name with out creating the instnace.

Execution:
here we have the static variables c which is assigned with the 0,next we are calling the c() than we are check if c<=0 than we are returning the instance of the W class.here we are using the post increment so later it will increment so it will return Instance of the W when instance is created constructor get invoked and prints 1..now w1 holding the Object..next we are calling the c() by passing the w1 than there we are checking it is==1 later it will become 2 so here also we are returning the instance so  constructor get invoked and prints 2 w2 holding the instance..next we are calling the c() with the w2 here condition is false and returns null next for w3() also it will be false for last 2 method calls wont create the instance so Bcz c alrdy reached 2 which is common to all objects

Posted by Uday Kumar    2015-02-17 04:20:45


ANS IS A

--Here hte concept of Static Keyword is used.

--The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

--->The static can be:

variable (also known as class variable)
method (also known as class method)
block
nested class

--Execution starts from main()

1 Statement:First c() method is called where conditional operator(? :) is used.c++ ---> 0<=0 (true) then object of w class ise created and passed to w1 object which calls the constructor of w class.Constructor prints value of c.

c=1 as postincrement operator is used where c is checked for 0 and then incremented later.

2 Statement:
Then,c() method is called by passing object w1.inside c() method again conditional operator is used to check if c++ i.e. 1==1 (true) then object of w class is returned to w2 object which calls the constructor of w class in which value of c gets printed.

c=2 as postincrement operator is used where c is checked for 1 and then incremented later.

3 Statement:Then,c() method is called by passing object w2.
now value of c is 2 so c==1 will evaluate to be false.so null will be retuened and object is not passed so constructor will not be called.

4 Statement:Then,c() method is called by passing object w3.
now value of c is 2 so c==1 will evaluate to be false.so null will be retuened and object is not passed so constructor will not be called.

OUTPUT:
c=1
c=2

Posted by Mânïshå Mùlchåndânï    2015-02-17 08:51:00


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Bolo Raven. 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    2015-02-18 03:36:55


© meritcampus 2019

All Rights Reserved.

Open In App