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:

  1. Can a class be both final and abstract?
  2. Can a static method use the keyword this?
  3. True or false: the signature of a method consists of its name, the number and type of its arguments, and its return type.
  4. True or false: a protected method in a superclass can be made private in a subclass.
  5. Do instances of the same class have access to one another's private members?
  6. Choose one:
    A member (variable or method) prefixed by the protected access modifier can be accessed from:
  7. Where is a class marked public not known?
  8. Are true, false, and null keywords?
  9. What is the bit-depth of a boolean?
  10. Consider the following program:
     
  11. 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:
    1. Program will run without error, printing:
      12 -46 
      10
    2. Program will fail compilation.
    3. Program will print output shown in 1), then fail with a null pointer 
      runtime exception. If so, why?
  12. Will the following program snippet compile?
  13. int[] weightList = new int[5];
    byte b = 4;
    char c = 'c';
    short s = 7;
    weightList[0] = b; 
    weightlist[1] = c; 
    weightList[2] = s; 
    	
  14. 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;
  15. Can a class that inherits from a top-level  protected class by marked private?
  16. 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?
     
  17. When an object goes to sleep, does it take its lock with it?
  18. Can a dead thread be brought back to life?
  19. True or false: whenever any class is instantiated, the constructor for Object runs?
  20. 1. Which has greater visibility, a method marked protected or one with no access modifier? 2. Same question, except for classes.
  21. 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.
  22. Can a class be declared native?
  23. 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.
  24. Can a class contain another class?
  25. Will this line compile:
    float f2 = 3.14;
  26. 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.

 

 

 

  1. 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.
  2. 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."
  3. False. Correct in C/C++, but the return type is not part of the signature in Java.
  4. False. The access specifier for the overriding method can allow more but not less access than the overridden method.
  5. Yes.
  6. All of the above.
  7. 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.
  8. 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.
  9. 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.
  10. 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.)
  11. 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.
  12. 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.
  13. 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.
  14.  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.)
  15. 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.
  16. No way. Will compile, but at run time you get an IllegalThreadStateException.
  17. 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.)
  18. 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.
  19. True. Boy! Are we going to have to work on interfaces!!!
  20. No. Methods can be declared native, not classes or variables.
  21. All true. Do you suspect we're going to have to devote some quality time to interfaces?
  22. 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.
  23. 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;
  24. 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