File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Flow Control Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Flow Control" Watch "Flow Control" New topic
Author

Flow Control

Deepak Jindal
Ranch Hand

Joined: Nov 16, 2000
Posts: 30
Q. Examine the following code. It says the answer to be c). Please explain it to me. Thanx.
public class Calc {
public static void main (String args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
System.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
System.out.println("Total " + total);
}
}
Does this code:
A. Produce a runtime error
B. Produce a compile time error
C. Print out "Total 0"
D. Generate the following as output:
i = 0 : j = 10
i = 1 : j = 9
i = 2 : j = 8
Total 30
Please select the most appropriate answer.
natchit
Greenhorn

Joined: Jan 16, 2001
Posts: 29
C
Sivalingam Sivasuthan
Ranch Hand

Joined: Jan 12, 2001
Posts: 75
hi Deepak Jindal:
That for loop never get execute as the codition is not getting evaluated to true(total>30). so the answer will be c.
Siva.


- Siva<br /> Sun Certified Programmer for the Java™2 Platform
Priya Kannan
Ranch Hand

Joined: Jan 19, 2001
Posts: 72
Hi Deepak,
Answer is "Total 0" b'cos the condition "total > 30" in the if statement fails the first time itself.Hey...you gotta read the Qns properly man!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flow Control