Sunteți pe pagina 1din 15

1.

Given a method in a protected class, what access modifier do you use to restrict access to that
method to only the other members of the same class?
A.final B.static
C.private D.protected
E. volatile
Core Java
.
interface DoMath
{
double getArea(int rad);
}
interface MathPlus
{
double getVol(int b, int h);
}
/* Missing Stateents ! */
which two code fra!ments inserted at end of the pro!ram, will allow to compile?
1. class AllMath extends DoMath { double getArea(int r); }
2. interface AllMath implements Mathlus { double get!ol(int x" int #); }
$. interface AllMath extends DoMath { float getA%g(int h" int l); }
&. class AllMath implements Mathlus { double getArea(int rad); }
'. abstract class AllMath implements DoMath" Mathlus { public double getArea(int rad)
{ return rad ( rad ( $.1&; } }
A.1 only B. only
C." and # D.1 and $
Core Java
".%hich three statements are true?
1. &he default constructor initialises method variables.
. &he default constructor has the same access as its class.
". &he default constructor invo'es the no(ar! constructor of the superclass.
$. )f a class lac's a no(ar! constructor, the compiler always creates a default constructor.
#. &he compiler creates a default constructor only when there are no other constructors for
the class.
A.1, and $ B., " and #
C.", $ and # D.1, and "
Core Java
$.%hat will be the output of the pro!ram?
class "est
{
#ublic static $oid ain(String %& args)
{
int '()*;
String su# ( (' + ,-) ! .sall. / (' + )))! .tin0. / .huge.;
S0ste1out1#rintln(su#);
}
}
A.small B.tiny
C.hu!e D.Compilation fails
Core Java
#.%hich of the followin! are le!al lines of code?
1. int w * +int,---.-.
. byte / * +byte,10001.
". lon! y * +byte,100.
$. byte 2 * +byte,1001.
A.1 and
B. and "
C." and $
D.All statements are correct.
Core Java
3.%hich two statements are e4uivalent?
1. 135$
. 1366
". 1378
$. 13666
A.1 and B. and $
C." and $ D.1 and "
Core Java
9.%hat will be the output of the pro!ram?
#ublic class "est
{
#ublic static $oid ain(String %& args)
{
int 2 ( ,;
do 3hile ( 2 + , )
S0ste1out1#rint(.2 is . 4 2);
3hile ( 2 5 , ) ;
}
}
A.) is 1
B.) is 1 ) is 1
C.:o output is produced.
D.Compilation error
Core Java
-.%hat will be the output of the pro!ram?
#ublic class 6"7'ce#t
{
#ublic static $oid thro3it ()
{
S0ste1out1#rint(.thro3it .);
thro3 ne3 6untie7'ce#tion();
}
#ublic static $oid ain(String %& args)
{
tr0
{
S0ste1out1#rint(.hello .);
thro3it();
}
catch (7'ce#tion re )
{
S0ste1out1#rint(.caught .);
}
finall0
{
S0ste1out1#rint(.finall0 .);
}
S0ste1out1#rintln(.after .);
}
}
A.hello throwit cau!ht
B.Compilation fails
C.hello throwit )untime*xception cau!ht after
D.hello throwit cau!ht finally after
Core Java
;.%hat will be the output of the pro!ram?
#ublic class 8
{
#ublic static $oid ain(String %& args)
{
tr0
{
badMethod();
S0ste1out1#rint(.A.);
}
catch (7'ce#tion e')
{
S0ste1out1#rint(.9.);
}
finall0
{
S0ste1out1#rint(.:.);
}
S0ste1out1#rint(.D.);
}
#ublic static $oid badMethod() {}
}
A.AC B.BC
C.ACD D.ABCD
Core Java
10.%hich of the followin! are <ava reserved words?
1. run
. import
". default
$. implement
A.1 and B. and "
C." and $ D. and $
Core Java
11.%hat will be the output of the pro!ram?
#ublic class "est
{
#ublic static $oid ain (String%& args)
{
String foo ( args%,&;
String bar ( args%)&;
String ba; ( args%<&;
S0ste1out1#rintln(.ba; ( . 4 ba;); /* =ine > */
}
}
And the command line invocation=
6 +a%a ,est red green blue
A.ba2 * B.ba2 * null
C.ba2 * blue D.>untime E/ception
Core Java
1.%hat will be the output of the pro!ram?
class ?a##0 e'tends "hread
{
final String9uffer sb, ( ne3 String9uffer();
final String9uffer sb) ( ne3 String9uffer();
#ublic static $oid ain(String args%&)
{
final ?a##0 h ( ne3 ?a##0();
ne3 "hread()
{
#ublic $oid run()
{
s0nchroni;ed(this)
{
h1sb,1a##end(.A.);
h1sb)1a##end(.9.);
S0ste1out1#rintln(h1sb,);
S0ste1out1#rintln(h1sb));
}
}
}1start();
ne3 "hread()
{
#ublic $oid run()
{
s0nchroni;ed(this)
{
h1sb,1a##end(.D.);
h1sb)1a##end(.:.);
S0ste1out1#rintln(h1sb));
S0ste1out1#rintln(h1sb,);
}
}
}1start();
}
}
A.ABBCAD
B.ABCBCAD
C.CDADACB
D.?utput determined by the underlyin! platform.
Core Java
1".%hat will be the output of the pro!ram?
class M0"hread e'tends "hread
{
#ublic static $oid ain(String %& args)
{
M0"hread t ( ne3 M0"hread();
"hread ' ( ne3 "hread(t);
'1start(); /* =ine @ */
}
#ublic $oid run()
{
for(int i ( *; i + <; 44i)
{
S0ste1out1#rint(i 4 .11.);
}
}
}
A.Compilation fails. B.1...."..
C.0..1....".. D.0..1....
Core Java
1$.%hich statement is true?
A.A static method cannot be synchroni2ed.
B.)f a class has synchroni2ed code, multiple threads can still access the nonsynchroni2ed
code.
C.
@ariables can be protected from concurrent access problems by mar'in! them with the
s#nchroni-ed 'eyword.
D.%hen a thread sleeps, it releases its loc's.
Core Java
1#.%hich statement is true?
A.Callin! )untime.gc() will cause eli!ible obAects to be !arba!e collected.
B.&he !arba!e collector uses a mar' and sweep al!orithm.
C.)f an obAect can be accessed from a live thread, it canBt be !arba!e collected.
D.)f obAect 1 refers to obAect , then obAect canBt be !arba!e collected.
Core Java
13.%hich statement is true about assertions in the <ava pro!rammin! lan!ua!e?
A.Assertion e/pressions should not contain side effects.
B.Assertion e/pression values can be any primitive type.
C.Assertions should be used for enforcin! preconditions on public methods.
D.
An AssertionError thrown as a result of a failed assertion should always be handled by
the enclosin! method.
Core Java
19.%hat will be the output of the pro!ram?
#ublic class AbB:o#
{
#ublic static $oid ain(String %& args )
{
int result ( *;
AbB:o# oc ( ne3 AbB:o#();
AbBect o ( oc;
if (o (( oc)
result ( ,;
if (o C( oc)
result ( result 4 ,*;
if (o1eDuals(oc) )
result ( result 4 ,**;
if (oc1eDuals(o) )
result ( result 4 ,***;
S0ste1out1#rintln(.result ( . 4 result);
}
}
A.1
B.10
C.101
D.1101
Core Java
1-.%hat will be the output of the pro!ram?
#ublic class "est,@>
{
#ublic static $oid ain(String%& args)
{
String s ( .foo.;
AbBect o ( (AbBect)s;
if (s1eDuals(o))
{
S0ste1out1#rint(.AAA.);
}
else
{
S0ste1out1#rint(.999.);
}
if (o1eDuals(s))
{
S0ste1out1#rint(.:::.);
}
else
{
S0ste1out1#rint(.DDD.);
}
}
}
A.AAACCC
B.AAADDD
C.BBBCCC
D.BBBDDD
Core Java
1;.%hat will be the output of the pro!ram?
int i ( ,, B ( ,*;
do
{
if(i44 5 EEB) /* =ine F */
{
continue;
}
} 3hile (i + -);
S0ste1out1#rintln(.i ( . 4 i 4 .and B ( . 4 B); /* =ine G */
A.i * 3 and A * #
B.i * # and A * #
C.i * 3 and A * 3
D.i * # and A * 3
Core Java
0.%hat will be the output of the pro!ram?
#ublic class 7'aHuestion@
{
static int B;
static $oid ethodA(int i)
{
boolean b;
do
{
b ( i+,* I ethod9(F); /* =ine G */
b ( i+,* II ethod9(>); /* =ine ,* */
}3hile (Cb);
}
static boolean ethod9(int i)
{
B 4( i;
return true;
}
#ublic static $oid ain(String%& args)
{
ethodA(*);
S0ste1out1#rintln( .B ( . 4 B );
}
}
A.A * 0
B.A * $
C.A * -
D.&he code will run with no output
1.%hich of the followin! class level +nonlocal, variable declarations will not compile?
A.protected int a.
B.transient int b * ".
C.private synchroni2ed int e.
D.volatile int d.
Core Java
.%hat will be the output of the pro!ram?
#ublic class Arra0"est
{
#ublic static $oid ain(String% & args)
{
float f,% &, f)% &;
f, ( ne3 float%,*&;
f) ( f,;
S0ste1out1#rintln(.f)%*& ( . 4 f)%*&);
}
}
A.)t prints fC0D * 0.0
B.)t prints fC0D * :a:
C.An error at f2 . f1; causes compile to fail.
D.)t prints the !arba!e value.
Core Java
".
class A
{
A( ) { }
}
class 9 e'tends A
{ }
%hich statement is true?
A.Class BBE constructor is public.
B.Class BBE constructor has no ar!uments.
C.Class BBE constructor includes a call to this( ).
D.:one of these.
Core Java
$.
/* Missing stateents ! */
#ublic class Je3"reeSet e'tends Ba$a1util1"reeSet
{
#ublic static $oid ain(String %& args)
{
Ba$a1util1"reeSet t ( ne3 Ba$a1util1"reeSet();
t1clear();
}
#ublic $oid clear()
{
"reeMa# ( ne3 "reeMa#();
1clear();
}
}
which two statements, added independently at be!innin! of the pro!ram, allow the code to
compile?
1. :o statement is re4uired
2. import +a%a.util.(;
$. import.+a%a.util.,ree(;
&. import +a%a.util.,ree/et;
'. import +a%a.util.,reeMap;
A.1 only B. and #
C." and $ D." and #
Core Java
#.%hat will be the output of the pro!ram?
class "3o
{
b0te ';
}
class PassA
{
#ublic static $oid ain(String %& args)
{
PassA # ( ne3 PassA();
#1start();
}
$oid start()
{
"3o t ( ne3 "3o();
S0ste1out1#rint(t1' 4 . .);
"3o t) ( fi'(t);
S0ste1out1#rintln(t1' 4 . . 4 t)1');
}
"3o fi'("3o tt)
{
tt1' ( F);
return tt;
}
}
A.null null $ B.0 0 $
C.0 $ $ D.0 0 0
Core Java
3.%hat will be the output of the pro!ram?
#ublic class 2f)
{
static boolean b,, b);
#ublic static $oid ain(String %& args)
{
int ' ( *;
if ( Cb, ) /* =ine @ */
{
if ( Cb) ) /* =ine G */
{
b, ( true;
'44;
if ( - 5 K )
{
'44;
}
if ( Cb, )
' ( ' 4 ,*;
else if ( b) ( true ) /* =ine ,G */
' ( ' 4 ,**;
else if ( b, I b) ) /* =ine ), */
' ( ' 4 ,***;
}
}
S0ste1out1#rintln(');
}
}
A.0 B.1
C.101 D.111
Core Java
9.%hat will be the output of the pro!ram?
#ublic class 2f,
{
static boolean b;
#ublic static $oid ain(String %& args)
{
short hand ( F);
if ( hand + -* LL Cb ) /* =ine @ */
hand44;
if ( hand 5 -* ); /* =ine G */
else if ( hand 5 F* )
{
hand 4( @;
hand44;
}
else
EEhand;
S0ste1out1#rintln(hand);
}
}
A.$1 B.$
C.#0 D.#1
Core Java
-.%hat will be the output of the pro!ram?
int i ( A;
3hile(,)
{
if(i (( F)
{
breaM;
}
44i;
}
S0ste1out1#rintln(.i ( . 4 i);
A.i * 0 B.i * "
C.i * $ D.Compilation fails.
Core Java
;.%hat will be the output of the pro!ram?
int i ( *, B ( -;
t#/ for (;;)
{
i44;
for (;;)
{
if(i 5 EEB)
{
breaM t#;
}
}
S0ste1out1#rintln(.i (. 4 i 4 ., B ( . 4 B);
A.i * 1, A * 0 B.i * 1, A * $
C.i * ", A * $ D.Compilation fails.
Core Java
"0.%hat will be the output of the pro!ram?
int 2 ( *;
label/
if (2 + )) {
S0ste1out1#rint(.2 is . 4 2);
244;
continue label;
}
A.) is 0
B.) is 0 ) is 1
C.Compilation fails.
D.:one of the above
Core Java
"1.%hat will be the output of the pro!ram?
tr0
{
int ' ( *;
int 0 ( - / ';
}
catch (7'ce#tion e)
{
S0ste1out1#rintln(.7'ce#tion.);
}
catch (Arithetic7'ce#tion ae)
{
S0ste1out1#rintln(. Arithetic 7'ce#tion.);
}
S0ste1out1#rintln(.finished.);
A.finished B.E/ception
C.Compilation fails. D.Arithmetic E/ception
Core Java
".%hich statement is true?
A.A try statement must have at least one correspondin! catch bloc'.
B.Fultiple catch statements can catch the same class of e/ception more than once.
C.
An Error that mi!ht be thrown in a method must be declared as thrown by that method,
or be handled within that method.
D.
E/cept in case of @F shutdown, if a tr# bloc' starts to e/ecute, a correspondin! finally
bloc' will always start to e/ecute.
Core Java
"".
class "est,
{
#ublic int $alue;
#ublic int hash:ode() { return F); }
}
class "est)
{
#ublic int $alue;
#ublic int hashcode() { return (int)($alueN-); }
}
which statement is true?
A.class ,est1 will not compile.
B.&he ,est1 hash0ode() method is more efficient than the ,est2 hash0ode() method.
C.&he ,est1 hash0ode() method is less efficient than the ,est2 hash0ode() method.
D.class ,est2 will not compile.
Core Java
"$.%hat will be the output of the pro!ram?
#ublic class Ooo
{
Ooo()
{
S0ste1out1#rint(.foo.);
}

class 9ar
{
9ar()
{
S0ste1out1#rint(.bar.);
}
#ublic $oid go()
{
S0ste1out1#rint(.hi.);
}
} /* class 9ar ends */
#ublic static $oid ain (String %& args)
{
Ooo f ( ne3 Ooo();
f1aMe9ar();
}
$oid aMe9ar()
{
(ne3 9ar() {})1go();
}
}/* class Ooo ends */
A.Compilation fails.
B.An error occurs at runtime.
C.)t prints GfoobarhiG
D.)t prints GbarhiG
Core Java
"#.
class 9ar { }
class "est
{
9ar do9ar()
{
9ar b ( ne3 9ar(); /* =ine K */
return b; /* =ine @ */
}
#ublic static $oid ain (String args%&)
{
"est t ( ne3 "est(); /* =ine ,, */
9ar ne39ar ( t1do9ar(); /* =ine ,) */
S0ste1out1#rintln(.ne39ar.);
ne39ar ( ne3 9ar(); /* =ine ,F */
S0ste1out1#rintln(.finishing.); /* =ine ,- */
}
}
At what point is the 1ar obAect, created on line 3, eli!ible for !arba!e collection?
A.after line 1
B.after line 1$
C.after line 9, when do1ar() completes
D.after line 1#, when main() completes
Core Java
"3.
class "est
{
#ri$ate Deo d;
$oid start()
{
d ( ne3 Deo();
this1taMeDeo(d); /* =ine @ */
} /* =ine > */
$oid taMeDeo(Deo deo)
{
deo ( null;
deo ( ne3 Deo();
}
}
%hen is the Demo obAect eli!ible for !arba!e collection?
A.After line 9
B.After line -
C.After the start() method completes
D.%hen the instance runnin! this code is made eli!ible for !arba!e collection.
Core Java
"9.
#ublic class 8
{
#ublic static $oid ain(String %& args)
{
8 ' ( ne3 8();
8 ') ( ,('); /* =ine K */
8 'F ( ne3 8();
') ( 'F; /* =ine > */
do:o#le'Stuff();
}
static 8 ,(8 ')
{
' ( ne3 8();
return ';
}
}
After line - runs. how many obAects are eli!ible for !arba!e collection?
A.0 B.1
C. D."
Core Java
"-.
#ublic class "est
{
#ublic $oid foo()
{
assert false; /* =ine - */
assert false; /* =ine K */
}
#ublic $oid bar()
{
3hile(true)
{
assert false; /* =ine ,) */
}
assert false; /* =ine ,F */
}
}
%hat causes compilation to fail?
A.1ine #
B.1ine 3
C.1ine 1
D.1ine 1$
Core Java
";.%hat will be the output of the pro!ram?
#ublic class SDrt7'a#le
{
#ublic static $oid ain(String %& args)
{
double $alue ( EG1*;
S0ste1out1#rintln( Math1sDrt($alue));
}
}
A.".0
B.(".0
C.:a:
D.Compilation fails.
Core Java
$0.%hat will be the output of the pro!ram?
String a ( .ne3s#a#er.;
a ( a1substring(-,@);
char b ( a1charAt(,);
a ( a 4 b;
S0ste1out1#rintln(a);
A.apa
B.app
C.apea
D.apep

S-ar putea să vă placă și