Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Java Questions (http://www.go4expert.com/forums/java-questions-t24755/)

bentuae 27Jan2011 12:04

Java Questions
 
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[i]);
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

virxen 27Jan2011 12:57

Re: Java Questions
 
part1 ---->nothing
part2 ----->8
part3 ---->12

bentuae 27Jan2011 13:24

Re: Java Questions
 
thanks alot :)

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

bentuae 27Jan2011 13:56

Re: Java Questions
 
please ,,,,,,,,,,,,,, some one help i need to finish it today ><

alpha34 27Jan2011 14:13

Re: Java Questions
 
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:

virxen 28Jan2011 03:10

Re: Java Questions
 
Code:

1)y = x - y;

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
                                  result ----->00 11 21 32 42

Code:

2)y = y+x;

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
                                  result----->00 11 23 36 410

Code:

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

  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
                                            result----->02 14 27 310 413

Code:

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

  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
                                            result----->11 34 59

Code:

5)      if (y < 5) {
            x = x +1;
            if (y < 3) {
                x = x -1;
            }
        }
        y = y -2;

  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
                                            result----->0-2 1-4 2-6 3-8 4-10


are you sure that you gave us the right codes?

alpha34 28Jan2011 11:09

Re: Java Questions
 
ya i even felt the same for 3rd and 5th sniplet....they give some different output...

virxen 29Jan2011 06:47

Re: Java Questions
 
Quote:

Originally Posted by alpha34 (Post 78422)
ya i even felt the same for 3rd and 5th sniplet....they give some different output...

for me it should be

Code:

      if (y < 5) {
            x = x +1;
            if (y < 3) {
                x = x -1;
            }
        }
        y = y+2;

matches-----> 4)02 14 36 48


All times are GMT +5.5. The time now is 15:29.