12
TTTK2934ITTTK2933 Fill in the blanks. a. Complete the program so that it writes "Good luck" on the computer monitor. class Bye { public static void main ( String [J args ) t System.out.pringln(" %od l'{ck "); l )' ) b. The source file that the above program is saved in should be called mqln Pll'r ' -- c. To compile the above program, enter this command into the DOS window: d. The compiler will create a file containing be named , This file will e. To run the b1'tecodes, enter this command into the DOS window: t o^Plle' f. The program that interprets the bytecodes on your hardware is called the Java {ile- g. The vo'iJ method is where a program starts running. I - (8 marks) 2. Circle the correct answer. a, What ts aJavaprimitive data type? @ n way of representing values that is so useful that it is a fundamental part of the language. B, A crude form of representing numbers, C. The part of Java that is the same as in older languages. D. A data type that cannot be used as part of an object. b. Which of the following irJqT the name of a Java primitive data tlpe? A. int ,/ @ tto.. 'C doubl-e z q$ str:.r,g/

midsem 2010

Embed Size (px)

Citation preview

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 1/12

TTTK2934ITTTK2933

1. Fill in the blanks.

a. Complete the program so that it writes "Good luck" on the computer monitor.

class Bye

{public static void main ( String [J args )

tSystem.out.pringln(" %od l'{ck "); l

)')

b. The source file that the above program is saved in should be called

mqln Pll'r' --

c. To compile the above program, enter this command into the DOS window:

d. The compiler will create a file containing

be named

, This file will

e. To run the b1'tecodes, enter this command into the DOS window:

t o^Plle'

f. The program that interprets the bytecodes on your hardware is called the Java

{ile-

g. The vo'iJ method is where a program starts running.

I -(8 marks)

2. Circle the correct answer.

a, What ts aJavaprimitive data type?

@ n way of representing values that is so useful that it is a fundamental part ofthe language.

B, A crude form of representing numbers,

C. The part of Java that is the same as in older languages.

D. A data type that cannot be used as part of an object.

b. Which of the following irJqT the name of a Java primitive data tlpe?A. int ,/

@tto..

'C doubl-e z

q$ str:.r,g/

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 2/12

TTTK2%4NTTK2933

c. Say that a particular item of data does NQT use a prirnitive data t1pe. What must

it be?

@ a" object.

B. Anumber.C. A literal.

D. A boolean.

d. What does the following program output?

class Q

tpublic staLic void main ( String I{

int, unitCost = 8;int items - 5;SysLem. ouL . println ( " total cost : Ir

)

args )

+ (unitCost*items) ) ;

total cost: + 40 xtotal cost: 8x5 *total cost: 40

"total cost: " 40 X

What is the value of the following expresslon:

(2 - 5) / 2 + 9

ciD. 10

What is the result of evaluating the following expression?( L/2 + 3.5 ) * 2.0 j.5

A. 8.0

8.8

@toD.0

g. What attributes do all real world objects have? /(n) OUiects have identity, state, and behavior. /Y OUi..ts have state and behavior. \,/C. Objects have size and weight.

D. Objects have existence.

i

A.

ee.

I\

r "r. aJ

Y,t , //

/V

,r^--rG)

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 3/12

TTTK2934ITTTK2933

3. a) Given the following Testl- ciass:

public class Testl {

public static void main(StringtJ args) t

in1 (1 o :^ehl !a+Cz'')

i

i_ i// varjable declaration

System. out .print ( " \n\d" ) a ri),'u

*tt"li^n i:fos ) i<zLl") l++)

if (ieo3 ==0lli%4== 0)

Err^..o^f.f/,n*rn

,'or,1'rl,o,,rr*,

! ,_rro In,l,t*Ir",J

)::

\

)

Fill in the box with appropriate java statements so that the program can display all

the numbers divisibie by 3 or 4 from 105 to 210, with 5 numbers per iine.

(6 marks)

t,7 ,.xt" ,

t t. r'y'/'t .-1

/

int total = 0;for (inL i = 1; i<20;

if (i z 4 r= o)Lotal +- 1;

continue;

for

b) Given the following program segment:

lt

t,

i --\ I

,-y'-t f."-,+t

5/

-1-u.)

/rl/')il y' .,',.

---t+', ttj

I:-1L

)

System. out.

print 1n(

" Total =rr

+ Lotal ) ;

i. What is the output for the above program? (3 Marks)

T-+o,t r 15

,l

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 4/12

TTTK2934ITTTK2933

ii. Convert the above f or loop statement to a while loop. (7 Marks) (

)

t")in1 {oial - o ,nf (, tol

i,. {WAiA

ir

?oi

i =t )

,,,

Ii

i

-t'ilt lf <

4r i) 1i<>)Lci % + !"")

*ta1f= | )

Co nf,n^<_ )

'Y

5

4. Answer the following questions by circling the correct answer or filling in the blanks,

a) Does the return statement in the following method cause syntax error?

public static void methodX(int x, int y) {

return x+y;

b) No(1 marks)

U) Idenrqffy and correct the errors in the following program:

pubtic class Testipublic static void main(String[] args) {

nPrintln ( "Wel-come Lo Java Programming I ",4) ;

)

public static void nPrintln (String msg) {

for (int i=0; i<n; i++)

System. out.println (msg) ;

return t,rue;

,/l

'l '-tlri

i

@

)

Answer:

Errors:

*i

, (6 marks)

i. nPd^fl^ a^J JA,, /d te tttr,-, - o-f. 1rlnflq ' .'t

(" Wn/."-o {u Toun froqrrru^;n, ! " +

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 5/12

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 6/12

TTTK2934/TTTK2933

Write a method that computes and retums the sum (in double) of the values in an

array of doubles. The method should have one argument of 6pe double array and

retum double value. (Hrnt: Use the length athibute to obtain the size of an array).

(5 marks)Answer:

,-bt',,- l.-ari. V ( d""6h (Jx ) t' J-t#l fqn= e

4or (:"i ia- ; I 1 X. /ei3t{ ) -e it+ ) .

t't jrnn * -- /,ti7 ;9,

JJ,rn^.--1. y:^sfn (" srni' S -g..rm ) ;€f*rn d"-Ltq X LI I ;

3 'i{

5. a) i. Name two attributes of a LaserPrinter object.

OnL^l*.^

b) Circle the correct answer

i. Object behaviour can affect object state.

ii. Object state can affect object behaviour

iii. Objects should reveal all of its information.

iv. An object only responds to messages that it understands

v.An object is created

using creat e keylvord

0#L^t1"^

ii. Name three messages (methods) that can be sent to a Laserprinter object(NOT including the constructors and getlset messages).

Lare.Pr{.^t". s*!tuhon ( )

Lorw fr!^+?, sJ, toh '{l ( )

(5 marks)

@rutr"FilD/rat'.

rrue/6D\

@nutt.

r*./@(5 marks)

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 7/12

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 8/12

TTTK2934ITTTK2933

c) Given the interface for class Not,ebook as below:

Notebook Descrintion- notes : String []- count:int

o stores the notes

o number of notes currently in the Notebook+ Notebook 0

* storeNotes(String) : void* numberOflt{otes0 : int

* showNotes(int) : void

. default constructor that initializethenotebook with maximum of 100 notes

o stores a note in the notebooko returns number of notes currently in the

notebook

. show a note, given the note number

//\ )r ;\t rd .-''.91r rg l-aJr.,^

Using class Not,ebook above, write a Javajlgglry_s"_ry"nt that creates a

Notebook object, inputs 3 notes and store it in the Notebook, Then display all

the notes with the note number.(6 marks)

ipp-:( ,ow"l-wAl -N i

fu c/n's ilufoA,ok t

futi;, stof,. vi,l r..ii^ ( f r,.i c 1 q11,t ) "(-

f.*^^.. S. = nrv S*n^u. ( SSsd--i^) ;

ft; o,b,c;J

(1-rnl.,1 n l: ^!- ry?ry/-'k t | )turr,6,, l nr-:^Qw N-t'<t'a1' C )irutgw'r- n3:^?w P.ie/,t-A- t)i*t;;;

.)i,=.i,J,"' cft-ilr"i' 'v ,,t,, . " ) ;

Q: fc.^e/+/^t()i

$: S.-w/.(A+ C);

C. t.. nsx+ /"+ ())

tur1Ll';k nl-_ nc- N"fiL,ok (a) ;N"|'L6,"L 4l_ &_tr N"iLL"ok (b) ;/Vaf16"r/< rtJ: ltqu ylrfaL",k g1 .

tjrru-. o*1. p,l^il^ (o nl.St"ruN"t€s ( ) + A2-5,6r<N,tts ( ) +

n3, JroqN,/rr f l) jo)Jtteh'D-r'f,'lntln ( al. ^n-1.,0pfu+qc) t A\..s6nn^*6rr0fp,r<s () +

n3.no^h.)fw,*s()))

SSxo^- o-r, f4^4n C' liryl", ft'q fl"res. '' f nl. sh.pNoter( ) f ,' ,,+

Al. t^o*NLi€€( ) .,1rt"'

+ nS,. Slvg,Nofa"t ) ) ;

I3

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 9/12

TTTK2934ITTTK2933

6. a) The following class definition contains errors in 4 lines. Identify and conect the

lines.

class MyCircle O t

private int x;public int y;protected double

public mycirclex = 0;

Y = 0'O;radius = 0.0;

)

radius;

o{

public inL get.X(int a) {

)<'=a;)

Given the following attributes of class Animation.

private SLring title;private String category;

private char language;private int length;

Write the appropriate constructor that will be

statement:

Animat.ion film;film = new Animation("Toy Story 3",

p< bt[- h+

,,/'omarks)

invoked by the following

llTTil rE Ivt,

ferqn 4 iL

!'b)

r80);(5 marks) (

)

@p^bft. An'ruf,on ( *dt o , Si/g 6, cl.o" . ,/",d ) t

tiilq< a;"/

cat<j"3= b i

l^X^3o= .;

3

f/,.<+e L,t x illucL i,rtti ,,)i@t r"".l,r ,or.d, if"bli. + uga'rctq() {

{=o jJ'zi

AJ!st-o'o iJ

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 10/12

TTTK2934ITTTK2933

Write the definition of method geLTirle O and isr,anguage O used in the

following segment:

System.out.prinLln("TitIe : il + filml.getTitleO ) ;

System.out.print ("Language : ") ;if (filmL. islanguage ( ,E' ) )

System. ouL . println ( "Eng1ish" ) ;

elseSystem. out .pqintln ( "other" ) ;

7. Answer the following questions.

a) Based on the codes below, fill in the ri

public class Car tprivate int tyre = 2;

ght Java keyr,vords.

,/

private Color color = Color.WHfTE;

public Car (int tyre, Color color) {.this Cqr tyre

=tyre

r Xthls cqr color = color,.)

public int geLTyre O {

N!:tqra tyre;

)

public v"iJ printNumofTyre O {

L.yres arer' + tyre) ;

,

t't It e))6, *rJ Setf;tlq() L

pq{qr^ ille ; ,r/

7.b[,. L*Lun obS'5z Ca". tll'

ll ( L" i*5*6e/

(el"l frq:. )

Q.ke r*^a g1n -,

3

System. out.println ('r The number of

(5 marks)

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 11/12

TTTK2934ITTTK2933

b) Circle the correct answer.

These 2 methods can exist in the same class.

public void printArea (double Iengrh){ }public void printarea (double widrh) { }

A class cannot have more than one constructor methods withthe same name and different number of parameters.

iii. Two separate classes can be declared in the same . j ava file.

An instance of a class can be destroyed by calling garbage

collector's method gc O .

v. A programmer must define at least one constructor in a Javaclass definition.

11.

c) Given the following code segment.

public static void main(SLrlng args| ) {

Handphone hpNokia. = n€w Handphone("Nokia");hpNokia. setPrice (1000. O0) ;Handphone hpSony = new Handphone ("Sony'r,2OIO) ;hpNokia. setprice (1500. 50, Co]or. BLACK) ;double t.otalprice = hpNokia.getpriceO +hpSony.getprice O ;

System. out.println ("Total price: il+ tot.al-price) ;

Based on the codes above, create Handphone class that contains declaration ofconstructor methods and object methods that have been used in the codes.Notes: You do not need to write the implementation code of the methods, onlyempty methods are required. However, instance variables (attributes) and localvariables (parameters) can be included where necessary.

(10 marks)

.& c/'ss l.lonJpl"^e ttirl3

^qhe. j

Jo,Ye p/rcu i

Itt Jear '

ft-alz

grl"ale

P/'.tta

'rvat1

True kFalse )\_-/\,r,

-,------<\ ITrue (-False ) ,, /-/'

G** nun. (

ffupurc. \L/

(4 marks)fi

)',,

10

8/3/2019 midsem 2010

http://slidepdf.com/reader/full/midsem-2010 12/12

l,abl'tc llo" dl lo^c

nqMq: Ni

f/,ce " P i

Jeq. =Y j

Coloqr : C i

]

S+l,t ,.5<N 1 J.,6L P1

silJ Y , 9+fu c){

X

yrbl". voiJ

p/tcc =

!

fubl',, J'n6/c.

fQ(qra

3

se-ffi,<P

q)

( J".llr a ) <V

;t#de t )

orlca- jI

ty u/ 'l/

c/qso L+,^l*,a- {,

&u,g no-a ;

&-hc f,te jGl". Lolor -

?'h"' v,it

wptf,trt(J""kE Nq)

3

,.bU

W X.rl,t,e @[(

P"!i. V*lfi"e ( ,/C

3

P'bt,

!

?"1f,- t*dpk* Cf4 l', in+ q) {.

\

TTTK2934ITTTK2933

q

I'SELAMAT MAJU JAYA''