All Questions


#201Given:

1 public class Derived extends Demo {
2 int L, M, N;
3 public Derived(int x, int y) {
4 M = x; N = y;
5 }
6 public Derived(int x) {
7 super(x);
8 }
9 }

Which of the following constructor signatures MUST exist in the Demo class for Derived to compile correctly?

(1)public Demo(int a, int b)
(2)public Demo(int c)
(3)public Demo()

Answer : -------------------------

#202Which of the following class declarations for a normal top level class are incorrect?

(1)public synchronized class Base extends Thread
(2)private protected class Base
(3)public abstract class Base
(4)class Base extends Thread

Answer : -------------------------

#203The GenericFruit class declares the following method:
public void setCalorieContent(float f)

You are writing a class Apple to extend GenericFruit and wish to add methods which overload the method in GenericFruit.

Select all of the following which would constitute legal declarations of overloading methods.

(1)protected float setCalorieContent(String s)
(2)protected void setCalorieContent(float x)
(3)public void setCalorieContent(double d)
(4)public void setCalorieContent(String s) throws NumberFormatException

Answer : -------------------------

#204Here is the hierarchy of Exceptions related to array index errors:

Exception
+-- RuntimeException
+-- IndexOutOfBoundsException
+-- ArrayIndexOutOfBoundsException
+-- StringIndexOutOfBoundsException

Suppose you had a method X which could throw both array index and string index exceptions. Assuming that X does not have any try-catch statements, which of the following statements are correct?

(1)The declaration for X must include "throws ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException"
(2)If a method calling X catches IndexOutOfBoundsException, both array and string index exceptions will be caught
(3)If the declaration for X includes "throws IndexOutOfBoundsException", any calling method must use a try-catch block
(4)The declaration for X does not have to mention exceptions

Answer : -------------------------

#205The following method is designed to convert an input string to a floating point number, while detecting a bad format. Assume that "factor" is correctly defined elsewhere.

1 public boolean strCvrt(String s){
2 try {
3 factor = Double.valueOf(s).doubleValue();
4 return (true);
5 } catch(NumberFormatException e) {
6 System.out.println("Bad Number " + s);
7 } finally {
8 System.out.println("Finally");
9 }
10 return (false);
11 }

Which of the following descriptions of the results of various inputs to the method are correct?

(1)Input = "0.1234". Result: factor = 0.1234, "Finally" is printed, true is returned
(2)Input = "0.1234". Result: factor = 0.1234, "Finally" is printed, false is returned
(3)Input = null. Result: factor = NaN, "Finally" is printed, false is returned
(4)Input = null. Result: factor unchanged, "Finally" is printed, NullPointerException is thrown

Answer : -------------------------

#206Given the following code fragment:

XXXX x; // variable x is declared and initialized here

switch(x) {
case 100:
System.out.println("One Hundred");
break;
case 200:
System.out.println("Two Hundred");
break;
case 300:
System.out.println("Three Hundred");
break;
}

Choose all of the declarations of x which will not cause a compilation error.

(1)byte x = 100;
(2)short x = 200;
(3)int x = 300;
(4)long x = 400;

Answer : -------------------------

#207In the following code for a class in which methodA has an inner class.

1 public class Base {
2 private static final int ID = 3;
3 private String name;
4 public void methodA(final int nn) {
5 int serialN = 11;
6 class inner {
7 void showResult() {
8 System.out.println("Result = " + XX);
9 }
10 } // inner
11 new inner();
12 } // methodA
13 }

Which variables would the statement in line 8 be able to use in place of XX? Check all that apply.

(1)int ID (line 2)
(2)String name (line 3)
(3)int nn (line 4)
(4)int serialN (line 5)

Answer : -------------------------

#208What happens when we attempt to compile and run the following code?

1 public class Logic {
2 static int minusOne = -1;
3 static public void main(String[] arguments) {
4 int N = minusOne >> 31;
5 System.out.println("N = " + N);
6 }
7 }


(1)The program will compile and run, producing the output "N = -1"
(2)The program will compile and run, producing the output "N = 1"
(3)A run time ArithmeticException will be thrown
(4)The program will compile and run, producing the output "N = 0"

Answer : -------------------------

#209What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)

public class AssertTest
{
public void methodA(int i)
{
assert i >= 0 : methodB();
System.out.println(i);
}

public void methodB()
{
System.out.println("The value must not be negative");
}

public static void main(String args[])
{
AssertTest test = new AssertTest();
test.methodA(-10);
}
}


(1)It will print -10
(2)It will result in Assertion Error showing the message -"The value must not be negative"
(3)The code will not compile
(4)None of these

Answer : -------------------------

#210What will happen when you attempt to compile and run the following code?

public class Static
{
static
{
int x = 5;
}

static int x,y;
public static void main(String args[])
{
x--;
myMethod();
System.out.println(x + y + ++x);
}

public static void myMethod()
{
y = x++ + ++x;
}
}


(1)Compile-time error
(2)prints 1
(3)prints 2
(4)prints 3
(5)prints 7
(6)prints 8

Answer : -------------------------

#211What will happen when you attempt to compile and run the following code?

class MyParent
{
int x, y;
MyParent(int x, int y)
{
this.x = x;
this.y = y;
}
public int addMe(int x, int y)
{
return this.x + x + y + this.y;
}
public int addMe(MyParent myPar)
{
return addMe(myPar.x, myPar.y);
}
}

class MyChild extends MyParent
{
int z;
MyChild (int x, int y, int z)
{
super(x,y);
this.z = z;
}
public int addMe(int x, int y, int z)
{
return this.x + x + this.y + y + this.z + z;
}
public int addMe(MyChild myChi)
{
return addMe(myChi.x, myChi.y, myChi.z);
}
public int addMe(int x, int y)
{
return this.x + x + this.y + y;
}
}

public class MySomeOne
{
public static void main(String args[])
{
MyChild myChi = new MyChild(10, 20, 30);
MyParent myPar = new MyParent(10, 20);
int x = myChi.addMe(10, 20, 30);
int y = myChi.addMe(myChi);
int z = myPar.addMe(myPar);
System.out.println(x + y + z);
}
}


(1)300
(2)240
(3)120
(4)180
(5)Compilation error
(6)None of the above

Answer : -------------------------

#212What will be the result of executing the following code?

1. boolean a = true;
2. boolean b = false;
3. boolean c = true;
4. if (a == true)
5. if (b == true)
6. if (c == true)
7. System.out.println("Some things are true in this world");
8. else
9. System.out.println("Nothing is true in this world!");
10. else if (a && (b = c))
11. System.out.println("It's too confusing to tell what is true and what is false");
12. else
13. System.out.println("Hey this won't compile");


(1)The code won't compile
(2)"Some things are true in this world" will be printed
(3)"Hey this won't compile" will be printed
(4)None of these

Answer : -------------------------

#213What will happen when you attempt to compile and run the following code?

interface MyInterface
{

}
public class MyInstanceTest implements MyInterface
{
static String s;

public static void main(String args[])
{
MyInstanceTest t = new MyInstanceTest();

if(t instanceof MyInterface)
{
System.out.println("I am true interface");
}
else
{
System.out.println("I am false interface");
}

if(s instanceof String)
{
System.out.println("I am true String");
}
else
{
System.out.println("I am false String");
}
}
}


(1)Compile-time error
(2)Runtime error
(3)Prints : "I am true interface" followed by " I am true String"
(4)Prints : "I am false interface" followed by " I am false String"
(5)Prints : "I am true interface" followed by " I am false String"
(6)Prints : "I am false interface" followed by " I am true String"

Answer : -------------------------

#214What results from attempting to compile and run the following code?

public class Ternary
{
public static void main(String args[])
{
int a = 5;
System.out.println("Value is - " + ((a < 5) ? 9.9 : 9));
}
}


(1)prints: Value is - 9
(2)prints: Value is - 5
(3)Compilation error
(4)None of these

Answer : -------------------------

#215In the following pieces of code, A and D will compile without any error. True/False?

A: StringBuffer sb1 = "abcd";
B: Boolean b = new Boolean("abcd");
C: byte b = 255;
D: int x = 0x1234;
E: float fl = 1.2;



(1)True
(2)False

Answer : -------------------------

#216Which of the following collection classes from java.util package are Thread safe?

(1)Vector
(2)ArrayList
(3)HashMap
(4)Hashtable

Answer : -------------------------

#217What will happen when you attempt to compile and run the following code?

class MyThread extends Thread
{
public void run()
{
System.out.println("MyThread: run()");
}

public void start()
{
System.out.println("MyThread: start()");
}
}

class MyRunnable implements Runnable
{
public void run()
{
System.out.println("MyRunnable: run()");
}

public void start()
{
System.out.println("MyRunnable: start()");
}
}

public class MyTest
{
public static void main(String args[])
{
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
thread.start();
}
}


(1)Prints : MyThread: start() followed by MyRunnable:run()
(2)Prints : MyThread: run() followed by MyRunnable:start()
(3)Prints : MyThread: start() followed by MyRunnable:start()
(4)Prints : MyThread: run() followed by MyRunnable:run()
(5)Compile time error
(6)None of the above

Answer : -------------------------

#218What will be the result of executing the following code?

// Filename; SuperclassX.java
package packageX;

public class SuperclassX
{
protected void superclassMethodX()
{
}
int superclassVarX;
}


// Filename SubclassY.java
1. package packageX.packageY;
2.
3. public class SubclassY extends SuperclassX
4. {
5. SuperclassX objX = new SubclassY();
6. SubclassY objY = new SubclassY();
7. void subclassMethodY()
8. {
9. objY.superclassMethodX();
10. int i;
11. i = objY.superclassVarX;
12. }
13. }


(1)Compilation error at line 5
(2)Compilation error at line 9
(3)Runtime exception at line 11
(4)None of these

Answer : -------------------------

#219What can cause a Thread to stop executing?

(1)Calling its own yield method
(2)Calling the yield method of another thread
(3)A call to the halt method of the Thread class
(4)Another thread is given higher priority

Answer : -------------------------

#220Which of the following statements are true?

(1)The keys of HashSet are not ordered
(2)The keys of LinkedHashSet are ordered
(3)The keys of LinkedHashSet are ordered but not sorted
(4)The keys of LinkedHashMap are sorted

Answer : -------------------------

#221Given:

public class Test{
public static void main(String[] args) {
Object a = new Object(); // the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();

d=c=b=a;
d=null;
}
}

How many objects are eligible for GC in the following code after d = null?


(1)1
(2)2
(3)3
(4)4

Answer : -------------------------

#222What, if anything, is wrong with the following code?

abstract class TestClass
{
transient int j;
synchronized int k;

final void TestClass(){}
static void f(){}
}




(1)The class TestClass cannot be declared abstract
(2)The variable j cannot be declared transient
(3)The variable k cannot be declared synchronized
(4)The constructor TestClass( ) cannot be declared final
(5)The method f( ) cannot be declared static

Answer : -------------------------

#223Which of these classes have a comparator() method? Select 2 correct options.

(1)TreeSet
(2)HashMap
(3)TreeMap
(4)HashSet
(5)ArrayList

Answer : -------------------------

#224Assume that Thread 1 currently holds the lock for an object (obj) for which 4 other threads, Thread 2 to 5, are waiting. Now, Thread 1 want to release the lock but as the same time, it want Thread 3 to get the lock. How will you accomplish this?

(1)Call t3.resume() after releasing the lock
(2)Call t3.release() after releasing the lock
(3)Instead of releasing the lock, call t3.accuire(obj);
(4)Instead of releasing the lock, call t3.notify(obj);
(5)None of these

Answer : -------------------------

#225What will be output by the following code?

public class MyFor {
public static void main(String argv[]){
int i;
int j;
outer:
for (i=1;i <3;i++)
inner:
for(j=1; j<3; j++) {
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
}
}
}


(1)Value for i=1 value for j=1
(2)Value for i=2 value for j=1
(3)Value for i=2 value for j=2
(4)Value for i=3 value for j=1

Answer : -------------------------

#226What will be the result of attempting to compile and run the following program?

public class TestClass
{
public static void main(String args[ ] )
{
A o1 = new C( );
B o2 = (B) o1;
System.out.println(o1.m1( ) );
System.out.println(o2.i );
}
}

class A { int i = 10; int m1( ) { return i; } }
class B extends A { int i = 20; int m1() { return i; } }
class C extends B { int i = 30; int m1() { return i; } }


(1)The progarm will fail to compile
(2)ClassCastException at runtime
(3)It will print 30, 20
(4)It will print 30, 30
(5)It will print 20, 20

Answer : -------------------------

#227
Consider the following lines of code:

System.out.println(null + true); //1
System.out.println(true + null); //2
System.out.println(null + null); //3

Which of the following statements are correct? Select 1 correct option


(1)None of the 3 lines will compile
(2)All the 3 line will compile and print nulltrue, truenull and nullnull respectively
(3)Line 1 and 2 won't compile but line 3 will print nullnull
(4)Line 3 won't compile but line 1 and 2 will print nulltrue and truenull respectively
(5)None of the above

Answer : -------------------------

#228Which of the following are true about the "default" constructor? Select 2 correct options

(1)It is provided by the compiler only if the class does not define any constructor
(2)It initializes the instance members of the class
(3)It calls the default 'no-args' constructor of the super class
(4)It initializes instance as well as class fields of the class
(5)It is provided by the compiler if the class does not define a 'no- args' constructor

Answer : -------------------------

#229Which of these methods from the Collection interface return the value true if the collection object was actually modified by the call? Select 3 correct options

(1)add( )
(2)retainAll( )
(3)containsAll( )
(4)contains( )
(5)remove()

Answer : -------------------------

#230Following is not a valid comment:

/* this comment /* // /** is not valid */


(1)True
(2)False

Answer : -------------------------

#231Which statements regarding the following code are correct?

class Outer
{
private void Outer() { }
protected class Inner
{
}
}

Select 1 correct option

(1)This code won't compile
(2)Constructor for Outer is public
(3)Constructor for Outer is private
(4)Constructor for Inner is public
(5)Constructor for Inner is protected

Answer : -------------------------

#232Consider the following method:

public float parseFloat( String s )
{
float f = 0.0f;
try
{
f = Float.valueOf( s ).floatValue();
return f ;
}
catch(NumberFormatException nfe)
{
f = Float.NaN ;
return f;
}
finally
{
f = 10.0f;
return f;
}
}

What will it return if the method is called with the input "0.0" ? Select 1 correct option



(1)It won't even compile
(2)It will return 10.0
(3)It will return Float.Nan
(4)It will return 0.0
(5)None of the above

Answer : -------------------------

#233The following code snippet will not compile.

int i = 10;
System.out.println( i<20 ? out1() : out2() );

Assume that out1 and out2 have method signature: public void out1(); and public void out2();

(1)True
(2)False

Answer : -------------------------

#234Giri has written the following class to prevent garbage collection of the objects of this class. Is he mistaken?

class SelfReferencingClassToPreventGC
{
private SelfReferencingClassToPreventGC a;
Object obj = new Vector();
public SelfReferencingClassToPreventGC()
{
a = this; //reference itself to make sure that this is not garbage collected.
}

public void finalize()
{
System.out.println("Object GCed");
}
}


(1)True
(2)False

Answer : -------------------------

#235What happens when you try to compile and run the following class:

public class TestClass
{
public static void main(String[] args) throws Exception
{
int a = Integer.MIN_VALUE;
int b = -a;
System.out.println( a+ " "+b);
}
}

Select 1 correct option

(1)It throws an OverFlowException
(2)It will print two same -ive numbers
(3)It will print two different -ive numbers
(4)It will print one -ive and one +ive number of same magnitude
(5)It will print one -ive and one +ive number of different magnitude

Answer : -------------------------

#236Consider the following code snippet:

void m1() throws Exception
{
try
{
// line1
}
catch (IOException e)
{
throw new SQLException();
}
catch(SQLException e)
{
throw new InstantiationException();
}
finally
{
throw new CloneNotSupportedException() // this is not a RuntimeException.
}
}

Which of the following statements are true? Select 2 correct options



(1)If IOException gets thrown at line1, then the whole method will end up throwing SQLException
(2)If IOException gets thrown at line1, then the whole method will end up throwing CloneNotSupportedException
(3)If IOException gets thrown at line1, then the whole method will end up throwing InstantiationException()
(4)If no exception is thrown at line1, then the whole method will end up throwing CloneNotSupportedException
(5)If SQLException gets thrown at line1, then the whole method will end up throwing InstantiationException()

Answer : -------------------------

#237Consider the following class hierarchy:

A
|
+----B1, B2
|
+----C1, C2


(B1 and B2 are subclasses of A and C1, C2 are subclasses of B1)


Assume that method public void m1(){ ... } is defined in all of these classes EXCEPT B1 and C1.

Which of the following statements are correct? Select 1 correct option

(1)objectOfC1.m1(); will cause a compilation error
(2)objectOfC2.m1(); will cause A's m1() to be called
(3)objectOfC1.m1(); will cause A's m1() to be called
(4)objectOfB1.m1(); will cause an expection at runtime
(5)objectOfB2.m1(); will cause an expection at runtime

Answer : -------------------------

#238What classes can an inner class extend? (Provided that the class is visible and is not final)

Select 1 correct option

(1)Only the encapsulating class
(2)Any top level class
(3)Any class
(4)It depends on whether the inner class is defined in a method or not
(5)None of the above

Answer : -------------------------

#239Which of the following statements are true?

(1)System.out.println( -1 >>> 2);will output a result larger than 10
(2)System.out.println( -1 >>> 2); will output a positive number
(3)System.out.println( 2 >> 1); Will output the number 1
(4)System.out.println( 1 <<< 2); will output the number 4

Answer : -------------------------

#240Given the following class definition, which of the following statements would be legal after the comment //Here?

class InOut{
String s= new String("Between");
public void amethod(final int iArgs){
int iam;
class Bicycle{
public void sayHello(){
//Here
}//End of bicycle class
}
}//End of amethod
public void another(){
int iOther;
}
}




(1)System.out.println(s);
(2)System.out.println(iOther);
(3)System.out.println(iam);
(4)System.out.println(iArgs);

Answer : -------------------------

#241Which statments regarding the following program are correct?

class A extends Thread
{
static protected int i = 0;
public void run()
{
for(; i<5; i++) System.out.println("Hello");
}
}

public class TestClass extends A
{
public void run()
{
for(; i<5; i++) System.out.println("World");
}

public static void main(String args [])
{
Thread t1 = new A();
Thread t2 = new TestClass();
t2.start(); t1.start();
}
}

Select 1 correct option

(1)It'll not compile as run method cannot be overridden
(2)It'll print both "Hello" and "World" 5 times each
(3)It'll print both "Hello" and "World" 5 times each but they may be interspersed
(4)Total 5 words will be printed
(5)Either 5 "Hello" or 5 "world" will be printed

Answer : -------------------------

#242Consider following two classes:

// file A.java
package p1;
public class A
{
protected int i = 10;
public int getI()
{
return i;
}
}

// file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i * 2;
}

public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println( a.getI() );
}
}

What will be the output of compiling and running class B?

Select 1 correct option


(1)It will print 10
(2)It will print 20
(3)It will not compile
(4)It will throw an exception at Run time
(5)None of the above

Answer : -------------------------

#243What will happen when you attempt to compile and run the following code with the command line "java hello there"

public class Arg{
String[] MyArg;
public static void main(String argv[]){
MyArg=argv;
}

public void amethod(){
System.out.println(argv[1]);
}
}


(1)Compile time error
(2)Compilation and output of "hello"
(3)Compilation and output of "there"
(4)None of the above

Answer : -------------------------

#244Which of the following statements are true [Check all correct answers]

(1)Checked exceptions are derived directly from Exception
(2)Checked exceptions are derived directly from RuntimeException
(3)Unchecked exceptions are derived directly from Exception
(4)Unchecked exceptions are derived direclty from RuntimeException
(5)Exception and RuntimeException are both subclasses of Throwable

Answer : -------------------------

#245Which of the following keywords are valid when declaring a top level class? [Check all correct answers]

(1)private
(2)native
(3)final
(4)transient
(5)abstract

Answer : -------------------------

#246After the execution of the code-fragment below, what will be the values of a, b and c?

public class TechnoSample {
public static void main(String[] args) {
int a = 2, b = 3, c = 4;

a = b++ + c;
c += b;
b += a * 2;

System.out.println("a: " + a + " b: " + b + " c: " + c);
}
}



(1)a: 2 b: 18 c: 8
(2)a: 7 b: 12 c: 8
(3)a: 7 b: 18 c: 8
(4)a: 7 b: 18 c: 4
(5)None of the above

Answer : -------------------------

#247What, if anything, is wrong with the following code?

abstract class TestClass
{
transient int j;
synchronized int k;

final void TestClass(){}
static void f()
{
k = j++;
}
}

Select 2 correct options


(1)The class TestClass cannot be declared abstract
(2)The variable j cannot be declared transient
(3)The variable k cannot be declared synchronized
(4)The constructor TestClass( ) cannot be declared final
(5)The method f( ) cannot be declared static

Answer : -------------------------

#248Which of the following statements are true?

(1)An object will be garbage collected when it becomes unreachable
(2)An object will be garbage collected if it has null assigned to it
(3)The finalize method will be run before an object is garbage collected
(4)Garbage collection assures that a program will never run out of memory

Answer : -------------------------

#249What will the following program print?

class TechnoSample
{
public static void main(String[] args)
{
int i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println(ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}
Select 1 correct option

(1)3, 4, 3
(2)3, 3, 3
(3)4, 3, 4
(4)4, 3, 3
(5)It will not compile

Answer : -------------------------

#250Consider following two classes:

//in file A.java
package p1;
public class A
{
protected int i = 10;
public int getI() { return i; }
}

//in file B.java
package p2;
import p1.*;
public class B extends p1.A
{
public void process(A a)
{
a.i = a.i*2;
}
public static void main(String[] args)
{
A a = new B();
B b = new B();
b.process(a);
System.out.println(a.getI());
}
}

What will be the output of compiling and running class B?

Select 1 correct option



(1)It will print 10
(2)It will print 20
(3)It will not compile
(4)It will throw an exception at Run time
(5)None of the above

Answer : -------------------------


2001-2003 Technopark. Developed by: Giri Mandalika