You are on page 1of 2

1 Identifier in Java is a word that programmers make up to refer to a Java programming elements by

name. They can be a class name, variable name, method name, package name, constant name and
others

(a) State whether the following identifier is valid or invalid and give the reason.
[14 marks]

Identifier Valid / Invalid Reason

Can’t use any other symbols other than


myfirst.cc Invalid
letter,digit, $ and_

total age Invalid Can’t have space

avareage mark Invalid Can’t have space

999number Invalid First character must not begin with a digit

1stNumber Invalid First character must not begin with a digit

Can’t use any other symbols other than


sale-price Invalid
letter,digit, $ and_

Can’t use any other symbols other than


total+hour Invalid
letter,digit, $ and_

for Invalid Reserved word cannot be used as identifier

char Invalid Reserved word cannot be used as identifier

public Invalid Reserved word cannot be used as identifier

while Invalid Reserved word cannot be used as identifier

class Invalid Reserved word cannot be used as identifier

if Invalid Reserved word cannot be used as identifier

private Invalid Reserved word cannot be used as identifier

2. Identify the suitable primitive data type based on statement intable below. [8 marks]

No Statement Primitive Data Type

double
(i) Calculate price after discount.

(ii) Have a default value of 0.0f float

Range of data that can be assigned is up to 7 float


(iii)
significant digits.
(iv) Calculate total visitors of a museum in a day. int

(v) Total item check out from shopping cart. int

(vi) Can be used to represent a true or false condition. boolean

(vii) To store gender selection such as M or F. char


(viii) Store value of average fuel refill in liter. double

3. Identify types of operator. [8 marks]

No Java Expression Types of Operator


(a) Z = a + b ; Arithmetic operator

(b) 6 != 6 ; Relational operator

(c) true || true ; Logical operator

(d) (x - y) % 2 ; Arithmetic operator

(e) if ( x == y) ; Arithmetic operator

(f) false && false ; Logical operator

(g) !a ; Logical operator

(h) 7 > 4 ; Relational operator

4. State the output. [4 marks]

i) false && true ; false

ii) false || true ; true

iii) n=8;

System.out.print ( n * n ) ;

System.out.print ( n + 5 ) ; 1 13

iv) (9 > 10) || ( 7 <= 3) && (!( 5 == 5) && (99 > 10)) ;

false

5. State 3 types of error in programming. [3 marks]

Syntax error, Logic error, runtime error

6. State type of error based on Java code below.

No Java statement Type of errors

(a) System.out.print (“Sum is ” + 7 – 9 ); Logic error

(b) System.out.println( 13 / 0 ); runtime error

(c) scanner comp = new scanner (System.in); Syntax error

You might also like