Java Reference
In-Depth Information
11
Exam & Solution
The 2-hour exam below gives an overall review of the main concepts of
programming in Java. The four independent exercises are given with their
corresponding solutions.
Solution 11.1 (Mysterious recursive function)
-
Consider the following program which compiles without any error:
public class
MysteriousProgram
{
public static void
display(
int
[] tab)
{
for
(
int
i=0; i
<
tab . length ; i++)
System . out . print ( tab [ i ] +
""
);
System . out . println () ;
}
public static void
swap2(
int
a,
int
b)
{
int
tmp = a ;
a=b;
b=tmp;
}
public static void
swap3(
int
[] tab,
int
i,
int
j)
{
int
tmp = tab [ i ] ;
tab [ i ] = tab [ j ] ;
tab [ j ] = tmp;
}
public static void
mysterious(
int
[] tab,
int
k)
{
for
(
int
j=k; j
<
t a b . l e n g t h ;
j ++)
{
swap3( tab , k ,
j ) ;
display(tab) ;
swap3( tab , k ,
j ) ;
}
}
Search WWH ::
Custom Search