Java Questions and Answers
These are things a serious Java programmer has to know. They are things
anybody who want to get Sun certification needs to know.
Added 12.20.2005: This is growing, as I
study and find questions of the sort you need to be able to answer to pass the
Sun exam.
I'm developing them in my own study. At this point (December 17, 2005) I'm
doing the JavaRanch elementary quizzes. Any time I come to a question I don't
know how to answer, or where I realize I am about to guess, I stop and study the
topic. There are lots of learning styles. Find one that works for you. Added
January 16, 2006: still working, with occasional time-outs to prepare for 221. I
can now usually get all or almost all the JavaRanch questions right, modulo
stupid/careless errors, but the questions on the exam are much
harder.
Gotta cut down on those stupid/careless answers. They cost just as much as a
question where you are totally lost. Read the question. Will the code compile?
Gotchas to watch out for:
- Is the public static void main (String[] args)
line absolutely correct? Are you sure know what variations in the argument are
legal? (String args[]) OK? How about
(String[] fred)?
- Although the code looks to be about some complex loop, did they
throw in an error like a protected class, or something like
if (true = Bool) which is an assignment,
not a comparison? Does an array declaration specify a size in an illegal
way? The question won't be about such things. You are expected
to catch them.
- Did something about polymorphism or inner classes sneak into the question?
- Etc. You have to know the language. That doesn't prove you could do
anything with it; they aren't testing that. But if the compiler
would complain, or if the program would compile but fail at run time, you've
got to know that. In 221 we didn't approach Java that way, at all.
- Can a class be both final and
abstract?
- Can a static method use the keyword this?
- True or false: the signature of a method consists of its name, the
number and type of its arguments, and its return type.
- True or false: a protected method in a superclass can be made private in
a subclass.
- Do instances of the same class have access to one another's private
members?
- Choose one:
A member (variable or method) prefixed by the
protected access modifier can
be accessed from:
- the same class
- a sub-class
- the same package
- all of the above.
- Where is a class marked public not known?
- Are true,
false, and null keywords?
- What is the bit-depth of a boolean?
- Consider the following program:
public class Arrays2 {
public static void main(String[] args) {
int[][] myArray = new int[3][];
myArray[0] = new int[2];
myArray[0][0] = 12;
myArray[0][1] = -46;
myArray[1] = new int[1];
myArray[1][0] = 10;
for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++) {
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
}
}
Choose one:
-
Program will run without error, printing:
12 -46
10
-
Program will fail compilation.
-
Program will print output shown in 1), then fail with a null pointer
runtime exception. If so, why?
- Will the following program snippet compile?
int[] weightList = new int[5];
byte b = 4;
char c = 'c';
short s = 7;
weightList[0] = b;
weightlist[1] = c;
weightList[2] = s;
- Will the following snippet compile?
int[] splats;
int[] dats = new int[4];
char[] letters = new char[5];
splats = dats; // OK, dats refers to an int array
splats = letters;
- Can a class that inherits from a top-level
protected
class by marked private?
- ParentClass contains this method:
public void publicMethod() {
// do stuff
}ChildClass inherits from
ParentClass and contains this method:
private void publicMethod(){
// do different stuff
}Will the code compile?
- When an object goes to sleep, does it take its lock with it?
- Can a dead thread be brought back to life?
- True or false: whenever any class is instantiated, the constructor for
Object runs?
- 1. Which has greater visibility, a method marked
protected or one with no
access modifier? 2. Same question, except for classes.
- True or false: If a concrete class implements the interface
Runnable, the class must contain a
method that is public, has a return
type of void, must be called
run(), and cannot take any arguments.
- Can a class be declared native?
- Which of the following statements are true?
-- Interface methods must not be static.
-- An interface can extend one or more other interfaces.
-- An interface cannot extend a class.
-- All interface methods are implicitly public
and abstract.
-- Interface constants must be public,
static, and
final--but they are implicitly, so you
don't have to write those modifiers.
-- Interfaces are implicitly abstract
whether you type abstract or not.
- Can a class contain another class?
- Will this line compile:
float f2 = 3.14;
- True or false: when a class is instantiated, the constructors of all of
its superclasses (which may be only Object)
are executed before the constructor of the class is executed.
- No. What would you do with it? An abstract class can't be instantiated,
and a private class can't be subclassed. It would just sit there, isolated
and useless. Compile-time error.
- No. this refers to the currently-executing instance
of an object. There
is no currently-executing instance of a static method. "Static" means "one
per class."
- False. Correct in C/C++, but the return type is not part of the
signature in Java.
- False. The access specifier for the overriding method can allow more but
not less access than the overridden method.
- Yes.
- All of the above.
- Nowhere. A public class is known to the universe. To use a class that is
in a different package you either have to import the class or use a
fully-qualified name, but it will compile.
- No. According to the Java Language Specification,
null, true,
and false are technically literal
values (sometimes referred to as manifest constants) and not keywords. But
you can't use them as identifiers. We are assured that this distinction is
not tested on the certification exam.
- Trick question, perhaps. We don't know. "Virtual machine
dependent" is the correct answer, which will impress and perhaps astound
interviewers. There is absolutely no Java equivalent of what C does, where
zero means false and anything else
means true.
- If you think you know the answer, email me! This appears to be typical
of some of the questions on the Sun exam. (I do know the answer.)
- Yes. byte,
char, and
short are all smaller than an int,
and will be promoted to int. And yes,
a char can be treated as having a
numerical value. Only thing that can't is
boolean.
- No. Fails at splats = letters;
"Type mismatch:
cannot convert from char[] to int[]".
It’s tempting to assume that because a variable of type
byte, short,
or char can be explicitly promoted
and assigned to an int, an array of
any of those types could be assigned to an int
array. You can’t do that in Java.
- A top-level class can never be marked protected;
only methods can. Call it a trick question is you like--but you better be
able to spot stuff like this. On the other hand, they might expect you to
know that an inner class can be protected. Without the "top-level"
qualifier, it might qualify as a dirty question.
- Nope. Compiler whines, "Cannot reduce the visibility of inherited
method." Visibility, in ascending order: private,
[no specifier], protected,
public. You can increase the visibility
of an inherited method or leave it the same, but you cannot reduce it. (And
be sure you know what protected
means.)
- Yes. (One of the approximately a zillion details you have to know about
threads. Plus a good bagful of concepts.) See the Sun tutorial listed in
Resources.
- No way. Will compile, but at run time you get an
IllegalThreadStateException.
- True. Whenever any class is instantiated, the constructors for all its
superclasses run also. Object (with a big Oh) is at the top of the
hierarchy; all objects are direct or indirect subclasses of Object. The
invocations work from the bottom up; the execution of the constructors works
from the top (Object) down. Remember: a child cannot be constructed before
its parent is constructed. (Like with people.) The instantiation of instance
variables for a class happens after the execution of its constructor. (Had
enough? Get used to it. This is about 10-20% of what you need to know
about constructors.)
- 1. Protected. In decreasing order of visibility of methods, it's
public/protected/default/private.
Can you remember PPDP? 2.
Won't compile if marked protected: the only permitted modifiers for a class are
public, final, and
abstract. Sun
loves this kind of question, namely something that tests your knowledge of
the language. Except they are much crueler: they wouldn't ask the question
this way;
they would simply mark as protected some class
which without that error would
do complicated stuff correctly. You waste your time on the logic, missing
the fact that it wouldn't compile. A good 10% of the questions do this kind
of thing.
- True. Boy! Are we going to have to work on interfaces!!!
- No. Methods can be declared native, not classes or variables.
- All true. Do you suspect we're going to have to devote some quality time
to interfaces?
- You bet. It's called an inner class. There are several varieties, of
which the most curious and confusing is one declared from within the
argument list of a method. This is the charmingly-named argument-defined
anonymous inner class. It ends with the sequence:
});
which you will see nowhere else in Java. There is a reason for it.
- Nope. 3.14 is a
double, and you can't convert a
double to a
float without potential loss of data.
If you want to force the conversion anyway, make it:
float f2 = (float)3.14;
It makes more sense, of course, to just say:
float f2 = 3.14f;
- True. And every class does have a constructor: if you don't write one
(or more), the compiler supplies a default constructor.
Back to Dan McCracken's Home Page