It's not a secret anymore!
The moose likes Mock Exam Errata and the fly likes Q from John Hunt M.E Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Q from John Hunt M.E " Watch "Q from John Hunt M.E " New topic

Q from John Hunt M.E

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
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);
A. Runtime Error
B. Compiletime Error
C. Prints out "Total = 0"

The correct answer is C. I also ran the prog. and the answer did come out to be C. However, I think it should be B for the following reasons:
1. Type of 'j' has not been declared.
2. In the first part of the for loop (int i = 0, j = 10)is incorrect.
Please Explain
Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
This is an acceptable way(htough can lead to confusion) way to declare multiple variables. It can be done inside the declaration section of a for loop or any where you can declare variables
int i=0, j=5, k=3; //accptable
This brings to lightt another requirement, you can not mix declarations in the declaration of a for loop
for(int 1 = 0, j = 10;
int i = 0;
int j = 0;
for(i = 0, j = 10;
int i = 0;
for(i = 0, int j =10;
int j = 0;
for(int i =0, j = 10;

Hope this helps

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
I agree. Here's the link:
subject: Q from John Hunt M.E
It's not a secret anymore!