You are on page 1of 6

Programming

Specimen Questions for Assessment Test 1


The following questions are based on the learning material of:
the lecture notes: chapters 1 and 2, and the Java Supplementary Material 1;
the Barnes & Kolling book "Objects First with Java": chapters 1 and 2;
the Worksheets 1 & 2.
Q1 is to test your knowledge of class, object and method identifiers.
Q2 is to test your knowledge of operators, expressions (including automatic type promotion),
assignment statements (including assignment conversion) and explicit type casting.
Q3 is to test your knowledge of important concepts. An example question is attached.
Q4 is designed to test your understanding of boolean expressions and variables. An example
question is attached.
Q5 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 attached
and answers will be given in workshops.

Dr D E Newton 1 23 March 2019


Programming

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

(a) public class Big Circle

(b) public class student

Field variables

(c) public int SmallNumber;


(d) private String "hello";

Local variables
(e) int $100;
(f) float myfirstvariable;

Method headers
(g) private int get%()
(h) private void testing(int)

Dr D E Newton 2 23 March 2019


Programming

Q2) Consider the following declarations.

int sum1, sum2;


float number;
double total;

Initially the values of the variables are as follows:


sum1 = 10 number = 2.5f
sum2 = 20 total = 10.25d

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.

a) number = sum1; ............


b) sum2 = (int) total; ............
c) number = sum1 / sum2; ............
d) sum1 = number * 4; ............
e) total = number; ............
f) number = sum2 % 2; ............
g) sum1 = (int) (sum2 / 3.0f); ............
h) number = (float) sum1 + 2.0; ............

etc

Dr D E Newton 3 23 March 2019


Programming

Q3) a) What defines the state of an object ?

b) Where do objects store data ?

c) In a method header, what does the word public signify ?

d) What is the purpose of a formal parameter of a method ?

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.

(iii) Two methods cannot have the same signature.

(iv) The arithmetic remainder operator (%) can only be used with whole numbers.

etc

Dr D E Newton 4 23 March 2019


Programming

Q4) Given the following assignments


i = 1; j = 3; k = 5;
bool1 = true; bool2 = false;
indicate T (=True) or F (=False) for each of the boolean expressions (a)-(h)
below. NOTE: you must also indicate any intermediate values that are used in the final
calculations, as in the following example.

F T
( F  F ) T
( i == j ) || bool2 || ( j != k) T

a) ! ( i + j >= 4 )

b) (i > 5) || (i < 5)

c) ( i != j ) || !bool2

d) ( i + j > k ) && ( k > i )

e) (j <= 7) && (j >= 7)

f) ( i + j < k ) && bool1 || (j == k)

g) ( i + j < k ) || bool1 && (i == j)

Dr D E Newton 5 23 March 2019


Programming

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.

Dr D E Newton 6 23 March 2019

You might also like