You are on page 1of 16

) 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 ?. 1.compiletime error at lines 3,4,5 2.compiltime error at line 4,5 3.compiletime error at line 3 4.Runtime Exception 5.None of the above Ans: 2 Explanation: no value shoud be specified in the rightsidebrackets when constructing an array

2) interface I{ void f1(); // 1 public void f2(); // 2 protected void f3(); // 3 private void f4(); // 4 } which lines generate compile time errors? 1.compiletime error at lines 1,2,3,4 2.compiletime error at line 3 3.compiletime error at line 1 4.compiletime error at lines 3,4

5.None of the above Answer: 4 Explanation: all methods declared within an interface are implicitly public, a weaker access level can not be declared. 3) 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 }} 1.compiletime error at lines 1,2,3,4,5 2 compiletime error at lines 2,3,4,5 3.compiletime error at lines 2,3,4 4.prints 3 5.None of the above Answer 2 Explanation: The access modifiers public, protected and private, can not be applied to variab les declared inside methods. 4) 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 1.Prints: ABC 2.Prints BC and Runtime Exception 3.Prints: BCD 4.Runtime Exception 5.None of the above Answer 2 Explanation: array index outof bounds exception only till a1[2] is allowed. 5) class C{ static int s; public static void main(String a[]){ C obj=new C(); obj.m1(); System.out.println(s); } void m1(); { int x=1; m2(x); System.out.println(x+""); } void m2(int x){ x=x*2; s=x; }} 1.prints 1,2 2.prints 2,0 3.prints 2,2

4.compile time error 5.Noneofthe above Answer: 1 Explanation: Only objects and arrays are passed by reference.other are passed by value.s is a static variable which is global to the class 6) class C { public static void main(String[] args) { int i1=1; switch(i1){ case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); }}} What is the result of attempting to compile and run the program? 1.prints one two three 2.prints one 3.compile time error 4.Runtime exceptionf 5.None of the above Answer: 1 Explanation: There is no break statement in case 1 so it causes the below case statements to execute regardless of their values 7) Each element must be unique Duplicate elements must not replace old elements.

Elements are not key/value pairs. Accessing an element can be almost as fast as performing a similar operation on an array. Which of these classes provide the specified features? 1.LinkedList 2.TreeMap 3.HashMap 4.HashSet 5.None of the above Answer: 4 8) class C1 { static interface I { static class C2 { } } public static void main(String a[]) { C1.I.C2 ob1=new C1.I.C2(); System.out.println("object created"); } }

What is the result of attempting to compile and run the program? 1.prints object created 2.Compile time error 3.Runtime Excepion

4.None of the above Answer: 1 Explanation: A static interface or class can contain static members.Static members can be acc essed without instantiating the particular class 9) class C1 { static class C2 { static int i1; } public static void main(String a[]) { System.out.println(C1.C2.i1); } }

What is the result of attempting to compile and run the program? 1.prints 0 2.Compile time error 3.Runtime exception 4.None of the above Answer: 1 Explanation: static members can be accessed without instantiating the particular class 10) A signed data type has an equal number of non-zero positive and negative values available 1.true 2.false

Answer: 2 Explanation: The range of negative numbers is greater by 1 than the range of positive numbers Go To Index

11) 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); }} Prints: 0,0 Prints: 1,0 Prints: 0,1 Compile-time error None of the above Explanation: No Explanation Available Ans: 2

12) 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)); }} Prints: float,double Prints: float,float Prints: double,float

Compile-time error None of the above Explanation: No Explanation Available Ans: 4 13) 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? Ans: 1 Ans: 2 Ans: 3 Ans: 4 None of the above Explanation: No Explanation Available Ans: 2 14) 1. StringBuffer s1 = 2. StringBuffer s2 = 3. StringBuffer s3 = How many objects are 0 Ans: 1 Ans: 2 Ans: 3 new StringBuffer("abc"); s1; new StringBuffer("abc"); created ?

Explanation: No Explanation Available Ans: 4 15) class c2 { { System.out.println("initializer"); } public static void main(String a[]) { System.out.println("main"); c2 ob1=new c2(); } } prints main and initializer prints initializer and main compile time error None of the above Explanation: No Explanation Available Ans: 1 16) 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); } } Prints true,false Print false,true Prints true,true compile time error None of the above Explanation: No Explanation Available

Ans: 3 17) 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); }} Prints nothing Prints: 1 Prints: 01 Compile-time error None of the above Explanation: a.run() method was called instead of a.start(); so the full program runs as a si ngle thread so a.run() is guaranteed to complete Ans: 2 18) 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 }} compile time error at line 3 compile time error at line 5 Runtime exception The code runs fine None of the above Explanation: bike is the superclass of arr.so they are compatible(superobject=subobject) but subobject=superobject not allowed

Ans: 4 19) class C{ public static void main String s1="hjhh"; String s2="\u0002"; String s3="'\\'"; }} (String[] args) { // 1 //2 //3

compile time error at line 1 compile time error at line 2 compile time error at line 3 Runtime exception the code runs without any error Explanation: A String literal is a sequence of characters enclosed in double quotes Ans: 5 20) Which data type is wider for the purpose of casting: float or long? float long Explanation: float is wider than long, because the entire range of long fits within the range of float. Ans: 1 Go To Index

21) class C{ public static void main (String[] args) { byte b1=33; //1 b1++; //2 byte b2=55; //3 b2=b1+1; //4 System.out.println(b1+""+b2); }} compile time error at line 2

compile time error at line prints 34,56 runtime exception none of the above

Explanation: b1+1 returns an integer value which can not be assigned to a byte variable Ans: 2

22) import java.util.*; class C { final Vector v; C() { v=new Vector(); } C(int i) { } public void someMethod() { System.out.println(v.isEmpty()); } } compile time error runtime exception the code compiles and runs fine none of the above Explanation: No Explanation Available Ans: 1 23) class C1{ public void m1(){ // 1 } } class C2 extends C1{ //2 private void m1(){ }

} compile time error at line1 compile time error at line2 Runtime exception None of the above Explanation: tending to assign weaker access not allowed Ans: 2 24) interface I{ int i; // line 1 } class C implements I{ public static void main(String a[]){ System.out.println(i); System.out.println(i++); //line 2 } } compile time error at line 1,2 compile time error at line 2 Runtime exception Noneofthe above Explanation: interface constants are final so,they must be initialized when declaring it and they can not be altered Ans: 1 25) An abstract class must have at least one abstract method true true Explanation: An abstract without abstract methods is allowed Ans: 2 26) <![CDATA[

class C { public static void main(String[] args) { boolean b1; b1=3<4<5; //1 System.out.println(b1); //2 }} ]]> compile time error at line 1 compile time error at line 2 Runtime exception None of the above Explanation: <![CDATA[ 3<4<5 evaulates to true<5 -->it's a wrong expression so it results in compiletim e error ]]> Ans: 1 27) class C{ public static void main(String[] args) { try { int i1=3/0; } catch(Exception e) { System.out.println("exception1"); } catch(NullPointerException e) { System.out.println("exception2"); } finally { System.out.println("finally"); } }}

compile time error runtime exception prints exception1 and finally prints exception1,exception2 and finally None of the above

Explanation: No Explanation Available Ans: 1 28) class C { public static void main(String[] args) { char c1=65; switch(c1){ case 'A': System.out.println("one"); default: System.out.println("two"); case 'b': System.out.println("three"); }}} prints one twot hree prints two three compile time error Runtime exception None of the above Explanation: char is a legal value for switch clause Ans: 1 29) class c1{ void go(){} } class c2 extends c1 { String go() { return null; } } compile time error runtime exceptione the code compiles and runs fine None of the above Explanation: No Explanation Available

Ans: 1 30) class base { base(int c) { System.out.println("base"); } } class Super extends base { Super() { System.out.println("super"); } public static void main(String [] a) { base b1=new Super(); } } compile time error runtime exceptione the code compiles and runs fine None of the above Explanation: No Explanation Available Ans: 1 Go To Index

You might also like