Discussion
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: A
Correct answer: Compilation fails.
Java requires the condition of an if to be a boolean, and odd is an int. There is no implicit conversion from a number to a boolean, so the C idiom of testing an integer for non-zero does not compile - it must be written if (odd != 0). The parameter x is never used at all, which is a hint that the fragment was never going to behave as its option about odd and even values suggests.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development