EECS2030 Sample Test 1


/**
 * This utility class converts newtons to dynes.
 * 
 * @author Franck van Breugel
 */
public class NewtonToDyne 
{
    private NewtonToDyne() {}
    
    /**
     * Number of dynes per newton. 
     */
    public static final int DYNES_PER_NEWTON = 100000;
    
    /**
     * Converts the given number of newtons to the corresponding number of dynes.
     * 
     * @param newton the number of newtons.
     * @pre. newton >= 0 && newton <= 100
     * @return the number of dynes corresponding to the given number of newtons.
     */
    public static int convert(int newton)
    {
	return newton * NewtonToDyne.DYNES_PER_NEWTON;
    }
}

Multiple choice question

Question 1 Consider the following code snippet.
int a = 0;
int b = 1;
Utility.swapper(a, b);
System.out.printf("%d %d", a, b);

Which of the following statements is correct?

A. The above snippet prints the string "1 0".
B. The above snippet prints the string "0 1".
C. What the snippet prints depends on the body of the swapper method.
D. None of the above.

Explanation: The method cannot change the values of a and b because Java uses pass by value to transfer arguments to a method. In the code above, the method receives the values 0 and 1 as inputs (it does NOT receive the variables a and b as inputs). The method has no access to the variables a and b, and therefore, cannot change the values of a and b.