Java Programming - Flow Control - Discussion
Discussion Forum : Flow Control - General Questions (Q.No. 4)
4.
public class While
{
public void loop()
{
int x= 0;
while ( 1 ) /* Line 6 */
{
System.out.print("x plus one is " + (x + 1)); /* Line 8 */
}
}
}
Which statement is true?Answer: Option
Explanation:
Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.
A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.
Discussion:
14 comments Page 1 of 2.
Sundar said:
1 decade ago
I got the following error while compiling the above code.
--------------------------------------------------------------
While.java:6: incompatible types
found : int
required: boolean
while ( 1 ) /* Line 6 */
^
1 error.
--------------------------------------------------------------
Therefore, the given answer is correct.
--------------------------------------------------------------
While.java:6: incompatible types
found : int
required: boolean
while ( 1 ) /* Line 6 */
^
1 error.
--------------------------------------------------------------
Therefore, the given answer is correct.
Kalyan said:
1 decade ago
Here class name is 'While' and starts with caps 'W' not with 'w'. So we can use While as class name. We should not use while keyword as class name.
Because keywords start with small letters and they are reserved in java. We have used caps 'W' in the given program so it is not matched with java keyword i.e. while.
Because keywords start with small letters and they are reserved in java. We have used caps 'W' in the given program so it is not matched with java keyword i.e. while.
Santosh said:
1 decade ago
1. While is a key word, here all letters are small letter.
2. While, it is not a key word because it starts with Capital letter.
That's why java is a case sensitive programming language.
2. While, it is not a key word because it starts with Capital letter.
That's why java is a case sensitive programming language.
Chandan kumar barnwal said:
8 years ago
Simple, while loop can have the only condition that will be true or false,
1 means true
0 means false, it accepts in C /C++, not in java
Java accept only true/false values as a condition.
1 means true
0 means false, it accepts in C /C++, not in java
Java accept only true/false values as a condition.
(2)
Anjaneyulu said:
1 decade ago
According to java naming rules keywords are not used for class name but in this Example your used while, in java while is a key word, but your not given the that error by.
Arun said:
1 decade ago
Answer is D.
Because while loop accepts boolean but not Integer as an expression. So the error will be in line no.6 only. Remaining code is perfect.
Because while loop accepts boolean but not Integer as an expression. So the error will be in line no.6 only. Remaining code is perfect.
M.SIDDHARTHA said:
1 decade ago
while is a keyword in JAVA and we can't assign a keyword to a class and give a satisfactory explanation for it.
Naveen pandey said:
1 decade ago
To being a keyword the word must be start with capital letter. So, the use of While for class name is correct.
Naveen pandey said:
1 decade ago
To being a keyword the word must be in small letter. So the use of While will not be use as a keyword.
Sanket said:
1 decade ago
no, 'while' will take boolean values!
Syntax:
while(condition)
{
//block of statements
}
Syntax:
while(condition)
{
//block of statements
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers