Scjp2 Questions
Scjp2 Questions
To
Sun Certified Programmer for Java 2
platform (Exam 3!"!2#$
Document version control
Date Author Version Comment
12/09/200
1
Ashraf Fouad 1.0 beta Creation
Correction:
(2.1): missing j ariab!e" o#erators $ere not
correct
(2.%): s1 & '()*+ instead of ',es+
2-/01/200
2
Ashraf Fouad 1.0 fina! .e!eased
Note To Holders:
/f ,ou receie an e!ectronic co#, of this document and #rint it out" #!ease $rite ,our name on
the e0uia!ent of the coer #age" for document contro! #ur#oses.
/f ,ou receie a hard co#, of this document" #!ease $rite ,our name on the front coer" for
document contro! #ur#oses.
Contributors:
My site Visitors
Chapter 1:
Language fundamentals
1.1) 1hich of the fo!!o$ing !ines are a!id dec!arations2 314 3%4
(a) char a = \u0061;
(b) char \u0061 = a;
(c) ch\u0061r a = a;
(d) char a = (char) 65;
1.2) /s an em#t, fi!e a a!id source fi!e2 314
(a) 5rue.
(b) Fa!se.
1.%) 1hich of these are a!id dec!aration of the main() method2 314
(a) static void main(String args[]) {! " !#
($) %u$&ic static int main(String args[]) {! " !#
(c) %u$&ic static void main(String args) {! " !#
(d) 'ina& static %u$&ic void main(String[] argum(nts) {! " !#
(() %u$&ic int main(String args[]) int argc) {! " !#
(') %u$&ic void main(String args[]) {! " !#
1.6) 1hich one of the fo!!o$ing are not a!id character contants2 374
*e!ect an, t$o.
(a) char c & 89u000018 :
(b) char c & 891018:
(c) char c & ;<:
(d) char c & 8910018 :
Answers:
1.1) (a)" (b)" (c)" (d)
A!! are a!id dec!arations. 5he 9uxxxx notation can be used an,$here in the source to
re#resent =nicode characters" and a!so casted integer to a char #rimitie t,#e.
1.2) (a)
A!though nonsensica!" an em#t, fi!e ia a a!id source fi!e. A source fi!e can contain an
o#tiona! #ac>age dec!aration" an, number of im%ort statements and an, number of c!ass
and interface definitions.
1.%) (d)
A a!id dec!aration of the main() method must be %u$&ic and static" hae void as
return t,#e and ta>e a sing!e arra, of String ob?ects as arguments. 5he order of the
static and %u$&ic >e,$ords is irre!eant. A!so" dec!aring the method 'ina& does not
affect the method@s #otentia! to be used as a main() method.
1.6) (a)" (d)
(ou cannot use fie digits after 9u.Achar c & ;<A is a!id because it $i!! ta>e it as ascii
a!ue. 891018 is re#resenting a octa! a!ue $hich is e0uia!ent to ;< $hich is a!id but
8910018 is not a!id as its decima! a!ue is <1% and ,ou can gie on!, those a!ues $hich
re#resent 0 to 2<< in decima!.
Chapter 2 :
Operator and assignments
2.1) 1hich statements about the out#ut of the fo!!o$ing #rogram are true2 314
%u$&ic c&ass *ogic {
%u$&ic static void main(String args[]) {
int i = 0;
int j = 0;
$oo&(an t = tru(;
$oo&(an r;
r = (t ++ 0,(i-=1));
r = (t ++ 0,(i-=.));
r = (t ++ 0,(j-=1));
r = (t // 0,(j-=.));
S0st(m1out1%rint&n( i - 2 3 - j );
#
#
(a) 5he first digit #rinted is 1.
(b) 5he first digit #rinted is 2.
(c) 5he first digit #rinted is %.
(d) 5he second digit #rinted is 1.
(e) 5he second digit #rinted is 2.
(f) 5he second digit #rinted is %.
2.2) 1hich statements about the out#ut of the fo!!o$ing #rogram are true2 3;4
%u$&ic static void main(String args[])
{
int i =0;
i = i--;
S0st(m1out1%rint&n(i);
#
(a) 0 is #rinted.
(b) 1 is #rinted.
2.%) 1hich statements about the out#ut of the fo!!o$ing #rogram are true2 314
%u$&ic c&ass 45ua&6(st {
%u$&ic static void main(String args[]) {
String s1 = 274S3;
String s. = 274S3;
i' ( s1 == s. ) S0st(m1out1%rint&n(2(5ua&3);
String s8 = n(9 String(274S3);
String s: = n(9 String(274S3);
i' ( s8 == s: ) S0st(m1out1%rint&n(2s8 (5 s:3);
#
#
(a) 'e0ua!+ is #rinted" 's% e0 s6+ is #rinted.
(b) 'e0ua!+ is #rinted on!,.
(c) 's% e0 s6+ is #rinted on!,.
(d) Bothing is #rinted.
2.6) 1hat ha##ens $hen ,ou tr, to com#i!e and run the fo!!o$ing code2
%u$&ic c&ass 45ua&s6(st {
%u$&ic static void main(String args[]) {
char ; = \u0005;
i' ( ; == 0<0005* )
S0st(m1out1%rint&n(245ua&3);
(&s(
S0st(m1out1%rint&n(2=ot 45ua&3);
#
#
(a) 5he com#i!er re#orts '/na!id character in in#ut+ in !ine %
(b) 5he #rogram com#i!es and #rints 'Bot e0ua!+.
(c) 5he #rogram com#i!es and #rints ')0ua!+.
2.<) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run the fo!!o$ing code2 3-4
%u$&ic c&ass ;s{
int i = 10;
int j;
char >= 1;
$oo&(an $;
%u$&ic static void main(String argv[]){
;s a = n(9 ;s();
a1am(thod();
#
%u$&ic void am(thod(){
S0st(m1out1%rint&n(j);
S0st(m1out1%rint&n($);
#
#
(a) Com#i!ation succeeds and at run time an out#ut of 0 and 'a&s(.
(b) Com#i!ation succeeds and at run time an out#ut of 0 and tru(.
(c) Com#i!e time error $ is not initia!ised.
(d) Com#i!e time error > must be assigned a char a!ue.
2.;) Cien the fo!!o$ing code $hat $i!! be the out#ut2 3-4
c&ass ?a&@o&d {
%u$&ic int i = 10;
#
%u$&ic c&ass A$Barm {
%u$&ic static void main(String argv[]) {
A$Barm o = n(9 A$Barm();
o1am(thod();
#
%u$&ic void am(thod(){
int i = CC;
?a&@o&d v = n(9 ?a&@o&d();
v1i=80;
anoth(r(v)i);
S0st(m1out1%rint&n(v1i);
# 4nd o' am(thod
%u$&ic void anoth(r(?a&@o&d v) int i){
i = 0;
v1i = .0;
?a&@o&d vh = n(9 ?a&@o&d();
v = vh;
S0st(m1out1%rint&n(v1i- D D-i);
#4nd o' anoth(r
#
(a) 10"0"%0
(b) 20"0"%0
(c) 20"99"%0
(d) 10"0"20
2.-) Dere are three #ro#osed a!ternaties to be used in a method to return 'a&s( if the ob?ect
reference < has the nu&& a!ue. 1hich statement $i!! $or>2 3%4
(a) i' ( < == nu&& ) r(turn 'a&s(;
(b) i' ( <1(5ua&s(nu&&) ) r(turn 'a&s(;
(c) i' ( < instanc(o' nu&& ) r(turn 'a&s(;
2.7) .1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass E1
. {
8 %u$&ic static void main(String arg[])
: {
5 int a[]={.).#;
6 int $=1;
F a[$]=$=0;
G S0st(m1out1%rint&n(a[0]);
C S0st(m1out1%rint&n(a[1]);
10 #
11 #
(a) Com#i!e time error at the !ine no. <.
(b) .un time error at the !ine no. <.
(c) Erogram com#i!es correct!, and #rint 2"0 $hen eFecuted.
(d) Erogram com#i!es correct!, and #rint 0"2 $hen eFecuted.
2.9) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 %u$&ic c&ass E:
. {
8 %u$&ic static void main(String[] args)
: {
5 $oo&(an t1 = tru() t. = 'a&s() t8 = t1;
6 $oo&(an t = 'a&s(;
F t ++= (t1 // ( t. ++ t8));
G S0st(m1out1%rint&n(t);
C #
10 #
(a) Erogram com#i!es correct!, and #rint true $hen eFecuted.
(b) Erogram com#i!es correct!, and #rint fa!se $hen eFecuted.
(c) Com#i!e time error.
(d) .un time error.
2.10) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass ;;{#
. c&ass HH (<t(nds ;;{#
8 c&ass E6
: {
5 %u$&ic static void main(String arg[])
6 {
F ;; a=nu&&;
G HH $=(HH)a;
C S0st(m1out1%rint&n($);
10 S0st(m1out1%rint&n($ instanc(o' HH);
11 S0st(m1out1%rint&n($ instanc(o' ;;);
1. #
18 #
(a) Erogram com#i!es correct!, and #rint nu!!"true"fa!se.
(b) Erogram com#i!es correct!, and #rint nu!!"true"true.
(c) Erogram com#i!es correct!, and #rint nu!!"fa!se"fa!se.
(d) Erogram com#i!es correct!, and #rint nu!!"fa!se"true.
(e) Com#i!e time error at !ine no.7 as nu!! a!ue cannot be casted.
(f) .un time error at !ine no. 7 as nu!! a!ue cannot be casted.
2.11) 1hich one of the fo!!o$ing are a!id character contants2 374
*e!ect an, t$o.
(a) char c & 89u000d8 :
(b) char c & 89u000a8:
(c) char c & 89u00008:
(d) char c & 89uface8 :
Answers:
2.1) (c)" (d)
=n!i>e G and H o#erators" the GG and HH o#erators short circuit the ea!uation of their
o#erands if the resu!t of the o#eration can be determined ?ust based on the a!ue of the
first o#erand. 5he second o#erand of the HH o#eration in the #rogram is neer ea!uated.
Variab!e i ends u# $ith the a!ue % $hich is the first digit #rinted" and ? ends u# $ith a
a!ue of 1 $hich is the second digit #rinted.
2.2) (a)
5o eF#!ain u in a more sim#!e $a,...
int I = 10;
int j;
Eost increment :
j = I--;
/n this statement the a!ue of > is 10" G there is a II o#erator a!so
attached $ith it" so it $i!! be assigned to ?" so no$ ? gets 10" but
> becomes as 11 because the II eF#ression comes after the ariab!e"
or if u see > as >II.
EreJincrement :
j = --I;
/n this case" > is haing 10 as a!ue" then the #reJincrement
o#eration is being #erformed" so > becomes 11 in this case" G then it
is being assigned to ?" so no$ ? becomes 11 G > a!so becomes 11.
5he thing $hich u hae must hae noticed in the #ost increment G #reJ
increment is that the a!ue of > is increased b, 1" $here as ? in the
first case becomes 10 G in second case ? becomes 11.
2.%) (b)
5he com#i!er creates one *tring ob?ect for both s1 and s2" thus 'e0ua!+ a##ears. Kut using
the ne$ *tring o#erator t$o distinct ob?ects are created so 's% e0 s6+ does not a##ear.
2.6) (c)
5he com#i!er #romotes ariab!e A to a !ong before the com#arison. 5he com#i!er does not
re#ort '/na!id character in in#ut+ in !ine % because this is correct form for initia!iLing a
char #rimitie. 5herefore" ans$er (a) is incorrect. Kecause (c) is correct (b) cannot
#ossib!, be correct.
2.<) (a)
5he defau!t a!ue for a boo!ean dec!ared at c!ass !ee! is 'a&s(" and integer is 0) and 1 is
$ithin the range of the char and don@t need casting.
2.;) (d)
/n the ca!! anoth(r(v)i);
A COPY of the reference to v is #assed and thus an, changes $i!! be intact after this ca!!.
2.-) (a)
5he ONLY correct $a, to chec> a reference for the nu!! a!ue. Ans$er (b) is incorrect
because if F is nu!!" there $i!! not be an ob?ect $hose e0ua!s method can be ca!!ed. 5his
statement $ou!d cause a Bu!!Eointer)Fce#tion at runtime $hen F is nu!!. Ans$er (c) is
incorrect because on!, a reference t,#e" such as a c!ass" or arra," can be the right o#erand
of the instanceof o#erator.
2.7) (c)
First of a!! a!ue of b $hich is 1 is assigned to arra, indeF then rest of the code $i!! be
eFecuted.
2.9) (c)
Com#i!e time error: 5here is no o#erator !i>e GG&
2.10) (c)
As nu&& a!ue can be casted but do remember it is of no use to cast nu&& a!ue that is $h,
it is $ritten in Mha!id Nugha!8s boo> that $e can not cast nu&& a!ue.*econd!,"instanc(o'
o#erator a!$a,s return 'a&s( for nu&& a!ue.
2.11) (c)" (d)
89u000d8 and 89u000a8 are not a!id as these a!ue do !ine brea>. Bote:89u000a8 is not a!id
een $hen used behind sing!e !ine comments (i.e. //.....)"but offcourse for mu!ti!ine
comment it is a!id. 89uface8 is a!id because $e can use an, character from range 0 J 9 or
A J F or a J f.
Chapter 3 :
Declarations and Access Control
%.1) 1hich of these arra, dec!arations and instantiations are not !ega!2 3143%4
*e!ect a!! a!id ans$ers:
(a) int []a[] = n(9 int[:][:];
($) int a[][] = n(9 int[:][:];
(c) int a[][] = n(9 int[][:];
(d) int []a[] = n(9 int[:][];
(() int[][] a = n(9 int[:][:];
(') int [ ] [ ] a = n(9 int[:][:];
%.2) 1hich of these arra, dec!arations and initia!iLation are not !ega!2 314
*e!ect a!! a!id ans$ers:
(a) int []i[] = {{1).#) {1#) {#) {1).)8##;
($) int i[]= n(9 int[.]{1) .#;
(c) int i[][] = n(9 int[][]{{1).)8#) {:)5)6##;
(d) int i[][] = {{1) .#) n(9 int[.]#;
(() int i[:] = {1) .) 8) :#;
%.%) Cien the fo!!o$ing code" $hich statements can be #!aced at the indicated #osition
$ithout causing com#i!ation errors2 314
%u$&ic c&ass 6hisJsag( {
int %&an(ts;
static int suns;
%u$&ic void ga>(() {
int i;
" ins(rt stat(m(nts h(r( "
#
#
*e!ect a!! a!id ans$ers:
(a) i = this1%&an(ts;
($) i = this1suns;
(c) this = n(9 6hisJsag(();
(d) this1i = :;
(() this1suns = %&an(ts;
%.6) Cien the fo!!o$ing #airs of method dec!arations" $hich of these statements ar true2 314
void '&0(int distanc() {#
int '&0(int tim() int s%((d) {r(turn tim(!s%((d#
void 'a&&(int tim() {#
int 'a&&(int distanc() {r(turn distanc(#
void g&id((int tim() {#
void K&id((int tim() {#
*e!ect a!! a!id ans$ers:
(a) 5he first #air of methods $i!! com#i!e correct!, and oer!oad the method name '&0.
(b) 5he second #air of methods $i!! com#i!e correct!, and oer!oad the method name
'a&&.
(c) 5he third #air of methods $i!! com#i!e correct!, and oer!oad the method name
g&id(.
(d) 5he second #air of methods $i!! not com#i!e correct!,.
(e) 5he third #air of methods $i!! not com#i!e correct!,.
%.<) Cien a c!ass named Koo>" $hich of these $ou!d be a!id definitions of constructors for
the c!ass2 314
S(&(ct a&& va&id ans9(rsL
(a) HooI(HooI $) {#
($) HooI HooI() {#
(c) %rivat( 'ina& HooI() {#
(d) void HooI() {#
(() %u$&ic static void HooI(String args[]) {#
(') a$stract HooI() {#
%.;) 1hich of these statements are true2 314
*e!ect a!! a!id ans$ers:
(a) A!! c!asses must define a constructor.
(b) A constructor can be dec!ared #riate.
(c) A constructor can dec!are a return a!ue.
(d) A constructor must initia!iLe a!! the member ariab!es of a c!ass.
(e) A constructor can access the nonJstatic members of a c!ass.
%.-) 1hat $i!! be the resu!t of attem#ting to com#i!e the fo!!o$ing #rogram2 314
Bu$&ic c&ass M0N&ass {
*ong var;
Bu$&ic void M0N&ass(&ong %aram) { var = %aram; # (1)
Bu$&ic static void main( String args[] ) {
M0N&ass a) $;
; = n(9 M0N&ass(); (.)
H = n(9 M0N&ass(5); (8)
#
#
*e!ect the one right ans$er:
(a) A com#i!ation error $i!! be encountered at (1)" since constructors shou!d not s#ecif, a
return a!ue.
(b) A com#i!ation error $i!! be encountered at (2)" since the c!ass does not hae a defau!t
constructor
(c) A com#i!ation error $i!! be encountered at (%)" since the c!ass does not hae a
constructor acce#ting a sing!e argument of t,#e int.
(d) 5he #rogram $i!! com#i!e correct!,.
%.7) Cien the fo!!o$ing c!ass" $hich of these are a!id $a,s of referring to the c!ass from
outside of the #ac>age n(t1$as(mast(r2 314
%acIag( n(t1$as(mast(r;
%u$&ic c&ass Has( {
"
#
*e!ect a!! a!id ans$ers:
(a) K, sim#!, referring to the c!ass as Has(.
(b) K, sim#!, referring to the c!ass as $as(mast(r1Has(
(c) K, sim#!, referring to the c!ass as n(t1$as(mast(r1Has(
(d) K, im#orting n(t1$as(mast(r1! and referring to the c!ass as Has(
(e) K, im#orting the #ac>age n(t1! and referring to the c!ass as $as(mast(r1Has(
%.9) 1hich one of the fo!!o$ing c!ass definitions is a !ega! definition of a c!ass that cannot be
instantiated2 314
*e!ect the one right ans$er:
(a) c&ass Khost {
a$stract void haunt()
#
($) a$stract c&ass Khost {
void haunt();
#
(c) a$stract c&ass Khost {
void haunt() {#;
#
(d) a$stract Khost {
a$stract void haunt();
#
(() static c&ass Khost {
a$stract haunt();
#
%.10) 1hich of these statements concerning the use of modifiers are true2 314
*e!ect a!! a!id ans$ers:
(a) /f no accessibi!it, modifier (#ub!ic" #rotected and #riate) is gien in a member
dec!aration of a c!ass" the member is on!, accessib!e for c!asses in the same #ac>age
and subc!asses of the c!ass.
(b) (ou cannot s#ecif, isibi!it, of !oca! ariab!es. 5he, are a!$a,s on!, accessib!e $ithin
the b!oc> in $hich the, ardec!ared.
(c) *ubc!asses of a c!ass must reside in the same #ac>age as the c!ass the, eFtend.
(d) Ooca! ariab!es can be dec!ared static.
(e) Pb?ects themse!es don not hae isibi!it," on!, references to the ob?ect.
%.11) Cien the fo!!o$ing source code" $hich one of the !ines that are commeneted out ma, be
reinserted $ithout introducing errors2 314
a$stract c&ass M0N&ass {
a$stract void '();
'ina& void g() {#
'ina& void h() {# (1)
%rot(ct(d static int i;
%rivat( int j;
#
'ina& c&ass M0Ath(rN&ass (<t(nds M0N&ass {
M0Ath(rN&ass(int n) { m = n; # (.)
%u$&ic static void main(String args[]) {
M0N&ass mc = n(9 M0Ath(rN&ass();
#
void '() {#
void h() {#
void I() {i--;# (8)
void &() {j--;# (:)
int m;
#
*e!ect the one right ans$er:
(a) (1)
(b) (2)
(c) (%)
(d) (6)
%.12) 1hich of these statements are true2 314
*e!ect a!! a!id ans$ers:
(a) A static method can ca!! other nonJstatic methods in the same c!ass b, using the
this >e,$ord.
(b) A c!ass ma, contain both static and nonJstatic ariab!es and both static and nonJ
static methods.
(c) )ach ob?ect of a c!ass has its o$n instance of each static member ariab!e.
(d) /nstance methods ma, access !oca! ariab!es of static methods.
(e) A!! methods in a c!ass are im#!icit!, #assed a this #arameter $hen ca!!ed.
%.1%) 1hich of these statements are true2 314
*e!ect a!! a!id ans$ers:
(a) 5ransient ariab!es $i!! not be saed during seria!iLation.
(b) Constructors can be dec!ared a$stract.
(c) 5he initia! state of an arra, ob?ect constructed $ith the statement int a[] = n(9
int[10] $i!! de#end on $hether the ariab!e a[] is a !oca! ariab!e" or a member
ariab!e of a c!ass.
(d) An, subc!ass of a c!ass $ith an a$stract method must im#!ement a method bod, for
that method.
(e) Pn!, static methods can access static members.
%.16) 1hich of the fo!!o$ing are Qaa modifiers2 3-4
(a) %u$&ic
(b) %rivat(
(c) 'ri(nd&0
(d) transi(nt
(e) vagrant
%.1<) 1h, might ,ou define a method as natie2 3-4
(a) 5o get to access hard$are that Qaa does not >no$ about.
(b) 5o define a ne$ data t,#e such as an unsigned integer.
(c) 5o $rite o#timised code for #erformance in a !anguage such as C/CII
(d) 5o oercome the !imitation of the #riate sco#e of a method
%.1;) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run this code2 3-4
%u$&ic c&ass Mod{
%u$&ic static void main(String argv[]){
#
%u$&ic static nativ( void am(thod();
#
(a) )rror at com#i!ation: nativ( method cannot be static.
(b) )rror at com#i!ation nativ( method must return a!ue.
(c) Com#i!ation but error at run time un!ess ,ou hae made code containing natie
amethod aai!ab!e.
(d) Com#i!ation and eFecution $ithout error.
%.1-) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run this code2 3-4
%rivat( c&ass Has({#
%u$&ic c&ass ?is{
transi(nt int i?a&;
%u$&ic static void main(String (&(%hant[]){
#
#
(a) Com#i!e time error: Has( cannot be %rivat(1
(b) Com#i!e time error indicating that an integer cannot be transi(nt1
(c) Com#i!e time error transi(nt not a data t,#e.
(d) Com#i!e time error ma!formed main method.
%.17) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run the fo!!o$ing code2 3-4
%u$&ic c&ass @o%({
%u$&ic static void main(String argv[]) {
@o%( h = n(9 @o%(();
#
%rot(ct(d @o%(() {
'or(int i =0; i ,10; i --){
S0st(m1out1%rint&n(i);
#
#
#
(a) Com#i!ation error: Constructors cannot be dec!ared #rotected.
(b) .un time error: Constructors cannot be dec!ared #rotected.
(c) Com#i!ation and running $ith out#ut 0 to 10.
(d) Com#i!ation and running $ith out#ut 0 to 9.
%.19) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run the fo!!o$ing code $ith the
command !ine Ahe!!o thereA2 3-4
%u$&ic c&ass ;rg{
String[] M0;rg;
%u$&ic static void main(String argv[]){
M0;rg=argv;
#
%u$&ic void am(thod(){
S0st(m1out1%rint&n(argv[1]);
#
#
(a) Com#i!e time error.
(b) Com#i!ation and out#ut of Ahe!!oA.
(c) Com#i!ation and out#ut of AthereA.
(d) Bone of the aboe.
%.20) 1hich of the fo!!o$ing method $i!! not gie ,ou an, error $hen #!aced on !ine no.%2 374
*e!ect one correct ans$er.
1 int(r'ac( i.
. {
8 @(r(
: #
(a) static void a$c();
($) a$stract void a$c();
(c) nativ( void a$c();
(d) s0nchroni>(d void a$c();
(() 'ina& void a$c();
(') %rivat( void a$c();
(g) %rot(ct(d void a$c();
Answers:
%.1) (c)
5he [] notation can be #!aced both before and after the ariab!e name in an arra,
dec!aration. Nu!tidimensiona! arra,s are created b, creating arra,s that can contain
references to other arra,s. 5he statement n(9 int[:][] $i!! create an arra, of !ength 6"
$hich can contain references to arra,s of int a!ues. 5he statement n(9 int[:][:] $i!!
create the same arra," but $i!! a!so create four arra,s" each containing four int a!ues.
.eferences to each of these arra,s are stored in the first arra,. 5he statement int [][:]
$i!! not $or>" because the dimensions must be created from !eft to right. )Ftra s#aces are
not significant.
%.2) (b)" (e)
5he siLe of the arra, cannot be s#ecified as in (b)" (e). 5he siLe of the arra, is gien
im#!icit!, b, the initia!iLation code. 5he siLer of the arra, is neer gien during the
dec!aration of an arra, reference. 5he siLe of an arra, is a!$a,s associated $ith the arra,
instance" not the arra, reference.
%.%) (a)" (b)" (e)
BonJstatic methods hae an im#!icit this ob?ect reference. 5he this reference is not a
norma! reference ariab!e that can be changed in the $a, attem#ted b, statement (c).
5he this reference can be used to refer to both ob?ect and c!ass members $ithin the
current conteFt. Do$eer" it cannot be used to refer to !oca! ariab!es in the $a,
attem#ted b, statement (d).
%.6) (a)" (d)
5he first and third #airs of methods $i!! com#i!e correct!,. 5he second #air of methods $i!!
not com#i!e correct!," since their method signatures do not differ and the com#i!er has
therefore no $a, of diffrentiating bet$een the t$o methods. Bote that return t,#e and the
names of the #arameters are not a #art of the method signatures. Koth methods in the
first #air named '&0 and therefore oer!oad this method name. 5he methods in #air three
do not oer!oad the method name g&id(" since on!, one method has the name. 5he
method name K&id( is distinct from the method name g&id(" as identifiers in Qaa are
caseJsensitie.
%.<) (a)
A constructor does not dec!are an, return t,#e" not een void. A constructor cannot be
'ina&" static or a$stract.
%.;) (b)" (e)
A constructor can be dec!ared #riate" but this means that this constructor can on!, be
used direct!, $ithin the c!ass. Constructors need not initia!iLe a!! the member ariab!es in a
c!ass. A member ariab!e $i!! be assigned a defau!t a!ue if not eF#!icit!, initia!iLed. A
constructor is nonJstatic" and as such it can access direct!, both the static and nonJstatic
members of the c!ass.
%.-) (c)
A com#i!ation error $i!! be encountered in (%)" since the c!ass does not hae a constructor
acce#ting a sing!e argument of t,#e int. 5he dec!aration at (1) dec!ares a method" not a
constructor" since it hae a return t,#e. 5he method ha##ens to hae the same name as
the c!ass" but that is irre!eant. 5he c!ass has an im#!icit defau!t constructor since the c!ass
contains no constructor dec!arations. 5his a!!o$s the instantiation at (2) to $or>.
%.7) (c)" (d)
A c!ass or interface name can be referred to b, using either its fu!!, 0ua!ified name or its
sim#!e name. =sing the fu!!, 0ua!ified name $i!! a!$a,s $or>" but in order to use the
sim#!e name it has to be im#orted. K, im#orting n(t1$as(mast(r1! a&& th( t0%( nam(s
'rom th( %acIag( n(t1$as(mast(r $i!! be im#orted and can no$ be referred to using
sim#!e names. /m#orting n(t1! $i!! not im#ort the sub#ac>age $as(mast(r.
%.9) (C)
A c!ass is uninstantiab!e if the c!ass is dec!ared a$stract. 5he dec!arartion of an abstract
method cannot #roide an im#!ementation. 5he dec!aration of a nonJabstract method must
#roide an im#!emenation. /f an, method in a c!ass is dec!ared a$stract" then the $ho!e
c!ass must be dec!ared a$stract. Definition (d) is not a!id" since it omits the c&ass
>e,$ord.
%.10) (b)" (e)
(ou cannot s#ecif, isibi!it, of !oca! ariab!es. 5he, are accessib!e on!, $ithin the b!oc> in
$hich the, are dec!ared. Pb?ects themse!es do not hae an, isibi!it," on!, the references
to the ob?ect. /f no isibi!it, modifier(#ub!ic" #rotected or #riate) is gien in the member
dec!aration of a c!ass" the member is on!, accessib!e to c!asses in the same #ac>age. A
c!ass does not hae access to members of a su#erc!ass $ith defau!t accessibi!it," un!ess
both c!asses are in the same #ac>age. /nheritance has no conse0uence $ith res#ect to
accessing members $it hdefau!:t accessibi!it,. Ooca! ariab!es acannot be dec!ared static
and cannot be gien an accessibi!it, modifier.
%.11) (c)
5he !ine 2void I() { i--; #3 can be reinserted $ithout introducing errors. .einserting
!ine (1) $i!! cause the com#i!ation to fai!" since M0Ath(rN&ass $i!! tr, to oerride a 'ina&
method. .einserting !ine(2) $i!! fai! since M0Ath(rN&ass $i!! no !onger hae a defau!t
constructor. 5he main() method needs a constructor that ta>es Lero arguments.
.einserting !ine (%) $i!! $or> $ithout an, #rob!ems" but reinserting !ine (6) $i!! fai!" since
the method $i!! tr, to access a %rivat( member of the su#erc!ass.
%.12) (b)
5he >e,$ord this can be on!, used in nonJstatic methods. Pn!, one instance of each
static member ariab!e of a c!ass is created. 5his instance is shared among a!! ob?ects of
the c!ass. Ooca! ariab!es are on!, accessib!e $ithin the !oca! sco#e" regard!ess of $hether
the !oca! sco#e is defined $ithin a static method.
%.1%) (a)
5he transi(nt >e,$ord signifies that the ariab!es shou!d not be stored $hen the ob?ect
are seria!iLed. Constructors cannot be dec!ared a$stract1 )!ements in an unitia!iLed arra,
ob?ect get the defau!t a!ue corres#onding to the t,#e of the e!ements. 1hether the
reference ariab!e #ointing to the arra, ob?ect is a !oca! or a member ariab!e does not
matter. Abstract methods from a su#erc!ass need not be im#!emented b, abstract
subc!ass.
%.16) (a)" (b)" (d)
5he >e,$ord transi(nt is eas, to forget as is not fre0uent!, used" and it is considered in
the access modifiers because it modifies the behaiour of the ob?ect.
%.1<) (a)" (c)
%.1;) (d)
/t $ou!d cause a run time error if ,ou had a ca!! to amethod though.
%.1-) (a)
A to# !ee! (non nested) c!ass cannot be %rivat(.
%.17) (d)
%.19) (a)
(ou $i!! get an error sa,ing something !i>e ACant ma>e a static reference to a non static
ariab!eA. Bote that the main method is static. )en if main $as not static the arra, argv
is !oca! to the main method and $ou!d thus not be isib!e $ithin am(thod.
%.20) (b)
/nterface methods can8t be natie" static" s,nchroniLed" fina!" #riate" or #rotected.
Chapter 4:
Flow Control and !ception handling
6.1) 5he fo!!o$ing method is designed to conert an in#ut string to a f!oating #oint number
$hi!e detecting a bad format. 3%4 3I think factor mst ha!e "een declared as float
mem"er !aria"le or somethin#4
%u$&ic $oo&(an strNvt(String s) {
tr0 {
'actor = O&oat1va&u(A'(s)1'&oat?a&u(();
r(turn tru(;
# catch (=um$(rOormat4<c(%tion () {
S0st(m1out1%rint&n(2Had num$(r 3 - s);
'actor = O&oat1=a=;
# 'ina&&0 {
S0st(m1out1%rint&n(2Oina&&03);
#
r(turn 'a&s(;
#
1hich of the fo!!o$ing descri#tions of the resu!ts of arious in#uts to the method are
correct2
(a) /n#ut & '0.2%6+ R .esu!t: factor & 0.2%6" 'Fina!!,+ is #rinted" tru( is returned.
(b) /n#ut & '0.2%6+ R .esu!t: factor & 0.2%6" 'Fina!!,+ is #rinted" 'a&s( is returned.
(c) /n#ut & nu!! R .esu!t: factor & =a=" 'Fina!!,+ is #rinted" 'a&s( is returned.
(d) /n#ut & nu!! R .esu!t: factor & unchanged" 'Fina!!,+ is #rinted" =u&&Boint(r4<c(%tion
is thro$n.
6.2) Dere is the hierarch, of eFce#tions re!ated to arra, indeF and string indeF errors: 3%4
4<c(%tion
-PP Quntim(4<c(%tion
-PP Rnd(<AutA'Hounds4<c(%tion
-PP ;rra0Rnd(<AutA'Hounds4<c(%tion
-PP StringRnd(<AutA'Hounds4<c(%tion
*u##ose ,ou had a method X that cou!d thro$ both arra, indeF and string indeF
eFce#tions. Assuming that X does not hae an, tr0Pcatch statements" $hich of the
fo!!o$ing statements are correct2
(a) 5he dec!aration for X must inc!ude 'thro9s ;rra0Rnd(<AutA'Hounds4<c(%tion)
StringRnd(<AutA'Hounds4<c(%tion+.
(b) /f a method ca!!ing X catches Rnd(<AutA'Hounds4<c(%tion" both arra, and string indeF
eFce#tions $i!! be caught.
(c) /f the dec!aration for X inc!udes 'thro9s Rnd(<AutA'Hounds4<c(%tion+" an, ca!!ing
method must use a tr0Pcatch b!oc>.
(d) 5he dec!aration for X does not hae to mention eFce#tions.
6.%) 1hich $i!! be the first !ine to cause an error in the fo!!o$ing code2 374
*e!ect one correct ans$er.
1 c&ass Nhar
. {
8 %u$&ic static void main(String arg[])
: {
5 9hi&(('a&s()
6 {
F S0st(m1out1%rint&n(D@(&&oD);
G #
C 9hi&(('a&s()
10 {
11 #
1. do;
18 9hi&(('a&s();
1: do
15 {
16 ;
1F #
1G 9hi&(('a&s();
1C #
.0 #
(a) Oine no. <
(b) Oine no. 9
(c) Oine no. 12
(d) Oine no. 1;
6.6) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 %u$&ic c&ass (<c(%tion
. {
8 %u$&ic static void main(String args[])
: {
5 S0st(m1out1%rint&n(D;D);
6 tr0
F {
G #
C catch(java1io1RA4<c(%tion t)
10 {
11 S0st(m1out1%rint&n(DHD);
1. #
18 S0st(m1out1%rint&n(DND);
1: #
15 #
(a) Com#i!e time error.
(b) Erogram com#i!es correct!, and #rints AAA $hen eFecuted.
(c) Erogram com#i!es correct!, and #rints AAA and ACA $hen eFecuted.
(d) .un time error.
6.<) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 %u$&ic c&ass (<c(%tion
. {
8 %u$&ic static void main(String args[])
: {
5 S0st(m1out1%rint&n(D;D);
6 tr0
F {
G r(turn;
C #
10 catch(4<c(%tion ()
11 {
1. S0st(m1out1%rint&n(DHD);
18 #
1: S0st(m1out1%rint&n(DND);
15 #
16 #
(a) Com#i!e time error in !ine no. 7 as main() method is dec!ared oid.
(b) Erogram com#i!es correct!, and #rints AAA $hen eFecuted.
(c) Erogram com#i!es correct!, and #rints AAA and ACA $hen eFecuted.
(d) Com#i!e time error at !ine no.16 due to statement not reached.
6.;) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 %u$&ic c&ass (<c(%tion
. {
8 %u$&ic static void main(String args[])
: {
5 S0st(m1out1%rint&n(D;D);
6 tr0
F {
G r(turn;
C #
10 catch(4<c(%tion ()
11 {
1. S0st(m1out1%rint&n(DHD);
18 #
1: 'ina&&0
15 {
16 S0st(m1out1%rint&n(DND);
1F #
1G #
1C #
(a) Com#i!e time error in !ine no. 7 as main() method is dec!ared oid.
(b) Erogram com#i!es correct!, and #rints AAA $hen eFecuted.
(c) Erogram com#i!es correct!, and #rints AAA and ACA $hen eFecuted.
(d) Erogram com#i!es correct!, and #rints AAA"AKA and ACA $hen eFecuted.
6.-) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 %u$&ic c&ass (<c(%tion
. {
8 %u$&ic static void main(String args[])
: {
5 S0st(m1out1%rint&n(D;D);
6 tr0
F {
G S0st(m1out1%rint&n(DHD);
C S0st(m1(<it(0);
C #
10 catch(4<c(%tion ()
11 {
1. S0st(m1out1%rint&n(DND);
18 #
1: 'ina&&0
15 {
16 S0st(m1out1%rint&n(DSD);
1F #
1G #
1C #
(a) Erogram com#i!es correct!, and #rints AAA $hen eFecuted.
(b) Erogram com#i!es correct!, and #rints AAA and AKA $hen eFecuted.
(c) Erogram com#i!es correct!, and #rints AAA and ACA $hen eFecuted.
(d) Erogram com#i!es correct!, and #rints AAA"AKA and ACA $hen eFecuted.
Answers:
6.1) (a)" (d)
(b) is $rong because the return a!ue in !ine 6 is used. (c) is $rong because a
Bu!!Eointer)Fce#tion is thro$n in !ine % and is not caught in the method" !ine - is neer
reached.
6.2) (b)" (d)
(b) is correct because eFce#tions obe, a hierarch, ?ust !i>e other ob?ects. Kecause these
eFce#tions descend from Quntim(4<c(%tion" the, do not hae to be dec!ared. 5herefore"
ans$er (d) is correct. 5he significant $ord here is 'must+. Kecause these eFce#tions
descend from Quntim(4<c(%tion" the, do not hae to be dec!ared. 5herefore" ans$er (a)
is incorrect. Ans$er (c) is incorrect for a simi!ar reason" because these eFce#tions descend
from Quntim(4<c(%tion. 5he, do not hae to be caught een if dec!ared b, method X.
6.%) (a)
/t $i!! gie ,ou error for unreached statement.A!! other statements are a!id.
6.6) (a)
(ou cannot use an, chec>ed eFce#tion in a catch b!oc> if it neer thro$n.
6.<) (b)
(ou can use return($ithout an, a!ue) in a method $hich is dec!ared oid.As return is
used it $i!! not #rint ACA but at the same time there $i!! be no error for that.
6.;) (c)
As this time $e hae used fina!!, so it $i!! #rint AAA and ACA.Fina!, $i!! a!$a,s eFecute
irres#ectie of $hether eFce#tion is thro$n or not.
6.-) (b)
*,stem.eFit() $i!! restrict the fina!!, b!oc> to be eFecuted.
Chapter ":
O#$ect Oriented programming
<.1) Assume $e hae the fo!!o$ing code in the fi!e /abc/def/S.?aa:
Oi&(L a$cd('E1javaL
%acIag( d(';
%u$&ic c&ass E {
%rivat( int %rivat(?ar;
int %acIag(?ar;
%rot(ct(d int %rot(ct(d?ar;
%u$&ic int %u$&ic?ar;
#
;nd this cod( is in a$c6(st(r1javaL
Oi&(L a$c6(st(r1javaL
im%ort d('1E;
%u$&ic c&ass 6(st(r (<t(nds E {
E su% = n(9 E();
Su$ su$ = n(9 Su$();
%u$&ic void som(M(thod() {
Oirst) tr0 to r('(r to su%Ts m(m($(rs1
su%1%rivat(?ar = 1; *in( 1
su%1%acIag(?ar = .; *in( .
su%1%rot(ct(d?ar = 8; *in( 8
su%1%u$&ic?ar = :; *in( :
=(<t) tr0 to r('(r to this o$j(ctTs m(m$(rs
su%%&i(d $0 c&ass E1
%rivat(?ar = 5; *in( 5
%acIag(?ar = 6; *in( 6
%rot(ct(d?ar = F; *in( F
%u$&ic?ar = G; *in( G
=(<t) &(tTs tr0 to acc(ss th( m(m$(rs o'
anoth(r instanc( o' 6(st(r1
6(st(r t = n(9 6(st(r();
t1%rivat(?ar = C; *in( C
t1%acIag(?ar = 10; *in( 10
t1%rot(ct(d?ar = 11; *in( 11
t1%u$&ic?ar = 1.; *in( 1.
Oina&&0) tr0 to r('(r to th( m(m$(rs in a
su$c&ass o' 6(st(r1
su$1%rivat(?ar = 18; *in( 18
su$1%acIag(?ar = 1:; *in( 1:
su$1%rot(ct(d?ar = 15; *in( 15
su$1%u$&ic?ar = 16; *in( 16
#
#
;nd this cod( is in a$cSu$1javaL
Oi&(L a$cSu$1javaL
%u$&ic c&ass Su$ (<t(nds 6(st(r {
#
Assume the director," /abc" is in the com#i!er8s COA**EA5D.
1hen ,ou tr, to com#i!e 5ester.?aa there $i!! be seera! com#i!er errors. For each of the
!abe!ed !ines aboe" decide $hether or not a com#i!er error $i!! be generated. 3<4
<.2) Cien the fo!!o$ing !isting of the Uidg(t c!ass: 374
c&ass Uidg(t (<t(nds 6hing(( { (1)
static %rivat( int 9idg(tNount = 0; (.)
%u$&ic String 9=am(; (8)
int 9=um$(r; (:)
static s0nchroni>(d int addUidg(t() { (5)
9=am( = 2R am Uidg(t V 3 - 9idg(tNount; (6)
r(turn 9idg(tNount; (F)
# (G)
%u$&ic Uidg(t() { (C)
9=um$(r = addUidg(t(); (10)
# (11)
# (1.)
1hat ha##ens $hen ,ou tr, to com#i!e the c!ass and use mu!ti#!e Uidg(t ob?ects in a
#rogram2
(a) 5he c!ass com#i!es and each Uidg(t $i!! get a uni0ue 9=um$(r and 9=am( ref!ecting the
order in $hich the 1idgets $ere created.
(b) 5he com#i!er ob?ects to !ine ;.
(c) 5he c!ass com#i!es" but a runtime error re!ated to the access of the ariab!e 9=am(
occurs in the addUidg(t method.
<.%) 5he fo!!o$ing method definition is designed to #arse and return an integer from an
in#ut string that is eF#ected to !oo> !i>e 'nnn"EaramBame.+ /n the eent of a
=um$(rOormat4<c(%tion" the method is to return R1.
%u$&ic int g(t=um (String s) { (1)
tr0 { (.)
String tm% = S1su$string(0) S1ind(<A'())); (8)
r(turn Rnt(g(r1%ars(Rnt(tm%); (:)
# catch (=um$(rOormat4<c(%tion () { (5)
S0st(m1out1%rint&n(2Bro$&(m in 3 - tm%); (6)
# (F)
r(turn W1; (G)
# (C)
1hat ha##ens $hen ,ou tr, to com#i!e this code and eFecute the method $ith an in#ut
string that does contain a comma se#arating the number from the teFt data2
(a) A com#i!er error in !ine ; #reents com#i!ation.
(b) 5he method #rints the error message to standard out#ut and returns R1
(c) A =u&&Boint(r4<c(%tion is thro$n in !ine %.
(d) A StringRnd(<AutA'Hounds4<c(%tion is thro$n in !ine %.
<.6) (our chief *oft$are designer has sho$n ,ou a s>etch of the ne$ Com#uter #arts s,stem
she is about to create. At the to# of the hierarch, is a C!ass ca!!ed Com#uter and under
this are t$o chi!d c!asses. Pne is ca!!ed OinuFEC and one is ca!!ed 1indo$sEC.
5he main difference bet$een the t$o is that one runs the OinuF o#erating *,stem and
the other runs the 1indo$s *,stem (of course another difference is that one needs
constant reJbooting and the other runs re!iab!,). =nder the 1indo$sEC are t$o *ub
c!asses one ca!!ed *erer and one Ca!!ed 1or>station. Do$ might ,ou a##raise ,our
designers $or>2
(a) Cie the goahead for further design using the current scheme.
(b) As> for a reJdesign of the hierarch, $ith changing the P#erating *,stem to a fie!d
rather than C!ass t,#e.
(c) As> for the o#tion of 1indo$sEC to be remoed as it $i!! soon be obso!ete.
(d) Change the hierarch, to remoe the need for the su#erf!uous Com#uter C!ass.
<.<) Cien the fo!!o$ing c!ass definition $hich of the fo!!o$ing can be !ega!!, #!aced after the
comment !ine //Dere 2 3-4
c&ass Has({
%u$&ic Has((int i){#
#
%u$&ic c&ass M0Av(r (<t(nds Has( {
%u$&ic static void main(String arg[]) {
M0Av(r m = n(9 M0Av(r(10);
#
M0Av(r(int i){
su%(r(i);
#
M0Av(r(String s) int i){
this(i);
@(r(
#
#
(a) M0Av(r m = n(9 M0Av(r();
(b) su%(r();
(c) this(D@(&&oD)10);
(d) Has( $ = n(9 Has((10);
<.;) *u##ose ,ou hae t$o c!asses defines as fo!!o$s: 3%4
c&ass ;%Has( (<t(nds A$j(ct im%&(m(nts Qunna$&(
c&ass ;%S(riv(d (<t(nds ;%Has( im%&(m(nts A$s(rv(r
Cien t$o ariab!es created as fo!!o$s:
;%Has( aHas( = n(9 ;%Has(();
;%S(riv(d aS(r = n(9 ;%S(riv(d();
1hich of the fo!!o$ing Qaa statements $i!! com#i!e and eFecute $ithout error2
(a) Qunna$&( rn = aS(r;
(b) Qunna$&( rn. = (Qunna$&() aHas(;
(c) A$s(rv(r o$ = aHas(;
(d) A$s(rv(r o$. = (A$s(rv(r) aHas(;
<.-) *u##ose $e hae an ;%Has( c!ass dec!ared as: 3%4
c&ass ;%Has( (<t(nds A$j(ct im%&(m(nts Qunna$&(
5he fo!!o$ing code fragment ta>es a reference to an ;%Has( ob?ect and assigns it to a
ariet, of ariab!es. 1hat $i!! ha##en $hen ,ou tr, to com#i!e and run this code2
;%Has( aHas( = n(9 ;%Has(();
Qunna$&( aQ = aHas(;
A$j(ct o$j = aQ;
;%Has( < = (;%Has()o$j;
(a) 5he com#i!er ob?ects to !ine 2
(b) 5he com#i!er ob?ects to !ine %
(c) 5he code com#i!es but $hen run thro$s a N&assNast4<c(%tion in !ine 6
(d) 5he code com#i!es and runs $ithout #rob!em.
<.7) *u##ose ,ou hae t$o c!asses defines as fo!!o$s: 3%4
c&ass ;%Has( (<t(nds A$j(ct im%&(m(nts Qunna$&(
c&ass ;%S(riv(d (<t(nds ;%Has( im%&(m(nts A$s(rv(r
Cien t$o ariab!es created as fo!!o$s:
;%Has( aHas( = n(9 ;%Has(();
;%S(riv(d aS(r = n(9 ;%S(riv(d();
1hich of the fo!!o$ing Qaa statements $i!! com#i!e and eFecute $ithout error2
(a) A$j(ct o$j = aHas(; Qunna$&( rn = o$j;
(b) A$j(ct o$j = aHas(; Qunna$&( rn = (Qunna$&()o$j;
(c) A$j(ct o$j = aHas(; A$s(rv(r o$ = (A$s(rv(r)aHas(;
(d) A$j(ct o$j = aS(r; A$s(rv(r o$. = o$j;
<.9) 1hat $i!! ha##en if ,ou attem#t to com#i!e and run the fo!!o$ing code2 364
c&ass Has( {#
c&ass Su$ (<t(nds Has( {#
c&ass Su$. (<t(nds Has( {#
%u$&ic c&ass N4< {
%u$&ic static void main(String argv[]){
Has( $ = n(9 Has(();
Su$ s = (Su$) $;
#
#
(a) Com#i!e and run $ithout error.
(b) Com#i!e time )Fce#tion.
(c) .untime )Fce#tion.
<.10) (ou hae these fi!es in the same director,. 1hat $i!! ha##en $hen ,ou attem#t to
com#i!e and run C!ass1.?aa if ,ou hae not a!read, com#i!ed Kase.?aa 2 364
Has(1java
%acIag( Has(;
c&ass Has( {
%rot(ct(d void am(thod() {
S0st(m1out1%rint&n(Dam(thodD);
#
#
N&ass11java
%acIag( N&ass1;
%u$&ic c&ass N&ass1 (<t(nds Has( {
%u$&ic static void main(String argv[]){
Has( $ = n(9 Has(();
$1am(thod();
#
#
(a) Com#i!e )rror: Nethods in Has( not found
(b) Com#i!e )rror: =nab!e to access #rotected method in base c!ass
(c) Com#i!ation fo!!o$ed b, the out#ut AamethodA
(d) Com#i!e error: *u#erc!ass N&ass11Has( of c!ass N&ass11N&ass1 not found
<.11) (ou are ta>ing oer an a0uarium simu!ation #ro?ect. (our #redecessor had created a
generic Oish c!ass that inc!udes an o<0g(nNonsum%tion method dec!ared as fo!!o$s:
%u$&ic '&oat o<0g(nNonsum%tion('&oat t(m%(ratur()
5he a0uarium simu!ation sums oF,gen consum#tion for a!! fish in the tan> $ith the
fo!!o$ing code fragment" $here fishes is an arra, of Oish ob?ect references:
'&oat tota& = 0;
'or (int i = 0; i , 'ish(s1&(ngth; i--) {
tota& -= 'ish(s[i]1o<0g(nNonsum%tion(t);
#
,ou are $riting a subc!ass for a #articu!ar fish s#ecies. (our tas> is to #roide a method
$ith s#eciesJs#ecific metabo!ism data that $i!! trans#arent!, fit into the simu!ation. Do
,ou $ant to oer!oad or oerride the o<0g(nNonsum%tion method2
(a) oer!oad.
(b) oerride.
<.12) 5he K(n(ricOruit c!ass dec!ares the fo!!o$ing method to return a '&oat number of
ca!ories in the aerage sering siLe: 3%4
%u$&ic '&oat av(Na&ori(s()
(our ;%%&( c!ass" $hich eFtends K(n(ricOruit" oerrides this method. /n a
Si(tS(&(ction c!ass that eFtends A$j(ct" ,ou $ant to use the K(n(ricOruit method
on an ;%%&( ob?ect. *e!ect the correct $a, to finish the statement in the fo!!o$ing code
fragment so the K(n(ricOruit ersion of av(Na&ori(s is ca!!ed using the g' reference"
or se!ect o#tion (d)
K(n(ricOruit g' = n(9 ;%%&(();
'&oat ca& = 'inish this stat(m(nt using g'
(a) g'1av(Na&ori(s();
(b) ((K(n(ricOruit)g')1av(Na&ori(s();
(c) g'1su%(r1av(Na&ori(s();
(d) 5here is no $a, to ca!! the K(n(ricOruit method.
<.1%) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 %u$&ic c&ass Nhi&d (<t(nds Bar(nt
. {
8 %u$&ic static int t(st(int i)
: {
5 r(turn .0;
6 #
F %u$&ic static void main(String[] args)
G {
C Bar(nt c = n(9 Nhi&d();
10 S0st(m1out1%rint&n(c1t(st(10));
11 #
1. #
18 c&ass Bar(nt
1: {
15 %u$&ic static int t(st(int i)
16 {
1F r(turn 5;
1G #
1C #
(a) Com#i!e time as $e can8t oeride static methods.
(b) .un time error as $e can8t oeride static methods.
(c) Erogram com#i!es correct!, and #rints < $hen eFecuted.
(d) Erogram com#i!es correct!, and #rints 20 $hen eFecuted.
<.16) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass sam%&(
. {
8 sam%&((String s)
: {
5 S0st(m1out1%rint&n(DStringD);
6 #
F sam%&((A$j(ct o)
G {
C S0st(m1out1%rint&n(DA$j(ctD);
10 #
11 #
1. c&ass constructor
18 {
1: %u$&ic static void main(String arg[])
15 {
16 sam%&( s1=n(9 sam%&((nu&&);
1F #
1G #
(a) Com#i!e time error as ca!! to constructor at !ine no. 1; is ambigious.
(b) .un time error as ca!! to constructor at !ine no. 1; is ambigious.
(c) Erogram com#i!es correct!, and #rints Aob?ectA $hen eFecuted.
(d) Erogram com#i!es correct!, and #rints AstringA $hen eFecuted.
<.1<) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass sam%&(
. {
8 sam%&((String s)
: {
5 S0st(m1out1%rint&n(DStringD);
6 #
F sam%&((StringHu''(r s$)
G {
C S0st(m1out1%rint&n(DStringHu''(rD);
10 #
11 #
1. c&ass constructor
18 {
1: %u$&ic static void main(String arg[])
15 {
16 sam%&( s1=n(9 sam%&((nu&&);
1F #
1G #
(a) Com#i!e time error as ca!! to constructor at !ine no. 1; is ambigious.
(b) .un time error as ca!! to constructor at !ine no. 1; is ambigious.
(c) Erogram com#i!es correct!, and #rints A*tringKufferA $hen eFecuted.
(d) Erogram com#i!es correct!, and #rints AstringA $hen eFecuted.
<.1;) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass ;
. {
8 void ca&&m(()
: {
5 S0st(m1out1%rint&n(D;D);
6 #
F int r=10;
G #
C c&ass H (<t(nds ;
10 {
11 %u$&ic void ca&&m(()
1. {
18 S0st(m1out1%rint&n(DHD);
1: #
15 int r=.0;
16 #
1F c&ass E16
1G {
1C %u$&ic static void main(String args[])
.0 {
.1 ; a = n(9 H();
.. a1ca&&m(();
.8 S0st(m1out1%rint&n(a1r);
.: #
.5 #
(a) Com#i!e time error.
(b) Erogram com#i!es correct!, and #rints AAA and 10 $hen eFecuted.
(c) Erogram com#i!es correct!, and #rints AKA and 20 $hen eFecuted.
(d) Erogram com#i!es correct!, and #rints AKA and 10 $hen eFecuted.
<.1-) 1hether the fo!!o$ing code com#i!e or not2 374
*e!ect an, t$o.
1 c&ass Has( {#
. c&ass ;gg (<t(nds Has(
8 {
: %u$&ic String g(tOi(&ds()
5 {
6 String nam( = D;ggD;
F r(turn nam(;
G #
C #
10 %u$&ic c&ass Rnh(rit(nc(
11 {
1. %u$&ic static void main(String argv[])
18 {
1: Has( a = n(9 ;gg();
15 S0st(m1out1%rint&n(a1g(tOi(&ds());
16 #
1F #
(a) /t $i!! com#i!e.
(b) /t $i!! not com#i!e.
(c) /t $i!! com#i!e if $e cast the ariab!e a for the ob?ect of c!ass Agg !i>e ((Agg)
a).getFie!ds()
(d) 1e can cast the ariab!e a for the ob?ect of c!ass Agg" but it is not re0uired.
Answers:
5.1) 1hen one c!ass eFtends another c!ass" instances of the subc!ass $i!! contain their o$n
co#ies of ALL members dec!ared in the su#erc!ass (inc!uding an, %rivat( ariab!es). /n
the eFam#!e aboe a!! instances of 6(st(r $i!! contain a!! four of the ariab!es dec!ared in
E. Do$eer" not a!! of these ariab!es $i!! be accessib!e (i.e." isib!e) b, instances of
6(st(r.
/n the descri#tions be!o$" $e sa, a ariab!e is inherited b, a subc!ass if it is accessib!e to
instances of the subc!ass.
su%1%rivat(?ar = 1; *in( 1
%rivat( members are on!, accessib!e from $ithin the c!ass in $hich the, are dec!ared.
Kecause %rivat(?ar $as dec!ared in c!ass E" it is not accessib!e from c!ass 6(st(r. 5he
fo!!o$ing com#i!er error resu!ts:
6(st(r1javaL10L %rivat(?ar has %rivat( acc(ss in d('1E
su%1%rivat(?ar = 1; *in( 1
X
su%1%acIag(?ar = .; *in( .
%acIag( members are on!, accessib!e to c!asses $ithin the same #ac>age as the c!ass that
dec!ares them. /n the case" the member %acIag(?ar" is dec!ared in c!ass E" in the d('
#ac>age. %acIag( is accessib!e on!, from $ithin c!asses in the d(' #ac>age. *ince the
6(st(r c!ass is in the Adefault packageA it does not hae access to %acIag(?ar. 5he
fo!!o$ing com#i!er error resu!ts:
6(st(r1javaL11L %acIag(?ar is not %u$&ic in d('1E; cannot $( acc(ss(d 'rom
outsid( %acIag(
su%1%acIag(?ar = .; *in( .
X
su%1%rot(ct(d?ar = 8; *in( 8
)en though c!ass 6(st(r eFtends c!ass E" instances of 6(st(r are not a!!o$ed access to
%rot(ct(d members in instances of E" #ro#er. Kecause su% is an instance of E" #ro#er" a
6(st(r instance $i!! not hae access to its #rotected members. An instance of c!ass 6(st(r
can on!, access %rot(ct(d members in instances of c!asses that are of t,#e 6(st(r. 5his
inc!udes c!asses that inherit from 6(st(r (such as the Su$ c!ass). 5he fo!!o$ing com#i!er
error resu!ts:
6(st(r1javaL1.L %rot(ct(d?ar has %rot(ct(d acc(ss in d('1E
su%1%rot(ct(d?ar = 8; *in( 8
X
su%1%u$&ic?ar = :; *in( :
%u$&ic members are aai!ab!e AeverywhereA. Conse0uent!," 6(st(r8s reference to
su%1%u$&ic?ar com#i!es $ithout error.
%rivat(?ar = 5; *in( 5
%rivat( members are on!, accessib!e from $ithin the c!ass in $hich the, are dec!ared.
Kecause %rivat(?ar $as dec!ared in c!ass E" it is not accessib!e from c!ass 6(st(r. Bote
that on Oine 1 $e $ere tr,ing to refer to a %rivat( member in another ob?ect (the member
su%). Pn !ine < $e are tr,ing to refer the co#, of %rivat(?ar that 6(st(r contains b,
irtue of being a subc!ass of E. /n terms of the isibi!it, of %rivat(?ar" ho$eer" it does
not matter. %rivat(?ar is a %rivat( member dec!ared in the E c!ass and can ONLY be
accessed from $ithin that c!ass. 5he fo!!o$ing com#i!er error resu!ts:
6(st(r1javaL1FL %rivat(?ar has %rivat( acc(ss in d('1E
%rivat(?ar = 5; *in( 5
X
%acIag(?ar = 6; *in( 6
As $as true for !ine 2" #ac>age members are on!, accessib!e to c!asses $ithin the same
#ac>age as the c!ass that dec!ares them. Dere $e are attem#ting to access the %acIag(?ar
member contained in an instance of the 6(st(r c!ass. /n other $ords" an instance of
6(st(r is tr,ing to access the %acIag(?ar it contains b, irture of its inheritance from E.
5he %rot(ct(d?ar member $as declared b, a c!ass in the d(' #ac>age and 6(st(r is not
in that #ac>age. 5herefore" 6(st(r cannot access the %acIag(?ar member. 5he fo!!o$ing
com#i!er error resu!ts:
6(st(r1javaL1GL %acIag(?ar is not %u$&ic in d('1E; cannot $( acc(ss(d 'rom
outsid( %acIag(
%acIag(?ar = 6; *in( 6
X
%rot(ct(d?ar = F; *in( F
5here is no com#i!er error for !ine -.
An instance of 6(st(r can refer to its o$n co#, of %rot(ct(d?ar" the co#, of
%rot(ct(d?ar contained in some other instance of 6(st(r" or the co#, of %rot(ct(d?ar
contained in an instance of a subc!ass of 6(st(r. An instance of 6(st(r cannot" ho$eer"
refer to a co#, of %rot(ct(d?ar in an ob?ect that is not an instance of 6(st(r.
%u$&ic?ar = G; *in( G
As $as the case $ith !ine 12" %u$&ic members are aai!ab!e AeverywhereA. Conse0uent!,"
6(st(r8s reference to %u$&ic?ar com#i!es $ithout error.
t1%rivat(?ar = C; *in( C
As $as the case $ith !ines 1 and <" the member" %rivat(?ar" is dec!ared in c!ass E and can
therefore on!, be accessed b, instances of c!ass E" #ro#er (i.e." nonJsubc!asses of c!ass E).
5he fo!!o$ing com#i!er error resu!ts:
6(st(r1javaL.5L %rivat(?ar has %rivat( acc(ss in d('1E
t1%rivat(?ar = C; *in( C
X
t1%acIag(?ar = 10; *in( 10
As $as the case $ith !ines 2 and ;" the member" %acIag(?ar" is dec!ared in c!ass E $hich
is in the d(' #ac>age. Pn!, instances of c!asses in the same #ac>age as c!ass E can access
the #ac>age ariab!e" %acIag(?ar. .emember that because the 6(st(r c!ass does not hae
a %acIag( statement it is #!aced in Qaa8s Adefault packageA. 5he fo!!o$ing com#i!er error
resu!ts:
6(st(r1javaL.6L %acIag(?ar is not %u$&ic in d('1E; cannot $( acc(ss(d 'rom
outsid( %acIag(
t1%acIag(?ar = 10; *in( 10
X
t1%rot(ct(d?ar = 11; *in( 11
(ou ma, $ant to refer bac> to the descri#tion of !ine -. /f $e hae a c!ass (ca!! it B) that
contains a %rot(ct(d member (ca!! it p) and another c!ass (ca!! it S) eFtends B" a!!
instances of S can access #rotected members in an, instance of c!ass S" inc!uding
subsc!asses of S.
Pnce again" in this coded eFam#!e" c!ass B is c!ass E" c!ass S is c!ass 6(st(r" and p is the
%rot(ct(d member %rot(ct(d?ar. Oine 11 is an eFam#!e of an instance of 6(st(r
attem#ting to reference a %rot(ct(d member in another instance of 6(st(r (re#resented
b, the member" t). *ince an instance of 6(st(r is attem#ting to reference a %rot(ct(d
member in another instance of 6(st(r" the !ine com#i!es c!ean!,.
t.publicVar = 12; // Line 12
As was the case with lines 4 and 8, %u$&ic ebers are a!ailable "e!er#where". $%nse&uentl#, 6(st(r's
re(erence t% t1%u$&ic?ar c%piles with%ut err%r.
sub.pri!ateVar = 1); // Line 1)
As was the case with lines 1, 5, and *, the eber, su$1%rivat(?ar, is declared in class E and can there(%re
%nl# be accessed b# instances %( class E, pr%per (i.e., n%n+subclasses %( class E). ,he (%ll%win- c%piler err%r
results.
6(st(r1javaL8.L %rivat(?ar has %rivat( acc(ss in d('1E
su$1%rivat(?ar = 18; *in( 18
X
sub.pac/a-eVar = 14; // Line 14
As was the case with lines 2, 0, and 11, the eber, su$1%acIag(?ar, is declared in class E which is in the
d(' pac/a-e. ,% access this eber the class atteptin- the access (6(st(r, in this case) ust be in the d('
pac/a-e. 2eeber that since 6(st(r d%es n%t ha!e a %acIag( stateent it is placed in 3a!a's "de(ault
pac/a-e". ,he (%ll%win- c%piler err%r results.
6(st(r1javaL88L %acIag(?ar is not %u$&ic in d('1E; cannot $( acc(ss(d 'rom
outsid( %acIag(
su$1%acIag(?ar = 1:; *in( 1:
X
sub.pr%tectedVar = 15; // Line 15
4%u a# want t% re(er bac/ t% the descripti%ns %( lines 5 and 11. Actuall#, this line c%piles cleanl# (%r e6actl# the
sae reas%n line 11 c%piled cleanl#. 2eeber that class Su$ e6tends class 6(st(r. $%nse&uentl#, an#
instances %( Su$ are als% c%nsidered instances %( 6(st(r.
sub.publicVar = 10; // Line 10
As was the case with lines 4, 8 and 12, %u$&ic ebers are a!ailable "e!er#where". $%nse&uentl#, 6(st(r's
re(erence t% su$1%u$&ic?ar c%piles with%ut err%r.
<.2) (b)
5he static method addUidg(t cannot access the member ariab!e 9=am(. *tatic methods
can refer to static ariab!es on!," such as $idgetCount.
<.%) (a)
Kecause the sco#e of the tm# *tring is confined to the tr, b!oc>" thus" it cannot be used in
!ine ;. ans$er (c) $ou!d not occur een if the sco#e of the tm# ariab!e $ere fiFed. ans$er
(d) $ou!d occur on!, if the sco#e of the tm# ariab!e $ere fiFed b, dec!aring and
initia!iLing tm# in the first !ine of the method.
<.6) (b)
5his 0uestion is about the re0uirement to understand the difference bet$een the AisJaA
and the AhasJaA re!ationshi#. 1here a c!ass is inherited ,ou hae to as> if it re#resents the
AisJaA re!ationshi#. As the difference bet$een the root and the t$o chi!dren are the
o#erating s,stem ,ou need to as> are OinuF and 1indo$s t,#es of com#uters.5he ans$er
is no" the, are both t,#es of P#erating *,stems. *o o#tion t$o re#resents the best of the
o#tions. (ou might consider haing o#erating s,stem as an interface instead but that is
another stor,.
Pf course there are as man, $a,s to design an ob?ect hierarch, as $a,s to #ronounce
K?arne *trous?ou#" but this is the sort of ans$er that *un $i!! #robab!, be !oo>ing for in
the eFam. Suestions hae been as>ed in discussion forums if this t,#e of 0uestion rea!!,
comes u# in the eFam. / thin> this is because some other moc> eFams do not contain an,
0uestions !i>e this. / assure ,ou that this t,#e of 0uestion can come u# in the eFam. 5hese
t,#es of 0uestion are testing ,our understanding of the difference bet$een the isJa and
hasJa re!ationshi# in c!ass design.
<.<) (d)
An, ca!! to this or su#er must be the first !ine in a constructor. As the method a!read, has
a ca!! to this" no more can be inserted.
<.;) (a)" (b)
Ans$er (a) is correct because the ;%S(riv(d c!ass inherits from ;%Has(" $hich im#!ements
Qunna$&(. Ans$er (b) is a!so correct because the inserted cast (Qunna$&() is not needed
but does not cause a #rob!em. Ans$er (c) fai!s to com#i!e because the com#i!er can te!!
that the ;%Has( c!ass does not im#!ement A$s(rv(r. Ans$er (d) com#i!es" but fai!s to
eFecute. Kecause of the s#ecific cast the com#i!er thin>s ,ou >no$ $hat ,ou are doing"
but the t,#e of the aHas( reference is chec>ed $hen the statement eFecutes and a
N&assNast4<c(%tion is thro$n.
<.-) (d)
5hese casts and assignment are a!! !ega!. (a) is incorrect incorrect because an ob?ect
reference can be assigned to an interface reference as !ong as the com#i!er >no$s that the
ob?ect im#!ements the interface. Ans$er (b) is incorrect because an interface reference
can be assigned to a reference to A$j(ct because A$j(ct is the base of the Qaa c!ass
hierarch,. Ans$er (c) is incorrect because the ob?ect referred to has not !ost its identit,"
so it #asses the runtime cast chec>.
<.7) (b)
/t com#i!es and runs" the com#i!er assumes ,ou >no$ $hat ,ou are doing $ith the cast to
Qunna$&(. Ans$er (a) fai!s to com#i!e. As far as the com#i!er is concerned" o$j is a #!ain
A$j(ct so it ob?ects to the assignment to a Qunna$&( reference. Ans$er (c) com#i!es but
fai!s to run. Kecause of the s#ecific cast" the com#i!er thin>s ,ou >no$ $hat ,ou are doing"
but the t,#e of the aHas( reference is chec>ed $hen the statement eFecutes" and a
N&assNast4<c(%tion is thro$n. Ans$er (d) fai!s to com#i!e. As far as the com#i!er is
concerned" o$j is a #!ain A$j(ct so it ob?ects to the assignment to an A$s(rv(r reference.
<.9) (c)
1ithout the cast to sub ,ou $ou!d get a com#i!e time error. 5he cast te!!s the com#i!er
that ,ou rea!!, mean to do this and the actua! t,#e of $ does not get reso!ed unti!
runtime. Casting do$n the ob?ect hierarch, as the com#i!er cannot be sure $hat has been
im#!emented in descendent c!asses. Casting u# is not a #rob!em because sub c!asses $i!!
hae the features of the base c!asses. 5his can fee! counter intuitie if ,ou are a$are that
$ith #rimities casting is a!!o$ed for $idening o#erations (ie $0t( to int).
<.10) (d)
=sing the %acIag( statement has an effect simi!ar to #!acing a source fi!e into a different
director,. Kecause the fi!es are in different #ac>ages the, cannot see each other. 5he stuff
about Fi!e1 not haing been com#i!ed $as ?ust to mis!ead" ?aa has the e0uia!ent of an
Aautoma>eA" $hereb, if it $as not for the #ac>age statements the other fi!e $ou!d hae
been automatica!!, com#i!ed.
<.11) (b)
b, oerriding the o<0g(nNonsum%tion method" the Qaa runtime $i!! ca!! the oerriding
method for a!! fish $here a s#ecific method is #roided or the generic method if there is
none. Ans$er (a) is incorrect because if ,ou oer!oaded the o<0g(nNonsum%tion method
using a different method signature" the Qaa runtime $ou!d not ca!! the s#ecific method for
Oish $here a s#ecific method $as #roided. /t $ou!d a!$a,s ca!! the generic method.
<.12) (d)
5here is no $a, for a c!ass outside the K(n(ricOruit hierarch, to ca!! the K(n(ricOruit
method using an ;%%&( reference. Ans$er (a) is incorrect because the runtime reso!ution
of method ca!!s finds the ;%%&( method. Ans$er (b) is incorrect because this eFtra cast
does not change the ob?ect t,#e. Ans$er (c) does not create a a!id Qaa statement.
<.1%) (c)
As both the method in c!ass Chi!d and Earent are static the method ca!! is bound at
com#i!e time $ith the c!ass $hose reference ariab!e is ta>en i.e. Bar(nt.
<.16) (d)
1heneer a method/Constructor has an argument $hich matches t$o different
methods/Constructors definations then it $i!! a!$a,s ca!! the most s#ecific one. As in our
case Pb?ect is a genera! c!ass and is su#er c!ass of a!! other c!asses so it $i!! ca!! the *tring
ersion of the Constructor.
<.1<) (a)
As in this case both c!asses are #eer i.e. both the c!asses is at the same !ee! in the
hierarch, chart.
<.1;) (d)
Nethod are bound at run time $here as ariab!es are bound at com#i!e time./t is the t,#e
of ob?ect $hich $i!! te!! that $hich method is to be ca!!ed(i.e. K in this case)" but it is the
t,#e of reference $hich $i!! te!! ,ou $hich ariab!e is to be ca!!ed(i.e. A in this case).
<.1-) (b)" (c)
5he #rogram $i!! not com#i!e as the there is no method named getFie!ds() defined in the
c!ass Kase./t $i!! com#i!e if $e cast the reference ariab!e a for the ob?ect of c!ass Agg !i>e
((Agg) a).getFie!ds()
Chapter %:
&ar#age Collection ' O#$ect Lifetime
;.1) 1hich of the fo!!o$ing statements about Qaa grabage co!!ection are true statements2 3%4
(a) 5he fo!!o$ing code $i!! start the garabage co!!ection mechanism:
Quntim(1g(tQuntim(()1gc();
6hr(ad10i(&d();
(b) 5he fo!!o$ing code $i!! start the garbage co!!ection mechanism:
S0st(m1gc();
6hr(ad1s&((%(1000);
(c) 5he garbage co!!ection 5hread has a !o$ #riorit,.
(d) 5he method b, $hich Qaa trac>s unused ob?ects is s#ecified in the !anguage definition.
(e) 5he method b, $hich Qaa determines that a chun> of memor, is garbage is u# to the
im#!ementor of the QVN.
;.2) 1hich of the fo!!o$ing statements about fina!iLe methods is true2 3%4
(a) 5he #ur#ose of a 'ina&i>( method is to recoer s,stem resources other than memor,.
(b) 5he #ur#ose of a 'ina&i>( method is to recoer memor, and other s,stem resources.
(c) (ou shou!d a!$a,s $rite a 'ina&i>( method for eer, c!ass.
(d) 5he order in $hich ob?ects are created contro!s the order in $hich their 'ina&i>(
methods are ca!!ed.
;.%) After $hich !ine the ob?ect initia!!, refered b, str is e!igib!e for garbage
co!!ection2 374
*e!ect one correct ans$er.
1 c&ass Kar$ag(
. {
8 %u$&ic static void main(String arg[])
: {
5 String str=n(9 String(D@(&&oD);
6 String str1=str;
F str=n(9 String(D@iD);
G str1=n(9 String(D@(&&o ;gainD);
C r(turn;
10 #
11 #
(a) After !ine no. ;
(b) After !ine no. -
(c) After !ine no. 7
(d) After !ine no. 9
Answers:
;.1) (c)" (e)
Ans$er (c) is correct because the QVN ssigns a !o$ #riorit, to the garbage co!!ection
thread. Ans$er (e) is a!so correct because #ic>ing the best garbage co!!ection method is
!eft u# to the indiidua! QVN im#!ementor. Ans$er (a)" (b) are incorrect because of the
#hrase 'will start+ these code fragments 'may+ start the garbage co!!ector" but there is no
guarantee. Ans$er (d) is incorrect because the !angauge s#ecification does not #rescribe a
method.
;.2) (a)
Ans$er (b) is incorrect because memor, is recoered b, garbage co!!ection: fina!iLers are
for other resources. Ans$er (c) is incorrect because ob?ects that do not use s,stem
resources other than memor, do not need fina!iLers. Ans$er (d) is incorrect because there
is no guarantee about the order of ob?ect fina!iLation.
;.%) (c)
At !ine no. - str refers to a ne$ ob?ect but its #reious ob?ect is sti!! refered b, str1 at that
time.At !ine no. 7 str1 changes its reference then the ob?ect initia!!, refered b, str is
e!igib!e for garbage co!!ection.
Chapter (:
)nner classes
-.1) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run this #rogram2 3-4
%u$&ic c&ass Aut(r {
%u$&ic String nam( = DAut(rD;
%u$&ic static void main(String argv[]) {
Rnn(r i = n(9 Rnn(r();
i1sho9=am(();
#4nd o' main
%rivat( c&ass Rnn(r {
String nam( =n(9 String(DRnn(rD);
void sho9=am(() {
S0st(m1out1%rint&n(nam();
#
#4nd o' Rnn(r c&ass
#
(a) Com#i!e and run $ith out#ut of APuterA.
(b) Com#i!e and run $ith out#ut of A/nnerA.
(c) Com#i!e time error because /nner is dec!ared as #riate.
(d) Com#i!e time error because of the !ine creating the instance of /nner.
-.2) 1hich of the fo!!o$ing statements are true2 3-4
(a) An inner c!ass ma, be defined as static.
(b) 5here are BP circumstances $here an inner c!ass ma, be defined as %rivat(.
(c) An anon,mous c!ass ma, hae on!, one constructor.
(d) An inner c!ass ma, eFtend another c!ass.
-.%) Cien the fo!!o$ing c!ass definition" $hich of the fo!!o$ing statements $ou!d be !ega! after
the comment //Dere2 3-4
c&ass RnAut{
String s= n(9 String(DH(t9((nD);
%u$&ic void am(thod('ina& int i;rgs) {
int iam;
c&ass Hic0c&({
%u$&ic void sa0@(&&o(){
@(r(
#4nd o' $ic0c&( c&ass
#
#4nd o' am(thod
%u$&ic void anoth(r(){
int iAth(r;
#
#
(a) *,stem.out.#rint!n(s):
(b) *,stem.out.#rint!n(iPther):
(c) *,stem.out.#rint!n(iam):
(d) *,stem.out.#rint!n(iArgs):
-.6) 1hich of the fo!!o$ing statements are true2 3-4
(a) Adding more c!asses ia im%ort statements $i!! cause a #erformance oerhead" on!,
im#ort c!asses ,ou actua!!, use.
(b) =nder no circumstances can a c!ass be defined $ith the %rivat( modifier.
(c) A inner c!ass ma, under some circumstances be defined $ith the %rot(ct(d modifier.
(d) An interface cannot be instantiated.
-.<) 5he fo!!o$ing is an out!ine of code for to#J!ee! c!ass. Assume that both c!asses hae
correct constructors ta>ing no #arameters. 3%4
c&ass =orma&N&ass {
static c&ass =(st(dN&ass {
m(thods and varia$&(s o' =(st(dN&ass
#
m(thods and varia$&(s o' =(st(dN&ass
#
1hich of the fo!!o$ing code fragments sho$s the correct $a, to dec!are and initia!iLe a
reference to a =(st(dN&ass ob?ect2
(a) =orma&N&ass1=(st(dN&ass m0=N = n(9 =orma&N&ass1=(st(dN&ass();
(b) =(st(dN&ass m0=N = n(9 =orma&N&ass()1n(9 =(st(dN&ass();
(c) =(st(dN&ass m0=N = n(9 =orma&N&ass1=(st(dN&ass();
-.;) 5he fo!!o$ing is an out!ine of code for to#J!ee! c!ass. Assume that both c!asses hae
correct constructors ta>ing no #arameters. 3%4
c&ass Has(N&ass {
static c&ass =(st(dN&ass {
m(thods and varia$&(s o' =(st(dN&ass
#
m(thods and varia$&(s o' Has(N&ass
#
1hich of the fo!!o$ing code fragments sho$s the correct $a, to dec!are and initia!iLe a
reference to a =(st(dN&ass ob?ect2
(a) Has(N&ass1=(st(dN&ass m0=N = n(9 Has(N&ass1=(st(dN&ass();
($) =(st(dN&ass m0=N = n(9 Has(N&ass()1n(9 =(st(dN&ass();
(c) =(st(dN&ass m0=N = n(9 Has(N&ass1=(st(dN&ass();
(d) Has(N&ass1=(st(dN&ass n$c = n(9 Has(N&ass()1n(9 =(st(dN&ass();
-.-) 1hich of the fo!!o$ing statements about this code are true2 364
%u$&ic c&ass Mor(com$( {
%u$&ic static void main(String argv[]){
Mor(com$( m = n(9 Mor(com$(();
m1go(n(9 6uring(){#);
#
%u$&ic void go(6uring t){
t1start();
#
#
c&ass 6uring (<t(nds 6hr(ad {
%u$&ic void run(){
'or(int i =0; i , .; i--){
S0st(m1out1%rint&n(i);
#
#
#
(a) Com#i!ation error due to ma!formed #arameter to go method.
(b) Com#i!ation error" c!ass 6uring has no start method.
(c) Com#i!ation and out#ut of 0 fo!!o$ed b, 1.
(d) Com#i!ation but runtime error.
Answers:
-.1) (d)
5his !oo>s !i>e a 0uestion about inner c!asses but it is a!so a reference to the fact that the
main method is static and thus ,ou cannot direct!, access a non static method. 5he !ine
causing the error cou!d be fiFed b, changing it to sa,
Rnn(r i = n(9 Aut(r()1n(9 Rnn(r();
5hen the code $ou!d com#i!e and run #roducing the out#ut A/nnerA
-.2) (a)" (d)
A static inner c!ass is a!so sometimes >no$n as a to# !ee! nested c!ass. 5here is some
debate if such a c!ass shou!d be ca!!ed an inner c!ass. / tend to thin> it shou!d be on the
basis that it is created inside the o#ening braces of another c!ass. Do$ cou!d an
anon,mous c!ass hae a constructor2. .emember a constructor is a method $ith no return
t,#e and the same name as the c!ass. /nner c!asses ma, be defined as #riate
-.%) (a)" (d)
A c!ass $ithin a method can on!, see fina! ariab!es of the enc!osing method. Do$eer it
the norma! isibi!it, ru!es a##!, for ariab!es outside the enc!osing method.
-.6) (c)" (d)
5he im%ort statement a!!o$s ,ou to use a c!ass direct!, instead of fu!!, 0ua!if,ing it $ith
the fu!! #ac>age name" adding more c!assess $ith the im#ort statement does not cause a
runtime #erformance oerhad. / assure ,ou this is true. An inner c!ass can be defined $ith
the #riate modifier.
-.<) (a)
(a) sho$s both correct reference ariab!e dec!aration and a correct constructor. (b)" (c)
are incorrect dec!aration of the reference ariab!e because the name of a nested c!ass
starts $ith the enc!osing c!ass name.
-.;) (d)
(d) sho$s both correct reference ariab!e dec!aration and a correct constructor. 5his
statement creates a Has(N&ass ob?ect" $hich the =(st(dN&ass must hae. (b)" (c) are
incorrect dec!aration of the reference ariab!e because the name of a nested c!ass starts
$ith the enc!osing c!ass name. Ans$er (a) does not create a Has(N&ass ob?ect for the
=(st(dN&ass to be associated $ith.
-.-) (c)
5he creation of an anon,mous c!ass as a #arameter to go is fair!, strange as ,ou $ou!d
eF#ect it to oerride a method in its #arent c!ass (6uring). (ou don8t hae to though. 5he
fact that c!ass 6uring eFtends 6hr(ad means the anon,mous instance that is #assed to go
has a start method $hich then ca!!s the run method.
Chapter *:
+hreads
7.1) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run the fo!!o$ing code2 3-4
%u$&ic c&ass Hground (<t(nds 6hr(ad{
%u$&ic static void main(String argv[]){
Hground $ = n(9 Hground();
$1run();
#
%u$&ic void start(){
'or (int i = 0; i ,10; i--){
S0st(m1out1%rint&n(D?a&u( o' i = D - i);
#
#
#
(a) A com#i!e time error indicating that no run method is defined for the 6hr(ad c!ass.
(b) A run time error indicating that no run method is defined for the 6hr(ad c!ass.
(c) C!ean com#i!e and at run time the a!ues 0 to 9 are #rinted out.
(d) C!ean com#i!e but no out#ut at runtime.
7.2) 1hat can cause a thread to sto# eFecuting2
(a) 5he #rogram eFits ia a ca!! to S0st(m1(<it(0);
(b) Another thread is gien a higher #riorit,.
(c) A ca!! to the thread8s sto% method.
(d) A ca!! to the ha&t method of the 6hr(ad c!ass.
7.%) 1hich statement is true of the fo!!o$ing code2 364
%u$&ic c&ass ;gg {
%u$&ic static void main(String argv[]){
;gg a = n(9 ;gg();
a1go();
#
%u$&ic void go(){
SSQoss ds1 = n(9 SSQoss(Don(D);
ds11start();
#
#
c&ass SSQoss (<t(nds 6hr(ad {
%rivat( String s6nam( = DD;
SSQoss(String s){
s6nam( = s;
#
%u$&ic void run(){
not9ait();
S0st(m1out1%rint&n(D'inish(dD);
#
%u$&ic void not9ait(){
9hi&((tru() {
tr0 {
S0st(m1out1%rint&n(D9aitingD);
9ait();
# catch(Rnt(rru%t(d4<c(%tion i(){
#
S0st(m1out1%rint&n(s6nam();
noti'0;&&();
#
#
#
(a) /t $i!! cause a com#i!e time error.
(b) Com#i!ation and out#ut of A$aitingA.
(c) Com#i!ation and out#ut of A$aitingA fo!!o$ed b, AfinishedA.
(d) .untime error" an eFce#tion $i!! be thro$n.
7.6) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run the fo!!o$ing code2 364
%u$&ic c&ass @o&t (<t(nds 6hr(ad {
%rivat( String s6hr(ad=am(;
%u$&ic static void main(String argv[]){
@o&t h = n(9 @o&t();
h1go();
#
@o&t() {
#
@o&t(String s) {
s6hr(ad=am( = s;
#
%u$&ic String g(t6hr(ad=am(() {
r(turn s6hr(ad=am(;
#
%u$&ic void go() {
@o&t 'irst = n(9 @o&t(D'irstD);
'irst1start();
@o&t s(cond = n(9 @o&t(Ds(condD);
s(cond1start();
#
%u$&ic void start(){
'or(int i = 0; i , .; i --){
S0st(m1out1%rint&n(g(t6hr(ad=am(() -i);
tr0 {
6hr(ad1s&((%(100);
# catch(Rnt(rru%t(d4<c(%tion () {
S0st(m1out1%rint&n((1g(tM(ssag(());#
#
#
#
(a) Com#i!e time error.
(b) Put#ut of first0" second0" first0" second1.
(c) Put#ut of first0" first1" second0" second1.
(d) .untime error.
7.<) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run the fo!!o$ing code2 364
c&ass HacIground im%&(m(nts Qunna$&({
int i = 0;
%u$&ic int run(){
9hi&( (tru() {
i--;
S0st(m1out1%rint&n(Di=D-i);
#
r(turn 1;
#
#4nd c&ass
(a) /t $i!! com#i!e and the run method $i!! #rint out the increasing a!ue of i.
(b) /t $i!! com#i!e and ca!!ing start $i!! #rint out the increasing a!ue of i.
(c) 5he code $i!! cause an error at com#i!e time.
(d) Com#i!ation $i!! cause an error because 9hi&( cannot ta>e a #arameter of tru(.
7.;) 1hich statement is true of the fo!!o$ing code2 364
%u$&ic c&ass Q%crav(n {
%u$&ic static void main(String argv[]){
Bmcrav(n %m1 = n(9 Bmcrav(n(Don(D);
%m11run();
Bmcrav(n %m. = n(9 Bmcrav(n(Dt9oD);
%m.1run();
#
#
c&ass Bmcrav(n (<t(nds 6hr(ad {
%rivat( String s6nam(=DD;
Bmcrav(n(String s) {
s6nam( = s;
#
%u$&ic void run(){
'or(int i =0; i , . ; i--){
tr0 {
s&((%(1000);
#catch(Rnt(rru%t(d4<c(%tion (){
#
0i(&d();
S0st(m1out1%rint&n(s6nam();
#
#
#
(a) Com#i!e time error" c!ass Q%crav(n does not im#ort java1&ang16hr(ad
(b) Put#ut of Pne Pne 5$o 5$o
(c) Put#ut of Pne 5$o Pne 5$o
(d) Put#ut of Pne 5$o Pne 5$o
7.-) (ou are creating a c!ass that eFtends A$j(ct and im#!ements Qunna$&(. (ou hae a!read,
$ritten a run method for this c!ass" and ,ou need a $a, to create a thread and hae it
eFecute the run method" 1hich of these start methods shou!d ,ou use2 3%4
(a) %u$&ic void start() { n(9 6hr(ad(this)1start(); #
(b) %u$&ic void start() { 6hr(ad m06 = n(9 6hr(ad(); m061start(); #
(c) %u$&ic void start() { 6hr(ad m06 = n(9 6hr(ad(this); m061run(); #
7.7) Qaa 5hread ; is attached to an ob?ect H" $hich is res#onsib!e for $riting data to a fi!e.
After $riting data" 5hread ; ca!!s the fo!!o$ing method in ob?ect H" $here it $aits unti!
more data is aai!ab!e. 3%4
%rivat( void s0nchroni>(d 9aitOorSata() { (1)
tr0 { (.)
9ait(); (8)
# catch (Rnt(rru%t(d4<c(%tion (<) { (:)
#
#
Another 5hread" eFecuting a method in another ob?ect N" needs to $a>e u# 5hread ;.
Assuming that ob?ect N has references to both ; and H" se!ect a!! of the fo!!o$ing code
fragments that $ou!d cause 5hread ; to eFit the 9aitOorSata method.
(a) ;1int(rru%t();
(b) s0nchroni>(d(;) { ;1noti'0;&&(); #
(c) s0nchroni>(d(H) { H1noti'0;&&(); #
(d) ;1r(sum(();
7.9) (ou hae created a 6im(Aut c!ass as an eFtension of 6hr(ad" the #ur#ose of $hich is to
#rint a '5ime@s =#+ message if the 5hread is not interru#ted $ithin 10 seconds of being
started. Dere is the run method that ,ou hae coded: 3%4
%u$&ic void run() {
S0st(m1out1%rint&n(2StartY3);
tr0 {
6hr(ad1s&((%(10000);
S0st(m1out1%rint&n(26im(s J%Y3);
# catch (Rnt(rru%t(d4<c(%tion () {
S0st(m1out1%rint&n(2Rnt(rru%t(dY3);
#
#
Cien that a #rogram creates and starts a 6im(Aut ob?ect" $hich of the fo!!o$ing
statements is true2
(a) )Fact!, 10 seconds after the start method is ca!!ed" '5ime@s =#T+ $i!! be #rinted.
(b) )Fact!, 10 seconds after the '*tartT+ is #rinted" '5ime@s =#T+ $i!! be #rinted.
(c) 5he de!a, bet$een '*tartT+ being #rinted and '5ime@s =#T+ $i!! be 10 seconds #!us or
minus one tic> of the s,stem c!oc>.
(d) /f '5ime@s =#T+ is #rinted" ,ou can be sure that at !east 10 seconds hae e!a#sed since
'*tartT+ $as #rinted.
Answers:
7.1) (d)
5his is a bit of a snea>, one as / hae s$a##ed around the names of the methods ,ou
need to define and ca!! $hen running a thread. /f the for !oo# $ere defined in a method
ca!!ed %u$&ic void run()
and the ca!! in the main method had been to $1start()
5he !ist of a!ues from 0 to 9 $ou!d hae been out#ut.
7.2) (a)" (b)" (c)
Qaa threads are some$hat #!atform de#endent and ,ou shou!d be carefu!! $hen ma>ing
assum#tions about 5hread #riorities. Pn some #!atforms ,ou ma, find that a 5hread $ith
higher #riorities gets to AhogA the #rocessor. (ou can read u# on this in more detai! at
htt#://?aa.sun.com/docs/boo>s/tutoria!/essentia!/threads/#riorit,.htm!
7.%) (d)
A ca!! to 9aitnoti'0 must be $ithin s,nchroniLed code. 1ith QDM1.2 this code thro$s the
error message
java1&ang1R&&(ga&MonitorStat(4<c(%tionL curr(nt thr(ad not o9n(r
at java1&ang1A$j(ct19ait(=ativ( M(thod)
at java1&ang1A$j(ct19ait(A$j(ct1javaL:.:)
at SSQoss1not9ait(Nom%i&(d Nod()
at SSQoss1run(;gg1javaL.1)
7.6) (c)
Bote that this code oerrides and ca!!s the start method. /f ,ou $ished to get the out#ut
miFed ,ou $ou!d need to oerride the run method but ca!! the start method.
7.<) (c)
5he error is caused because run shou!d hae a void not an int return t,#e.
An, c!ass that is im#!ements an interface must create a method to match a!! of the
methods in the interface. 5he Qunna$&( interface has one method ca!!ed run that has a
void return t,#e.5he sun com#i!er gies the error
M(thod r(d('in(d 9ith di''(r(nt r(turn t0%(L int run() 9as d('in(d as void
run();
7.;) (b)
Ans$er (c) $ou!d $ou!d be true if the code ca!!ed the start method instead of the run
method ($e!! it is on m, 1indo$s machine an,$a," /8m not sure it $ou!d be for eer
im#!ementation of Qaa 5hreads). /f ,ou ca!! the run method direct!, it ?ust acts as an,
other method and does not return to the ca!!ing code unti! it has finished eFecuting.
7.-) (a)
Ans$er (a) is o>" it does not matter that there is no reference to the thread in the start
method. Ans$er (b) is incorrect because the ne$ 6hr(ad is not attached to the Qunna$&(
ob?ect so it cannot find the run method. /nstead" the defau!t run in the 6hr(ad c!ass $i!!
be eFecuted. Ans$er (c) is incorrect because the 6hr(ad that is eFecuting the start
method ca!!s run in the 6hr(ad c!ass. 5he m06 5hread is not started.
7.7) (a)" (c)
Ans$er (a) is correct because it $i!! generate an Rnt(rru%t(d4<c(%tion" bringing thread
; out of the 9ait. Kecause !ine (6) catches this eFce#tion" the thread eFits the
9aitOorSata method norma!!,. Ans$er (c) is correct because it remoes a!! 5hreads
$aiting for ob?ect H from the $ait !ist" inc!uding thread ;. Ans$er (b) is incorrect because
it refers to the thread ;" not to the ob?ect H for $hich the thread is $aiting. Ans$er (d) is
incorrect because the r(sum( method $or>s on!, $ith sus#ended threads and is a
de#recated method.
7.9) (d)
/t is the on!, statement that can be made $ith confidence. Ans$er (a)" (b)" (c) are a!!
incorrect because the eF#iration of a s!ee# timer does not guarantee that a 5hread $i!! run
R on!, that it can run.
Chapter ,:
Fundamental classes
9.1) 1hat is the out#ut of the fo!!o$ing:
StringHu''(r s$1 = n(9 StringHu''(r(D;mitD);
StringHu''(r s$. = n(9 StringHu''(r(D;mitD);
String ss1 = D;mitD;
S0st(m1out1%rint&n(s$1==s$.);
S0st(m1out1%rint&n(s$11(5ua&s(s$.));
S0st(m1out1%rint&n(s$11(5ua&s(ss1));
S0st(m1out1%rint&n(DBoddarD1su$string(8));
a) fa!se
fa!se
fa!se
dar
b) fa!se
true
fa!se
Eoddar
c) Com#i!er )rror
d) true
true
fa!se
dar
9.2) 1hich of the fo!!o$ing $i!! out#ut J6.0 2 364
(a) S0st(m1out1%rint&n(Math1'&oor(P:1F));
(b) S0st(m1out1%rint&n(Math1round(P:1F));
(c) S0st(m1out1%rint&n(Math1c(i&(P:1F));
(d) S0st(m1out1%rint&n(Math1min(P:1F));
9.%) 1hat $i!! be the resu!t $hen ,ou attem#t to com#i!e this #rogram2 364
%u$&ic c&ass Qand {
%u$&ic static void main(String argv[]){
int iQand;
iQand = Math1random();
S0st(m1out1%rint&n(iQand);
#
#
(a) Com#i!e time error referring to a cast #rob!em.
(b) A random number bet$een 1 and 10.
(c) A random number bet$een 0 and 1
(d) A com#i!e time error about random being an unrecognised method.
9.6) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass E.
. {
8 %u$&ic static void main(String arg[])
: {
5 StringHu''(r s[]={D;D)DHD#;
6 S0st(m1out1%rint&n(s[0]-D)D-s[1]);
F #
G #
(a) Erogram com#i!es correct!, and #rint A"K $hen eFecuted.
(b) Com#i!e time error.
(c) .un time error.
9.<) 1hat $i!! be the a!ue of str after !ine no.-2 374
)nter ,our ans$er in the gien fie!d. Don8t use an, eFtra signs !i>e (AA)0uotes or an, string
!i>e Astr&A. For eFam#!e" if ,our ans$er is the string AU(VA ,ou ?ust $rite U(V
1 c&ass string
. {
8 %u$&ic static void main(String ds[])
: {
5 String str=D;D;
6 str1concat(DHD);
F str-=DND;
G #
C #
9.;) 1hat $i!! be the resu!t of com#i!ing and running the gien #rogram2 374
*e!ect one correct ans$er.
1 c&ass strings
. {
8 %u$&ic static void main(String arg[])
: {
5 i'(DStringD1ind(<A'(DSD)P10) ==0)
6 {
F S0st(m1out1%rint&n(D45ua&D);
G #
C (&s(
10 {
11 S0st(m1out1%rint&n(D=ot 45ua&D);
1. #
18 #
1: #
(a) Com#i!e time error as $e #assed negatie a!ue in indeFPf() method.
(b) .un time error as $e #assed negatie a!ue in indeFPf() method.
(c) Erogram com#i!es and #rints )0ua! $hen eFecuted.
(d) Erogram com#i!es and #rints Bot )0ua! $hen eFecuted.
Answers:
9.1) (a)
Bo dec!aration is added" but 1hen / run the eFam#!e / found that the (5ua&s() method in
the StringHu''(r c!ass is said that it is inherited from the A$j(ct c!ass" and / thin> it
didn@t oerride this method" as $hen / o#ened String c!ass documentation it didn@t said
that it inherits the (5ua&s() from the A$j(ct c!ass" and it gae fu!! descri#tion to it. And if
this $as true and the (5ua&s() method is the one inherited b, the A$j(ct it is 'a&s("
because it $i!! return tru( on!, if the t$o reference denote the same ob?ect.
9.2) (c)
P#tions (a) and (b) $i!! #roduce J< and o#tion 6 $i!! not com#i!e because the min method
re0uires 2 #arameters.
9.%) (a)
5his is a bit of a snea>, one as the Math1random method returns a #seudo random number
bet$een 0 and 1" and thus o#tion (c) is a #!ausib!e Ans$er. Do$eer the number returned
is a dou$&( and so the com#i!er $i!! com#!ain that a cast is needed to conert a dou$&( to
an int.
9.6) (b)
Com#i!e time error:Casting needed to conert *tring to *tringKuffer in !ine no. <. 3Nine
o#inion this $i!! not $or>" to ma>e it $or> ne$ *tringKuffer('A+)" ne$
*tringKuffer('K+) 4
9.<) AC
After concatenating AKA in str the resu!t is not stored so it $i!! not effect at a!!.Noreoer
main reason behind this is that *trings are immutab!e"it a!$a,s return ne$ ob?ect $hich
means if $e don8t sae the resu!ts then it $i!! not effect the origna! string at a!!.
9.;) (c)
1e can use negatie argument in indeFPf() and !ast/ndeFPf() method./n such case it $i!!
start searching from Lero.
Chapter 1-:
Collections
10.1) 1hich most c!ose!, matches a descri#tion of a Qaa Na#2 3-4
(a) A ector of arra,s for a 2D geogra#hic re#resentation.
(b) A c!ass for containing uni0ue arra, e!ements.
(c) A c!ass for containing uni0ue ector e!ements.
(d) An interface that ensures that im#!ementing c!asses cannot contain du#!icate >e,s.
10.2) Do$ does the set co!!ection dea! $ith du#!icate e!ements2 3-4
(a) An eFce#tion is thro$n if ,ou attem#t to add an e!ement $ith a du#!icate a!ue.
(b) 5he add method returns 'a&s( if ,ou attem#t to add an e!ement $ith a du#!icate a!ue.
(c) A set ma, contain e!ements that return du#!icate a!ues from a ca!! to the e0ua!s
method.
(d) Du#!icate a!ues $i!! cause an error at com#i!e time.
10.%) 1hich of the fo!!o$ing most c!ose!, describes a bitset co!!ection2 364
(a) A c!ass that contains grou#s of uni0ue se0uences of bits.
(b) A method for f!i##ing indiidua! bits in instance of a #rimitie t,#e.
(c) An arra, of boo!ean #rimities that indicate Leros or ones.
(d) A co!!ection for storing bits as onJoff information" !i>e a ector of bits.
Answers:
10.1) (d)
10.2) (b)
10.%) (d)
5his is the descri#tion gien to a bitset in Kruce )c>e!s A5hin>ing in QaaA boo>. 5he
reference to uni0ue se0uence of bits $as an attem#t to mis!ead because of the use of the
$ord *et in the name bitset. Borma!!, something ca!!ed a set im#!ies uni0ueness of the
members" but not in this conteFt.
Chapter 11:
A.+ components
Chapter 12:
La/out 0anager
12.1) 1hat most c!ose!, matches the a##earance $hen this code runs2 3-4
im%ort java1a9t1!;
%u$&ic c&ass Nom%*a0 (<t(nds Oram({
%u$&ic static void main(String argv[]){
Nom%*a0 c& = n(9 Nom%*a0();
#
Nom%*a0(){
Ban(& % = n(9 Ban(&();
%1s(tHacIground(No&or1%inI);
%1add(n(9 Hutton(DAn(D));
%1add(n(9 Hutton(D69oD));
%1add(n(9 Hutton(D6hr((D));
add(DSouthD)%);
s(t*a0out(n(9 O&o9*a0out());
s(tSi>((800)800);
s(t?isi$&((tru();
#
#
(a) 5he buttons $i!! run from !eft to right a!ong the bottom of the Frame.
(b) 5he buttons $i!! run from !eft to right a!ong the to# of the frame.
(c) 5he buttons $i!! not be dis#!a,ed.
(d) Pn!, button three $i!! sho$ occu#,ing a!! of the frame.
Answers:
12.1) (b)
5he ca!! to the s(t*a0out(n(9 O&o9*a0out()) resets the Oa,out manager for the entire
frame and so the buttons end u# at the to# rather than the bottom.
Chapter 13:
vent 1andling
1%.1) 1hat $i!! ha##en $hen ,ou attem#t to com#i!e and run this code2 3-4
S(monstration o' (v(nt hand&ing
im%ort java1a9t1(v(nt1!;
im%ort java1a9t1!;
%u$&ic c&ass M0Uc (<t(nds Oram( im%&(m(nts Uindo9*ist(n(r{
%u$&ic static void main(String argv[]){
M0Uc m9c = n(9 M0Uc();
#
%u$&ic void 9indo9N&osing(Uindo94v(nt 9(){
S0st(m1(<it(0);
#4nd o' 9indo9N&osing
%u$&ic void M0Uc(){
s(tSi>((800)800);
s(t?isi$&((tru();
#
#4nd o' c&ass
(a) )rror at com#i!e time.
(b) Visib!e Frame created that that can be c!osed.
(c) Com#i!ation but no out#ut at run time.
(d) )rror at com#i!e time because of comment before import statements.
Answers:
1%.1) (a)
/f ,ou im#!ement an interface ,ou must create bodies for a!! methods in that interface.
5his code $i!! #roduce an error sa,ing that M0Uc must be dec!ared a$stract because it
does not define a!! of the methods in Uindo9*ist(n(r. (d) is nonsense as comments can
a##ear an,$here. (c) suggesting that it might com#i!e but not #roduce out#ut is meant
to mis!ead on the basis that $hat !oo>s !i>e a constructor is actua!!, an ordinar, method
as it has a return t,#e.
Chapter 14:
2ainting
Chapter 1":
Files ' 3treams
1<.1) 1hich of the fo!!o$ing can ,ou #erform using the Oi&( c!ass2 364
(a) Change the current director,.
(b) .eturn the name of the #arent director,.
(c) De!ete a fi!e.
(d) Find if a fi!e contains teFt or binar, information.
Answers:
1<.1) (b)" (c)
/t is sur#rising that ,ou can8t change the current director,. /t is not so sur#rising that ,ou
can8t te!! if a fi!e contains teFt or binar, information.
.eferences:
314 htt#://$$$.?aa#rogramming$or!d.com