1) 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 variables 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 accessed 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 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
new StringBuffer("abc"); s1; new StringBuffer("abc"); created ?
0 Ans: 1 Ans: 2 Ans: 3 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 single 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[] args) { String s1="hjhh"; // 1 String s2="\u0002"; //2 String s3="'\\'"; //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 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
4
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) compile time error at line 1 compile time error at line 2 Runtime exception None of the above Explanation: it's a wrong expression so it results in compiletime 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
31) class c2{ final int i1; c2() { i1=i1+1; } { i1=2; } public static void main(String a[]) { c2 ob1=new c2(); System.out.println(ob1.i1); } } compile time error prints 3 prints 2 none of the above Explanation:
No Explanation Available Ans: 1 32) class C{ public static void main(String a[]) int i1=9; int i2; if(i1>3) { i2=8; } System.out.println(i2); }}
{
compile time error Runtim error prints 8 prints 0 None of the above Explanation: since variable i1 is not final the value is not known at compiletime itself.so generate compile time error Ans: 1 33) class A extends Thread { public void run() { System.out.print("A"); } } class B { public static void main (String[] args) { A a = new A(); a.start(); a.start(); // 1 } } compile time error Runtime Exception the code compile and runs fine none of the above Explanation: If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown Ans: 2
34) class c1 { static{ System.out.println("static"); } public static void main(String a[]) { System.out.println("main"); } } prints static main prints main prints main static compiletime error none of the above Explanation: No Explanation Available Ans: 1 35) class A{ static String m(float i) {return "float";} static String m(double i) {return "double";} public static void main (String[] args) { int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1)); }} prints float,foat prints float,double prints double,double compile time error None of the above Explanation: No Explanation Available Ans: 1 36) When a byte is added to a char, what is the type of the result? byte int
long non of the above Explanation: The result of all arithmetic performed with the binary operators (not the assignment operators) is an int, a long, a float, or a double. Here byte and char are promoted to int, so the result is an int. Ans: 2 37) class C extends Thread{ public static void main(String argv[]){ C b = new C(); b.run(); } public void start(){ for (int i = 0; i <10; i++){ System.out.println("Value of i = " + i); } } }
compile time error runtime Exception prints values from 0 to 9 None of the above Explanation: No Explanation Available Ans: 4 38) What is the signature of the run() method of the Runnable interface? void run() public void run(Runnable target) public void run() public static void run() Explanation: No Explanation Available Ans: 3 39)
class C{ public static void main(String args[]) { int a = 1; a += ++a + a++; System.out.print(a); }} compile time error Runtime Exception Prints 5 Prints 4 None of the above Explanation: No Explanation Available Ans: 3 40) interface I { public class Inner {
//1 ///2
Inner ( ) { System .out . println ( "Inner Created" ) ; } }; }; compile time error at line 1 compile time error at line 2 the code compiles fine none of the above Explanation: No Explanation Available Ans: 3
41) abstract class C1{ public void m1(){ }} abstract class C2{ public void m2(){ }}
//1 //2
compile time error at line1 compile time error at line2
The code compiles fine None of the above Explanation: since the class C2 is abstract it can contain abstract methods Ans: 3 42) interface I{ void f1(); public void f2(); protected void f3(); private void f4(); abstract void f5(); } line
1,2,3,4
line
3,4
line
3
line
2,3,4
line
3,4,5
// // // // //
1 2 3 4 5
Explanation: all methods declared within an interface are implicitly public, a weaker access level can not be declared Ans: 2 43) abstract class vehicle{ abstract public void speed(); } class car extends vehicle{ public static void main (String args[]) { vehicle ob1; ob1=new car(); //1 }} compiletime error at line
1
forces the class car to be declared as abstract Runtime Exception None of the above Explanation: No Explanation Available Ans: 2
44) class command { public static void main (String[] a1) { System.out.println(a1.length()); System.out.println(a1[0]); System.out.println(a1); //2 }}
//1 //2 //3
compile time error at line1 compile time error at line2 compile time error at line3 Runtime exception Explanation: length is not a method. it's a variable Ans: 1 45) abstract class A {} // 1 transient class B {} // 2 private class C {} // 3 static class D {} // 4 Which of these declarations will not produce a compile-time error? Ans: 1 Ans: 2 Ans: 3 Ans: 4 None of the above Explanation: The modifiers, private and static, can be applied to a nested class, but can not be applied to a class that is not nested transient is allowed only for variables. Ans: 1 46) class c1 { public void m1(Object o1) { System.out.println("object"); } public void m1(String o1) { System.out.println("string"); } public int m1(int c)
{ return c; } public static void main(String a[]) { c1 ob1=new c1(); ob1.m1("hai"); } } print object prints string compile time error non of the above Explanation: No Explanation Available Ans: 2 47) public static double sin(double angle) What are the units of the "angle" argument? Degrees Radians Both None of the above Explanation: No Explanation Available Ans: 2 48) class base { base() { System.out.println("base"); } base(int i1) { } } class Super extends base { Super() { System.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 of the above Explanation: No Explanation Available Ans: 1 49) The Throwable class is the superclass of all exceptions in the Java language. true false Explanation: No Explanation Available Ans: 1 50) class C { private String get ( String str ) { try { throw new Exception ( ) ; return str ; } catch ( Exception e ) { return null ; } } public static void main ( String peace [ ] ) { try { System.out.println ( ( new C ( ) ).get ( " C " ) ) ; } catch ( Exception e ) { System.out.println( "Exception" ) ; } } };
compile time error prints Exception the code compiles and runs fine none of the above Explanation: No Explanation Available Ans: 1 51) Which of the following methods are static members of the Thread class? join run sleep start wait Explanation: No Explanation Available Ans: 3 52) 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); } }} compile time errors print hai and finally the code compiles and runs fine none of the above Explanation: No Explanation Available
Ans: 1 53) 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); } } prints false true false print true false true prints true true true prints false false false compiletime error Explanation: No Explanation Available Ans: 1 54) class C { public static void main ( String ka [ ] ) { Thread t = Thread . currentThread ( ) ; t . setPriority ( - 1 ) ; System . out . println ( " Done ! " ) ; } }; compile time error Runtime Exception The code compiles and runs fine None of the above Explanation: No Explanation Available Ans: 2 55) 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(); }} prints m1 method in C1 class prints m1 method in anonumous class compile time error Runtime error none of the above Explanation: the anonymous class overrides the m1 method in c1.so according to the dynamic dispatch the m1 method in the anonymous is called Ans: 2 56) class c1 extends Exception {} class c2 { static void m1() { throw new c1(); } public static void main(String a[]) { } } compile time error Runtime Exception The code compiles and runs fine None of the above Explanation: No Explanation Available Ans: 1
57) 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); }} Prints Object Prints String compiletime error None of the above Explanation: The more specific of the two, m(String x), is chosen Ans: 2 58) all classes of the java.lang package are automatically imported true false Explanation: No Explanation Available Ans: 1 59) The constructor for the Math class is private, so it cannot be instaniated true false Explanation: No Explanation Available Ans: 1 60) class C { public static void main(String[] args) { int x=2; int y=3; if((y==x++)|(x<++y)){ System.out.println(x+""+y); } }}
prints
34
prints
33
compile time error Runtime exception None of the above Explanation: No Explanation Available Ans: 1
61) interface I{ final class C1 { //1 static int i=9;; //2 } } class C2 implements I{ public static void main(String a[]){ System.out.println(I.C1.i); ///3 }} compile time error at line 1 compile time error at line 2 compile time error at line 3 prints 9 Runtime exception Explanation: interfaces classes are by default static,final. so,no compile time errors are genearated Ans: 4 62) 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"); } }} Prints finally compile time error Runtime Exception None of the above Explanation: No Explanation Available Ans: 1 63) class C { public static void main ( String ka [ ] ) { while ( false ) { System.out.println ( "Value" ) ; } } } compile time error prints Value infinitely Runtime Exception None of the above Explanation: No Explanation Available Ans: 1 64) class C { public static void main(String[] args) { System.out.println(4+5+"String"); }} prints 9string prints 45string
compile time error Runtime exception None of the above Explanation: arguments to the method are evalutated from left to right so 4+5+"string" ==> 9+string==>9string Ans: 1 65) 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)); }} Prints: false,false Prints: true,false Prints: false,true Prints: true,true None of the above Explanation: s1 and sb2 are pointing to different object references Ans: 1 66) The relationship between a class and its superclass is has-a is -a None of the above Explanation: No Explanation available Ans: 2 67) 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? Ans: 1 Ans: 2 Ans: 3 0 None of the above Explanation: Main thread is a thread and calling run methods will not creat thread. Ans: 1 68) String objects once created can not be modified true false Explanation: No Explanation available Ans: 1 69) Which of the following modifiers can be applied to a class that is not a nested class? public protected private static Explanation: No Explanation Available Ans: 1
70) if(0.0 == -0.0) { System.out.println("true"); } else{ System.out.println("false"); } prints false
prints true Explanation: No Explanation available Ans: 2 71) class C { public static void main(String[] args) { double d1 = Math.floor(0.5); double d2 = Math.floor(1.5); System.out.print(d1 + "," + d2); }} Prints: 0.0,1.0 Prints: 0.0,2.0 Prints: 1.0,1.0 Prints: 1.0,2.0 None of the above Explanation: No Explanation Available Ans: 1 72) System.out.println("String".substring(0,4)); This statement will Print will print "Strin" will print "Stri" will cause compiler error none of the above Explanation: No Explanation Available Ans: 2 73) if("String".replace('t','T') == "String".replace('t','T')) System.out.println("Equal"); else System.out.println("Not Equal"); will Print Equal will Print Not Equal
compile time error none of the above Explanation: No Explanation Available Ans: 2 74) Which of the following classes by multiple threads?
will not allow unsynchronized read operations
Vector TreeMap TreeSet HashMap HashSet Explanation: No Explanation Available Ans: 1 75) StringBuffer objects once created can not be modified true false Explanation: No Explanation available Ans: 2