Sunteți pe pagina 1din 48

javace rt if icat io ns.

ne t

http://www.javacertificatio ns.net/javacert/scjp1.6Mo ck.jsp

Java Certification scjp 1.6 scjp 6.0 ocpjp 6 Mock Exam Practice Questions
Java Certif ications

JavaCertif ications.net provides java certf ication tutorials and mock questions.

Tutorials
OCPJP/

OCPJP 6 Free Mock Exam Practice Questions


Quest ions no -1 What is t he out put for t he below code ? class A implement s Runnable{ public void run(){ Syst em.out .print ln(Thread.current Thread().get Name()); } } public class Test { public st at ic void main(St ring... args) { A a = new A(); Thread t = new Thread(a); Thread t 1 = new Thread(a); t .set Name("t "); t 1.set Name("t 1"); t .set Priorit y(10); t 1.set Priorit y(-3); t .st art (); t 1.st art (); } }

Options are A.t t1 B.t1 t C.t t D.Compilation succeed but Runtime Exception Answer : D is the correct answer. T hread priorities are set using a positive integer, usually between 1 and 10. t1.setPriority(-3); throws java.lang.IllegalArgumentException.

ads

Quest ions no -2 What is t he out put for t he below code ? class A implement s Runnable{ public void run(){ Syst em.out .print ln(Thread.current Thread().get Name()); } } 1. public class Test { 2. public st at ic void main(St ring... args) { 3. A a = new A(); 4. Thread t = new Thread(a); 5. t .set Name("good"); 6. t .st art (); 7. } 8. }

Options are A.good B.null C.Compilation f ails with an error at line 5 D.Compilation succeed but Runtime Exception Answer : A is the correct answer. T hread.currentT hread().getName() return name of the current thread.

Quest ions no -3 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring[] args) { Boolean expr = t rue; if (expr) { Syst em.out .print ln("t rue"); } else { Syst em.out .print ln("false"); } } }

opt ions A)t rue B)Compile Error - can't use Boolean object in if(). C)false D)Compile Properly but Runt ime Except ion. Correct answer is : A Explanat ions : In t he if st at ement , condit ion can be Boolean object in jdk1.5 and jdk1.6. In t he previous version only boolean is allowed.

Quest ions no -4 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring[] args) { List list = new ArrayList (); list .add(0, 59); int t ot al = list .get (0); Syst em.out .print ln(t ot al); } }

opt ions A)59 B)Compile t ime error, because you have t o do int t ot al = ((Int eger)(list .get (0))).int Value(); C)Compile t ime error, because can't add primit ive t ype in List . D)Compile Properly but Runt ime Except ion. Correct answer is : A Explanat ions :Manual conversion bet ween primit ive t ypes (such as an int ) and wrapper classes (such as Int eger) is necessary when adding a primit ive dat a t ype t o a collect ion in jdk1.4 but The new aut oboxing/unboxing feat ure eliminat es t his manual conversion in jdk 1.5 and jdk 1.6.

Quest ions no -3 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring[] args) { Int eger i = null; int j = i; Syst em.out .print ln(j); } } opt ions A)0 B)Compile wit h error C)null D)NullPoint erExcept ion Correct answer is : D Explanat ions :An Int eger expression can have a null value. If your program t ries t o aut ounbox null, it will t hrow a NullPoint erExcept ion.

Quest ions no -4 What is t he out put for t he below code ? public class Out er { privat e int a = 7; class Inner { public void displayValue() { Syst em.out .print ln("Value of a is " + a); } }

public class Test { public st at ic void main(St ring... args) t hrows Except ion { Out er mo = new Out er(); Out er.Inner inner = mo.new Inner(); inner.displayValue(); } }

opt ions A)Value of a is 7 B)Compile Error - not able t o access privat e member. C)Runt ime Except ion D)Value of a is 8 Correct answer is : A Explanat ions : An inner class inst ance can never st and alone wit hout a direct relat ionship t o an inst ance of t he out er class. you can access t he inner class is t hrough a live inst ance of t he out er class. Inner class can access privat e member of t he out er class.

Quest ions no -5 What is t he out put for t he below code ? public class B { public St ring get Count ryName(){ ret urn "USA"; } public St ringBuer get Count ryName(){ St ringBuer sb = new St ringBuer(); sb.append("UK"); ret urn sb; } public st at ic void main(St ring[] args){ B b = new B(); Syst em.out .print ln(b.get Count ryName().t oSt ring()); } } opt ions A)Compile wit h error B)USA C)UK D) Runt ime Except ion Correct answer is : A Explanat ions : You cannot have t wo met hods in t he same class wit h signat ures t hat only dier by ret urn t ype.

Quest ions no -6 What is t he out put for t he below code ? public class C { } public class D ext ends C{ } public class A { public C get OBJ(){ Syst em.out .print ln("class A - ret urn C"); ret urn new C(); } } public class B ext ends A{ public D get OBJ(){ Syst em.out .print ln("class B - ret urn D"); ret urn new D(); } } public class Test { public st at ic void main(St ring... args) { A a = new B(); a.get OBJ(); } }

opt ions A)Compile wit h error - Not allowed t o override t he ret urn t ype of a met hod wit h a subt ype of t he original t ype. B)class A - ret urn C C)class B - ret urn D D) Runt ime Except ion Correct answer is : C Explanat ions : From J2SE 5.0 onwards. You are now allowed t o override t he ret urn t ype of a met hod wit h a subt ype of t he original t ype.

Quest ions no -7 What is t he out put for t he below code ? public class A { public St ring get Name() t hrows ArrayIndexOut OfBoundsExcept ion{ ret urn "Name-A"; } } public class C ext ends A{ public St ring get Name() t hrows Except ion{ ret urn "Name-C"; } } public class Test { public st at ic void main(St ring... args) { A a = new C(); a.get Name(); } } opt ions A)Compile wit h error B)Name-A C)Name-C D)Runt ime Except ion Correct answer is : A Explanat ions : Except ion Except ion is not compat ible wit h t hrows clause in A.get Name(). Overridden met hod should t hrow only same or sub class of t he except ion t hrown by super class met hod.

Quest ions no -8 What is t he out put for t he below code ? import java.ut il.regex.Mat cher; import java.ut il.regex.Pat t ern; public class Test { public st at ic void main(St ring... args) { Pat t ern p = Pat t ern.compile("a*b"); Mat cher m = p.mat cher("b"); boolean b = m.mat ches(); Syst em.out .print ln(b);

} }

opt ions A)t rue B)Compile Error C)false D)b Correct answer is : A Explanat ions : a*b means "a" may present zero or more t ime and "b" should be present once.

Quest ions no -9 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { St ring input = "1 sh 2 sh red sh blue sh"; Scanner s = new Scanner(input ).useDelimit er("\\s*sh\\s*"); Syst em.out .print ln(s.next Int ()); Syst em.out .print ln(s.next Int ()); Syst em.out .print ln(s.next ()); Syst em.out .print ln(s.next ()); s.close(); } }

opt ions A)1 2 red blue B)Compile Error - because Scanner is not dend in java. C)1 sh 2 sh red sh blue sh D)1 sh 2 sh red blue sh Correct answer is : A Explanat ions : java.ut il.Scanner is a simple t ext scanner which can parse primit ive t ypes and st rings using regular expressions.

Quest ions no -10 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { Pat t ern p = Pat t ern.compile("a{3}b?c*"); Mat cher m = p.mat cher("aaab"); boolean b = m.mat ches(); Syst em.out .print ln(b);

} }

opt ions A)t rue B)Compile Error C)false D)NullPoint erExcept ion Correct answer is : A Explanat ions : X? X, once or not at all X* X, zero or more t imes X+ X, one or more t imes X{n} X, exact ly n t imes X{n,} X, at least n t imes X{n,m} X, at least n but not more t han m t imes

Quest ions no -11 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { Pat t ern p = Pat t ern.compile("a{1,3}b?c*"); Mat cher m = p.mat cher("aaab"); boolean b = m.mat ches(); Syst em.out .print ln(b);

} }

opt ions A)t rue B)Compile Error C)false D)NullPoint erExcept ion Correct answer is : A Explanat ions : X? X, once or not at all X* X, zero or more t imes X+ X, one or more t imes X{n} X, exact ly n t imes X{n,} X, at least n t imes X{n,m} X, at least n but not more t han m t imes

Quest ions no -12 What is t he out put for t he below code ? public class A { public A() { Syst em.out .print ln("A"); } } public class B ext ends A implement s Serializable { public B() { Syst em.out .print ln("B"); } } public class Test { public st at ic void main(St ring... args) t hrows Except ion { B b = new B(); Object Out put St ream save = new Object Out put St ream(new FileOut put St ream("dat ale")); save.writ eObject (b); save.ush(); Object Input St ream rest ore = new Object Input St ream(new FileInput St ream("dat ale")); B z = (B) rest ore.readObject ();

} }

opt ions A)A B A B)A B A B C)B B D)A B Correct answer is : A Explanat ions :On t he t ime of deserializat ion , t he Serializable object not creat e new object . So const ruct or of class B does not called. A is not Serializable object so const ruct or is called.

Quest ions no -13 What is t he out put for t he below code ? public class A {} public class B implement s Serializable { A a = new A(); public st at ic void main(St ring... args){ B b = new B(); t ry{ FileOut put St ream fs = new FileOut put St ream("b.ser"); Object Out put St ream os = new Object Out put St ream(fs); os.writ eObject (b); os.close(); }cat ch(Except ion e){ e.print St ackTrace(); } } }

opt ions A)Compilat ion Fail B)java.io.Not SerializableExcept ion: Because class A is not Serializable. C)Run properly D)Compilat ion Fail : Because class A is not Serializable. Correct answer is : B Explanat ions :It t hrows java.io.Not SerializableExcept ion:A Because class A is not Serializable. When JVM t ries t o serialize object B it will t ry t o serialize A also because (A a = new A()) is inst ance variable of Class B. So t hows Not SerializableExcept ion.

Quest ions no -14 What is t he out put for t he below code running in t he same JVM? public class A implement s Serializable { t ransient int a = 7; st at ic int b = 9; } public class B implement s Serializable { public st at ic void main(St ring... args){ A a = new A(); t ry { Object Out put St ream os = new Object Out put St ream( new FileOut put St ream("t est .ser")); os.writ eObject (a); os. close(); Syst em.out .print ( + + a.b + " "); Object Input St ream is = new Object Input St ream(new FileInput St ream("t est .ser")); A s2 = (A)is.readObject (); is.close(); Syst em.out .print ln(s2.a + " " + s2.b); } cat ch (Except ion x) { x.print St ackTrace(); } } }

opt ions A)9 0 9 B)9 7 9 C)0 0 0 D)0 7 0 Correct answer is : A Explanat ions :t ransient variables are not serialized when an object is serialized. In t he case of st at ic variable you can get t he values in t he same JVM.

Quest ions no -15 What is t he out put for t he below code ? public enum Test { BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45); privat e int hh; privat e int mm; Test (int hh, int mm) { assert (hh >= 0 && hh <= 23) : "Illegal hour."; assert (mm >= 0 && mm <= 59) : "Illegal mins."; t his.hh = hh; t his.mm = mm; } public int get Hour() { ret urn hh; } public int get Mins() { ret urn mm; } public st at ic void main(St ring args[]){ Test t = new BREAKFAST; Syst em.out .print ln(t .get Hour() +":"+t .get Mins()); } }

opt ions A)7:30 B)Compile Error - an enum cannot be inst ant iat ed using t he new operat or. C)12:50 D)19:45 Correct answer is : B Explanat ions : As an enum cannot be inst ant iat ed using t he new operat or, t he const ruct ors cannot be called explicit ly. You have t o do like Test t = BREAKFAST;

Quest ions no -16 What is t he out put for t he below code ? public class Test { enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } enum Mont h { JAN, FEB } public st at ic void main(St ring[] args) { int [] freqArray = { 12, 34, 56, 23, 5, 13, 78 }; // Creat e a Map of frequencies Map ordinaryMap = new HashMap(); for (Day day : Day.values()) { ordinaryMap.put (day, freqArray[day.ordinal()]); } // Creat e an EnumMap of frequencies EnumMap frequencyEnumMap = new EnumMap(ordinaryMap); // Change some frequencies frequencyEnumMap.put (null, 100); Syst em.out .print ln("Frequency EnumMap: " + frequencyEnumMap); } } opt ions A)Frequency EnumMap: {MONDAY=12, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=78} B)Compile Error C)NullPoint erExcept ion D)Frequency EnumMap: {MONDAY=100, TUESDAY=34, WEDNESDAY=56, THURSDAY=23, FRIDAY=5, SATURDAY=13, SUNDAY=123} Correct answer is : C Explanat ions : The null reference as a key is NOT permit t ed.

Quest ions no -17 public class EnumTypeDeclarat ions { public void foo() { enum SimpleMeal { BREAKFAST, LUNCH, DINNER } } } Is t he above code Compile wit hout error ? opt ions A)Compile wit hout error B)Compile wit h error C)Compile wit hout error but Runt ime Except ion D)Compile wit hout error but Enum Except ion Correct answer is : B Explanat ions : An enum declarat ion is a special kind of class declarat ion: a) It can be declared at t he t op-level and as st at ic enum declarat ion. b) It is implicit ly st at ic, i.e. no out er object is associat ed wit h an enum const ant . c) It is implicit ly nal unless it cont ains const ant -specic class bodies, but it can implement int erfaces. d) It cannot be declared abst ract unless each abst ract met hod is overridden in t he const ant -specic class body of every enum const ant . e) Local (inner) enum declarat ion is NOT OK! Here in public void foo() { enum SimpleMeal { BREAKFAST, LUNCH, DINNER } } enum declarat ion is local wit hin met hod so compile t ime error.

Quest ions no -18 What is t he out put for t he below code ? public enum Test { int t ; BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45); privat e int hh; privat e int mm; Test (int hh, int mm) { assert (hh >= 0 && hh <= 23) : "Illegal hour."; assert (mm >= 0 && mm <= 59) : "Illegal mins."; t his.hh = hh; t his.mm = mm; } public int get Hour() { ret urn hh; } public int get Mins() { ret urn mm; } public st at ic void main(St ring args[]){ Test t = BREAKFAST; Syst em.out .print ln(t .get Hour() +":"+t .get Mins()); } }

opt ions A)7:30 B)Compile Error C)12:15 D)19:45 Correct answer is : B Explanat ions : The enum const ant s must be declared before any ot her declarat ions in an enum t ype. In t his case compile error because of declarat ion int t ; before enum declarat ion.

Quest ions no -19 What is t he out put for t he below code ? public class B ext ends Thread{ public st at ic void main(St ring argv[]){ B b = new B(); b.run(); } public void st art (){ for (int i = 0; i < 10; i++){ Syst em.out .print ln("Value of i = " + i); } } }

opt ions A)A compile t ime error indicat ing t hat no run met hod is dened for t he Thread class B)A run t ime error indicat ing t hat no run met hod is dened for t he Thread class C)Clean compile and at run t ime t he values 0 t o 9 are print ed out D)Clean compile but no out put at runt ime Correct answer is : D Explanat ions : This is a bit of a sneaky one as I have swapped around t he names of t he met hods you need t o dene and call when running a t hread. If t he for loop were dened in a met hod called public void run() and t he call in t he main met hod had been t o b.st art () The list of values from 0 t o 9 would have been out put .

Quest ions no -20 What is t he out put for t he below code ? public class Test ext ends Thread{ st at ic St ring sName = "good"; public st at ic void main(St ring argv[]){ Test t = new Test (); t .nameTest (sName); Syst em.out .print ln(sName); } public void nameTest (St ring sName){ sName = sName + " idea "; st art (); } public void run(){ for(int i=0;i < 4; i++){ sName = sName + " " + i; } } }

opt ions A)good B)good idea C)good idea good idea D)good 0 good 0 1 Correct answer is : A Explanat ions : Change value in local met hods wouldnt change in global in case of St ring ( because St ring object is immut able).

Quest ions no -21 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring argv[]){ Test 1 pm1 = new Test 1("One"); pm1.run(); Test 1 pm2 = new Test 1("Two"); pm2.run(); } } class Test 1 ext ends Thread{ privat e St ring sTname=""; Test 1(St ring s){ sTname = s; } public void run(){ for(int i =0; i < 2 ; i++){ t ry{ sleep(1000); }cat ch(Int errupt edExcept ion e){} yield(); Syst em.out .print ln(sTname); } } }

opt ions A)Compile error B)One One Two Two C)One Two One Two D)One Two Correct answer is : B Explanat ions : If you call t he run met hod direct ly it just act s as any ot her met hod and does not ret urn t o t he calling code unt il it has nished. execut ing

Quest ions no -22 What is t he out put for t he below code ? public class Test ext ends Thread{ public st at ic void main(St ring argv[]){ Test b = new Test (); b.st art (); } public void run(){ Syst em.out .print ln("Running"); } }

opt ions A)Compilat ion clean and run but no out put B)Compilat ion and run wit h t he out put "Running" C)Compile t ime error wit h complaint of no Thread import D)Compile t ime error wit h complaint of no access t o Thread package Correct answer is : B Explanat ions : The Thread class is part of t he core java.lang package and does not need any explicit import st at ement .

Quest ions no -23 What is t he out put for t he below code ? public class Tech { public void t ech() { Syst em.out .print ln("Tech"); } } public class At ech { Tech a = new Tech() { public void t ech() { Syst em.out .print ln("anonymous t ech"); } }; public void dot his() { a.t ech(); } public st at ic void main(St ring... args){ At ech at ech = new At ech(); at ech.dot his(); }

opt ions A)anonymous t ech B)Compile Error C)Tech D)anonymous t ech Tech Correct answer is : A Explanat ions : This is anonymous subclass of t he specied class t ype. Anonymous inner class ( anonymous subclass ) overriden t he Tech super class of t ech() met hod. Therefore Subclass met hod will get called.

Quest ions no -24 What is t he out put for t he below code ? public class Out er { privat e St ring x = "Out er variable"; void doSt u() { St ring z = "local variable"; class Inner { public void seeOut er() { Syst em.out .print ln("Out er x is " + x); Syst em.out .print ln("Local variable z is " + z); } } } }

opt ions A)Out er x is Out er variable. B)Compile Error C)Local variable z is local variable. D)Out er x is Out er variable Local variable z is local variable Correct answer is : B Explanat ions : Cannot refer t o a non-nal variable z inside an inner class dened in a dierent met hod. Quest ions no -25 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { for(int i = 2; i < 4; i++) for(int j = 2; j < 4; j++) assert i!=j : i; } } opt ions A)The class compiles and runs, but does not print anyt hing. B)The number 2 get s print ed wit h Assert ionError C)The number 3 get s print ed wit h Assert ionError D)compile error Correct answer is : B Explanat ions : When i and j are bot h 2, assert condit ion is false, and Assert ionError get s generat ed.

Quest ions no -26 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { for(int i = 2; i < 4; i++) for(int j = 2; j < 4; j++) if(i < j) assert i!=j : i; } } opt ions A)The class compiles and runs, but does not print anyt hing. B)The number 2 get s print ed wit h Assert ionError C)The number 3 get s print ed wit h Assert ionError D)compile error Correct answer is : A Explanat ions : When if condit ion ret urns t rue, t he assert st at ement also ret urns t rue. Hence Assert ionError not generat ed.

Quest ions no -26 What is t he out put for t he below code ? public class NameBean { privat e St ring st r; NameBean(St ring st r ){ t his.st r = st r; } public St ring t oSt ring() { ret urn st r; } } import java.ut il.HashSet ; public class CollClient { public st at ic void main(St ring ... sss) { HashSet myMap = new HashSet (); St ring s1 = new St ring("das"); St ring s2 = new St ring("das"); NameBean s3 = new NameBean("abcdef"); NameBean s4 = new NameBean("abcdef"); myMap.add(s1); myMap.add(s2); myMap.add(s3); myMap.add(s4); Syst em.out .print ln(myMap); } }

opt ions A)das abcdef abcdef B)das das abcdef abcdef C)das abcdef D)abcdef abcdef Correct answer is : A Explanat ions : Need t o implement 'equals' and 'hashCode' met hods t o get unique Set for user dend object s(NameBean). St ring object int ernally implement s 'equals' and 'hashCode' met hods t herefore Set only st ored one value.

Quest ions no -27 Synchronized resizable-array implement at ion of t he List int erface is _____________?

opt ions A)Vect or B)ArrayList C)Hasht able D)HashMap Correct answer is : A Explanat ions : Vect or implement s List , RandomAccess - Synchronized resizable-array implement at ion of t he List int erface wit h addit ional "legacy met hods." Quest ions no -28 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring argv[]){ ArrayList list = new ArrayList (); ArrayList list St r = list ; ArrayList list Buf = list ; list St r.add(0, "Hello"); St ringBuer bu = list Buf.get (0); Syst em.out .print ln(bu.t oSt ring()); } }

opt ions A)Hello B)Compile error C)java.lang.ClassCast Except ion D)null Correct answer is : C Explanat ions : java.lang.St ring cannot be cast t o java.lang.St ringBuer at t he code St ringBuer bu = list Buf.get (0); So t hows java.lang.ClassCast Except ion.

Quest ions no -29 What is t he out put for t he below code ? import java.ut il.LinkedList ; import java.ut il.Queue; public class Test { public st at ic void main(St ring... args) {

Queue q = new LinkedList (); q.add("newyork"); q.add("ca"); q.add("t exas"); show(q); } public st at ic void show(Queue q) { q.add(new Int eger(11)); while (!q.isEmpt y ( ) ) Syst em.out .print (q.poll() + " "); } }

opt ions A)Compile error : Int eger can't add B)newyork ca t exas 11 C)newyork ca t exas D)newyork ca Correct answer is : B Explanat ions : q was originally declared as Queue<St ring>, But in show() met hod it is passed as an unt yped Queue. not hing in t he compiler or JVM prevent s us from adding an Int eger aft er t hat . If t he show met hod signat ure is public st at ic void show(Queue<St ring> q) t han you can't add Int eger, Only St ring allowed. But public st at ic void show(Queue q) is unt yped Queue so you can add Int eger. poll() Ret rieves and removes t he head of t his queue, or ret urns null if t his queue is empt y.

Quest ions no -30 What is t he out put for t he below code ? public int erface Test Inf { int i =10; } public class Test { public st at ic void main(St ring... args) { Test Inf.i=12; Syst em.out .print ln(Test Inf.i); }

} opt ions A)Compile wit h error B)10 C)12 D) Runt ime Except ion Correct answer is : A Explanat ions : All t he variables declared in int erface is Implicit ly st at ic and nal , t herefore can't change t he value. Quest ions no -31 What is t he out put for t he below code ? public class Test { st at ic { int a = 5; } public st at ic void main(St ring[] args){ Syst em.out .print ln(a); } } opt ions A)Compile wit h error B)5 C)0 D) Runt ime Except ion Correct answer is : A Explanat ions : A variable declared in a st at ic init ialiser is not accessible out side it s enclosing block.

Quest ions no -32 What is t he out put for t he below code ? class A { { Syst em.out .print ("b1 "); } public A() { Syst em.out .print ("b2 "); } } class B ext ends A { st at ic { Syst em.out .print ("r1 "); } public B() { Syst em.out .print ("r2 "); } { Syst em.out .print ("r3 "); } st at ic { Syst em.out .print ("r4 "); } } class C ext ends B { public st at ic void main(St ring[] args) { Syst em.out .print ("pre "); new C(); Syst em.out .print ln("post "); } } opt ions A)r1 r4 pre b1 b2 r3 r2 post B)r1 r4 pre b1 b2 post C)r1 r4 pre b1 b2 post r3 r2 D)pre r1 r4 b1 b2 r2 r3 post Correct answer is : A Explanat ions : All st at ic blocks execut e rst t hen blocks and const ruct or. Blocks and const ruct or execut es (super class block t hen super class const ruct or, sub class block t hen sub class const ruct or). Sequence for st at ic blocks is super class rst t hen sub class. Sequence for blocks is super class rst t hen sub class.

Quest ions no -33 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) t hrows Except ion { Int eger i = 34; int l = 34; if(i.equals(l)){ Syst em.out .print ln(t rue); }else{ Syst em.out .print ln(false); } } } opt ions A)t rue B)false C)Compile Error D) Runt ime Except ion Correct answer is : A Explanat ions : equals() met hod for t he int eger wrappers will only ret urn t rue if t he t wo primit ive t ypes and t he t wo values are equal. Quest ions no -34 Which st at ement is t rue about out er class? opt ions A)out er class can only declare public , abst ract and nal B)out er class may be privat e C)out er class can't be abst ract D)out er class can be st at ic Correct answer is : A Explanat ions : out er class can only declare public , abst ract and nal.

Quest ions no -35 What is t he out put for t he below code ? st at ic public class Test { public st at ic void main(St ring[] args) { char c = 'a'; swit ch(c){ case 65: Syst em.out .print ln("one");break; case 'a': Syst em.out .print ln("t wo");break; case 3: Syst em.out .print ln("t hree"); } } } opt ions A)one B)t wo C)Compile error - char can't be permit t ed in swit ch st at ement D)Compile error - Illegal modier for t he class Test ; only public, abst ract & nal are permit t ed. Correct answer is : D Explanat ions : out er class can only declare public , abst ract and nal. Illegal modier for t he class Test ; only public, abst ract & nal are permit t ed

Quest ions no -36 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { ArrayList list = new ArrayList (); list .add(1); list .add(2); list .add(3); for(int i:list ) Syst em.out .print ln(i); } } opt ions A)1 2 3 B)Compile error , can't add primit ive t ype in ArrayList C)Compile error on for(int i:list ) , Incorrect Synt ax D)0 0 0 Correct answer is : A Explanat ions : JDK 1.5, 1.6 allows add primit ive t ype in ArrayList and for(int i:list ) synt ax is also correct . for(int i:list ) is same as for(int i=0; i < list .size();i++){ int a = list .get (i); }

Quest ions no -37 What is t he out put for t he below code ? public class SuperClass { public int doIt (St ring st r, Int eger... dat a)t hrows ArrayIndexOut OfBoundsExcept ion{ St ring signat ure = "(St ring, Int eger[])"; Syst em.out .print ln(st r + " " + signat ure); ret urn 1; } } public class SubClass ext ends SuperClass{ public int doIt (St ring st r, Int eger... dat a) t hrows Except ion { St ring signat ure = "(St ring, Int eger[])"; Syst em.out .print ln("Overridden: " + st r + " " + signat ure); ret urn 0; } public st at ic void main(St ring... args) { SuperClass sb = new SubClass(); t ry{ sb.doIt ("hello", 3); }cat ch(Except ion e){ } } } opt ions A)Overridden: hello (St ring, Int eger[]) B)hello (St ring, Int eger[]) C)This code t hrows an Except ion at Runt ime D)Compile wit h error Correct answer is : D Explanat ions : Except ion Except ion is not compat ible wit h t hrows clause in SuperClass.doIt (St ring, Int eger[]). The same except ion or subclass of t hat except ion is allowed.

Quest ions no -38 What is t he result of execut ing t he following code, using t he paramet ers 0 and 3? public void divide(int a, int b) { t ry { int c = a / b; } cat ch (Except ion e) { Syst em.out .print ("Except ion "); } nally { Syst em.out .print ln("Finally"); } opt ions A)Print s out : Except ion Finally B)Print s out : Finally C)Print s out : Except ion D)Compile wit h error Correct answer is : B Explanat ions : nally block always execut ed whet her except ion occurs or not . 0/3 = 0 Does not t hrows except ion. Quest ions no -39 Which of t he below st at ement is t rue about Error? opt ions A)An Error is a subclass of Throwable B)An Error is a subclass of Except ion C)Error indicat es serious problems t hat a reasonable applicat ion should not t ry t o cat ch. D)An Error is a subclass of IOExcept ion Correct answer is : A and C Explanat ions : An Error is a subclass of Throwable t hat indicat es serious problems t hat a reasonable applicat ion should not t ry t o cat ch. Quest ions no -40 Which of t he following is t ype of Runt imeExcept ion?

opt ions A)IOExcept ion B)ArrayIndexOut OfBoundsExcept ion C)Except ion D)Error Correct answer is : B Explanat ions : Below is t he t ree. java.lang.Object java.lang.Throwable java.lang.Except ion java.lang.Runt imeExcept ion java.lang.IndexOut OfBoundsExcept ion java.lang.ArrayIndexOut OfBoundsExcept ion

Quest ions no -41 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) t hrows Except ion { File le = new File("t est .t xt "); Syst em.out .print ln(le.exist s()); FileWrit er fw = new FileWrit er(le); Syst em.out .print ln(le.exist s()); } }

opt ions A)t rue t rue B)false false C)false t rue D)t rue false Correct answer is : C Explanat ions :Creat ing a new inst ance of t he class File, you're not yet making an act ual le, you're just creat ing a lename. So le.exist s() ret urn false. FileWrit er fw = new FileWrit er(le) do t hree t hings: It creat ed a FileWrit er reference variable fw. It creat ed a FileWrit er object , and assigned it t o fw. It creat ed an act ual empt y le out on t he disk. So le.exist s() ret urn t rue. Quest ions no -42 When comparing java.io.BueredWrit er and java.io.FileWrit er, which capabilit y exist as a met hod in only one of t wo ?

opt ions A)closing t he st ream B)ushing t he st ream C)writ t ing t o t he st ream D)writ t ing a line separat or t o t he st ream Correct answer is : D Explanat ions :A newLine() met hod is provided in BueredWrit er which is not in FileWrit er.

Quest ions no -43 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring[] args) { int i1=1; swit ch(i1){ case 1: Syst em.out .print ln("one"); case 2: Syst em.out .print ln("t wo"); case 3: Syst em.out .print ln("t hree"); } } } opt ions A)one t wo t hree B)one C)one t wo D)Compile error. Correct answer is : A Explanat ions : There is no break st at ement in case 1 so it causes t he below case st at ement s t o execut e regardless of t heir values. Quest ions no -44 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring[] args) { char c = 'a'; swit ch(c){ case 65: Syst em.out .print ln("one");break; case 'a': Syst em.out .print ln("t wo");break; case 3: Syst em.out .print ln("t hree"); } } } opt ions A)one t wo t hree B)one C)t wo D)Compile error - char can't be in swit ch st at ement . Correct answer is : C Explanat ions : Compile properly and print t wo.

Quest ions no -45 What is t he out put for t he below code ? import java.ut il.NavigableMap; import java.ut il.concurrent .Concurrent SkipList Map; public class Test { public st at ic void main(St ring... args) { NavigableMap navMap = new Concurrent SkipList Map(); navMap.put (4, "April"); navMap.put (5, "May"); navMap.put (6, "June"); navMap.put (1, "January"); navMap.put (2, "February"); navMap.put (3, "March"); navMap.pollFirst Ent ry(); navMap.pollLast Ent ry(); navMap.pollFirst Ent ry(); Syst em.out .print ln(navMap.size());

} }

opt ions A)Compile error : No met hod name like pollFirst Ent ry() or pollLast Ent ry() B)3 C)6 D)4 Correct answer is : B Explanat ions : pollFirst Ent ry() Removes and ret urns a key-value mapping associat ed wit h t he least key in t his map, or null if t he map is empt y. pollLast Ent ry() Removes and ret urns a key-value mapping associat ed wit h t he great est key in t his map, or null if t he map is empt y.

Quest ions no -46 What is t he out put for t he below code ? import java.ut il.NavigableMap; import java.ut il.concurrent .Concurrent SkipList Map; public class Test { public st at ic void main(St ring... args) { NavigableMap navMap = new Concurrent SkipList Map(); Syst em.out .print (navMap.last Ent ry());

} } opt ions A)Compile error : No met hod name like last Ent ry() B)null C)NullPoint erExcept ion D)0 Correct answer is : B Explanat ions : last Ent ry() Ret urns a key-value mapping associat ed wit h t he great est key in t his map, or null if t he map is empt y.

Quest ions no -47 What is t he out put for t he below code ? import java.ut il.NavigableMap; import java.ut il.concurrent .Concurrent SkipList Map; public class Test { public st at ic void main(St ring... args) { NavigableMapnavMap = new Concurrent SkipList Map(); navMap.put (4, "April"); navMap.put (5, "May"); navMap.put (6, "June"); navMap.put (1, "January"); navMap.put (2, "February"); Syst em.out .print (navMap.ceilingKey(3));

} } opt ions A)Compile error : No met hod name like ceilingKey() B)null C)NullPoint erExcept ion D)4 Correct answer is : D Explanat ions : Ret urns t he least key great er t han or equal t o t he given key, or null if t here is no such key. In t he above case : 3 is not a key so ret urn 4 (least key great er t han or equal t o t he given key).

Quest ions no -48 What is t he out put for t he below code ? import java.ut il.NavigableMap; import java.ut il.concurrent .Concurrent SkipList Map; public class Test { public st at ic void main(St ring... args) { NavigableMapnavMap = new Concurrent SkipList Map(); navMap.put (4, "April"); navMap.put (5, "May"); navMap.put (6, "June"); navMap.put (1, "January"); navMap.put (2, "February"); Syst em.out .print (navMap.oorKey(3));

} } opt ions A)Compile error : No met hod name like oorKey() B)null C)NullPoint erExcept ion D)2 Correct answer is : D Explanat ions : Ret urns t he great est key less t han or equal t o t he given key, or null if t here is no such key. In t he above case : 3 is not a key so ret urn 2 (great est key less t han or equal t o t he given key).

Quest ions no -49 What is t he out put for t he below code ? public class Test { public st at ic void main(St ring... args) { List lst = new ArrayList (); lst .add(34); lst .add(6); lst .add(6); lst .add(6); lst .add(6); lst .add(5); NavigableSet nvset = new TreeSet (lst ); Syst em.out .print ln(nvset .t ailSet (6)); } }

opt ions A)Compile error : No met hod name like t ailSet () B)6 34 C)5 D)5 6 34 Correct answer is : B Explanat ions : t ailSet (6) Ret urns element s are great er t han or equal t o 6.

Quest ions no -50 What is t he out put for t he below code ? import import import import java.ut il.ArrayList ; java.ut il.List ; java.ut il.NavigableSet ; java.ut il.TreeSet ;

public class Test { public st at ic void main(St ring... args) { List lst = new ArrayList (); lst .add(34); lst .add(6); lst .add(6); lst .add(6); lst .add(6); NavigableSet nvset = new TreeSet (lst ); nvset .pollFirst (); nvset .pollLast (); Syst em.out .print ln(nvset .size());

} }

opt ions A)Compile error : No met hod name like pollFirst () or pollLast () B)0 C)3 D)5 Correct answer is : B Explanat ions : pollFirst () Ret rieves and removes t he rst (lowest ) element , or ret urns null if t his set is empt y. pollLast () Ret rieves and removes t he last (highest ) element , or ret urns null if t his set is empt y.

Quest ions no -51 What is t he out put for t he below code ? import import import import java.ut il.ArrayList ; java.ut il.List ; java.ut il.NavigableSet ; java.ut il.TreeSet ;

public class Test { public st at ic void main(St ring... args) { List lst = new ArrayList (); lst .add(34); lst .add(6); lst .add(2); lst .add(8); lst .add(7); lst .add(10); NavigableSet nvset = new TreeSet (lst ); Syst em.out .print ln(nvset .lower(6)+" "+nvset .higher(6)+ " "+ nvset .lower(2)); } } opt ions A)1 2 7 10 34 null B)2 7 null C)2 7 34 D)1 2 7 10 34 Correct answer is : B Explanat ions : lower() Ret urns t he great est element in t his set st rict ly less t han t he given element , or null if t here is no such element . higher() Ret urns t he least element in t his set st rict ly great er t han t he given element , or null if t here is no such element .

Quest ions no -52 What is t he out put for t he below code ? import import import import import java.ut il.ArrayList ; java.ut il.It erat or; java.ut il.List ; java.ut il.NavigableSet ; java.ut il.TreeSet ;

public class Test { public st at ic void main(St ring... args) { List lst = new ArrayList (); lst .add(34); lst .add(6); lst .add(2); lst .add(8); lst .add(7); lst .add(10); NavigableSet nvset = new TreeSet (lst ); Syst em.out .print ln(nvset .headSet (10)); } }

opt ions A)Compile error : No met hod name like headSet () B)2, 6, 7, 8, 10 C)2, 6, 7, 8 D)34 Correct answer is : C Explanat ions : headSet (10) Ret urns t he element s element s are st rict ly less t han 10. headSet (10,false) Ret urns t he element s element s are st rict ly less t han 10. headSet (10,t rue) Ret urns t he element s element s are st rict ly less t han or equal t o 10.

Quest ions no -53 What is t he out put for t he below code ? import java.io.Console; public class Test { public st at ic void main(St ring... args) { Console con = Syst em.console(); boolean aut h = false; if (con != null) { int count = 0; do { St ring uname = con.readLine(null); char[] pwd = con.readPassword("Ent er %s's password: ", uname); con.writ er().writ e("\n\n"); } while (!aut h && ++count < 3); }

} }

opt ions A)NullPoint erExcept ion B)It works properly C)Compile Error : No readPassword() met hod in Console class. D)null Correct answer is : A Explanat ions : passing a null argument t o any met hod in Console class will cause a NullPoint erExcept ion t o be t hrown.

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