DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Help!!!!!!!!!!!

  1. #1
    Andy Guest


    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 {
    }while(c<10 && d!=20);

    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> ::=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. #2
    Peter Seymour Guest

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

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

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

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

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.