Top 15 Core Java pre-interview technical written test questions and answers
Written tests can help short-list candidates and often used by recruitment agencies and prospective employers. Here are some popular written test questions and answers.
Note: Multiple choice questions may have more than one right answers.
Q1. What will be the output of the following code snippet?
package com.writtentest1;
public class Base {
int i;
public Base() {
add(1);
}
public void add(int j) {
i += j;
}
}
package com.writtentest1;
public class Extension extends Base {
public Extension() {
add(2);
}
public void add(int j) {
i += j*2;
}
}
package com.writtentest1;
public class Main {
public static void main(String[] args) {
exec(new Extension());
}
private static void exec(Base base) {
base.add(8);
System.out.println(base.i);
}
}
a) 21
b) 22
c) 20
d) 16
e) 17
A1. The answers is b, i.e 22.
Step 1: When new Extension( ) is invoked it invokes the constructor Extension( ).
Step 2: The trick here is that default constructors implicitly call super( ), so Base( ) constructor will be invoked next.
Step 3: Base( ) --> add(1) method on Extension class as the stored object is of type Extension and polymorphic will kick in. The value of i becomes 0 + 1*2 = 2.
Step 4: add(2) is invoked from Extension constructor. The i becomes 2 + 2*2 = 6;
Step 5: Finally, add(8) is invoked from Main --> exec, and i becomes 6 + 2*8 = 22.
Q2. What will be happen when you run the following code?
package com.writtentest2;
public class ThreadMain {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.print("Going to sleep. ");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("Has woken up. ");
}
};
Thread thread = new Thread(runnable);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("Completed.");
}
}
a) prints: Going to sleep. Has woken up. Completed.
b) InterruptedException is thrown with a stack trace.
c) prints: Completed. Going to sleep. Has woken up.
d) prints: Completed.
A2. The answer is a.
There are 2 threads involved in this. Main thread and the worker thread "thread". The main thread spawns the new thread and when thread.start( ) is invoked the run( ) method inside the anonymous inner class for Runnable interface is executed and prints "Going to sleep. ". In the mean time the Main thread is blocked due to thread.join( ) statement. The worker thread sleeps for at least 5 seconds, and then prints "Has woken up. " and it completes its execution. Once the worker thread "thread" has finished, the main thread continues and prints "Completed."
Try commenting out thread.join( ); and its try/catch block see what happens to the output.
Q3: Given the following interface,
package com.writtentest3;
public interface Ixy {
abstract String get();
abstract void set(String value);
}
Which of the following statements are correct?
a)
package com.writtentest3;
public abstract class IxyImpl implements Ixy {
public void calc(String value){}
}
b)
package com.writtentest3;
public class IxyImpl implements Ixy {
@Override
public String get() {
return null;
}
public void calc(String value){}
}
c)
package com.writtentest3;
public class IxyImpl {
@Override
public String get() {
return null;
}
public void calc(String value){}
}
d)
package com.writtentest3;
public class IxyImpl {
public String get() {
return null;
}
public void calc(String value){}
}
A3. Answer is: a and d.
a: No compile error since the class is abstract, even though it does not implement all the methods from the interface.
b: Compile error because it is a concrete class, and does not implement the abstract method void set(String value).
c: Compile error because it does not implement the interface Ixy, but the method public String get( ) is still annotated with @Override
d: No compile error as the @Override annotation is no longer there.
You may also like
- Top 15 Core Java pre-interview technical written test questions and answers 4 -7
- Top 15 Core Java pre-interview technical written test questions and answers 8 - 9
- Top 15 Core Java pre-interview technical written test questions and answers 10 - 13
- Top 15 Core Java pre-interview technical written test questions and answers 14 - 15
Labels: Core Java, Written tests

3 Comments:
Hi,...i am not getting the exact flow of the first Q: Executor class base class...i didn't get the calculation...i am confused on how you derived the first calculation: (0+1*2)
Could you please explain how you got this?
i is initially 0. Even though the reference is a Base class, the object stored in it is of type Extension. Hence, it will only invoke the overridden method add(...) in the Extension class, which has i += j*2; Try creating and running this program.
nice sir...
Post a Comment
Subscribe to Post Comments [Atom]
<< Home