Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 25)
25.
What will be the output of the program?
public class ExamQuestion6 
{
    static int x; 
    boolean catch()
    {
        x++; 
        return true; 
    } 
    public static void main(String[] args)
    {
        x=0; 
        if ((catch() | catch()) || catch()) 
            x++; 
        System.out.println(x); 
    } 
}
1
2
3
Compilation Fails
Answer: Option
Explanation:

Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

Discussion:
11 comments Page 1 of 2.

Rui said:   1 decade ago
It's not just that. It is also not possible to refer to instance methods in a static context :)

Victor Anica said:   1 decade ago
Exactly; for example we rename the troubling method to "test()", then the following message will occur when compiling : cannot make a static reference to the non-static reference test() from the type <className>.

Sandeep Gujjar said:   1 decade ago
Hi friends I want to know what is logical difference b/w || and |?

Iroshan said:   1 decade ago
Actually explanation is ok. But not fully right.

Another reason is to display the compilation error is.

- non static methods can not be referenced in static context. Main method is static.

Yogesh Prabhukhanolkar said:   1 decade ago
Its not about just that only.

First line of main makes x=0;

x is instance variable, it can't be accessed directly, first line itself fails!

Arpit said:   1 decade ago
If it would not a compilation error then what would be the answer.

Daniel said:   1 decade ago
Another thing is catch is reserved keyword, so it won't compile.

Biswa said:   1 decade ago
Even if you change the method name to catch1, it will still not compile since its not declared static.

Raaaaaaaa said:   9 years ago
How it works? please explain.

public class NewClass
{
static int x;

static boolean catc()
{
x++;

return true;
}
public static void main(String[] args)
{

if ((catc() |catc()) || catc())
{
x++;
System.out.println(x);
}
}
}

The output is 3.

Anil said:   8 years ago
Replay for given question
public class NewClass
{
static int x;

static boolean catc()
{
x++;

return true;
}
public static void main(String[] args)
{

if ((catc() |catc()) || catc())
{
x++;
System.out.println(x);
}
}
}
---------------------------
@Raaaaaaaa
The output of the given question is 3 because,

if(catc()|catc())// call for two times so x value is become 2 (bit wise( |) will perform first it will convert into binary then perform OR gate operation)
and the (catc()|catc()) give output is true so next condition will not check so other catc() will not execute.

* inside the if condition there is statement x++ so it will increase the value of x by 1 so in next line sop(x)=>3.


Post your comments here:

Your comments will be displayed after verification.