Click to See Complete Forum and Search --> : Help!!!!!!!!!!!!


Andy
01-12-2001, 08:10 AM
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?.

J. During
01-16-2001, 05:21 PM
I'll do the best I can......


"Andy" <andrew.constable@ntlworld.com> wrote:
>
>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?

The reason they got 29 is because the way the loop is structured. By setting
the value of the int variable num to 23 and the result to 29, the loop runs
in the following sequence:

1. num=23 so it is greater than 1; result=29 so it is less than 32. Therefore
the conditions of the loop are satisfied and it runs the code inside the
loop block. We divide num by 2 to get 12.5 and add 1 to result to get 30.
The loop then starts the first time

2. The second time through (but only the first time through the loop), num=12.5
and result=30. Again both conditions are met. num becomes 6.25 and result=31

3. The third time through (only the second time through the loop), num=6.25
and result=30. Again both conditions are met. num becomes 3.125 and result
becomes 32.

4. It exits the loop now after "looping" twice because result is now equal
to 32. Remember it tested the conditions first, ran the loop block and then
did the loop twice.

> 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
> ?
can't help you with this one. Looks like a nested loop inside a loop.

>
> 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?
They got 29 because it runs the code in the following sequence.
1. c=3 because c+=3 is same as c=c+3. d=22 conditions are both met so it
loops through the code

2. c=6, d=21...again both conditions are satisfied...loops again

3. c=9, d=20....the condition on d has been met and c is still less than
10...System prints out 20+9 = 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?.
Haven't even heard of Backus-Naur form grammar....sorry.
>

Joseph A. Woo
02-07-2001, 12:15 PM
The answer is wrong and must be a publishing mistake. The loop is generally
considered the code within the brackets of a for, do/while or while loop.
That code executes 3 times if result is initialized to 29.

If someone makes the argument that the first time through the loop wasn't
actually a loop, that is rediculous and proably came from some academic...technically,
the jump instruction in this case would be called three times, therefore
three loops.

The published answer is flat wrong.

Good luck,
Joseph A. Woo
Alliance Capital Management, LP
Assistant Vice President
Internet Software Engineering

"Andy" <andrew.constable@ntlworld.com> wrote:
>
>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?.
>
>
>