Saturday, January 19, 2013

JAVA Interview Questions - II

Question 1
What 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);
}
}
It will print -10
It will result in Assertion Error showing the message -"The value must not be negative".
The code will not compile.
None of these


Question 2:-What 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;
}
}
Compile time error prints : 1
prints : 2 prints : 3
prints : 7 prints : 8

Question 3
Given the following code, what will be the output?
class Value
{
public int i = 15;
}
public class Test
{
public static void main(String argv[])
{
Test t = new Test();
t.first();
}
public void first()
{
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i)
{
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
15 0 20 0
15 0 15
20 20


Question 4
What 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
{
in 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);
}
}
300 240
120 180
Compilation Error None of the above

Question 5
The class AssertionError has "is - a" relationship with these classes:
RuntimeException
Error
VirtualMachineError
IllegalAccessException
Throwable

Question 6
What 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) System.out.println("Some things are true in this world");
7. else System.out.println("Nothing is true in this world!");
8. else if (a && (b = c)) System.out.println("It's too confusing to tell what is true and what is false");
9. else System.out.println("Hey this won't compile");
The code won't compile
"Some things are true in this world" will be printed
"Hey this won't compile" will be printed
None of these

Question 7
What 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");
}
}
}
Compile time error
Runtime error
Prints : "I am true interface" followed by " I am true String"
Prints : "I am false interface" followed by " I am false String"
Prints : "I am true interface" followed by " I am false String"
Prints : "I am false interface" followed by " I am true String"

Question 8

What 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));

}

}




Prints: Value is - 9
Prints: Value is - 5
Compilation error
None of these

Question 9

In 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;






Question 10

Considering the following code, which variables may be referenced correctly at line 12?



public class Outer

{

public int a = 1;

private int b = 2;

public void method(final int c)

{

int d = 3;

class Inner

{

private void iMethod(int e)

{



}

}

}

}






a b
c d
e
Question 11

What will be the result of executing the following code?



public static void main(String args[])

{

char digit = 'a';

for (int i = 0; i < 10; i++)

{

switch (digit)

{

case 'x' :

{

int j = 0;

System.out.println(j);

}

default :

{

int j = 100;

System.out.println(j);

}

}

}



int i = j;

System.out.println(i);

}


100 will be printed 11 times. 100 will be printed 10 times and then there will be a runtime exception.
The code will not compile because the variable i cannot be declared twice within the main() method. The code will not compile because the variable j cannot be declared twice within the switch statement.
None of these.


Question 12

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



Vector ArrayList
HashMap Hashtable


Question 13

What 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();

}

}




Prints : MyThread: start() followed by MyRunnable:run() Prints : MyThread: run() followed by MyRunnable:start()
Prints : MyThread: start() followed by MyRunnable:start() Prints : MyThread: run() followed by MyRunnable:run()
Compile time error None of the above


Question 14

What 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. }


Compilation error at line 5 Compilation error at line 9
Runtime exception at line 11 None of these

Question 15

Consider the class hierarchy shown below: --------------------------------------------------------------------



class FourWheeler implements DrivingUtilities



class Car extends FourWheeler



class Truck extends FourWheeler



class Bus extends FourWheeler



class Crane extends FourWheeler



----------------------------------------------------------------------



Consider the following code below:

1. DrivingUtilities du;

2. FourWheeler fw;

3. Truck myTruck = new Truck();

4. du = (DrivingUtilities)myTruck;

5. fw = new Crane();

6. fw = du;

Which of the statements below are true?



Line 4 will not compile because an interface cannot refer to an object. The code will compile and run.
The code will not compile without an explicit cast at line 6, because going down the hierarchy without casting is not allowed. The code at line 4 will compile even without the explicit cast.
The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime.
Question 16

What results from the following code?

1. class MyClass

2. {

3. void myMethod(int i) {System.out.println("int version");}

4. void myMethod(String s) {System.out.println("String version");}

5. public static void main(String args[])

6. {

7. MyClass obj = new MyClass();

8. char ch = 'c';

9. obj.myMethod(ch);

10. }

11. }



Line 4 will not compile as void methods can't be overridden.
An exception at line 9
Line 9 will not compile as there is no version of myMethod, which takes a char as argument.
The code compiles and produces output: int version.
The code compiles and produces output: String version
Question 17

What is the result when you compile and run the following code?



public class ThrowsDemo

{

static void throwMethod()

{

System.out.println("Inside throwMethod.");

throw new IllegalAccessException("demo");

}



public static void main(String args[])

{

try

{

throwMethod();

}

catch (IllegalAccessException e)

{

System.out.println("Caught " + e);

}

}

}



Compilation error Runtime error
Compile successfully, nothing is printed. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo
Question 18

What will be printed when you execute the following code?

class X

{

Y b = new Y();

X()

{

System.out.print("X");

}

}


class Y

{

Y()

{

System.out.print("Y");

}

}


public class Z extends X

{

Y y = new Y();


Z()

{

System.out.print("Z");

}


public static void main(String[] args)

{

new Z();

}

}



Z
YZ
XYZ
YXYZ

Question 19

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

Boolean b = new Boolean("TRUE");



if(b.booleanValue())

{

System.out.println("Yes : " + b);

}

else

{

System.out.println("No : " + b);

}



The code will not compile.
It will print - Yes : true
It will print - Yes : TRUE
It will print - No : false
It will print - No : FALSE


Question 20

What is the result when you compile and run the following code?

public class Test

{

public void method()

{

for(int i = 0; i < 3; i++)

{

System.out.print(i);

}

System.out.print(i);

}

}



0122
0123
Compilation error
None of these

Question 21

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


int Output = 10;

boolean b1 = false;


if((b1 == true) && ((Output += 10) == 20))

{

System.out.println("We are equal " + Output);

}


else

{

System.out.println("Not equal! " + Output);

}



Compilation error, attempting to perform binary comparison on logical data type.
Compilation and output of "We are equal 10".
Compilation and output of "Not equal! 20".
Compilation and output of "Not equal! 10".
Question 22

What will be the result of executing the following code?



Given that Test1 is a class.



1. Test1[] t1 = new Test1[10];

2. Test1[][] t2 = new Test1[5][];

3. if (t1[0] == null)

4. {

5. t2[0] = new Test1[10]

6. t2[1] = new Test1[10]

7. t2[2] = new Test1[10]

8. t2[3] = new Test1[10]

9. t2[4] = new Test1[10]

10. }

11. System.out.println(t1[0]);

12. System.out.println(t2[1][0]);



The code will not compile because the array t2 is not initialized in an unconditional statement before use.
The code will compile but a runtime exception will be thrown at line 12.
The code will compile but a runtime exception will be thrown at line 11.
None of these.
Question 23

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

class Base

{

int i = 99;


public void amethod()

{

System.out.println("Base.amethod()");

}

Base()

{

amethod();

}

}


public class Derived extends Base

{

int i = -1;



public static void main(String argv[])

{

Base b = new Derived();

System.out.println(b.i);

b.amethod();

}



public void amethod()

{

System.out.println("Derived.amethod()");

}


}




Derived.amethod()

-1

Derived.amethod()


Derived.amethod()

99

Derived.amethod()


99

Derived.amethod()


Compile time error

@


Question 24

What will be the output on compiling/running the following code?


public class MyThread implements Runnable

{

String myString = "Yes ";


public void run()

{

this.myString = "No ";

}


public static void main(String[] args)

{

MyThread t = new MyThread();

new Thread(t).start();


for (int i=0; i < 10; i++)

System.out.print(t.myString);

}

}



Compilation Error
Prints : Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes and so on.
Prints : No No No No No No No No No No and so on.
Prints : Yes No Yes No Yes No Yes No Yes No and so on.
The Output cannot be determined
Question 25

Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to create new integer count. What will happen when other threads use the following code?



class MyClass

{

static private int myCount = 0;

int yourNumber;



private static synchronized int nextCount()

{

return ++myCount;

}



public void getYourNumber()

{

yourNumber = nextCount();

}

}



The code will give compilation error.
The code will give runtime error.
Each thread will get a unique number.
The uniqueness of the number among different Threads can't be guaranteed.

Question 26

Which of the following lines will print false?



1. public class MyClass

2. {

3. static String s1 = "I am unique!";

4. public static void main(String args[])

5. {

6. String s2 = "I am unique!";

7. String s3 = new String(s1);

8. System.out.println(s1 == s2);

9. System.out.println(s1.equals(s2));

10. System.out.println(s3 == s1);

11. System.out.println(s3.equals(s1));

12. System.out.println(TestClass.s4 == s1);

13. }

14. }

15.

16. class TestClass

17. {

18. static String s4 = "I am unique!";

19. }



Lines 10 and 12
Line 12 only
Lines 8 and 10
None of these


Question 27

What is displayed when the following code is compiled and executed?



String s1 = new String("Test");

String s2 = new String("Test");



if (s1==s2)

System.out.println("Same");



if (s1.equals(s2))

System.out.println("Equals");



Same
Equals
The code compiles, but nothing is displayed upon execution.
The code fails to compile.

Question 28

What is displayed when the following is executed?


class Parent

{

private void method1()

{

System.out.println("Parent's method1()");

}


public void method2()

{

System.out.println("Parent's method2()");

method1();

}


}


class Child extends Parent

{

public void method1()

{

System.out.println("Child's method1()");

}


public static void main(String args[])

{

Parent p = new Child();

p.method2();

}

}



Compile time error
Run time error
prints : Parent's method2()
Parent's method1()

prints : Parent's method2()
Child's method1()


Question 29

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



String str = "Java";

StringBuffer buffer = new StringBuffer(str);



if(str.equals(buffer))

{

System.out.println("Both are equal");

}

else

{

System.out.println("Both are not equal");

}



It will print - Both are not equal
It will print - Both are equal
Compile time error as you can not use equals for objects of different classes
Runtime error as you can not use equals for objects of different classes
None of these

Question 30

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

public class MyThread extends Thread

{

String myName;


MyThread(String name)

{

myName = name;

}


public void run()

{

for(int i=0; i<100;i++)

{

System.out.println(myName);

}

}


public static void main(String args[])

{

try

{

MyThread mt1 = new MyThread("mt1");

MyThread mt2 = new MyThread("mt2");

mt1.start();

// XXX

mt2.start();

}

catch(InterruptedException ex)

{

}

}

}



The above code in its current condition will not compile.
In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join(); can be placed at //XXX position.
In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.sleep(100); can be placed at //XXX position.
In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.run(); can be placed at //XXX position.
In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), there is no need to write any code.