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…)

Java QA: 10 Interview Questions for Java - Beginners Level

thanassis | Java Questions And Answers | Saturday, January 20th, 2007

This is a interview test for a Java programmer at a beginner’s level. There are 10 questions but with 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 : Syntax - Datatypes

Associate Java’s 8 Datatypes to their respective memory representations

1. char A. signed 8-bit
2. int   B. signed 16-bit
3. double   C. floating point 64-bit
4. float   D. signed 64-bit
5. short   E. unsigned 16-bit
6. boolean   F. signed 32-bit
7. long   G. floating point 32-bit
8. byte   H. 1 bit

2. Question: Syntax - CastingWhich of the following expressions contain illegal casts that will not get compiled?

1. byte b = (byte)6.25f ;
2. boolean bool = (boolean) 1 ;
3. int i = (int) true ;
4. char ch1 = (char) ((int)2.5f );
5. char ch2 = (short) 0×320 ;

3. Question: Syntax - Methods

What is going to be the output of the following fragment of code at the printing statement?
….
int x = 5;
int[] arr = { 3 };
m(x, arr):
System.out.println(“x=” + x * arr[0]);
….
public void m(int value, int[] arr) {
value+=5;
arr[0]+=10;
}

1. x = 5
2. x = 15
3. x = 65
4. x = 30
5. x = 130

4. Question: Language Features - Exception Handling

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

1. Exceptions are only thrown at the runtime of a program
2. Both Error and Exception are subclasses of a class named Throwable. 3. Errors can be caught much the same way as exceptions
4. Exceptions subclassing RuntimeException do not need explicit exception handling to pass the compilation stage
5. With proper exception handling, some compiler errors can be avoided

5. Question: Language Features - Packages

Consider the diagram below where package A has two subpackages, B and C. Which of the following statements are true and which are false?

1. import A.*; Imports all the classes in package A and in package B and C
2. import A.A*; Imports all the classes in package A that start with the letter A
3. import A; Imports all the classes of package A
4. import B.B; Imports class B from package B
5. import *; Imports all the packages described by the CLASSPATH

6. Question: Language Features - Applets

Which of the following statements concerning Applet’s security on the client are true and which are false?

Typically an Applet is restricted by the SecurityManager in:
1. Using resources such as local printers and filestores
2. Opening any socket
3. Asking the OS for more memory to run itself
4. Opening more than a predefined number of Frames
5. Finding out about the client’s username and the VM’s version
7. Question: Language Features - Static vs Instance Methods

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

1. Static methods are inherited the same way instance methods do.
2. Static methods can call instance methods as long as they have a reference to the object that the instance method belongs to.
3. It is possible to have static methods described as private.
4. It is possible to have static methods described as protected.
5. If a static method is described as final then it will be in-lined by the compiler

8. Question: APIs - java.lang

Which of the following statements concerning the java.lang package are true and which are false?

Package java.lang
1. is implicitly available to all java sources
2. has several sub-packages
3. contains useful classes such as the Vector and the HashTable
4. contains classes designed for sub-classing such as the class String
5. contains all the classes that make the parsing of java sources by the compiler possible and all those used for the basic needs of all the api’s.

9. Question: APIs - java.util

Which of the following statements concerning the java.util api are true and which are false?

1. A StringTokenizer can tokenize a String or a Stream depending on its constructor
2. A StringTokenizer uses a delimiter that can be any character or String
3. A StringTokenizer can recognize and parse numerical values, putting them into a field named nval
4. A Stack can store both datatypes and objects
5. The util package contains the Date and Time classes

10. Question: APIs - java.io

Which of the following statements concerning the java.io api are true and which are false?

1. The family of Readers/Writers has been built to replace Streams and to provide better efficiency
2. A File object in java represents either a file or a directory
3. In Unix, java’s File objects can change file permissions
4. A BufferedReader can be constructed by passing any Reader or Stream
5. Both objects and primitive datatypes can be directly serialized.

(more…)

Java QA: Is java is secure and how?

thanassis | Java Questions And Answers | Friday, January 19th, 2007

Security has been implemented in Java since day one.
On four levels:

First, On the language level. Java has tokens to define private spaces (private members, packages, namespaces)

Second, On the runtime level with its bytecode verifier. The bytecode verifier checks to ensure that the class bytecode hasn’t been tampered.

Third, With a Sandbox model guarded by a Security Manager. For example, a SecurityManager object disallows file/net access when running as an applet.

Fourth, With security packages that allow application developers to do things like encoding/decoding/trusting certificates etc.

Although no platform is 100% secure, most of the times you hear about java viruses, trojans, malware etc they do not represent holes in the Java language/platform but in specific implementations of a JVM, (i.e. explorer’s JVM)

Java QA: Difference between:Architecture - Framework - Methodology

thanassis | Java Questions And Answers | Wednesday, January 3rd, 2007

Software Architecture refers to the structural choices and to the plan followed by the designer concerning the software. There are architectural patterns that software architects are using to structure their software and to communicate it to others. Popular architectural patterns include the Model-View-Controller pattern (MVC), Broker, Blackboard and many others.

(more…)

Java QA: Why do we need Abstract Classes in Java?

thanassis | Java Questions And Answers | Wednesday, January 3rd, 2007

We need the notion of a parent class (from which we can inherit) that is prohibitted from being instantiated. We can think about abstract classes as semicomplete generalised implementations that can not exist as objects on their own, they can only exist as parts of child objects.
(more…)

Java QA:What is Overriding?

thanassis | Java Questions And Answers | Wednesday, January 3rd, 2007

We can think of overriding as redefining. When we want to redefine an inherited behaviour in a more specialised way, we must provide implementation with the exact method signature as the inherited method (that we want to redefine).

(more…)

Java QA: What is Factory Method in java ?

thanassis | Java Questions And Answers | Wednesday, January 3rd, 2007

The term “Factory Method” refers to a method that is part of the factory design pattern. The factory design pattern suggests that when we want to decouple the instantiations of objects from their type we can defer the instantiations to a class method (static method).
(more…)

Java QA:Differentiate between upcasting & downcasting. Explain with reference of classes.

thanassis | Java Questions And Answers | Tuesday, January 2nd, 2007

In the object oriented world we have structures of objects made of classes. Classes can be ordered through a special inheritance relationship into hierarchies of parent/child relationships.

Upcasting means taking a reference to an object and changing its type to that of any of its parent classes. (for java, also all the interfaces that any of the parent classes may be implementing).

(more…)