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);
}
}
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.
Anshu said:
8 years ago
Is that not necessary to assign a value to a static variable as soon as it is declared?
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.
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.
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.
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.
Biswa said:
1 decade ago
Even if you change the method name to catch1, it will still not compile since its not declared static.
Daniel said:
1 decade ago
Another thing is catch is reserved keyword, so it won't compile.
Arpit said:
1 decade ago
If it would not a compilation error then what would be the answer.
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!
First line of main makes x=0;
x is instance variable, it can't be accessed directly, first line itself fails!
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.
Another reason is to display the compilation error is.
- non static methods can not be referenced in static context. Main method is static.
Sandeep Gujjar said:
1 decade ago
Hi friends I want to know what is logical difference b/w || and |?
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>.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers