DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Andy Guest

Help!!!!!!!!!!!

Can someone explain the following questions from a mock java exam.

1.To what value must result be set for the loop to execute 2 times:

int num = 23;
int result = ?;

while((num>1)&& (result<32)) {
num = num/2;
result= result + 1
}

The answer that is on the answer sheet is 29, can someone explain how?

2.The following code produces which sequence of numbers?

boolean t;
int a = 0;
int b = 1;
int c = 1;
for(int n = 0; a < 25; n++) {
t = false;
for (int j = a ; j < a + 4; j++) {
if (a%2==0) t = true;
if (t) system.out.println(j);
}
c = a;
a = b;
b = c+b;
}

can some explain how they got the sequence of numbers : 0,1,2,3,2,3,4,5,8,9,10,11
?

3.In the following problem what is printed?

int c = 0;
int d = 23,
do {
c+=3;
d-=1;
}while(c<10 && d!=20);
system.out.println(c+d);

The answer on the sheet is 29, but I get 25?, can someone explain how they
get 29?

4.Nothing to do with java but this is part of the mock paper.

For the Backus-Naur Form (BNF) grammar which of the following is an INVALID
string?

<string> ::=a<A>
<A> ::=b<A>|a<B>|<C>
<B> ::=a<B>|<C>
<C> ::=b

The answer that they give is aaba, can anyone explain how they got that answer?.

2. Peter Seymour Guest

Re: Help!!!!!!!!!!!

1.
The answer is 30 the num integer is misleading (well as is the whole piece
of code)

2.
It does.
It's so convoluted you just have to go through it line by line

3.
The loop executes 3 times
c | d
3 22
6 21
9 20 (d==20 so break)
the result is then 29 not 25

4.
Don't know BNF

Where did you get this stuff?
It looks like someone's gone to great lengths to make code this unreadable
and pointless.

Pete

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise