Specimen Questions For Assessment Test 1
Specimen Questions For Assessment Test 1
Specimen Questions For Assessment Test 1
Q1) For each of the following sets of declarations, indicate which are
(i) valid Java and conforming to the conventions on identifiers (mark with a tick
);
(ii) valid Java but not conforming to the conventions on identifiers (mark with a
tick and write down suitable corrections to make the declaration conform to
the conventions);
(ii) invalid Java (mark with a cross and write down suitable corrections to make
the declaration both valid and conform to the conventions).
Class headers
Field variables
Local variables
(e) int $100;
(f) float myfirstvariable;
Method headers
(g) private int get%()
(h) private void testing(int)
Examine each of the following Java assignment statements. Which of them lead to a
"loss of precision" compile time error ? For those that are invalid, briefly state the
reason e.g. "cannot assign a double to an int". For those that are valid, state what
value the variable on the left-hand side of the assignment statement will have. For
each, clearly indicate the data type e.g. use 2.0d for a double value, not simply 2.0 or
2; and 2i for an integer value.
Note : consider only the effect of each statement on the initial values specified above.
etc
e) Consider each of the following statements. Indicate which are correct by placing a
tick next to the statement and which are incorrect by placing a cross next to the
statement. Incorrect answers will be penalised.
(i) When objects are being created, no method may be called before the constructor
of the class is called.
(ii) In the Shapes project, a blue triangle and a green triangle are objects from two
different classes.
(iv) The arithmetic remainder operator (%) can only be used with whole numbers.
etc
F T
( F F ) T
( i == j ) || bool2 || ( j != k) T
a) ! ( i + j >= 4 )
b) (i > 5) || (i < 5)
c) ( i != j ) || !bool2
Q5) This question is designed to allow you to demonstrate your coding abilities by
asking you to write a simple method or methods. A selection of the type of question that will
be asked is given below. Work your way through these. Additional small questions may be
asked to check your understanding of the code.
Example 1
Write an accessor method which returns the int value of the field age.
Example 2
Write a method which has an int formal parameter and returns a boolean value according
to the following rule: the method should return the value true if the value of the argument
(i.e. actual parameter) is an even number; otherwise it returns false.
Example 3
Write a method which has two int formal parameters num1 and num2, and which returns the
average of these two values.