Part5 – Interview Questions - Interview Questions
- 0[Ans]
- 1
- Compilation fails.
- An exception is thrown at runtime.
- 0[Ans]
- 3
- Compilation fails.
- An exception is thrown at runtime.
- (a == c)[Ans]
- (d == e)[Ans]
- (b == d)
- (a == b)
- (b == c)
- (d == 10.0)[Ans]
- abc
- null
- abcdef
- Compilation fails.
- The code runs with no output.
- An exception is thrown at runtime.[Ans]
- b.equals(b) returns true.
- b.equals(c) returns the same result as b == c.[Ans]
- b.eqials(c) can return false even if c.equals(b) returns true.[Ans]
- b.equals(c) throws an exception if b and c are different wrapper types.
- b.equals(c) returns false if the type of wrapper objects being compared are different.
- true
- false
- Compilation fails.
- An exception is thrown at runtime.[Ans]
- equal
- not equal
- exception
- Compilation fails.[Ans]
- public class X { }
public class Y extends X { }[Ans] - public interface Shape { }
public interface Rectangle extends Shape{ }[Ans] - public interface Color { }
public class Shape { private Color color; } - public interface Species { }
public class Animal { private Species species; } - public class Person { }
public class Employee {
public Employee(Person person) { } - interface Component { }
class Container implements Component {
private Component[] children;
}[Ans] - float getVar() { return x; }
- public float getVar() { return x; }[Ans]
- public double getVar() { return x; }
- protected float getVar() { return x; }[Ans]
- public float getVar(float f) { return f; }
- 1
- 2
- An exception is thrown at runtime.
- Compilation fails because of an error in line 8.[Ans]
- Compilation fails because of an error in line 14.
- Performance of class methods is improved.
- Implementation details of the class are hidden.[Ans]
- Access modifiers can be omitted on class data members.
- Code that uses the encapsulation class can access data members directly.
- Internal operation of the class can be modified without impacting clients of that class.[Ans]
- Compilation fails.[Ans]
- hello from a
- hello from b
- hello from b
hello from a - hello from a
hello from b - Compilation fails.
- An instance of the Inner class can be constructed with “new Outer.Inner()”.[Ans]
- An instance of the Inner class cannot be constructed outside of package foo.
- An instance of the Inner class can be constructed only from within the Outer class.
- From within the package foo, and instance of the Inner class can be constructed with “new Inner()”.
- check can never return true.
- check can return true when setXY is called by multiple threads.[Ans]
- check can return true when multiple threads call setX and setY separately.
- check can return true only if SyncTest is changed to allow x and y to be set separately.
- Thread X is interrupted.
- Thread X is interrupted.
- Thread X’s wait() times out.
- Thread Z calls Thread.sleep(100);
- Thread Z releases the lock on A and calls the notify() method on thread X.
- Thread Z releases the lock on A and calls the notifyAll() method on objectA.[Ans]
- Compilation fails.
- An exception is thrown.[Ans]
- The wait() method has no effect.
- Thread X receives the lock immediately.
- Object A moves the thread to the wait pool.
- AAA[Ans]
- BBB
- Compilation fails.
- The code runs with no output
- Compilation fails.
- The program prints pairs of values for x and y that might not always be the same on
the same line (for example, “x = 2, y = 1”). - The program prints pairs of values for x and y that are always the same on the same line (for example, “x = 1, y = 1”).In addition, each value appears only once (for example, “x = 1, y = 1” followed by “x = 2, y = 2”).
The thread name at the start of the line shows that both threads are executing concurrently. - The program prints pairs of values for x and y that are always the same on the same line (for example, “x = 1, y = 1”).In addition, each value appears only once (for example, “x = 1, y = 1” followed by “x = 2, y = 2”).
The thread name at the start of the line shows that only a single thread is actually executing.[Ans] - To call the wait() method, a thread most own the lock of the current thread.
- To call the wait() method, a thread must own the lock of the object on which the call is to be made.[Ans]
- To call the join() method, a thread must own the lock of the object on which the call is to be made.
- To call the sleep() method, a thread must own the lock of the object which the call is to be made.
- To call the yield() method, a thread must own the lock of the object on which the cal is to be made.
- A done
- B done
- A done
B done - B done
A done - There is no exception that the application will print anything.[Ans]
- The application outputs “A done” and “B done”, in no guaranteed order.
- Exiting from a synchronized block.
- Calling the wait method on an object.[Ans]
- Calling the notify method on an object.
- Calling the notifyAll method on an object.
- for (int i=0; i< a.size(); i++)
System.out.println(a.get(i))); - for (int i=0; i< a.size(); i++)
System.out.println(a[i]);[Ans] - while( iter.hasNext() )
System.out.println(iter.next()) ;[Ans] - for (int i=0, i< a.size(); i++)
System.out.println(iter[i]); - for (int i=0; i< a.size(); i++)
System.out.println(iter.get(i)); - No statement is required.[Ans]
- import jave.io.*;
- include java.io.*;
- import jave.io.PrintWriter;
- include java.io.PrintWriter;
- float f = 1F;[Ans]
- float f = 1.0.;
- float f = ‘1’;[Ans]
- float f = “1”;
- float f = 1.0d;
- 0
- null
- Compilation fails.
- A NullPointerException is thrown at runtime.[Ans]
- An ArrayIndexOutOfBoundsException is thrown at runtime.
- baz =
- baz = null
- baz = blue
- Compilation fails.
- An exception is thrown at runtime.[Ans]
- void run()
- public void run()[Ans]
- public void start()
- void run(int priority)
- public void run(int priority)
- public void start(int priority)
- 42[Ans]
- An exception is thrown at runtime.
- Compilation fails because of an error on line 12.
- Compilation fails because of an error on line 16.
- Compilation fails because of an error on line 17.
- run
- import[Ans]
- default[Ans]
- implement
- If the hashCode values are different, the objects might be equal.
- If the hashCode values are the same, the object must be equal.
- If the hashCode values are the same, the objects might be equal.[Ans]
- If the hashCode values are different, the objects must be unequal.[Ans]
- 0 ... 32767
- 0 ... 65535[Ans]
- –256 ... 255
- –32768 ... 32767
- Range is platform dependent.
- f[0] = 0
- f[0] = 0.0[Ans]
- Compilation fails.
- An exception is thrown at runtime.
- NULL
- Compilation fails.[Ans]
- The code runs with no output.
- An exception is thrown at runtime.
- java.util.Map[Ans]
- java.util.List
- java.util.Hashable
- java.util.Collection
- –2
- NaN[Ans]
- Infinity
- Compilation fails.
- An exception is thrown at runtime.
- abcd[Ans]
- ABCD
- dccd
- dcba
- Compilation fails.
- An exception is thrown at runtime.
- The code compiles and prints “A,B”.
- The code compiles and prints “A,A”.
- The code compiles and prints “B,B”.
- The code compiles and prints “AB,B”.[Ans]
- The code compiles and prints “AB,AB”.
- The code does not compile because “+” cannot be overloaded for StringBuffer.
- Double d = Math.cos(42);
- Double d = Math.cosine(42);
- Double d = Math.cos(Math.toRadians(42));[Ans]
- Double d = Math.cos(Math.toDegrees(42));
- Double d = Math.cosine(Math.toRadians(42));
- a.wait();
- t.wait();
- t.join();[Ans]
- t.yield();
- t.notify();
- a.notify();
- t.interrupt();
- Int foo = (int) Math.max(bar);
- Int foo = (int) Math.min(bar);
- Int foo = (int) Math.abs(bar);
- Int foo = (int) Math.ceil(bar);
- Int foo = (int) Math.floor(bar);[Ans]
- Int foo = (int) Math.round(bar);
- Void setVar(float f) {x = f;}
- Public void setVar(int f) {x = f;}
- Public void setVar(float f) {x = f;}[Ans]
- Public double setVar(float f) {x = f;}
- Public final void setVar(float f) {x = f;}[Ans]
- Protected float setVar() {x=3.0f; return 3.0f; }
- The Mycircle class is fully encapsulated.
- The diameter of a given MyCircle is guaranteed to be twice its radius.[Ans]
- Lines 6 and 7 should be in a synchronized block to ensure encapsulation.
- The radius of a MyCircle object can be set without affecting its diameter.
- false
- default
- _object[Ans]
- a-class
- Compilation succeeds and d takes the value 253.
- Line 5 contains an error that prevents compilation.[Ans]
- Line 5 throws an exception indicating “Out of range”
- Line 3 and 4 contain error that prevent compilation.
- The compilation succeeds and d takes the value of 1.
- 3/2
- 3<2
- 3*4[Ans]
- 3<<2[Ans]
- 3*2^2
- 3<<<2
- 0...32767
- 0...65535
310 - 035
Leading the way in IT testing and certification tools, www.testking.com - -128...127[Ans]
- -256...255
- Range is platform dependent
What is the result?
Given:
public class X (
private static int a;
public static void main(String [] args) {
modify(a);
System.out.println(a);
}
public static void modify(int a) {
a++;
}
}
What is the result?
Given:
public class Test {
public static void add3 (Integer i) {
int val = i.intValue();
val += 3;
310 - 035
Leading the way in IT testing and certification tools, www.testking.com
-51 -
i = new Integer(val);
}
public static void main(String args[]) {
Integer i = new Integer(0);
add3(i);
System.out.println(i.intValue());
}
}
Which three evaluate to true? (Choose three)
Given:
public static void main( String[] args ) {
Integer a = new Integer(10);
Integer b = new Integer(10);
Integer c = (a);
int d = 10;
double e = 10.0;
}
What is the result?
Given:
String a = null;
a.concat(“abc”);
a.concat(“def”);
System.out.println(a);
Given that b and c refer to instances of wrapper classes, which two statements are true? (Choose two)
If the program is invoked using the command line:
java Test One Two Three –d What is the result?
Given:
public class Test {
public static void main(String [] args) {
System.out.println(args.length > 4 &&
args[4].equals(“-d”));
}
}
Note:The correct answer to this question is D. The args[4] generates a runtime exception error because there are only 4 strings and the expression args[4] prints the 5th String but like it was said earlier, there are only 4 strings.
What is the result?
Given:
try {
if ((new Object))(.equals((new Object()))) {
System.out.println(“equal”);
)else{
System.out.println(“not equal”);
}
}catch (Exception e) {
System.out.println(“exception”);
}
Which three demonstrate an “is a” relationship? (Choose three)
Which two are valid examples of method overriding when inserted at line 7? (Choose two)
Given:
class BaseClass {
private float x = 1.of;
protected float getVar() { return x; }
}
class SubClass extends BaseClass {
private float x = 2.Of;
// insert code here
}
What is the result?
Given:
class A {
public byte getNumber() {
return 1;
}
}
class B extends A {
310 - 035
Leading the way in IT testing and certification tools, www.testking.com
-55 -
public short getNumber() {
return 2;
}
public static void main(String args[]) {
B b = new B();
System.out.println(b.getNumber());
}
}
Which two are benefits of fully encapsulating a class? (Choose two)
What is the result when main is executed?
Given:
class A {
public A() {
System.out.println(“hello from a”);
}
}
class B extends A {
public B () {
System.out.println(“hello from b”);
super();
}
}
public class Test {
public static void main(String args[]) {
A a = new B();
}
}
Which statement is true?
Given:
package foo;
public class Outer {
public static class Inner {
}
}
Under which condition will check return true when called from a different class?
Given:
public class SyncTest {
private int x;
private int y;
private synchronized void setX( int i ) { x = i; }
public void setXY( int i ) { setX(i); setY(i); }
public synchronized boolean check() { return x != y; }
}
Thread Z holds the lock on object A. Thread X is blocked inside a wait call on ObjectA. What allows thread X to become runnable?
What happens when thread X executes a wait() method on object A, without ownin object A’s lock?
What is the result?
Given:
class MyThread extends Thread {
public void run() { System.out.println(“AAA”); }
public void run(Runnable r)
{
System.out.println(“BBB”);
}
public static void main(String[] args) {
new Thread(new MyThread()).start();
}
}
What is the result?
Given:
public class X implements Runnable {
private int x;
private int y;
public static void main(String [] args) {
X that = new X();
(new Thread( that )).start();
(new Thread( that )).start();
}
public void run() {
for (;;) {
synchronized (this) {
x++;
y++;
}
System.out.println(Thread.currentThread().getName() +
“x = “ + x + “, y = “ + y);
}
}
}
Which statement is true?
What is the result?
Given:
public class A extends Thread {
A() {
setDaemon(true);
}
public void run() {
(new B()).start();
try {
Thread.sleep(60000);
} catch (InterruptedException x) {}
System.out.println(“A done”);
}
class B extends Thread {
public void run() {
try {
Thread.sleep(60000);
} catch (InterruptedException x) {}
System.out.println(“B done”);
}
}
public static void main(String[] args) {
(new A()).start();
}
}
What can cause a thread to become non-runnable?
Which two, added at line 7, print the names in the ArrayList in alphabetical order?(Choose two)
Given:
ArraryList a = new ArrayList();
a.add(“Alpha”);
a.add(“Bravo”):
a.add(“Charlie”);
a.add(“Delta”);
Iterator iter = a.iterator();
Which statement at Point X on line 1 is required to allow this code to compile?
Given:
// Point X
public class foo {
public static void main(String[] args)
throws Exception {
jave.io.PrintWriter out = new jave.io.PrintWriter(
new jave.io.OutputStreamWriter(System.out), true);
out.println(“Hello”);
}
}
Which two are valid declarations of a float? (Choose two)
What is the result?
Given:
public class Test {
private static int[] x;
public static void main(String[] args) {
System.out.println(x[0]);
}
}
And the command line invocation:java Test red green blue What is the result?
Given:
public class Test {
public static void main( String[] args) {
String foo = args[1];
String bar = args[2];
String baz = args[3];
System.out.println(“baz = “ + baz);
}
}
EXPLANATION: Ajava.lang.ArrayIndexOutOfBoundsException is thrown because of line 3, should be args[0].
Which method must be defined by a class implementing the java.lang.Runnable interface?
What is the result?
Given:
public static void main(String[] args) {
Object obj = new Object() {
public int hashCode() {
returns 42;
}
};
System.out.println(obj.hashCode());
}
Which two are reserved words in the Java programming language? (Choose two)
Which two statements are true regarding the return values of property written hashCode and equals methods from two instances of the same class? (Choose two)
What is the numerical range of a char?
Explanation: Range for char/Character = 0 ==>65535
What is the result?
Given:
public class Test {
private static float[] f = new float[2];
public static void main(String args[]) {
System.out.println(“f[0] = “ + f[0]);
}
}
What is the result?
Given:
public class Test {
public static void main(String[] args) {
String str = NULL;
System.out.println(str);
}
}
Explanation: NULL should be "null"
Which interface does java.util.Hashtable implement?
What is the result?
Given:
System.out.println(Math.sqrt(-4D));
What is the result?
Given:
String a = “ABCD”;
String b = a.toLowerCase();
b.replace(‘a’, ‘d’);
b.replace(‘b’, ‘c’);
System.out.println(b);
What is the result?
Given:
public class Foo {
public static void main (String [] args) {
StringBuffer a = new StringBuffer (“A”);
StringBuffer b = new StringBuffer (“B”);
operate (a,b);
system.out.printIn{a + “,” +b};
)
static void operate (StringBuffer x, StringBuffer y) {
x.append {y};
y = x;
)
}
Which method is an appropriate way to determine the cosine of 42 degrees?
Which statement al line 17 will ensure that j=10 at line 19?
class A implements Runnable (
int i;
public void run () (
try (
thread.sleep(5000);
i= 10;
) catch(InterruptedException e) {}
)
)
public class Test {
public static void main (string args[]) (
try (
A a = new A ();
Thread t = new Thread (a);
t.start();
int j= a.i;
) catch (Exception e) {}
)
)
Which code determines the int value foo closest to, but not greater than, a double value bar?
Which two are valid examples of method overriding? (Choose Two)
Given:
class BaseClass{
private float x= 1.0f;
protected void setVar (float f) {x = f;}
}
class SubClass extends BaseClass {
private float x = 2.0f;
//insert code here
}
Which statement is true?
public class Mycircle {
public double radius;
public double diameter;
public void setRadius(double radius)
this.radius = radius;
this.diameter= radius * 2;
}
public double getRadius() {
return radius;
}
}
Which is a valid identifier?
Which statement is true?
Given:
public class X {
public static void main (String[] args) {
byte b = 127;
byte c = 126;
byte d = b + c;
}
}
Which two are equivalent? (Choose Two)
What is the numerical range of a byte?
-72 -