Links

Java QA: 10 Interview Questions for Java – Advanced Level

thanassis | Java Questions And Answers | Tuesday, February 13th, 2007

This is a interview test for a Java programmer at an advanced level. There are 10 questions but more than a single answer to most of the questions. The correct answers are given at the end of the article together with the points awarded for each correct option and the difficulty of the question on a 1-10 scale. The best score you can get is 100 by answering correctly to all questions.

1. Question : Language Features – OO rules

What is going to happen when the following program executes?

class A {
public void a() {
b();
}
private void b() {
System.out.println(“Executing A’s b()”);
}
}
class B extends A {
public void b() {
System.out.println(“Executing B’s b()”);
}
}
…
public static void main(String[] argv) {
B b = new B();
b.b();
b.a();
}
1. B’s b() will run and then A’s b() will run
2. A’s b() is going to be called twice
3. B’s b() is going to be called twice
4. A’s b() will run and the B’s b() will run
5. The compiler will not allow the above code to compile 

2. Question : APIs – Swing

Swing Components can change their look and feel (l&F) at runtime.
Which of the l&f come standard with swing?

1. macintosh l&f
2. metal l&f
3. motif l&f
4. windows l&f
5. vax l&f

3. Question : VM Architecture : Parts and Design

Which of the following statements concerning the Virtual Machine are true and which are false?

1. Object are stored in the heap, local variables are stored in the stack.
2. The Virtual Machine has 8 registers, each one has the length of one word.
3. The Virtual Machine does not differentiate between an instance method and a constructor method.
4. A StackFrame gets created into the VM’s stack area for every thread executed.
5. The VM keeps a program counter for every thread.

4. Question : VM Architecture : Performance

Which of the following statements concerning performance are true and which are false?

1. Methods called through an interface reference are slower than those called through abstract methods because they can never be pointed directly by fields in the class pool of the caller
2. The invocation of an interface method is slower than Instantiation
3. The statement x = 3 executes faster than the statement x = 15.
4. Hotspot compilers convert all the bytecode into native code at runtime
5. Polymorphic calls are almost as fast as static method calls.

5. Question : Language Features – Classloaders

When two objects loaded through different Classloaders can they communicate?

1. When the invoker object access the other object through an interface that has been loaded using the invoker object’s classloader.
2. When they belong to the same package
3. When the invoker’s classloader has resolved the class of the other object
4. When the Security Manager allows it
5. Never, objects loaded using different Classloaders can’t communicate.

6. Question : Language Features – Reflection

Which of the following are valid ways of making an instance from class X

1. X myX = new X();
2. X myX = (X)((X.class).newInstance())
3. X myX = (X)Class.instantiate(X);
4. X myX = (X)(X.class).getClass().forName(“X”).newInstance();
5. X myX = (X)(Class.forName(“X”)).newInstance())

7. Question : Language Tools – Debuggers – Dissassemblers – Profilers

Which of the following statements are true and which are false?

1. JPDA stands for Java Personal Digital Assistant
2. SerVer is a jdk tool that allows serial verification of test profiles
3. The 4 first bytes on the header of any valid class file says JAVABABE
4. The bytecode form of a program can’t store the identifier names
5. Changing a String value of a bytecode class file will damage the file and render it unloadable.

8. Question : Object Orientation Principles – Patterns Basic

What is the pattern that best expresses the relationship between AWT’s classes Component and Container?

1. Factory
2. Strategy
3. Composite
4. Decorator
5. Proxy

9. Question : Object Orientation Principles – Patterns Advanced

Which of the following statements concerning the use of design patterns into the various java API’s are true and which are false?

1. An AWT’s Toolkit plays the role of an “Abstract Factory”
2. The LayoutManager in AWT plays the role of a “Decorator”
3. The Enumeration Interface reveals actually a “Mediator”
4. A Point in AWT is a “Wrapper”
5. A File is implemented in java using the “Composite” pattern

10. Question : Java API Design

The implementation of which design pattern would significantly improve the layout mechanism in terms of simplicity and readability?

1. State
2. Singleton
3. Null Object
4. Decorator
5. Facade

(more…)

Java QA: 10 Interview Questions for Java – Intermediate Level

thanassis | Java Questions And Answers | Friday, February 2nd, 2007

This is an interview test for Java programmers at internediate level. There are 10 questions and more than a single answer to most of the questions. The correct answers are given at the end of the article together with the points awarded for each correct option and the difficulty of the question on a 1-10 scale. The best score you can get is 100 by answering correctly to all the questions.

1. Question : Syntax – Algorithms

What would be the behaviour of the next fragment of code if called with x(4,””) :

public static void x(int rC, String c) {
String temp = c;
if (rC ==0) {
System.out.println(temp);
return;
}
else  {
rC--;
for (int i=0; i<26;i++) {
temp = c +(char)(65+i);
x(rC, temp);
}
}
}

1. A counter counting from A to Z will be print for 4 times
2. The 264 combinations of strings containing letters A to Z will be print
3. The compiler will throw an error for calling x() from x() itself
4. The program will hang in a never ending loop
5. The program will print for 264 times the alphabet in capital letters.

2. Question : Syntax – Object Casting

Which statements concerning object casting are true and which are false?:

An object can be cast to :
1. Any type from which the object’s class is a direct or indirect extension or implementation.
2. Any datatype providing that there is a wrapper for it.
3. An Interface only if the class of the object directly implements that Interface.
4. Any other type as long as for that type there is package visibility
5. Any type that is a sutype of an object. This is called downcasting. Downcasting in java is safe because it is checked by the compiler.

3. Question : Language Features – Interface with Environment

Concerning Java’s Interface with the environment. Which of the following statements are true and which are false?

1. Java can execute external processes but it can’t communicate with them
2. Java can communicate with spawn processes but it can’t control them
3. Properties in Java are stored in an object that inherits from a Hashtable
4. Environmental Variables can be passed through command line arguments in a java program
5. Applets have methods to read all datatypes as parameters given in a HTML page

4. Question : Language Features – String’s Operator Overloading

What is the output of the following extract of code when we call run()?

void run() {
int x=0;
byte[] b= { 14 } ;
Employee e = new Employee();
String s = "My String";
m(x,b,e,s);
System.out.print("x:"+x+" b[0]:"+b[0]+" Name:"+e.getName()+" Str:"+s);
}
public  static void m(int x, byte[] b, Employee e, String s) {
x=5;
b[0]=5;
e.setName("Ron");
s = s + "has been added";
}

1. x:5 b[0]:14 Name:Ron Str:My String has been added
2. x:0 b[0]:5 Name:Ron Str:My String
3. x:0 b[0]:14 Name:Ron Str: My String has been added
4. x:0 b[0]:5 Name:Ron Str: My String has been added
5. x:5 b[0]:5 Name:Ron Str: My String

5. Question : Language Features – Threading

Concerning Java’s multi-threading abilities. Which of the following statements are true and which are false?

1. When a method is synchronized there is no possibility of a Deadlock.
2. The thread that starts the application has always the minimum priority
3. A thread has its own stack area to store all local variables of the executing method
4. True concurrent execution is not possible using java
5. Notify() will wake up the thread that is waiting the longest.

6. Question : Object Orientation Principles – Polymorphism

Which of the following statements is true?

A polymorphic call is:
1. A call on a method that is defined many times with the same name within the same class but has different number/types of arguments. The selection of the method that actually gets’ invoked is based on matching its arguments
2. A call where the selection of the method to run on an object is based on the type of the object at creation time. This selection is based on the name only and arguments are irrelevant.
3. A call that gets delegated to a method of another object
4. A call of the constructor of the parent class. It can be called only as the first expression of a constructor to implement polymorphic chaining.
5. Any call that is indirect.

7. Question : APIs – awt

What is an Adapter in AWT?

1. A GUI component that looks like a Slider but does not have a Border
2. A “helper” class that adapts data from GUI controls to models
3. A “dummy” implementation of a Listener interface used for convenience.
4. An object that translates coordinates between coordinate systems
5. A native implementation of a GUI component

8. Question : APIs – awt

Can you get a Graphics object within the constructor of any AWT Component’s subclass?

1. Yes, you can just call getGraphics() that gets inherited from Component
2. Yes, you can just use fGraphics that is a protected field of Component
3. There is no way to get it. The graphics context object is given only to the paint(graphics g) method which is called asynchronously.
4. No, it hasn’t been created yet by the peer.
5. Depends on the Component

9. Question : APIs – net

Which of the following statements concerning Sockets are true and which are false?

1. Sockets communicate though TCP/IP
2. A ServerSocket blocks when it waits to accept a call
3. Sockets communicate through Streams
4. A ServerSocket automatically spawns a new thread to handle the incoming client call and then it continues to listen for new client calls.
5. Serializable objects can be transferred between Sockets using ObjectInputStream and ObjectOutputStream

10. Question : APIs – RMI

How is “marshalling” done in RMI?

Through
1. A java tool called rmic
2. HTTP tunneling
3. Serialization
4. The use of JNI
5. The use of JNDI

(more…)