Java Programming - Flow Control - Discussion
Discussion Forum : Flow Control - General Questions (Q.No. 3)
3.
public void test(int x)
{
int odd = 1;
if(odd) /* Line 4 */
{
System.out.println("odd");
}
else
{
System.out.println("even");
}
}
Which statement is true?Answer: Option
Explanation:
The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.
Discussion:
15 comments Page 1 of 2.
Vikash rai said:
6 years ago
Integer can not behave like boolean. And there is not a complete condition statement in if condition.
So it will throw compilation error.
Thanks.
So it will throw compilation error.
Thanks.
Sajib said:
8 years ago
How it becomes compilation error? 0 if false otherwise true.
(2)
Surendra said:
9 years ago
With in if condition boolean expression is given.
(1)
Shrek said:
9 years ago
As far as I have learned, any number other than 0 in the if condition results in a true result. 0 returns false and hence in this case if should actually return true and print ODD.
Although, I am not sure if this is invalid incase of java.
Although, I am not sure if this is invalid incase of java.
(1)
Shantanu said:
1 decade ago
What when we use while (1)?
That time it gives exact output in c++. It is not allowed in java.
That time it gives exact output in c++. It is not allowed in java.
Anjan Chandra Das said:
1 decade ago
In java the condition parameters are "true" and "false" i.e. in Boolean format.
But in C or C++ the positive integers are taken as true like.
If (500) // is taken as true in C language. So, Don't be confuse the feature of java and C.
But in C or C++ the positive integers are taken as true like.
If (500) // is taken as true in C language. So, Don't be confuse the feature of java and C.
(1)
Nashath Nasar said:
1 decade ago
Hi guys,
Option A is the correct answer, because there should be a Boolean expression within the brackets followed by "if". Here is an integer number (Odd) within the brackets. That's why it's compile error.
Option A is the correct answer, because there should be a Boolean expression within the brackets followed by "if". Here is an integer number (Odd) within the brackets. That's why it's compile error.
(1)
Bandana said:
1 decade ago
In line 4. There is no Boolean condition. It just an integer. That's why it is compile error.
Bharathraj said:
1 decade ago
How it is a compile error?
(1)
Surajit said:
1 decade ago
In C programming language while(1){} is a infinite loop. But in java while(1){} is a compilation error because in condition it expects either true of false(both keyword). So, the same c program equivalent code in java is while(true){} which represents infinite loop.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers