Quiz6_92932.pdf

May 12, 2014
Java Quiz 6
Sharif University of Technology
Name :
Student Id :
1.
Will the following code result in an exception? If so, what is the exception type? If not, what is
the output?
public class Main
{
public static void main(String[] args)
{
System.out.println(a(10));
}
public static int a(int n)
{
try
{
return a(n - 1) / (n - 1);
}
catch (Exception e)
{
return a(n);
}
}
}
2.
Describe the usage of the following annotations in JUnit:
@Test:
@Before:
@After:
@BeforeClass:
@AfterClass:
3. Consider a method that inputs two matrices and outputs their multiplication:
public int[][] mul(int[][] a, int[][] b)
Write a JUnit test that tests the method.
1
(hint: sizes are important + if the answer is correct for two arbitrary matrices, it’s probably correct
for all)
4. What are the bad smells of the following code?
int[][] a, b;
... // setting the initial value for a and b
for (int c = 0; c < a.length; c++)
for (int d = 0; d < a[0].length; d++)
{
a[c][d] = a[c][d] + a[d][c];
a[d][c] = a[c][d] - a[d][c];
a[c][d] = a[c][d] - a[d][c];
}
for (int e = 0; e < b.length; e++)
for (int f = 0; f < b[0].length; f++)
{
b[e][f] = b[e][f] + b[f][e];
b[f][e] = b[e][f] - b[f][e];
b[e][f] = b[e][f] - b[f][e];
}
2