You are on page 1of 8

1) class Example{ int i; public static void main (String args[]) { System.out.println(new Example().

i); }} Determine the output of the program 2)In the switch() statement, which of the following data type is not allowed? a. Int b. byte c. short d. long e. String 3) class Student { int marks[]={70, 80, 90}; Student() {} public static void main(String args[]) { System.out.println(new Student().marks[1]+new Student.marks[2]); } } What is the output of the program 4)class Add{ int i; void first() { int i=4, j=5; this.i=i+j; second(10); } void second(int i) { int j=9; i=i+j; System.out.println(this.i); System.out.println(i); } public static void main(String ar[]) { Add a =new Add(); a.first(); }} 5)class C { public static void main(String[] args) { int[]a1[]=new int[3][3]; //3

int a2[4]={3,4,5,6}; //4 int a2[5]; //5 }} What is the result of attempting to compile and run the program ?. a) compiletime error at lines 3,4,5 b) compiltime error at line 4,5 c) compiletime error at line 3 d) Runtime Exception e) None of the above 6)interface I{ void f1(); // 1 public void f2(); // 2 protected void f3(); // 3 private void f4(); // 4 } which lines generate compile time errors? a) compiletime error at lines 1,2,3,4 b) compiletime error at line 3 c) compiletime error at line 1 d) compiletime error at lines 3,4 e) None of the above 7)class C{ int i; public static void main (String[] args) { int i; //1 private int a = 1; //2 protected int b = 1; //3 public int c = 1; //4 System.out.println(a+b+c); //5 }} a) compiletime error at lines 1,2,3,4,5 b) compiletime error at lines 2,3,4,5 c) compiletime error at lines 2,3,4 d)prints 3 e)None of the above

8)class C { public static void main (String[] a1) { System.out.print(a1[1] + a1[2] + a1[3]); }} What is the result of attempting to compile and run the program? java command A B C a) Prints: ABC b) Prints BC and Runtime Exception c) Prints: BCD d) Runtime Exception e) 1None of the above 9)class C{ static int f1(int i) { System.out.print(i + ","); return 0; } public static void main (String[] args) { int i = 0; i = i++ + f1(i); System.out.print(i); }} What is the output of this program? a) 0,0 b) 1,0 c) 0,1 d) Compile-time error e) None of the above 10)class C{ static String m(int i) {return "int";} static String m(float i) {return "float";} public static void main (String[] args) { long a1 = 1; double b1 = 2; System.out.print(m(a1)+","+ m(b1)); }} What does this program print? a) float,double b) float,float
1

c) double,float d) Compile-time error e) None of the above 11)class C{ public static void main(String a[]){ C c1=new C(); C c2=m1(c1); C c3=new C(); c2=c3; //6 anothermethod(); } static C m1(C ob1){ ob1 =new C(); return ob1; } } After line 6, how many objects are eligible for garbage collection? 12)1. StringBuffer s1 = new StringBuffer("abc"); 2. StringBuffer s2 = s1; 3. StringBuffer s3 = new StringBuffer("abc"); How many objects are created ? 13)class c2{{ System.out.println("initializer"); } public static void main(String a[]) { System.out.println("main"); c2 ob1=new c2(); } } Determine the output of the program a) prints main and initializer b) prints initializer and main c) compile time error d) None of the above 14)class c1 { public static void main(String a[]) { c1 ob1=new c1(); Object ob2=ob1;

System.out.println(ob2 instanceof Object); System.out.println(ob2 instanceof c1); } } a) Prints true,false b) Print false,true c) Prints true,true d) compile time error e) None of the above 15)class A extends Thread { private int i; public void run() {i = 1;} public static void main(String[] args) { A a = new A(); a.run(); System.out.print(a.i); }} a) Prints nothing b) Prints: 1 c) Prints: 01 d) Compile-time error e) None of the above 16)class bike {} class arr extends bike{ public static void main(String[] args) { arr[] a1=new arr[2]; bike[] a2; a2=a1; //3 arr[] a3; a3=a1; //5 }} a) compile time error at line 3 b) compile time error at line 5 c) Runtime exception d) The code runs fine e) None of the above 17)class C{ public static void main(String[] args) { try { try { try {} catch(RuntimeException e) {} } catch(Exception e) {} }

catch(NullPointerException e) {} finally{ System.out.println("finally"); }}} a) Prints finally b) compile time error c) Runtime Exception d) None of the above 18)class H { public static void main (String[] args) { String s1 = "HHH"; StringBuffer sb2 = new StringBuffer(s1); System.out.print(sb2.equals(s1) + "," + s1.equals(sb2)); }} a) Prints: false,false b) Prints: true,false c) Prints: false,true d) Prints: true,true e) None of the above 19)class A extends Thread { private int i; public void run() {i = 1;} public static void main(String[] args) { A a = new A(); a.run(); System.out.print(a.i); }} How many threads are created in this Program? a) 1 b) 2 c) 3 d) 0 e) None of the above 20)class A extends Thread { private int i; public void run() {i = 1;} public static void main(String[] args) { A a = new A(); a.run(); System.out.print(a.i); }} How many threads are created in this Program?

a) 1 b) 2 c) 3 d) 0 e) None of the above 21)class C { public static void main(String[] args) { try{ int i1=3/0; } System.out.println("hai"); catch(NullpointerException e) {} finally{ System.out.println("finally); }}} a) compile time errors b) print hai and finally c) the code compiles and runs fine d) none of the above 22)class c1 { public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); System.out.println(Float.NaN==Double.NaN); }} a) prints false true false b) print true false true c) prints true true true d) prints false false false e) compiletime error 23)class c1 { public void m1() { System.out.println("m1 method in C1 class"); }} class c2 { public c1 m1() { return new c1(){ public void m1() { System.out.println("m1 mehod in anonymous class"); }};} public static void main(String a[]) { c1 ob1 =new c2().m1();

ob1.m1(); }} a) prints m1 method in C1 class b) prints m1 method in anonumous class c) compile time error d) Runtime error e) none of the above 24)class c1 extends Exception {} class c2 { static void m1() { throw new c1(); } public static void main(String a[]) {}} a) compile time error b) Runtime Exception c) The code compiles and runs fine d) None of the above 25)class C { static void m1(Object x) {System.out.print("Object");} static void m1(String x) {System.out.print("String");} public static void main(String[] args) { m1(null); }} a) Prints Object b) Prints String c) compiletime error d) None of the above

You might also like