You are on page 1of 51

1) Class C { public static void main(String[] args) { int[]a1[]=new int[3][3]; //3 int a [!]={3"!"#"$%; //!

int a [#]; //# %% &'at is t'e result o( attempting to compile and run t'e program )* 1*compiletime error at lines 3"!"# *compiltime error at line !"# 3*compiletime error at line 3 !*+untime ,-ception #*.one o( t'e above /ns0 ,-planation0 no value s'oud be speci(ied in t'e rig'tsidebrac1ets w'en constructing an arra2

) inter(ace 3{ void (1(); // 1

public void ( (); // protected void (3(); // 3 private void (!(); // ! % w'ic' lines generate compile time errors) 1*compiletime error at lines 1" "3"! *compiletime error at line 3 3*compiletime error at line 1 !*compiletime error at lines 3"! #*.one o( t'e above /nswer0 ! ,-planation0 all met'ods declared wit'in an inter(ace are implicitl2 public" a wea1er access level can not be declared* 3) class C{ int i; public static void main (String[] args) { int i; //1 private int a = 1; // protected int b = 1; //3 public int c = 1; //!

S2stem*out*println(a4b4c); //# %% 1*compiletime error at lines 1" "3"!"# compiletime error at lines "3"!"# 3*compiletime error at lines "3"! !*prints 3 #*.one o( t'e above /nswer ,-planation0 5'e access modi(iers public" protected and private" can not be applied to variables declared inside met'ods* !) class C { public static void main (String[] a1) { S2stem*out*print(a1[1] 4 a1[ ] 4 a1[3]); %% &'at is t'e result o( attempting to compile and run t'e program)

6ava command / 7 C 1*8rints0 /7C *8rints 7C and +untime ,-ception 3*8rints0 7C9 !*+untime ,-ception

#*.one o( t'e above /nswer ,-planation0 arra2 inde- outo( bounds e-ception onl2 till a1[ ] is allowed* #) class C{ static int s; public static void main(String a[]){ C ob6=new C(); ob6*m1(); S2stem*out*println(s); % void m1(); { int -=1; m (-); S2stem*out*println(-4::); % void m (int -){ -=-; ; s=-; %%

1*prints 1" *prints "< 3*prints " !*compile time error #*.oneo(t'e above /nswer0 1 ,-planation0 =nl2 ob6ects and arra2s are passed b2 re(erence*ot'er are passed b2 value*s is a static variable w'ic' is global to t'e class $) class C { public static void main(String[] args) { int i1=1; switc'(i1){ case 10 S2stem*out*println(:one:); case 0

S2stem*out*println(:two:); case 30 S2stem*out*println(:t'ree:); %%% &'at is t'e result o( attempting to compile and run t'e program)

1*prints one two t'ree *prints one 3*compile time error !*+untime e-ception( #*.one o( t'e above /nswer0 1 ,-planation0 5'ere is no brea1 statement in case 1 so it causes t'e below case statements to e-ecute regardless o( t'eir values >) ,ac' element must be uni?ue 9uplicate elements must not replace old elements* ,lements are not 1e2/value pairs* /ccessing an element can be almost as (ast as per(orming a similar operation on an arra2* &'ic' o( t'ese classes provide t'e speci(ied (eatures) 1*@in1ed@ist *5reeAap 3*Bas'Aap !*Bas'Set #*.one o( t'e above /nswer0 ! C)

class C1 { static inter(ace 3 { static class C { % % public static void main(String a[]) { C1*3*C ob1=new C1*3*C ();

S2stem*out*println(:ob6ect created:); % %

&'at is t'e result o( attempting to compile and run t'e program) 1*prints ob6ect created *Compile time error 3*+untime ,-cepion !*.one o( t'e above /nswer0 1 ,-planation0

/ static inter(ace or class can contain static members*Static members can be accessed wit'out instantiating t'e particular class D) class C1 { static class C { static int i1; % public static void main(String a[]) { S2stem*out*println(C1*C *i1); % %

&'at is t'e result o( attempting to compile and run t'e program) 1*prints < *Compile time error 3*+untime e-ception !*.one o( t'e above /nswer0 1 ,-planation0

static members can be accessed wit'out instantiating t'e particular class 1<) / signed data t2pe 'as an e?ual number o( nonEFero positive and negative values available 1*true *(alse /nswer0 ,-planation0 5'e range o( negative numbers is greater b2 1 t'an t'e range o( positive numbers

11) class C{ static int (1(int i) { S2stem*out*print(i 4 :":); return <; % public static void main (String[] args) { int i = <; i = i44 4 (1(i); S2stem*out*print(i); %%

8rints0 <"< 8rints0 1"< 8rints0 <"1

CompileEtime error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 ) class C{ static String m(int i) {return :int:;% static String m((loat i) {return :(loat:;% public static void main (String[] args) { long a1 = 1; double b1 = ; S2stem*out*print(m(a1)4:":4 m(b1)); %%

8rints0 (loat"double 8rints0 (loat"(loat 8rints0 double"(loat CompileEtime error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 ! 13) class C { public static void main(String a[]) {

C c1=new C(); C c =m1(c1); C c3=new C(); c =c3; //$ anot'ermet'od(); % static C m1(C ob1){ ob1 =new C(); return ob1; % % /(ter line $" 'ow man2 ob6ects are eligible (or garbage collection)

/ns0 1 /ns0 /ns0 3 /ns0 ! .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1!) 1* String7u((er s1 = new String7u((er(:abc:); * String7u((er s = s1; 3* String7u((er s3 = new String7u((er(:abc:); Bow man2 ob6ects are created )

< /ns0 1

/ns0 /ns0 3 ,-planation0 .o ,-planation /vailable /ns0 ! 1#) class c { { S2stem*out*println(:initialiFer:); % public static void main(String a[]) { S2stem*out*println(:main:); c ob1=new c (); % %

prints main and initialiFer prints initialiFer and main compile time error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 1$) class c1 {

public static void main(String a[]) { c1 ob1=new c1(); =b6ect ob =ob1; S2stem*out*println(ob instanceo( =b6ect); S2stem*out*println(ob instanceo( c1); % %

8rints true"(alse 8rint (alse"true 8rints true"true compile time error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 3 1>) class / e-tends 5'read { private int i; public void run() {i = 1;% public static void main(String[] args) { / a = new /(); a*run(); S2stem*out*print(a*i); %%

8rints not'ing 8rints0 1

8rints0 <1 CompileEtime error .one o( t'e above ,-planation0 a*run() met'od was called instead o( a*start(); so t'e (ull program runs as a single t'read so a*run() is guaranteed to complete /ns0 1C) class bi1e { % class arr e-tends bi1e{ public static void main(String[] args) { arr[] a1=new arr[ ]; bi1e[] a ; a =a1; //3 arr[] a3; a3=a1; //# %%

compile time error at line 3 compile time error at line # +untime e-ception 5'e code runs (ine .one o( t'e above ,-planation0 bi1e is t'e superclass o( arr*so t'e2 are compatible(superob6ect=subob6ect) but subob6ect=superob6ect not allowed

/ns0 ! 1D) class C{ public static void main (String[] args) { String s1=:'6'':; // 1 String s =:Gu<<< :; // String s3=:HGGH:; //3 %%

compile time error at line 1 compile time error at line compile time error at line 3 +untime e-ception t'e code runs wit'out an2 error ,-planation0 / String literal is a se?uence o( c'aracters enclosed in double ?uotes /ns0 # <) &'ic' data t2pe is wider (or t'e purpose o( casting0 (loat or long)

(loat long ,-planation0 (loat is wider t'an long" because t'e entire range o( long (its wit'in t'e range o( (loat*

/ns0 1

1) class C{ public static void main (String[] args) { b2te b1=33; //1 b144; // b2te b =##; //3 b =b141; //! S2stem*out*println(b14::4b ); %%

compile time error at line compile time error at line prints 3!"#$ runtime e-ception none o( t'e above ,-planation0 b141 returns an integer value w'ic' can not be assigned to a b2te variable /ns0 ) import 6ava*util*;; class C { (inal Iector v; C() { !

v=new Iector(); % C(int i) { % public void someAet'od() { S2stem*out*println(v*is,mpt2()); % %

compile time error runtime e-ception t'e code compiles and runs (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 3) class C1{ public void m1(){ // 1 % % class C e-tends C1{ // private void m1(){ % %

compile time error at line1 compile time error at line

+untime e-ception .one o( t'e above ,-planation0 tending to assign wea1er access not allowed /ns0 !) inter(ace 3{ int i; // line 1 % class C implements 3{ public static void main(String a[]){ S2stem*out*println(i); S2stem*out*println(i44); //line % %

compile time error at line 1" compile time error at line +untime e-ception .oneo(t'e above ,-planation0 inter(ace constants are (inal so"t'e2 must be initialiFed w'en declaring it and t'e2 can not be altered /ns0 1 #) /n abstract class must 'ave at least one abstract met'od

true true ,-planation0 /n abstract wit'out abstract met'ods is allowed /ns0 $) JK[C9/5/[ class C { public static void main(String[] args) { boolean b1; b1=3J!J#; //1 S2stem*out*println(b1); // %% ]]L

compile time error at line 1 compile time error at line +untime e-ception .one o( t'e above ,-planation0 JK[C9/5/[ 3J!J# evaulates to trueJ# EELitHs a wrong e-pression so it results in compiletime error ]]L /ns0 1 >) class C{

public static void main(String[] args) { tr2 { int i1=3/<; % catc'(,-ception e) { S2stem*out*println(:e-ception1:); % catc'(.ull8ointer,-ception e) { S2stem*out*println(:e-ception :); % (inall2 { S2stem*out*println(:(inall2:); % %%

compile time error runtime e-ception prints e-ception1 and (inall2 prints e-ception1"e-ception .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 C) class C { public static void main(String[] args) { and (inall2

c'ar c1=$#; switc'(c1){ case H/H0 S2stem*out*println(:one:); de(ault0 S2stem*out*println(:two:); case HbH0 S2stem*out*println(:t'ree:); %%%

prints one twot 'ree prints two t'ree compile time error +untime e-ception .one o( t'e above ,-planation0 c'ar is a legal value (or switc' clause /ns0 1 D) class c1{ void go(){% % class c e-tends c1 { String go() { return null; % %

compile time error

runtime e-ceptione t'e code compiles and runs (ine .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 3<) class base { base(int c) { S2stem*out*println(:base:); % % class Super e-tends base { Super() { S2stem*out*println(:super:); % public static void main(String [] a) { base b1=new Super(); % %

compile time error runtime e-ceptione t'e code compiles and runs (ine .one o( t'e above

,-planation0 .o ,-planation /vailable /ns0 1

31) class c { (inal int i1; c () { i1=i141; % { i1= ; % public static void main(String a[]) { c ob1=new c (); S2stem*out*println(ob1*i1); % %

compile time error prints 3 prints none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 3 )

class C{ public static void main(String a[]) int i1=D; int i ; i((i1L3) { i =C; % S2stem*out*println(i ); %%

compile time error +untim error prints C prints < .one o( t'e above ,-planation0 since variable i1 is not (inal t'e value is not 1nown at compiletime itsel(*so generate compile time error /ns0 1 33) class / e-tends 5'read { public void run() { S2stem*out*print(:/:); % % class 7 { public static void main (String[] args) { / a = new /(); a*start(); a*start(); // 1 % %

compile time error +untime ,-ception t'e code compile and runs (ine none o( t'e above ,-planation0 3( t'e start met'od is invo1ed on a t'read t'at is alread2 running" t'en an 3llegal5'readState,-ception will probabl2 be t'rown /ns0 3!) class c1 { static{ S2stem*out*println(:static:); % public static void main(String a[]) { S2stem*out*println(:main:); % %

prints static main prints main prints main static compiletime error none o( t'e above ,-planation0

.o ,-planation /vailable /ns0 1 3#) class /{ static String m((loat i) {return :(loat:;% static String m(double i) {return :double:;% public static void main (String[] args) { int a1 = 1; long b1 = ; S2stem*out*print(m(a1)4:":4 m(b1)); %%

prints (loat"(oat prints (loat"double prints double"double compile time error .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 3$) &'en a b2te is added to a c'ar" w'at is t'e t2pe o( t'e result)

b2te int long

non o( t'e above ,-planation0 5'e result o( all arit'metic per(ormed wit' t'e binar2 operators (not t'e assignment operators) is an int" a long" a (loat" or a double* Bere b2te and c'ar are promoted to int" so t'e result is an int* /ns0 3>)

class C e-tends 5'read{ public static void main(String argv[]){ C b = new C(); b*run(); % public void start(){ (or (int i = <; i J1<; i44){ S2stem*out*println(:Ialue o( i = : 4 i); % % %

compile time error runtime ,-ception prints values (rom < to D .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !

3C) &'at is t'e signature o( t'e run() met'od o( t'e +unnable inter(ace)

void run() public void run(+unnable target) public void run() public static void run() ,-planation0 .o ,-planation /vailable /ns0 3 3D) class C{ public static void main(String args[]) { int a = 1; a 4= 44a 4 a44; S2stem*out*print(a); %%

compile time error +untime ,-ception 8rints # 8rints ! .one o( t'e above ,-planation0 .o ,-planation /vailable

/ns0 3 !<) inter(ace 3 { //1 public class 3nner { /// 3nner ( ) { S2stem *out * println ( :3nner Created: ) ; % %; %;

compile time error at line 1 compile time error at line t'e code compiles (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 3

!1) abstract class C1{ public void m1(){ %% abstract class C { public void m (){ %%

//1

//

compile time error at line1

compile time error at line 5'e code compiles (ine .one o( t'e above ,-planation0 since t'e class C /ns0 3 ! ) inter(ace 3{ void (1(); // 1 public void ( (); // protected void (3(); // 3 private void (!(); // ! abstract void (#(); // # %

is abstract it can contain abstract met'ods

line 1" "3"! line 3"! line 3 line "3"!

line 3"!"# ,-planation0 all met'ods declared wit'in an inter(ace are implicitl2 public" a wea1er access level can not be declared /ns0 !3) abstract class ve'icle{ abstract public void speed();

% class car e-tends ve'icle{ public static void main (String args[]) { ve'icle ob1; ob1=new car(); //1 %%

compiletime error at line 1 (orces t'e class car to be declared as abstract +untime ,-ception .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !!) class command { public static void main (String[] a1) { S2stem*out*println(a1*lengt'()); //1 S2stem*out*println(a1[<]); // S2stem*out*println(a1); //3 // %%

compile time error at line1 compile time error at line compile time error at line3 +untime e-ception

,-planation0 lengt' is not a met'od* itHs a variable /ns0 1 !#) abstract class / {% // 1 transient class 7 {% // private class C {% // 3 static class 9 {% // ! &'ic' o( t'ese declarations will not produce a compileEtime error)

/ns0 1 /ns0 /ns0 3 /ns0 ! .one o( t'e above ,-planation0 5'e modi(iers" private and static" can be applied to a nested class" but can not be applied to a class t'at is not nested transient is allowed onl2 (or variables* /ns0 1 !$) class c1 { public void m1(=b6ect o1) { S2stem*out*println(:ob6ect:); % public void m1(String o1) {

S2stem*out*println(:string:); % public int m1(int c) { return c; % public static void main(String a[]) { c1 ob1=new c1(); ob1*m1(:'ai:);

% %

print ob6ect prints string compile time error non o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !>) public static double sin(double angle) &'at are t'e units o( t'e :angle: argument)

9egrees +adians 7ot'

.one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 !C) class base { base() { S2stem*out*println(:base:); % base(int i1) { % % class Super e-tends base { Super() { S2stem*out*println(:super:); super(1); % public static void main(String [] a) { base b1=new Super(); % %

compile time error prints base and super prints super and base none o( t'e above

,-planation0 .o ,-planation /vailable /ns0 1 !D) 5'e 5'rowable class is t'e superclass o( all e-ceptions in t'e Mava language*

true (alse ,-planation0 .o ,-planation /vailable /ns0 1 #<) class C { private String get ( String str ) { tr2 { t'row new ,-ception ( ) ; return str ; % catc' ( ,-ception e ) { return null ; % % public static void main ( String peace [ ] ) { tr2 { S2stem*out*println ( ( new C ( ) )*get ( : C : ) ) ; % catc' ( ,-ception e )

{ S2stem*out*println( :,-ception: ) ; % % %;

compile time error prints ,-ception t'e code compiles and runs (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1

#1) &'ic' o( t'e (ollowing met'ods are static members o( t'e 5'read class)

6oin run sleep start wait ,-planation0 .o ,-planation /vailable

/ns0 3 # ) class C { public static void main(String[] args) { tr2 { int i1=3/<; % S2stem*out*println(:'ai:); catc'(.ullpointer,-ception e) { % (inall2 { S2stem*out*println(:(inall2); % %%

compile time errors print 'ai and (inall2 t'e code compiles and runs (ine none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 #3) class c1 { public static void main(String a[]) {

S2stem*out*println(9ouble*.a.==9ouble*.a.); S2stem*out*println(9ouble*.a.K=9ouble*.a.); S2stem*out*println(Nloat*.a.==9ouble*.a.); % %

prints (alse true (alse print true (alse true prints true true true prints (alse (alse (alse compiletime error ,-planation0 .o ,-planation /vailable /ns0 1 #!) class C { public static void main ( String 1a [ ] ) { 5'read t = 5'read * current5'read ( ) ; t * set8riorit2 ( E 1 ) ; S2stem * out * println ( : 9one K : ) ; % %;

compile time error +untime ,-ception 5'e code compiles and runs (ine .one o( t'e above

,-planation0 .o ,-planation /vailable /ns0 ##) class c1 { public void m1() { S2stem*out*println(:m1 met'od in C1 class:); % % class c { public c1 m1() { return new c1(){ public void m1() { S2stem*out*println(:m1 me'od in anon2mous class:); %%;% public static void main(String a[]) { c1 ob1 =new c ()*m1(); ob1*m1(); %%

prints m1 met'od in C1 class prints m1 met'od in anonumous class compile time error +untime error none o( t'e above

,-planation0 t'e anon2mous class overrides t'e m1 met'od in c1*so according to t'e d2namic dispatc' t'e m1 met'od in t'e anon2mous is called /ns0 #$) class c1 e-tends ,-ception {% class c { static void m1() { t'row new c1(); % public static void main(String a[]) { % %

compile time error +untime ,-ception 5'e code compiles and runs (ine .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 #>) class C { static void m1(=b6ect -) {S2stem*out*print(:=b6ect:);% static void m1(String -) {S2stem*out*print(:String:);%

public static void main(String[] args) { m1(null); %%

8rints =b6ect 8rints String compiletime error .one o( t'e above ,-planation0 5'e more speci(ic o( t'e two" m(String -)" is c'osen /ns0 #C) all classes o( t'e 6ava*lang pac1age are automaticall2 imported

true (alse ,-planation0 .o ,-planation /vailable /ns0 1 #D) 5'e constructor (or t'e Aat' class is private" so it cannot be instaniated

true (alse

,-planation0 .o ,-planation /vailable /ns0 1 $<) class C { public static void main(String[] args) { int -= ; int 2=3; i(((2==-44)O(-J442)){ S2stem*out*println(-4::42); % %%

prints 3! prints 33 compile time error +untime e-ception .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1

$1) inter(ace 3{ (inal class C1 { //1 static int i=D;; // %

% class C implements 3{ public static void main(String a[]){ S2stem*out*println(3*C1*i); ///3 %%

compile time error at line 1 compile time error at line compile time error at line 3 prints D +untime e-ception ,-planation0 inter(aces classes are b2 de(ault static"(inal* so"no compile time errors are genearated /ns0 ! $ ) class C{ public static void main(String[] args) { tr2 { tr2 { tr2 { % catc'(+untime,-ception e) { % % catc'(,-ception e) { %

% catc'(.ull8ointer,-ception e) { % (inall2 { S2stem*out*println(:(inall2:); % %%

8rints (inall2 compile time error +untime ,-ception .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 $3) class C { public static void main ( String 1a [ ] ) { w'ile ( (alse ) { S2stem*out*println ( :Ialue: ) ; % % %

compile time error prints Ialue in(initel2 +untime ,-ception

.one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 $!) class C { public static void main(String[] args) { S2stem*out*println(!4#4:String:); %%

prints Dstring prints !#string compile time error +untime e-ception .one o( t'e above ,-planation0 arguments to t'e met'od are evalutated (rom le(t to rig't so !4#4:string: ==L D4string==LDstring /ns0 1 $#) class B { public static void main (String[] args) { String s1 = :BBB:; String7u((er sb = new String7u((er(s1); S2stem*out*print(sb *e?uals(s1) 4 :": 4 s1*e?uals(sb )); %%

8rints0 (alse"(alse 8rints0 true"(alse 8rints0 (alse"true 8rints0 true"true .one o( t'e above ,-planation0 s1 and sb are pointing to di((erent ob6ect re(erences /ns0 1 $$) 5'e relations'ip between a class and its superclass is

'asEa is Ea .one o( t'e above ,-planation0 .o ,-planation available /ns0 $>) class / e-tends 5'read { private int i; public void run() {i = 1;% public static void main(String[] args) { / a = new /(); a*run(); S2stem*out*print(a*i);

%% Bow man2 t'reads are created in t'is 8rogram)

/ns0 1 /ns0 /ns0 3 < .one o( t'e above ,-planation0 Aain t'read is a t'read and calling run met'ods will not creat t'read* /ns0 1 $C) String ob6ects once created can not be modi(ied

true (alse ,-planation0 .o ,-planation available /ns0 1 $D) &'ic' o( t'e (ollowing modi(iers can be applied to a class t'at is not a nested class)

public

protected private static ,-planation0 .o ,-planation /vailable /ns0 1

><) i((<*< == E<*<) { S2stem*out*println(:true:); % else{ S2stem*out*println(:(alse:); %

prints (alse prints true ,-planation0 .o ,-planation available /ns0 >1) class C { public static void main(String[] args) { double d1 = Aat'*(loor(<*#); double d = Aat'*(loor(1*#); S2stem*out*print(d1 4 :": 4 d ); %%

8rints0 <*<"1*< 8rints0 <*<" *< 8rints0 1*<"1*< 8rints0 1*<" *< .one o( t'e above ,-planation0 .o ,-planation /vailable /ns0 1 > ) S2stem*out*println(:String:*substring(<"!)); 5'is statement will 8rint

will print :Strin: will print :Stri: will cause compiler error none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 >3) i((:String:*replace(HtH"H5H) == :String:*replace(HtH"H5H)) S2stem*out*println(:,?ual:); else S2stem*out*println(:.ot ,?ual:);

will 8rint ,?ual will 8rint .ot ,?ual compile time error none o( t'e above ,-planation0 .o ,-planation /vailable /ns0 >!) &'ic' o( t'e (ollowing classes will not allow uns2nc'roniFed read operations b2 multiple t'reads)

Iector 5reeAap 5reeSet Bas'Aap Bas'Set ,-planation0 .o ,-planation /vailable /ns0 1 >#) String7u((er ob6ects once created can not be modi(ied

true

(alse ,-planation0 .o ,-planation available /ns0

You might also like