You are on page 1of 31

Question 1 of 61

Consider the following method:


public static void doIt( String String ) // 1
{
int i = 10;
i : for (int k = 0 ; k< 10; k++) // 2
{
System.out.println( String + i); //3
if( k*k > 10) continue i; //4
}
}

Which of the following statements regarding the above method are correct?

Select 1 correct option.


a It will not compile because of line 1.
b It will not compile because of line 2.
c It will not compile because of line 3.
d It will not compile because of line 4.
e It will compile and run without problems.

Given the following source code, which of the lines that are commented out may be
reinserted without introducing errors?
abstract class Bang
{
//abstract void f(); //(0)
final void g(){}
//final void h(){} //(1)
protected static int i;
private int j;
}
final class BigBang extends Bang
{
//BigBang(int n) { m = n } //(2)
public static void main(String args[])
{
Bang mc = new BigBang();
}
void h(){}
//void k(){ i++; } //(3)
//void l(){ j++; } //(4)
int m;
}
Select 1 correct option.
a final void h( ) { } //(1)
b BigBang(int n) { m = n } //(2)
c void k( ) { i++ } //(3)
d void l( ) { j++ } //(4)
e abstract void f( ) ; //(0)

Question 3 of 61

An anonymous class can be declared in a static method.

Select 1 correct option.


a True
b False

Question 4 of 61

Consider the following code in which searchBook() method has an inner class.
Which variables are accessible at line 1?
public class BookStore
{
private static final int taxId = 300000;
private String name;
public String searchBook( final String criteria )
{
int count = 0;
class Enumerator
{
String interate( int k)
{
//line 1
// lots of code.....
return "";
}
// lots of code.....
}
// lots of code.....
return "";
}
}

Select 4 correct options


a taxId
b name
c criteria
d count
e k
Question 5 of 61

Which integral type in Java has a range from -2 ^31 to 2^31-1

Select 1 correct option.


a char
b int
c long
d double
e byte

Question 6 of 61

What will the following code print when run?


public class Test
{
static String s = "";
public static void m0(int a, int b)
{
s +=a;
m2();
m1(b);
}
public static void m1(int i)
{
s += i;
}
public static void m2()
{
throw new NullPointerException("aa");
}
public static void m()
{
m0(1, 2);
m1(3);
}
public static void main(String args[])
{
try
{
m();
}
catch(Exception e){ }
System.out.println(s);
}
}

Select 1 correct option.


a 1
b 12
c 123
d 2
e It will throw exception at runtime.

Question 7 of 61

What do you need to do to define and start a thread?

Select 1 correct option.


a Extend from class Thread, override method run() and call method start();
b Extend from class Runnable, override method run() and call method start();
c Extend from class Thread, override method start() and call method run();
d Implement interface Runnable and call start()
e Extend from class Thread, override method start() and call method start();

Question 8 of 61

Consider the following code:


class Super
{
static{ System.out.print("super "); }
}
class One
{
static { System.out.print("one "); }
}
class Two extends Super
{
static { System.out.print("two "); }
}
class Test
{
public static void main(String[] args)
{
One o = null;
Two t = new Two();
}
}
What will be the output when class Test is run ?

Select 1 correct option.


a It will print one, super and two.
b It will print one, two and super.
c It will print super and two.
d It will print two and super
e None of the above.

Question 9 of 61

What will be the output if you run the following program?


public class TestClass
{
public static void main(String args[])
{
int i;
int j;
for (i = 0, j = 0 ; j < 1 ; ++j , i++)
{
System.out.println( i + " " + j );
}
System.out.println( i + " " + j );
}
}

Select 1 correct option.


a 0 0 will be printed twice.
b 1 1 will be printed once.
c 0 1 will be printed followed by 1 2.
d 0 0 will be printed followed by 1 1.
e It will print 0 0 and then 0 1.

Question 10 of 61

Given :
byte b = 1;
char c = 1;
short s = 1;
int i = 1;
which of the following expressions are valid?

Select 3 correct options


a s=b*b;
b i = b << b ;
c s <<= b ;
d c=c+b;
e s += i ;
Question 11 of 61

Which one can hold a larger integer value, char or short ?

Select 1 correct option.


a char
b short
c largest integer that both can hold are same.
d They cannot be compared as char can hold only character values.
e None of the above

Question 12 of 61

What will the following code print when run?


public class Test
{
static String j = "";
public static void method( int i)
{
try
{
if(i == 2)
{
throw new Exception();
}
j += "1";
}
catch (Exception e)
{
j += "2";
return;
}
finally
{
j += "3";
}
j += "4";
}
public static void main(String args[])
{
method(1);
method(2);
System.out.println(j);
}
}

Select 1 correct option.


a 13432
b 13423
c 14324
d 12434
e 12342

Question 13 of 61

After which line may the object created at line 1 be garbage collected?
public class TestClass
{
public static Object getObj()
{
Object obj = new Object(); //1
Object obj2 = obj; //2
obj2 = null; //3
return obj; //4
}
public static void main(String[] args)
{
Object ob = getObj(); //5
ob = null; //6
}
}
Select 1 correct option.
a 2
b 3
c 4
d 5
e 6

Question 14 of 61

The numeric value for the letter 'a' is 97.


Which of these code fragments will successfully declare and initialize a variable of type
'char' with this value?

Select 2 correct options


a char ch = 'a';
b char ch = '\97';
c char ch = '\u0097';
d char ch = 0x97;
e char ch = 97;
Question 15 of 61

Given the following code, which of these constructors can be added to class B without
causing a compile time error?
class A
{
int i;
public A(int x) { this.i = x; }
}
class B extends A
{
int j;
public B(int x, int y) { super(x); this.j = y; }
}

Select 2 correct options


a B( ) { }
b B(int y ) { j = y; }
c B(int y ) { super(y*2 ); j = y; }
d B(int y ) { i = y; j = y*2 }
e B(int z ) { this(z, z); }

Question 16 of 61

Which of the following are valid operators in Java?

Select 4 correct options


a !
b ~
c &
d %=
e $

Question 17 of 61

What classes can an inner class extend ?


(Provided that the class is visible and is not final.)

Select 1 correct option.


a Only the encapsulating class.
b Any top level class.
c Any class.
d It depends on whether the inner class is defined in a method or not.
e None of the above.
Question 18 of 61

Which of the following statements are true?

Select 2 correct options


a method length() of String class is a final method.
b You can make mutable subclasses of the String class.
c StringBuffer extends String.
d StringBuffer is a final class.
e String class is not final.

Question 19 of 61

Consider the following class:


public class IntPair
{
private int a;
private int b;
public void setA(int i){ this.a = i; }
public int getA(){ return this.a; }
public void setB(int i){ this.b = i; }
public int getB(int b){ return b; }
public boolean equals(Object obj)
{
return ( obj instanceof IntPair && this.a == ((IntPair) obj).a );
}
public int hashCode()
{
//1
}
}
Which of the following options would be valid at //1?

Select 4 correct options


a return 0;
b return a;
c return a+b;
d return a*a;
e return a/2;
Question 20 of 61

Consider the following class :


What will happen if it is run with the following command line:
java Parser one
public class Parser
{
public static void main( String[] args)
{
try
{
int i = 0;
i = Integer.parseInt( args[0] );
}
catch(NumberFormatException e)
{
System.out.println("Problem in " + i );
}
}
}

Select 1 correct option.


a It will print 'Problem in 0'
b It will throw an exception and end without printing anything.
c It will not even compile.
d It will not print anything if the argument is '1' instead of 'one'.
e None of the above.

Question 21 of 61

When extending the Thread class to provide a thread's behaviour, which methods should
be overriden?

Select 1 correct option.


a begin( )
b start( )
c run( )
d perform_work( )
e do( )
Question 22 of 61

What statements regarding the following code snippet are ture?


int a = 4;
int b = 6;
System.out.println( a--b ); // ie. a (no space) minus (no space)
minus (no space) b

Select 4 correct options


a It will print 10.
b It won't compile.
c It will compile if a--b is replaced by a- -b
d It will compile if a--b is replaced by a---b //ie. 3 minus signs
e It will compile if a--b is replaced by a+-b

Question 23 of 61

What is wrong with the following code?


class MyException extends Exception {}
public class TestClass
{
public static void main(String[] args)
{
TestClass tc = new TestClass();
try
{
tc.m1();
}
catch (MyException e)
{
tc.m1();
}
finally
{
tc.m2();
}
}
public void m1() throws MyException
{
throw new MyException();
}
public void m2() throws RuntimeException
{
throw new NullPointerException();
}
}

Select 1 correct option.


a It will not compile because you cannot throw an exception in finally block.
b It will not compile because you cannot throw an exception in catch block.
c It will not compile because NullPointerException cannot be created this way.
d It will not compile.
e It will compile but will throw an exception when run.

Question 24 of 61

If a Thread's priority is not specified explicitly then it gets a priority of


Thread.NORM_PRIORITY

Select 1 correct option.


a True
b False

Question 25 of 61

Given the following class, which of these given blocks can be inserted at line 1 without
errors?
public class InitClass
{
private static int loop = 15 ;
static final int INTERVAL = 10 ;
boolean flag ;
//line 1
}

Select 4 correct options


a static {System.out.println("Static"); }
b static { loop = 1; }
c static { loop += INTERVAL; }
d static { INTERVAL = 10; }
e { flag = true; loop = 0; }
Question 26 of 61

Given the declaration


interface Worker { void perform_work(); }
which of the following methods/class are valid?
1.
Worker getWorker(int i)
{
return new Worker(){ public void perform_work()
{ System.out.println(i); } };
}
2.
Worker getWorker(final int i)
{
return new Worker() { public void perform_work()
{ System.out.println(i); } };
}
3.
Worker getWorker(int i)
{
int x = i;
class MyWorker implements Worker { public void perform_work()
{ System.out.println(x); } };
return new MyWoker();
}
4.
Worker getWorker(final int i)
{
class MyWorker implements Worker { public void perform_work()
{ System.out.println(i); } };
return new MyWorker();
}
5.
class TestClass
{
Worker getWorker(int i)
{
return new MyWorker( i);
}
public static class MyWorker implements Worker
{
int x;
MyWorker(int i) { x = i; }
public void perform_work( ) { System.out.println(x); }
}
}

Select 3 correct options


a 1.
b 2.
c 3.
d 4.
e 5.

Question 27 of 61

Which of the following may pause/stop the current thread?

Select 3 correct options


a calling Thread.yield()
b calling someObject.wait()
c calling someObj.notify()
d calling end() method on the Thread object.

Question 28 of 61

Consider the following code:


class Super { static String ID = "QBANK"; }
class Sub extends Super
{
static { System.out.print("In Sub"); }
}
class Test
{
public static void main(String[] args)
{
System.out.println(Sub.ID);
}
}
What will be the output when class Test is run ?

Select 1 correct option.


a It will print 'In Sub' and 'QBANK'.
b It will print 'QBANK'.
c Depends on the implementation of JVM.
d It will not even compile.
e None of the above.
Question 29 of 61

Following is a program to capture words from command line and create two collections.
One that keeps only unique words and one that keeps all the words in the order that they
were entered. What should replace AAA and BBB?
import java.util.*;
import java.io.*;
public class TestClass
{
static Collection unique = new AAA();
static Collection ordered = new BBB();
public static void main(String args[]) throws Exception
{
BufferedReader bfr = new BufferedReader( new InputStreamReader( System.in
));
String s = bfr.readLine();
while(s != null && s.length() >0)
{
unique.add(s);
ordered.add(s);
s = bfr.readLine();
}
System.out.println(unique);
System.out.println(ordered);
}
}

Select 2 correct options


a Set, List
b LinkedList, HashSet
c HashSet, LinkedList
d HashSet, Vector
e Vector, TreeSet

Question 30 of 61

Which of the following is thrown when an assertion fails?

Select 1 correct option.


a AssertionError
b AssertionException
c RuntimeException
d AssertionFailedException
e Exception
Question 31 of 61

What will be the output when the following program is run?


public class TestClass
{
char c;
public void m1()
{
char[ ] cA = { 'a' , 'b'};
m2(c, cA);
System.out.println( ( (int)c) + ", " + cA[1] );
}
public void m2(char c, char[ ] cA)
{
c = 'b';
cA[1] = cA[0] = 'm';
}
public static void main(String args[])
{
new TestClass().m1();
}
}

Select 1 correct option.


a Compile time error.
b ,m
c 0,m
d b,b
e b,m

Question 32 of 61

Which package are the standard collection classes part of?

Select 1 correct option.


a java.io
b javax.collections
c java.collections
d java.util
e java.util.collection
f javax.nio

Question 33 of 61

An overloading method must have a different parameter list and same return type as that
of the overloaded method.
Select 1 correct option.
a True
b False

Question 34 of 61

Which of the following code snippets will compile fine?

Select 3 correct options


a while (false) { x=3; }
b if (false) { x=3; }
c do{ x = 3; } while(false);
d for( int i = 0; i< 0; i++) x = 3;

Question 35 of 61

Consider the following program...


public class TestClass implements Runnable
{
int x = 0, y = 0;
public void run()
{
while(true)
{
x++; y++;
System.out.println(" x = "+x+" , y = "+y);
}
}
public static void main(String[] args)
{
TestClass tc = new TestClass();
new Thread(tc).start();
new Thread(tc).start();
}
}

Select 1 correct option.


a It will throw exception at run time as two thread cannot be created from the
same object.
b It will keep on printing values which show x and y always as equal and
increasing by 1 at each line.
c It will keep on printing values which show x and y always as different.
d Nothing can be said about the sequence of values.
e It will keep on printing values which show x and y always as equal but may
increase more than 1 at each line.
Question 36 of 61

Given the following interface definition, which definitions are valid?


interface I1
{
void setValue(String s);
String getValue();
}
Options :
(a)
class A extends I1
{
String s;
void setValue(String val) { s = val; }
String getValue() { return s; }
}
(b)
interface I2 extends I1
{
void analyse();
}
(c)
abstract class B implements I1
{
int getValue(int i) { return 0; }
}
(d)
interface I3 implements I1
{
void perform_work();
}

Select 2 correct options


a Definition a.
b Definition b.
c Definition c.
d Definition d.
Question 37 of 61

Consider that you are writing a set of classes related to a new Data Transmission Protocol
and have created your own exception hierarchy derived from java.lang.Exception as
follows:
java.lang.Exception
+-- enthu.trans.ChannelException
+-- enthu.trans.DataFloodingException, enthu.trans.FrameCollisionException
(I.e. both DataFlooding and FrameCollision are subclasses of ChannelException)
Your base class, "TransSocket" has a method declared as follows:
long connect(String ipAddr) throws ChannelException
You also want to write another "AdvancedTransSocket" class, derived from
"TransSocket" which overrides the above mentioned method. Which of the following are
valid declaration of the overriding method?

Select 2 correct options


a int connect(String ipAddr) throws DataFloodingException
b int connect(String ipAddr) throws ChannelException
c long connect(String ipAddr) throws FrameCollisionException
d long connect(String ipAddr) throws Exception
e long connect(String str)

Question 38 of 61

Which of the following statements will evaluate to true?

Select 1 correct option.


a "String".replace('g','G') == "String".replace('g','G')
b "String".replace('g','g') == new String("String").replace('g','g')
c "String".replace('g','G')=="StrinG"
d "String".replace('g','g')=="String"
e None of these.
Question 39 of 61

What will be the result of attempting to compile and run the following code?
public class TestClass
{
public static void main(String args[] )
{
Outer out = new Outer();
System.out.println(out.getInner().getOi());
}
}
class Outer
{
private int oi = 20;
class Inner
{
int getOi() { return oi; }
}
Inner getInner() { return new Inner() ; }
}

Select 1 correct option.


a The code will fail to compile as the inner class Inner is not defined properly.
b The code will fail to compile, you cannot pass the reference of inner classes
cannot be passed outside the outer class.
c The code will fail to compile, since the method getOi( ) is not visible from the
main( ) method in the TestClass.
d The code will compile without error and will print 20 when run.
e None of the above.

Question 40 of 61

Which of the following statements are true?

Select 2 correct options


a Private methods cannot be overriden in subclasses.
b A subclass can override any method in a non-final superclass.
c An overriding method can declare that it throws a wider spectrum of exceptions
than the method it is overriding.
d The parameter list of an overriding method must be a subset of the parameter list
of the method that it is overriding.
e The over riding method may opt not to declare any throws clause even if the
original method has a throws clause.
Question 41 of 61

After which line will the object created at line XXX be eligible for garbage collection?
public Object getObject(Object a) //0
{
Object b = new Object(); //XXX
Object c, d = new Object(); //1
c = b; //2
b = a = null; //3
return c; //4
}

Select 1 correct option.


a //2
b //3
c //4
d Never in this method.
e Can't say.

Question 42 of 61

Which of these statements concerning nested classes and interfaces are true?

Select 3 correct options


a An instance of a top-level nested class has an inherent outer instance.
b A top-level nested class can contain non-static member variables.
c A top-level nested interface can contain static member variables.
d A top-level nested interface has an inherent outer instance associated with it.
e For each instance of the outer class, there can exist many instances of a non-
static inner class.
Question 43 of 61

What will the following class print ?


class InitTest
{
public static void main(String[] args)
{
int a = 1;
int b = 1;
a = a++;
b = b++ + b;
System.out.println(a+ " "+b);
}
}

Select 1 correct option.


a It will print 2, 3
b It will print 1, 3
c It will print 2, 2
d It will print 1, 2
e It will not compile.

Question 44 of 61

Consider the following program...


public class Outer
{
private double d = 10.0;
//put inner class here.
}
Which of the following options are correct?
1. class Inner
{
public void m1() { this.d = 20.0; }
}
2. abstract class Inner
{
public void m1() { d = 20.0; }
}
3. final class Inner
{
public void m1() { d = 20.0; }
}
4. private class Inner
{
public void m1() { d = 20.0; }
}
Select 3 correct options
a 1
b 2
c 3
d 4

Question 45 of 61

Consider the following classes:


Which is the correct sequence of the digits that will be printed when B is run?
class A
{
public A() { }
public A(int i) { System.out.println(i ); }
}
class B
{
static A s1 = new A(1);
A a = new A(2);
public static void main(String[] args)
{
B b = new B();
A a = new A(3);
}
static A s2 = new A(4);
}

Select 1 correct option.


a 1 ,2 ,3 4.
b 1 ,4, 2 ,3
c 3, 1, 2, 4
d 2, 1, 4, 3
e 2, 3, 1, 4
Question 46 of 61

Consider the following code:


public class Conversion
{
public static void main(String[] args)
{
int i = 1234567890;
float f = i;
System.out.println(i - (int)f);
}
}

Select 1 correct option.


a It will print 0.
b It will not print 0.
c It will not compile.
d It will throw an exception at runtime.
e None of the above.

Question 47 of 61

Which of the following statements are correct?

Select 1 correct option.


a A List stores elements in a Sorted Order.
b A Set keeps the elements sorted and a List keeps the elements ordered.
c A SortedSet keeps the elements ordered.
d An OrderedSet keeps the elements sorted.
e An OrderedList keeps the elements ordered.
Question 48 of 61

What will the following program print when run?


class Super
{
public String toString()
{
return "4";
}
}
public class SubClass extends Super
{
public String toString()
{
return super.toString()+"3";
}
public static void main(String[] args)
{
System.out.println( new SubClass() );
}
}

Select 1 correct option.


a 43
b 7
c It will not compile.
d It will throw an exception at runtime.
e None of the above.

Question 49 of 61

Which methods can be applied to a String objects?

Select 3 correct options


a equals(object)
b equals(string)
c prune()
d append()
e intern()
Question 50 of 61

Consider the following code...


public class TestClass
{
public static int switchTest(int k)
{
int j = 1;
switch(k)
{
case 1: j++;
case 2: j++;
case 3: j++;
case 4: j++;
case 5: j++;
default : j++;
}
return j + k;
}
public static void main(String[] args)
{
System.out.println( switchTest(4) );
}
}

What would it print?

Select 1 correct option.


a 5
b 6
c 7
d 8
e 9
Question 51 of 61

What can be done to get the following code compile and run?
public float parseFloat( String s )
{
float f = 0.0f; // 1
try
{
f = Float.valueOf( s ).floatValue(); // 2
return f ; // 3
}
catch(NumberFormatException nfe)
{
f = Float.NaN ; // 4
return f; // 5
}
finally {
return f; // 6
}
return f ; // 7
}

Select 4 correct options


a Remove line 3, 6
b Remove line 5
c Remove line 5, 6
d Remove line 7
e Remove line 3, 7

Question 52 of 61

Which of these expressions will return true?

Select 4 correct options


a "hello world" .equals("hello world")
b "HELLO world".equalsIgnoreCase("hello world")
c "hello".concat(" world").trim().equals("hello world")
d "hello world" .compareTo("Hello world") < 0
e "Hello world".toLowerCase( ).equals("hello world")
Question 53 of 61

Consider the following code to count objects and save the most recent object ...
int i = 0 ;
Object prevObj ;
public void saveObject(List e )
{
prevObject = e ;
i++ ;
}
Which of the following calls will work without throwing an exception?

Select 3 correct options


a saveObject( new ArrayList() );
b Collection c = new ArrayList(); saveObject( c );
c List l = new ArrayList(); saveObject(l);
d saveEvent(null);
e saveEvent(0);

Question 54 of 61

Select the correct statement regarding the following code:


public void m1()
{
int i;
...
try
{
assert i == 20;
}
catch(Exception e)
{
i = 20;
}
System.out.println(i);
...
}

Select 1 correct option.


a It will print 20.
b It will print 20 only if 'i' is equal to 20 before the assertion.
c A exception thrown by a failed assertion cannot be caught using a try/catch
block.
d It will throw an AssertionException if 'i' is not equal to 20.
e The program will not compile.
Question 55 of 61

Consider the following lines of code:


boolean greenLight = true;
boolean pedestrain = false;
boolean rightTurn = true;
boolean otherLane = false;
You can go ahead only if the following expression evaulates to 'true' :
(( (rightTurn && !pedestrain || otherLane) || ( ? && !pedestrain && greenLight ) ) ==
true )
What variables can you put in place of '?' so that you can go ahead ?

Select 1 correct option.


a rightTurn
b otherLane
c Any variable would do.
d None of the variable would allow to go.

Question 56 of 61

Which method declarations will enable a class to be run as a standalone program?

Select 2 correct options


a static void main(String args[ ])
b public void static main(String args[ ])
c public static main(String[ ] argv)
d final public static void main(String [ ] array)
e public static void main(String args[ ])

Question 57 of 61

Which of the following is the correct way to start a new thread?

Select 1 correct option.


a A thread starts automatically when instantiated. ie. new Thread(); or new Thread
( runnable );
b Create a new thread and call the method begin( ) on the thread.
c Create a new thread and call the method start( ) on the thread.
d Create a new thread and call the method run( ) on the thread.
e Create a new thread and call the method resume( ) on the thread.
Question 58 of 61

Given the following code, which method declarations can be inserted at line 1 without
any problems?
public class OverloadTest
{
public int sum(int i1, int i2) { return i1 + i2; }
// 1
}

Select 3 correct options


a public int sum(int a, int b) { return a + b; }
b public int sum(long i1, long i2) { return (int) i1; }
c public int sum(int i1, long i2) { return (int) i2; }
d public long sum(long i1, int i2) { return i1 + i2; }
e public long sum(int i1, int i2) { return i1 + i2; }

Question 59 of 61

Consider this code:


class A { }
class B extends A implements X1
{ }
class C extends B implements X2
{
D d = new D();
}
class D { }
Which of the following statements are true?

Select 3 correct options


a D is-a B.
b B has-a D.
c C is-a A
d c is-like-a X1
e c is-like-a X2

Question 60 of 61

Which of the following statements regarding the assertion mechanism of Java is NOT
correct?

Select 2 correct options


a Assertions require changes at the JVM level.
b Assertions require changes at the API level.
c Assertions can be enabled or disabled at runtime.
d Code that uses Assertions cannot be run on version below 1.4
e Code written for JDK version 1.3 cannot be compiled under JDK version 1.4

Question 61 of 61

Select the correct order of restrictiveness for access modifiers...


(First one should be least restrictive)

Select 1 correct option.


a public < protected < package (ie. no modifier) < private
b public < package (ie. no modifier) < protected < private
c public < protected < private < package (ie. no modifier)
d protected < package (ie. no modifier) < private < public
e depends on the implementation of the class or method.

You might also like