In: Computer Science
JAVA programing language:
What is printed when the following code is executed?
int columns;
int rows;
for(rows = 1; rows < 2; ++rows) {
for(columns = 1; columns < 3; ++columns) {
System.out.print("x");
}
System.out.println():
}
select one:
A)
xx
B)
xxx
xxx
C)
x
x
D)
xx
xx
xx
Answer is (A) -xx
EXPLANATION-
WHEN THE FIRST FOR LOOP EXECUTES THAT IS
//
for(row=1; row<2,++rows)
Then row will get value 1 then the control will go to the condition i.e (row<2) which is true, then the control will come to the next for loop i.e-
for(column =1; column<3;++columns)
Column is initialised with value 1 then it will check the condition (column<3) .since column is having a value 1 then (1<3) i.e the condition is true,then the control will go the next line i.e-
System.out.print("x");
So it will print -x
Then the value of column will incremented to 2, then again it will check the condition (2<3) which is true then again print -x ,
then again the value of column will be incremented to 3 and then check the condition (3<3) which evaluates to false so, the control will jump to the next line which iwill change the line to a new line and then jump to the first for loop and the value of rows will be incremented to 2 and check the condition i.e (2<2) which evaluates to false and the control will go out of that block.
So, the OUTPUT is- xx