Answers for S&P1 Test 1
held on 9th Feb 05
Q1. Declare a variable (speedOfLight) to store the speed of light (in meters per second) with high precision; initialize it with the value 112000.45.[2]
Ans: double speedOfLight=112000.45;
Q2. Declare and initialize a variable with the identifier gg, datatype float, and initial value 2.55 using a single statement.[3]
Ans: float gg = 2.55;
Q3. You are to represent monetary amounts (i.e. floating-point values consisting of dollars, a decimal, and cents; e.g. 2.19) using whole number datatypes (not real-valued datatypes). How would you do this? [4]
Ans: I would have two ints: one for dollars and one for
cents or 2.19 as 219 cents. I could then use the discrete datatypes int or long
to represent any dollar amount.
Q4. Which of the following choices
is correct, A, B or both: [5]
A. The range of a double datatype is greater than a float
B. The precision of a double datatype is greater than a float
Ans: Both are correct
Q5. Write a simple expression that typecasts a byte datatype with the identifier bib into a char datatype with the identifier cic. [6]
Ans: cic = (char) bib;
Q6. Write a one-line expression that tests whether a previously declared double variable with the identifier value does not equal 3.14.[5]
Ans: ( value != 3.14 )
Q7. Write a method with the following signature that returns the sum of both parameters: [7]
public double sum(int i, int j) {
// your code goes here
}
Ans: public double sum(int i, int j){
return i + j;
}
Q8. Write a method with the
following signature that returns true if the parameter is equal to 3 or 4,
otherwise false: [7]
public boolean ttt(int number) {
// you code goes here
}
Ans: public boolean ttt(int number){
return number= =3 || number= =4;
}
Q9. Write a method with the
following signature using if-else statements.
public void registrar_menu(int i);
The method does the following:
Displays “Add a course” if the
parameter equals 1
Displays “Drop a course” if the
parameter equals 2
Displays “Withdraw” if the
parameter equals 3
Displays “Error” if the parameter
equals anything else. [13]
public void registrar_menu(int i){
// your code should go here
}
An answer:
public void
registrar_menu(int i) {
if (i = = 1)
System.out.println(“Add
a course”);
else if (i = = 2)
System.out.println(“Drop
a course”);
else if (i = = 3)
System.out.println(“Withdraw”);
else
System.out.println(“Error”);
}
Q10. I have declared the following
two methods with the indicated signatures within the same class. [8]
public long time_of_day();
public long time_of_day(double x);
Is this legal Java?
Ans: True
Q11. What is the name of the first method that is called (if it exists) when a class is executed in JDK? [3]
Ans: main
Q12. What is the output produced by
the following fragment of program code: [5]
int number;
number = (1/2)*3;
System.out.println(" (1/2)*3
equals " + number);
Ans: (1/2)*3 equals 0 because 1 / 2 =0 for integers
Q13. Write a method with the
signature:
public void mmm(int n)
with a for loop that will print out
the numbers 1,4,7, 10 ... until the parameter value n is reached. The method
should ensure that the parameter n value is 1 or greater than 1, otherwise
informing the user of their error. [17]
public void mmm(int n){
// your code goes here
}
An ans:
public void mmm(int n){
if (n >= 1){
for (int i = 1; i <= n; i+=3)
System.out.println(i);
}
else
System.out.println("Enter parameter 1 or greater");
}
Q14. Show the exact output that
would be produced when the following program is executed. (If you explain your
reasoning, you might get more partial credit for an incorrect answer.) [15]
public class TestQuestion {
public
static void main(String[] args) {
int x, y;
x = 1;
y = 10;
while (x < y) {
x = x + 3;
System.out.println("x = " + x);
} // end while
} // end
main
} // end class TestQuestion
Ans:
x = 4
x = 7
x = 10