In the series of OCJP certification dumps, This is third set in which 10 most important questions is being covered, let's practice these questions.
I will suggest you to first solve all the given questions yourself then only look at the solutions. If you have any doubt in any solution please comment. You can also share your score in comment box below.
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will compile without errors, will print |work| twice, and terminate normally every time the program is run.
(c) The program will compile without errors, will print |work| three times, and terminate normally every time the program is run.
(d) The program will compile without errors, will print |work| twice, and throw an IllegalThreadStateException, every time the program is run.
(e) None of the above.
2. What will be the result of attempting to compile and run the following program?
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will compile without errors, will print |T1|, and terminate normally every time the program is run.
(c) The program will compile without errors, will print |T1|, and throw an IllegalStateException, every time the program is run.
(d) None of the above.
3. Which letters will be printed when the following program is run?
2. (c)
Explanation : The main thread create thread with name '|T1|' and start, after that run method will be executed, that will print the name and throw IllegalStateException every time it run.
3. (a), (b), (c)
SET-3
1. What will be the result of attempting to compile and run the following program?
package com.javawins; public class Worker extends Thread { public void run() { System.out.print("|work|"); } public static void main(String[] args) { Worker worker = new Worker(); worker.start(); worker.run(); worker.start(); } }
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will compile without errors, will print |work| twice, and terminate normally every time the program is run.
(c) The program will compile without errors, will print |work| three times, and terminate normally every time the program is run.
(d) The program will compile without errors, will print |work| twice, and throw an IllegalThreadStateException, every time the program is run.
(e) None of the above.
2. What will be the result of attempting to compile and run the following program?
package com.javawins; public class Threader extends Thread { Threader(String name) { super(name); } public void run() throws IllegalStateException { System.out.println(Thread.currentThread().getName()); throw new IllegalStateException(); } public static void main(String[] args) { new Threader("|T1|").start(); } }
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will compile without errors, will print |T1|, and terminate normally every time the program is run.
(c) The program will compile without errors, will print |T1|, and throw an IllegalStateException, every time the program is run.
(d) None of the above.
3. Which letters will be printed when the following program is run?
package com.javawins.certification; public class MyClass { public static void main(String[] args) { B b = new C(); A a = b; if (a instanceof A) System.out.println("A"); if (a instanceof B) System.out.println("B"); if (a instanceof C) System.out.println("C"); if (a instanceof D) System.out.println("D"); } } class A {} class B extends A {} class C extends B {} class D extends C {}
Select the three correct answers.
(a) A will be printed.
(b) B will be printed.
(c) C will be printed.
(d) D will be printed.
4. What will be output when run the following program
package com.javawins.certification; public class MyClass { public static void main(String[] args) { I x = new D(); if (x instanceof I) System.out.println("I"); if (x instanceof J) System.out.println("J"); if (x instanceof C) System.out.println("C"); if (x instanceof D) System.out.println("D"); } } interface I {} interface J {} class C implements I {} class D extends C implements J {}
Select the one correct answer.
(a) IJCD
(b) ICD
(c) DC
(d) ID
5. What will the program print when compiled and run?
package com.javawins.certification; public class NumberTest { public static void main(String[] args) { Integer i = -10; Integer j = -10; System.out.print(i == j); System.out.print(i.equals(j)); Integer n = 128; Integer m = 128; System.out.print(n == m); System.out.print(n.equals(m)); } }Select the one correct answer.
(a) falsetruefalsetrue
(b) truetruetruetrue
(c) falsetruetruetrue
(d) truetruefalsetrue
(e) None of the above.
6. What will the program print when compiled and run?
package com.javawins.certification; public class NumberTest { public static void main(String[] args) { Integer i = new Integer(-10); Integer j = new Integer(-10); Integer k = -10; System.out.print(i == j); System.out.print(i.equals(j)); System.out.print(i == k); System.out.print(i.equals(k)); } }
Select the one correct answer.
(a) falsetruefalsetrue
(b) truetruetruetrue
(c) falsetruetruetrue
(d) truetruefalsetrue
(e) None of the above.
7. What is the result of compiling and running the following program?
package com.javawins.certification; public class VarArgu { static void printFirst(Integer... ints) { System.out.println("Integer...: " + ints[0]); } static void printFirst(Number... nums) { System.out.println("Number...: " + nums[0]); } static void printFirst(Object... objs) { System.out.println("Object...: " + objs[0]); } public static void main(String[] args) { printFirst(10); printFirst((byte)20); printFirst('3', '0'); printFirst("40"); printFirst((short)50, 55); printFirst((Number[])new Integer[] {70, 75}); } }
Select the one correct answer.
(a) The program does not compile because of ambiguous method calls.
(b) The program compiles and prints:
Integer...: 10
Integer...: 20
Integer...: 3
Object...: 40
Integer...: 50
Number...: 70
(c) The program compiles and prints:
Integer...: 10
Number...: 20
Object...: 3
Object...: 40
Number...: 50
Number...: 70
(d) The program compiles and prints:
Integer...: 10
Integer...: 20
Integer...: 3
Object...: 40
Number...: 50
Number...: 70
8. What will be the result of compiling and running the following program?
package com.javawins.certification; public class Polymorphism { public static void main(String[] args) { AA ref1 = new CC(); BB ref2 = (BB) ref1; System.out.println(ref2.f()); } } class AA { int f() { return 0; } } class BB extends AA { int f() { return 1; } } class CC extends BB { int f() { return 2; } }
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will compile but will throw a ClassCastException, when run.
(c) The program will compile and print 0, when run.
(d) The program will compile and print 1, when run.
(e) The program will compile and print 2, when run.
9. Which method headers will result in a correct implementation of a finaliser for the following class?
package com.javawins.certification; public class Curtain { // (1) INSERT METHOD HEADER HERE ... { System.out.println("Final curtain"); super.finalize(); } }
Select the two correct answers.
(a) void finalize() throws Throwable
(b) void finalize() throws Exception
(c) void finalize()
(d) protected void finalize() throws Throwable
(e) protected void finalize() throws Exception
(f) protected void finalize()
(g) public void finalize() throws Throwable
(h) public void finalize() throws Exception
(i) public void finalize()
(j) private void finalize() throws Throwable
(k) private void finalize() throws Exception
(l) private void finalize()
10. What will be the result of attempting to compile and run the following program?
package com.javawins.certification; public class RefEqual { public static void main(String[] args) { String s = "ab" + "12"; String t = "ab" + 12; String u = new String("ab12"); System.out.println((s == t) + " " + (s == u)); } }
Select the one correct answer.
(a) The program will fail to compile.
(b) The program will print false false, when run.
(c) The program will print false true, when run.
(d) The program will print true false, when run.
(e) The program will print true true, when run.
***************** SOLUTION ****************
1. (d)
Explanation : Main thread and child thread will print |work|, so in output two there will be two times |work| after that when main thread try to start the worker thread twice it will throw IllegalThreadStateException.
2. (c)
Explanation : The main thread create thread with name '|T1|' and start, after that run method will be executed, that will print the name and throw IllegalStateException every time it run.
3. (a), (b), (c)
4. (a)
5. (d)
6. (a)
6. (a)
7. (c)
8. (e)
9. (d), (g)
10. (d)
No comments:
Post a Comment