Part4 – Interview Questions - Interview Questions
- Compilation fails.[Ans]
- “odd” will always be output.
- “even” will always be output.
- “odd” will be output for odd values of x, and “even” for even values.
- “even” will be output for add values of x, and “odd” for even values.
- int[] ia = new int[15];[Ans]
- float fa = new float[20];
- char[] ca = “Some String”;
- Object oa = new float[20];[Ans]
- int ia[][] = { 4, 5, 6, }, { 1, 2, 3 };
- 4,4
- 4,5
- 5,4
- 5,5
- Compilation fails.[Ans]
- finished[Ans]
- Compilation fails.
- An AssertionError is thrown.
- An AssertionError is thrown and finished is output.
- public
- private[Ans]
- protected
- transient
- default access
- byte[Ans]
- long
- char[Ans]
- float
- Short
- Long
- BD
- BCD
- BDE[Ans]
- BCDE
- ABCDE
- Compilation fails.
- finished
- Compilation fails.[Ans]
- An AssertionError is thrown and finished is output.
- An AssertionError is thrown with the message “assertion failed”.
- An AssertionError is thrown with the message “assertion passed”.
- int
- byte
- long
- short
- float
- double[Ans]
- static class InnerOne {
public double methoda() { return d1; }
} - static class InnerOne {
static double methoda() { return d1; }
} - private class InnerOne {
public double methoda() { return d1; }
}[Ans] - protected class InnerOne {
static double methoda() { return d1; }
} - public abstract class InnerOne {
public abstract double methoda();
}[Ans] - Hello
- Hello Foo
- Hello world
- Compilation fails.
- The code does not run.[Ans]
- If a is true and b is true then the output is “A&&B”.
- If a is true and b is false then the output is “notB”.
- If a is false and b is true then the output is “ELSE”.[Ans]
- If a is false and b is false then the output is “ELSE”.
- int[] scores = {3, 5, 7};
- int [][] scores = {2,7,6}, {9,3,45};[Ans]
- String cats[] = {“Fluffy”, “Spot”, “Zeus”};
- boolean results[] = new boolean [3] {true, false, true};[Ans]
- Integer results[] = {new Integer(3), new Integer(5), new Integer(8)};
- String[] dogs = new String[]{new String(“Fido”),new
String(“Spike”), new String(“Aiko”)}; - i = 1, j = 0
- i = 1, j = 4
- i = 3, j = 4
- i = 3, j = 0
- Compilation fails.[Ans]
- Add a method body to methodA.
- Replace lines 5 – 7 with a semicolon (“;”).[Ans]
- Remove the abstract qualifier from the declaration of Test.
- Remove the abstract qualifier from the declaration of methodA.
- Remove the abstract qualifier from the declaration of methodB.[Ans]
- 0
- 2
- 4
- 6[Ans]
- 9
- 13
- There are no instanced of A that will become eligible for garbage collection.
- Explicitly setting myA to null marks that instance to be eligible for garbage collection.[Ans]
- Any call on tryIt() causes the private instance of A to be marked for garbage collection.
- Private instances of A become eligible for garbage collection when instances of Alpha become eligible for garbage collection.[Ans]
- 0
- 1
- 2[Ans]
- Compilation fails.
- i = 6 and j = 5[Ans]
- i = 5 and j = 5
- i = 6 and j = 4
- i = 5 and j = 6
- i = 6 and j = 6
- assert (!(map.contains(x)));
map.add(x); - if (x > 0) {
} else {
assert (x==0);
} - public void aMethod(int x) {
assert (x > 0);
}[Ans] - assert (invariantCondition());
return retval; - switch (x) {
case 1: break;
case 2: creak;
default: assert (x == 0); - public void aMethod() {}
- private void aMethod() {}
- public void aMethod(String s) {}[Ans]
- private Y aMethod() { return null; }
- public X aMethod() { return new Y(); }[Ans]
- ABCD
- Compilation fails.[Ans]
- C is printed before exiting with an error message.
- BC is printed before exiting with an error message.
- BCD is printed before exiting with an error message.
- public
- private
- protected[Ans]
- transient
- default access
- Ex0 caught[Ans]
- exception caught
- Compilation fails because of an error at line 2.
- Compilation fails because of an error at line 6.
- line 21[Ans]
- line 23
- line 27
- line 29
- Class A
- Compilation fails.
- An exception is thrown at line 2.
- An exception is thrown at line 6.
- The code executes with no output.[Ans]
- After line 8.
- After line 10.[Ans]
- After line 4, when doBar() completes.
- After line 11, when main() completes.
- Tested
- Compilation fails.[Ans]
- The code runs with no output.
- An exception is thrown at runtime.
- Programs will not run out of memory.
- Objects that will never again be used are eligible for garbage collection.
- Objects that are referred to by other objects will never be garbage collected.
- Objects that can be reached from a live thread will never be garbage collected.[Ans]
- Objects are garbage collected immediately after the system recognizes they are eligible.
- class A {
}[Ans] - class A {
public A() {}
} - class A {
public A(int x) {}
} - class Z {}
class A extends Z {
void A() {}
}[Ans] - int
- byte
- long
- short
- float
- double[Ans]
- new Inner(); // At line 3[Ans]
- new Inner(); // At line 8
- new o.Inner(); // At line 8
- new Outer.Inner(); // At line 8
- x.delete()
- x.finalize()
- Runtime.getRuntime().gc()
- Explicitly setting the object’s reference to null.
- Ensuring there are no references to the object.
- Only the garbage collection system can destroy an object.[Ans]
- x = 6 y = 0
- x = 7 y = 0
- x = 6 y = -1
- x = 7 y = -1
- Compilation fails.[Ans]
- f[0] = f0;
- f[0] = farray;[Ans]
- f[0] = farray[0];
- f[0] = farray[0][0];
- 0 2 4
- 0 2 4 5
- 0 1 2 3 4
- Compilation fails.[Ans]
- An exception is thrown at runtime.
- After line 5.
- After line 6.
- After line 7 (when the methods ends).
- There is no way to be absolutely certain.[Ans]
- The object is NOT eligible for garbage collection.
- No code is necessary.
- throws Exception[Ans]
- catch ( Exception e )
- throws RuntimeException
- catch ( TestException e)
- i = 0
- i = 3
- i = 4[Ans]
- i = 5
- Compilation fails.
- finished
- Exception
- Compilation fails.
- Arithmetic Exception[Ans]
- Compilation succeeds.
- An exception is thrown.
- Compilation fails because of an error at line 2.
- Compilation fails because of an error at line 6.[Ans]
- Error[Ans]
- Event
- Object
- Throwable[Ans]
- Exception[Ans]
- class A {
public A(int x) {}
} - class A {
}
class B extends A {
B() {}
} - class A {
A() {}
}
class B {
public B() {}
} - class Z {
public Z(int) {}
}
class A extends Z {
}[Ans] - init i = min;
while( i < max ) {
} - int i = min;
do
System.out.println(i++);
} while( i< max );[Ans] - for(int i=min; i
System.out.println(++I); - for(int i=; i++
System.out.println(I++) - d >= 1.0
- 0.0 <= d < 1.0[Ans]
- 0.0 <= d < Double.MAX_VALUE
- 0.0 <= d <= Double.MAX_VALUE
- Double.MIN_VALUE <= d < Double.MAX_VALUE
- Compilation fails.[Ans]
- The code runs with no output.
- An exception is thrown at runtime.
- The code runs, outputing a concatenated list of the arguments passed to the program.
- true[Ans]
- null
- false
- Compilation fails.
- The code runs with no output.
- An exception is thrown at runtime.
- i = 1, j = 2
- i = 1, j = 1[Ans]
- i = 42, j = 2
- Compilation fails
Which statement is true?
Given:
public void test(int x) {
int odd = x%2;
if (odd) {
System.out.println(“odd);
} else {
System.out.println(“even”);
}
}
Which two create an instance of an array? (Choose two)
What is the output?
Given:
class Super {
public int getLenght() { return 4; }
}
public class Sub extends Super {
public long getLenght() { return 5; }
public static void main(String[] args) {
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(
sooper.getLenght() + “,” + sub.getLenght() );
}
}
What is the result?
Given:
public class Test {
public static void main(String[] args) {
int x = 0;
assert (x > 0): “assertion failed”;
System.out.println(“finished”);
}
}
Note:This question is a bit tricky because it lacks the following information: It should include a statement that says whether or not assertions are enabled. If they are indeed enabled, the correction answer is C. but if they are not, the correct answer is A. Assertions are not enabled by default so if the question is not changed, the most logical answer is A.
You want to limit access to a method of a public class to members of the same class.Which access accomplishes this objective?
Which two are acceptable types for x? (Choose two)
Given:
switch(x) {
default:
System.out.println(“Hello”);
}
What is the result?
Given:
public class X {
public static void main(String [] args) {
try {
badMethod();
System.out.print(“A”);
}
catch (RuntimeException ex) {
System.out.print(“B”);
}
catch (Exception ex1) {
System.out.print(“C”);
}
finally {
System.out.print(“D”);
}
System.out.print(“E”);
}
public static void badMethod() {
throw new RuntimeException();
}
}
What is the result?
Given:
public class Test {
public static void main(String[] args) {
int x = 0;
assert (x > 0) ? “assertion failed” : “assertion passed”;
System.out.println(“Finished”);
}
}
What is the narrowest valid returnType for method A in line 2?
Given:
public class ReturnIt {
return Type methodA(byte x, double y) {
return (long)x / y * 2;
}
}
Which two are valid if inserted at line 3? (Choose two)
Given:
public class OuterClass {
private double d1 = 1.0;
// insert code here
}
What is the result if this code is executed with
java Foo world
Given:
public class Foo {
public void main( String[] args ) {
System.out.println( “Hello” + args[0] );
}
}
What is correct?
Given:
public void foo( boolean a, boolean b ){
if( a ) {
System.out.println( “A” );
} else if ( a && b ) {
System.out.println( “A&&B” );
} else {
if ( !b ) {
System.out.println( “notB” );
} else {
System.out.println( “ELSE” );
}
}
}
Which two cause a compiler error? (Choose two)
What is the result?
Given:
int i = 0, j = 5;
tp;
for (;;) {
i++;
for(;;) {
if (i> --j) {
break tp;
break tp;
}
}
System.out.println(“i=” +i “,j =”+j);
Which two changes, independently applied, allow this code to compile? (Choose two)
Given:
public abstract class Test {
public abstract void methodA();
public abstract void methodB()
{
System.out.println(“Hello”);
}
}
What is the result?
Given:
public class Test {
public static void main(String Args[]) {
int i =1, j = 0;
switch(i) {
case 2: j +=6;
case 4: j +=1;
default: j +=2;
case 0: j +=4;
}
System.out.println(“j =” +j);
System.out.println(“j =” +j);
}
}
Which two statements are correct? (Choose two)
Given:
class A {
}
class Alpha {
private A myA = new A();
void dolt( A a ) {
a = null;
}
void tryIt() {
dolt( myA );
}
}
What is the result?
Given:
class Super {
public int i = 0;
public Super(String text) {
i = 1;
}
}
public class Sub extends Super {
public Sub(String text) {
i = 2;
}
public static void main(String args[]) {
Sub sub = new Sub(“Hello”);
System.out.println(sub.i);
}
}
What is the result?
Given:
int i = 1,j = 10;
do{
if (i>j) {
continue;
}
j--;
} while (++i <6);
System.out.println(“i = “ +i+” and j = “+j);
Which fragment is an example of inappropriate use of assertions?
Which two methods can be added to the definition of class Y? (Choose two)
Given:
public class X {
public X aMethod() { return this;}
}
public class Y extends X {
}
What is the result?
Given:
public class X {
public static void main(String [] args) {
try {
badMethod();
System.out.print(“A”);
}
catch (Exception ex) {
System.out.print(“C”);
}
finally {
System.out.print(“B”);
}
System.out.print(“D”);
}
public static void badMethod() {
throw new Error();
}
}
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
What is the result?
Given:
class Exc0 extends Exception { }
class Exc1 extends Exc0 { }
public class Test {
public static void main(String args[]) {
try {
throw new Exc1();
} catch (Exc0 e0) {
System.out.println(“Ex0 caught”);
} catch (Exception e) {
System.out.println(“exception caught”);
}
}
}
Which line is a violation of appropriate use of the assertion mechanism?
Given:
public float getSalary(Employee e) {
assert validEmployee(e);
float sal = lookupSalary(e);
assert (sal>0);
return sal;
}
private int getAge(Employee e) {
assert validEmployee(e);
int age = lookupAge(e);
assert (age>0);
return age;
}
What is the result?
Given:
public class A {
void A() {
System.out.println(“Class A”);
}
public static void main(String[] args) {
new A();
}
}
At what point is the Bar object, created on line 3, eligible for garbage collection?
Given:
class Bar { }
class Test {
Bar doBar() {
Bar b = new Bar();
return b;
}
public static void main (String args[]) {
Test t = new Test();
Bar newBar = t.doBar();
System.out.println(“newBar”);
newBar = new Bar();System.out.println(“finishing”);
}
}
Answer: B
What is the result?
Given:
interface Beta {}
class Alpha implements Beta {
String testIt() {
return “Tested”;
}
}
public class Main1 {
static Beta getIt() {
return new Alpha();
}
public static void main( String[] args ) {
Beta b = getIt();
System.out.println( b.testIt() );
}
}
Which statement is true?
In which two cases does the compiler supply a default constructor for class A? (Choose two)
What is the narrowest valid returnType for methodA in line2?
Given:
public class ReturnIt {
return Type methodA(byte x, double y) {
return (short)x / y * 2;
}
}
Which instantiates an instance of Inner?
Given:
public class Outer{
public void someOuterMethod() {
// Line 3
}
public class Inner{}
public static void main( String[]argv ) {
Outer o = new Outer();
// Line 8
}
}
What allows the programmer to destroy an object x?
What is the result?
Given:
int x = 1, y =6;
while (y--) {
x++;
}
System.out.println(“x =” + x + “y =” +y);
What is valid?
Given:
float f[][][] = new float[3][][];
float f0 = 1.0f;
float[][] farray = new float[1][1];
What is the result?
Given:
for (int i =0; i < 4; i +=2) {
System.out.print(i + “”);
}
System.out.println(i);
When is the B object, created in line 3, eligible for garbage collection?
Given:
void start() {
A a = new A();
B b = new B();
b = null;
a = null;
System.out.println(“start completed”);
}
At Point X on line 4, which code is necessary to make the code compile?
Given:
public class Exception Test {
class TestException extends Exception {}
310 - 035
Leading the way in IT testing and certification tools, www.testking.com
-45 -
public void runTest() throws TestException {}
public void test() /* Point X */ {
runTest();
}
}
What is the result?
Given:
int i = 0;
while (true) {
if(i==4) {
break;
}
++i;
}
System.out.println(“i=”+i);
What is the result?
Given:
try {
int x = 0;
int y = 5 / x;
} catch (Exception e) {
System.out.println(“Exception”);
} catch (ArithmeticException ae) {
System.out.println(“Arithmetic Exception”);
}
System.out.println(“finished”);
What is the result?
Given:
abstract class AbstractIt {
abstract float getFloat();
}
public class AbstractTest extends AbstractIt {
private float f1 = 1.0f;
private float getFloat() { return f1; }
}
Which four can be thrown using the throw statement? (Choose four)
What produces a compiler error?
If min and max are arbitrary integers, what gives the same result?
Given:
for( int i = min; i
}
Which is true about d after line 1?
Given:
double d = Math.random();
What is correct?
Given:
public class Alpha{
private static Character() ids;
public static void main( String[] args){
ids = new Character[args.length];
for (int i=0; i
System.out.print( ids[i] );
}
}
}
And the code is invoked by using the command:
java Alpha –b TRUE What is the result?
Given:
public class Alpha{
public static void main( string[] args ){
if ( args.length == 2 ) {
if ( args.[0].equalsIgnoreCase(“-b”) )
System.out.println( new Boolean( args[1] ));
}
}
}
What is the result?
Given:
int i = 0, j = 1;
if ((i++ == 1) && (j++ == 2)) {
i = 42;
}
System.out.println(“i = “ + i + “, j = “ + j);