You are on page 1of 21

Question 1 of 47

What will the following program print?


public class TestClass
{
static String str;
public static void main(String[] args)
{
System.out.println(str);
}
}

Select 1 correct option.


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

Question 2 of 47

What will be the output of the following lines ?


System.out.println("" +5 + 6); //1
System.out.println(5 + "" +6); // 2
System.out.println(5 + 6 +""); // 3
System.out.println(5 + 6); // 4

Select 1 correct option.


a 56, 56, 11, 11
b 11, 56, 11, 11
c 56, 56, 56, 11
d 56, 56, 56, 56
e 56, 56, 11, 56
Question 3 of 47

Consider the following code:


class MyRunnable implements Runnable
{
public static void main(String[] args)
{
new Thread( new MyRunnable(2) ).start();
}
public void run(int n)
{
for(int i=0; i<n; i++)
{
System.out.println("Hello World");
}
}
}

What will be the output when this program is compiled and run from the command line?

Select 1 correct option.


a It'll print "Hello World" once.
b It'll print "Hello World" twice.
c This program will not even compile.
d This will compile but will throw an exception at runtime.
e It will compile and run but it will not print anything.

Question 4 of 47

Consider the following lines of code...


1. public void method()
{
2. String a,b;
3. a=new String("hello world");
4. b=new String("good bye");
5. System.out.println(a+b+" ! ");
6. a=null;
7. a=b;
8. System.out.println(a);
}
In the absence of compiler optimization, after which earliest line the object originally
referred by a is definitely eligible to be garbage collected?
(Do not add spaces or dots. Just type in the number.)
Question 5 of 47

Write the name of the method used to schedule a thread for execution?
(Do not enter any space or brackets.)

Question 6 of 47

The following method will compile and run without any problems.
public void switchTest(byte x)
{
switch(x)
{
case 'b': // 1
default : // 2
case -2: // 3
case 80: // 4
}
}

Select 1 correct option.


a True
b False

Question 7 of 47

'3b1' is the hex value for the Greek letter alpha in the Unicode.
Write the Unicode literal which can be used to initialize a char variable to alpha.
(Just write what should replace uuuuuu in the following statement?
char alpha = 'uuuuuu' ; )
Question 8 of 47

Consider the following program...


What will happen if you try to compile and run the program?
public class TestClass
{
public static void main(String[] args) { calculate(2); }
public static void calculate(int x)
{
String val;
switch(x)
{
case 2:
default:
val = "def";
}
System.out.println(val);
}
}

Select 2 correct options


a It will not compile saying that variable ' val' may not have been initialized..
b It will compile and print 'def'.
c As such it will not compile but will compile if calculate(2); is replaced
by calculate(3);
d It will compile for any int values in calculate(...);
Question 9 of 47

What will be the result of compiling and running the following program ?
class NewException extends Exception {}
class AnotherException extends Exception {}
public class ExceptionTest
{
public static void main(String[] args) throws Exception
{
try
{
m2();
}
finally
{
m3();
}
catch (NewException e){}
}
public static void m2() throws NewException { throw new
NewException(); }
public static void m3() throws AnotherException{ throw new
AnotherException(); }
}

Select 1 correct option.


a It will compile but will throw AnotherException when run.
b It will compile but will throw NewException when run.
c It will compile and run without throwing any exceptions.
d It will not compile.
e None of the above.
Question 10 of 47

What will be the output of the following program ?


class Test
{
public static void main(String[] args)
{
String s = "going";
print(s, s = "gone");
}
static void print(String a, String b)
{
System.out.println(a +", "+ b );
}
}

Select 1 correct option.


a It will print going, gone
b It will print gone, gone.
c It will print gone, going.
d It will print going, going.
e It will not compile.

Question 11 of 47

Using a continue in a while loop causes the loop to break the current iteration and start
the next iteration of the loop.

Select 1 correct option.


a True
b False

Question 12 of 47

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.


a None of the 3 lines will compile.
b All the 3 line will compile and print nulltrue, truenull and nullnull respectively.
c Line 1 and 2 won't compile but line 3 will print nullnull.
d Line 3 won't compile but line 1 and 2 will print nulltrue and truenull
respectively.
e None of the above.

Question 13 of 47

What will be the result of trying to compile and execute of the following program?
public class TestClass
{
public static void main(String args[] )
{
int i = 0 ;
int[] iA = {10, 20} ;
iA[i] = i = 30 ;
System.out.println(""+ iA[ 0 ] + " " + iA[ 1 ] + " "+i) ;
}
}

Select 1 correct option.


a It will throw ArrayIndexOutofBoundsException at Runtime.
b Compile time Error.
c It will prints 10 20 30
d It will prints 30 20 30
e It will prints 0 20 30

Question 14 of 47

What will be the result of attempting to compile and run the following code?
public class PromotionTest
{
public static void main(String args[])
{
int i = 5;
float f = 5.5f;
double d = 3.8;
char c = 'a';
if (i == f) c++;
if (((int) (f + d)) == ((int) f + (int) d)) c += 2;
System.out.println(c);
}
}
Select 1 correct option.
a The code will fail to compile.
b It will print d.
c It will print c.
d It will print b
e It will print a.

Question 15 of 47

Given the following declaration, select the correct way to get the size of the array,
assuming that the array has been initialized.
int[] intArr;

Select 1 correct option.


a intArr[ ].length( )
b intArr.length( )
c intArr.length
d intArr[ ].size( )
e intArr.size( )

Question 16 of 47

Under what situations does a class get a default constructor?

Select 1 correct option.


a All classes in Java get a default constructor.
b You have to define atleast one constructor to get the default constructor.
c If the class does not define any constructors on it's own.
d All classes get default constructor from Object class.
e None of the above.
Question 17 of 47

In java, strings are immutable. A direct implication of this is...

Select 1 correct option.


a you cannot call methods like "1234".replace('1', '9');
b you cannot change a string object, once it is created.
c you can change a string object only by the means of it's methods.
d you cannot extend String class.
e you cannot compare string objects.
Question 18 of 47

The JVM exits when main() method reaches its end.

Select 1 correct option.


a True
b False

Question 19 of 47

Consider the following class...


class TestClass
{
int x;
public static void main(String[] args)
{
// lot of code.
}
}

Select 1 correct option.


a By declaring x as static, main can access this.x
b By declaring x as public, main can access this.x
c By declaring x as protected, main can access this.x
d main() cannot access this.x as it is declared now i.e. without any modifier.
e By declaring x as private, main can access this.x

Question 20 of 47

Consider the following code:


class MyThread extends Thread
{
int i = 0;
public void run()
{
while(true)
{
if( i%2 == 0 ) System.out.println("Hello
World");
}
}
}

What will be the output when this program is compiled and run from the command line?

Select 1 correct option.


a It'll keep printing "Hello World".
b It'll print "Hello World" only once.
c This program will not even compile.
d This will compile but an exception will be thrown at runtime.
e It will compile and run but it will not print anything.

Question 21 of 47

Which of these group of statements are valid?

Select 2 correct options


a {{}}
b { continue ; }
c block : { break block ; }
d block : { continue block ; }
e The break keyword can only be used if there exists an enclosing loop construct
( i.e. while, do-while or for ).

Question 22 of 47

Consider the following hierarchy of Exception classes :


Exception
+-- RuntimeException
+-- IndexOutOfBoundsException
+-- ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException
Which of the following statements are correct for a method that can throw
ArrayIndexOutOfBounds as well as StringIndexOutOfBounds Exceptions but does not
have try catch blocks to catch the same?

Select 3 correct options


a The method calling this method will either have to catch these 2 exceptions or
declare them in it's throws clause.
b It's ok if it declares just 'throws ArrayIndexOutOfBoundsException'
c It must declare 'throws ArrayIndexOutOfBoundsException,
StringIndexOutOfBoundsException'
d It's ok if it declares just 'throws IndexOutOfBoundsException'
e It does not need to declare any throws clause.

Question 23 of 47

Which variables of the encapsulationg class, can an inner class access if the inner class
is defined in a static method of encapsulating class?

Select 2 correct options


a All static variables
b All final instance variables
c All instance variables
d All automatic variables.
e All final automatic variables

Question 24 of 47

What is the result of compiling and running the following code ?


public class TestClass
{
static int si = 10;
public static void main (String args[])
{
new TestClass();
}
public TestClass()
{
System.out.println(this);
}
public String toString()
{
return "TestClass.si = "+this.si;
}
}

Select 1 correct option.


a The class will not compile as you cannot override toString() method.
b The class will not compile as si being static, this.si is not a valid statement.
c It will print TestClass@nnnnnnnn, where nnnnnnn is the hashcode of the
TestClass object refered to by 'this'.
d It will print TestClass.si = 10;
e None of the above.

Question 25 of 47

Which of the following command line switches is required for the assert statements to be
executed while running a Java class?

Select 1 correct option.


a ea
b ua
c a
d No switch is needed, they are on by default.
e source
Question 26 of 47

Which of the following exceptions can be declared in the throws clause of a method
overriding the method:
public void perform_work( ) throws IOException {...}

Select 4 correct options


a IOException
b Exception
c NullPointerException
d No throws clause
e Any Subclass of IOException

Question 27 of 47

Consider :
class A { public void perform_work(){} }
class B extends A { public void perform_work(){} }
class C extends B { public void perform_work(){} }
How can you let perform_work() method of A to be called from an instance method in
C?

Select 1 correct option.


a ( (A) this ).perform_work( );
b super.perform_work( );
c super.super.perform_work( );
d this.super.perform_work( );
e It is not possible.

Question 28 of 47

What will be the result of attempting to compile and run the following class?
public class TestClass
{
public static void main(String args[ ] )
{
int i, j, k;
i = j = k = 9;
System.out.println(i);
}
}

Select 2 correct options


a The code will not compile as unlike in c++, operator '=' cannot be chained.
b The code will not compile as 'j' is being used before getting initialized.
c The code will compile correctly and will display '9' when run.
d The code will not compile as 'j' and 'i' are being used before getting initialized.
e All the vairables will get a value of 9.

Question 29 of 47

Consider the following method...


public static void ifTest(boolean flag)
{
if (flag) //1
if (flag) //2
if (flag) //3
System.out.println("False True");
else //4
System.out.println("True False");
else //5
System.out.println("True True");
else //6
System.out.println("False False");
}
Which of the following statements are correct ?

Select 2 correct options


a If run with an argument of 'false', it will print 'False False'
b If run with an argument of 'false', it will print 'True True'
c If run with an argument of 'true', it will print 'True False'
d It will never print 'True True'
e It will not compile.

Question 30 of 47

Which of the following are also called as "short circuit" logical operators?

Select 2 correct options


a &
b ||
c &&
d |
e ^
Question 31 of 47

What will the following class print when run?


class TestClass
{
int i = getInt();
int k = 20;
public int getInt() { return k+1; }
public static void main(String[] args)
{
TestClass t = new TestClass();
System.out.println(t.i+" "+t.k);
}
}

Select 1 correct option.


a It will not even compile.
b It will compile but throw an exception at runtime.
c It will print 1 20
d It will print 21 20
e None of the above.

Question 32 of 47

Which of the given statements are correct for a method that overrides the following
method:
public void add(int a) {...}

Select 3 correct options


a It must return void
b It may return int
c It can declare any Exception in throws clause
d It can declare any RuntimeException in throws clause.
e It can be abstract.

Question 33 of 47

Which of these statements are valid when occurring by themselves?

Select 3 correct options


a while ( ) break ;
b do { break ; } while (true) ;
c if (true) { break ; }
d switch (1) { default : break ; }
e for ( ; true ; ) break ;

Question 34 of 47

What will be the output when running the following program?


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

Select 1 correct option.


a 0 0 will be printed twice.
b 0 0 will be printed once.
c It will keep on printing 0 0
d It will not compile.
e It will print 0 0 and then 0 1.

Question 35 of 47

Which of the following switches is/are used for controlling the execution of assertions at
run time?

Select 2 correct options


a ua
b da
c enableassertions
d assert
e keepassertions
Question 36 of 47

Which lines of code are valid declarations of a native method when occurring within the
declaration of the following class?
public class TestClass
{
// insert declaration of a native method here
}

Select 2 correct options


a native public void nativeMethod(int i);
b private native void nativeMethod(int i);
c protected int native nativeMethod();
d public abstract native void nativeMethod(int i);
e native int nativeMethod(int i) {}

Question 37 of 47

What should be passed as a command line parameter while running this program so that
it'll print "Good Choice"?
public class TestClass
{
public static void main(String args[])
{
double d = Double.parseDouble(args[0]);
if( Math.floor(d) == ((int)d))
if( Math.ceil(d) == Math.round(d) )
System.out.println("Good Choice");
}
}

Select 2 correct options


a Any positive number (e.g 4, 4.0, 4.5, 4.9 etc)
b Any positive number with no decimal part or .0 decimal part (e.g 4, 4.0 etc) but
no negative number.
c Any number with no decimal part or .0 decimal part (e.g 4, 4.0, -4, -4.0 etc)
d Any number whose decimal part is bigger than or equals to .5 (eg. -3.5, -3.6, 3.5,
3.6 etc)
e Any positive number whose decimal part is bigger than or equals to .5 (eg. 3.5,
3.6 etc)
Question 38 of 47

What will the following program print?


public class TestClass
{
public static void main(String[] args)
{
unsigned byte b = 0;
b--;
System.out.println(b);
}
}

Select 1 correct option.


a 0
b -1
c 255
d -128
e It will not compile.

Question 39 of 47

Which of the following are valid escape sequences ?

Select 4 correct options


a \b
b \t
c \f
d \r
e \x

Question 40 of 47

Which line will print the string "MUM"?


public class TestClass
{
public static void main(String args [])
{
String s = "MINIMUM";
System.out.println(s.substring(4, 7)); //1
System.out.println(s.substring(5)); //2
System.out.println(s.substring(s.indexOf('I', 3))); //3
System.out.println(s.substring(s.indexOf('I', 4))); //4
}
}

Select 1 correct option.


a 1
b 2
c 3
d 4
e None of these.

Question 41 of 47

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 obj && this.a == ((IntPair) obj).a && this.b == ((IntPair)
obj).b );
}
public int hashCode()
{
//1
}
}
Which of the following options would NOT be valid at //1?

Select 1 correct option.


a return a;
b return a*b;
c return a+b;
d return b;
e None of these is invalid.
Question 42 of 47

Consider the following classes in one file named A.java...


abstract class A
{
protected int m1(){ return 0; }
}
class B extends A
{
int m1(){ return 1; }
}

Which of the following statements are correct...

Select 1 correct option.


a The code won't compile as you cannot have more than 1 class in 1 file.
b The code won't compile as class B does not override the method m1() in a right
way.
c The code won't compile as A is an abstract class.
d The code won't compile as A does not have any abstract method.
e The code will compile file.

Question 43 of 47

What will be the result of attempting to run the following program?(See Exhibit)
public class StringArrayTest
{
public static void main(String args[])
{
String[][][] arr =
{
{ { "a", "b" , "c"}, { "d", "e", null } },
{ {"x"}, null },
{{"y"}},
{ { "z","p"}, {} }
};
System.out.println(arr[0][1][2]);
}
}

Select 1 correct option.


a It will throw NullPointerBoundsException.
b It will throw ArrayIndexOutOfBoundsException.
c It will print 'null'.
d It will run without any error but will print nothing.
e None of the above.
Question 44 of 47

You have just written a Java class named AssertTest.java that uses the assertion facility.
Write down the syntax for compiling this program using Javac.
[Do not write the classpath or any other switches, just use the one/ones that is/are
absolutely needed. Start with: javac ...]

Question 45 of 47

What is the result of executing the following fragment of code:


boolean b1 = false;
int i1 = 2;
int i2 = 3;
if (b1 = i1 == i2)
{
System.out.println("true");
} else
{
System.out.println("false");
}

Select 1 correct option.


a Compile time error.
b It will print true;
c It will print false;
d Runtime error.
e It will print nothing.

Question 46 of 47

Which of the following statments are correct regarding the equals() method?

Select 1 correct option.


a It must be symetric but need not be transitive.
b It must be reflexive but need not be transitive.
c It must be symetric and transitive but not reflexive.
d If passed a null, it must return false.
e None of these.
Question 47 of 47

Consider the following program...


public class TestClass
{
public int methodA(int a){ return a*2; } //1
public long methodA(int a){ return a; } //2
public static void main(String[] args)
{
int i = 0;
i = new TestClass().methodA(2);
System.out.println( i );
}
}

Select 1 correct option.


a Line 2 correctly overrides the method at line 1.
b Line 2 correctly overloads the method at line 1.
c There is neither overloading nor orverriding.
d The program will not compile.
e The program will compile and print 4.

You might also like