Java Questions

Discussion in 'Java' started by bentuae, Jan 27, 2011.

  1. bentuae

    bentuae New Member

    Joined:
    Jan 27, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    UAE
    hi all

    i need your help to anwer these question :wacky: please help me i will fail if i did not anwer it :cryin:

    Part 1
    int j;
    for(int i=0;i<14;i++) {
    if(i<10) {
    j = 2 + i;
    }
    System.out.println("j: " + j + " i: " + i);
    }
    What is WRONG with the above code?
    1) Integer "j" is not initialized.
    2) Nothing.
    3) You cannot declare integer i inside the for-loop declaration.
    4) The syntax of the "if" statement is incorrect.
    5) You cannot print integer values without converting them to strings.

    Part 2
    int values[] = {1,2,3,4,5,6,7,8};
    for(int i=0;i< X; ++i)
    System.out.println(values);
    Referring to the above, what value for X will print all members of array "values"?
    1) 1
    2) 7
    3) 8
    4) 9
    5) None, since there is a syntax error in the array declaration

    Part 3
    What is the value of k after the following code fragment?

    int k = 0;
    int n = 12
    while (k < n)
    {k = k + 1;}
    1) 0
    2) 11
    3) 12
    4) 13
    5) None

    _________________


    A short Java program is listed below. One block of the program is missing. Your challenge is to match the candidate block of code (on the left), with the output that you’d see if the block were inserted. Not all the lines of output will be used, and some of the lines of output might be used more than once. Draw lines connecting the candidate blocks of code with their matching output.
    Class Test {
    public static void main (String [] args) {
    int x = 0;
    int y = 0;
    while (x < 5) {
    there some thing missing here

    System.out.print(x + “ ” + y + “ ” );
    x = x + 1;
    }
    }
    }
    Candidates:


    1)y = x - y;
    2)y = y + x;

    3)y = y + 2;
    if (y > 4) {
    y = y+1;
    }

    4)x = x + 1;
    y = y + x


    5)If (y < 5) {
    x = x +1;
    if (y < 3) {
    x = x -1;
    }
    }
    Y = y -2;
    Possible output:
    1) 22 46

    2) 11 34 59
    3) 02 14 26 38
    4)02 14 36 48
    5) 00 11 21 32
    6)11 21 32 42 53
    7)00 11 23 36 410

    8)02 14 25 36 47
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    part1 ---->nothing
    part2 ----->8
    part3 ---->12
     
  3. bentuae

    bentuae New Member

    Joined:
    Jan 27, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    UAE
    thanks alot :)

    i will be happy more if any one solve the match qusetion :confused::confused:
     
  4. bentuae

    bentuae New Member

    Joined:
    Jan 27, 2011
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    UAE
    please ,,,,,,,,,,,,,, some one help i need to finish it today ><
     
  5. alpha34

    alpha34 New Member

    Joined:
    Dec 2, 2009
    Messages:
    25
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    student
    Location:
    somewhere in india
    for those match question 1st code gives 5th output
    for 2nd code it gives 7th output
    for 4th code it gives 2nd output
    however for 3rd the output comes as predicted ie 02 14 26 but problem here is that
    y = y + 2; => here y will be 6
    if (y > 4) { =>here condition satisfies and it enter's loop
    y = y+1; => here y becomes 7 coz previously y has value 6...
    }

    but according to the output answer remains 26 but it should be 27 so im bit confused how did tht come but im pretty much sure tht 02 14 26 is the right one for this

    and for 5th
    If (y < 5) { =>intially y=0 so condition satisfied and enters loop
    x = x +1; =>x becomes 1
    if (y < 3) { =>again y=0 and condition is satisfied
    x = x -1;=> here x becomes 0
    }
    }
    Y = y -2;=>here since y=0 thus y becomes -2
    im not sure abt its output coz y's value get's negative.....tht was my assesment abt code.....hope i was helpfull :)but someone plz look into 5th code coz its bit tricky one...:crazy:
     
  6. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    Code:
    [B]1)y = x - y;[/B]
    
    x=0,y=0,enter while
       x=0,y=0-->00,x=1
       x=1,y=1-->11,x=2
       x=2,y=1--->21,x=3
       x=3,y=2--->32,x=4
       x=4,y=2---->42,x=5 
                                      [B]result ----->00 11 21 32 42[/B]
    
    Code:
    [B]2)y = y+x;[/B]
    
    x=0,y=0,enter while
       x=0,y=0-->00,x=1
       x=1,y=1-->11,x=2
       x=2,y=3--->23,x=3
       x=3,y=6--->36,x=4
       x=4,y=10---->410,x=5 
                                      [B]result----->00 11 23 36 410[/B]
    
    Code:
    [B]3)y = y + 2;
    if (y > 4) {
    y = y+1;
    }[/B]
       x=0,y=0,enter while
             x=0,y=2-->02,x=1
             x=1,y=4-->14,x=2
             x=2,y=7--->27,x=3
             x=3,y=10--->310,x=4
             x=4,y=13---->413,x=5
                                                [B]result----->02 14 27 310 413[/B]
    
    Code:
    [B]4)x = x + 1;
       y = y + x;[/B]
       x=0,y=0,enter while
             x=1,y=1-->11,x=2
             x=3,y=4-->34,x=4
             x=5,y=9--->59,x=6
                                                [B]result----->11 34 59[/B]
    
    Code:
    [B]5)       if (y < 5) {
                x = x +1;
                if (y < 3) {
                    x = x -1;
                }
            }
            y = y -2;[/B]
       x=0,y=0,enter while
             x=0,y=-2-->0-2,x=1
             x=1,y=-4-->1-4,x=2
             x=2,y=-6--->2-6,x=3
             x=3,y=-8--->3-8,x=4
             x=4,y=-10---->4-10,x=5
                                                [B]result----->0-2 1-4 2-6 3-8 4-10[/B]
    
    

    are you sure that you gave us the right codes?
     
  7. alpha34

    alpha34 New Member

    Joined:
    Dec 2, 2009
    Messages:
    25
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    student
    Location:
    somewhere in india
    ya i even felt the same for 3rd and 5th sniplet....they give some different output...
     
  8. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    for me it should be

    Code:
           if (y < 5) {
                x = x +1;
                if (y < 3) {
                    x = x -1;
                }
            }
            y = y[COLOR=Red][B]+[/B][/COLOR]2;
    
    matches-----> 4)02 14 36 48
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice