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) 0x320 ;

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

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 –

Which of the following statements concerning the 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.


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.


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.

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


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

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