Sample Questions Best site for GRE, LSAT, SAT, GMAT, TOEFL, CCNA, CCSA and interview sample questions  

Technical Sample Questions : C |  C++ |  Oracle |  Java | Unix |  Operating Systems |  Data Structure

Java Sample Questions ›› Java questions

Technical General Java Sample Questions

  1. Explain carefully what null means in Java, and why this special value is necessary.

    Answer: When a variable is of object type (that is, declared with a class as its type rather than one of Java's primitive types), the value stored in the variable is not an object.Objects exist in a part of memory called the heap, and the variable holds a pointer or reference to the object.Null is a special value that can be stored in a variable to indicate that it does not actually point to any object.

  2. What is a constructor? What is the purpose of a constructor in a class?

    Answer: A constructor is a special kind of subroutine in a class.It has the same name as the name of the class, and it has no return type, not even void.A constructor is called with the new operator in order to create a new object.Its main purpose is to initialize the newly created object, but in fact, it can do anything that the programmer wants it to do.

  3. Suppose that Kumquat is the name of a class and that fruit is a variable of type Kumquat.What is the meaning of the statement "fruit = new Kumquat();"? That is, what does the computer do when it executes this statement? (Try to give a complete answer.The computer does several things.)

    Answer: This statement creates a new object belonging to the class Kumquat, and it stores a reference to that object in the variable fruit.More specifically, when the computer executes this statement, it allocates memory to hold a new object of type Kumquat.It calls a constructor, which can initialize the instance variables of the object as well as perform other tasks.A reference to the new object is returned as the value of the expression "new Kumquat()".Finally, the assignment statement stores the reference in the variable, fruit.So, fruit can now be used to access the new object.

  4. What is meant by the terms instance variable and instance method?

    Answer: Instance variables and instance methods are non-static variables and methods in a class.This means that they do not belong to the class itself.Instead, they specify what variables and methods are in an object that belongs to that class.That is, the class contains the source code that defines instance variables and instance methods, but actual instance variables and instance methods are contained in object.

  5. Explain what is meant by the terms subclass and superclass.

    Answer: In object oriented programming, one class can inherit all the properties and behaviors from another class.It can then add to and modify what it inherits.The class that inherits is called a subclass, and the class that it inherits from is said to be its superclass.In Java, the fact that ClassA is a subclass of ClassB is indicated in the definition of ClassA as follows:

    class ClassA extends ClassB {...}

  6. Explain the term polymorphism.

    Answer: Polymorphism refers to the fact that different objects can respond to the same method in different ways, depending on the actual type of the object.This can occur because a method can be overridden in a subclass.In that case, objects belonging to the subclass will respond to the method differently from objects belonging to the superclass.

  7. Java uses "garbage collection" for memory management.Explain what is meant here by garbage collection.What is the alternative to garbage collection?

    Answer: The purpose of garbage collection is to identify objects that can no longer be used, and to dispose of such objects and reclaim the memory space that they occupy.If garbage collection is not used, then the programmer must be responsible for keeping track of which objects are still in use and disposing of objects when they are no longer needed.If the programmer makes a mistake, then there is a "memory leak," which might gradually fill up memory with useless objects until the program crashes for lack of memory.

  8. For this problem, you should write a very simple but complete class.The class represents a counter that counts 0, 1, 2, 3, 4,....The name of the class should be Counter.It has one private instance variable representing the value of the counter.It has two instance methods: increment() adds one to the counter value, and getValue() returns the current counter value.Write a complete definition for the class, Counter.

    Answer: Here is a possible answer.

    public class Counter {
    private int value = 0;
    public void increment() {
    public int getValue() {
    return value;

  9. This problem uses the Counter class from Question 9.The following program segment is meant to simulate tossing a coin 100 times.It should use two Counter objects, headCount and tailCount, to count the number of heads and the number of tails.Fill in the blanks so that it will do so.

    Counter headCount, tailCount;
    tailCount = new Counter();
    headCount = new Counter();
    for ( int flip = 0;flip < 100;flip++ ) {
    if (Math.random() < 0.5)
    System.out.println("There were " + ___________________ + " heads.");
    System.out.println("There were " + ___________________ + " tails.");

    Answer: The variable headCount is a variable of type Counter, so the only thing that you can do with it is call the instance methods headCount.increment() and headCount.getValue().Call headCount.increment() to add one to the counter.Call headCount.getValue() to discover the current value of the counter.Note that you can't get at the value of the counter directly, since the variable that holds the value is a private instance variables in the Counter object.Similarly for tailCount.Here is the program with calls to these instance methods filled in:

    Counter headCount, tailCount;
    tailCount = new Counter();
    headCount = new Counter();
    for ( int flip = 0;flip < 100;flip++ ) {
    if (Math.random() < 0.5)
    System.out.println(("There were " + headCount.getValue() + " heads.");
    System.out.println(("There were " + tailCount.getValue() + " tails.");

  10. Programs written for a graphical user interface have to deal with "events." Explain what is meant by the term event.Give at least two different examples of events, and discuss how a program might respond to those events.

    Answer: An event is anything that can occur asynchronously, not under the control of the program, to which the program might want to respond.GUI programs are said to be "event-driven" because for the most part, such programs simply wait for events and respond to them when they occur.In many (but not all) cases, an event is the result of a user action, such as when the user clicks the mouse button, types a character, or clicks a button.The program might respond to a mouse-click on a canvas by drawing a shape, to a typed character by adding the character to an input box, or to a click on a button by clearing a drawing.More generally, a programmer can set up any desired response to an event by writing an event-handling routine for that event.

« Previous || Next »

Java Sample Question Number: 1-10| 11-20| 21-30| 31-40| 41-50| 51-60| 61-70| 71-80| 81-90| 91-100| 101-110| 111-120| 121-130| 131-140| 141-150| 151-160| 161-170| 171-180| 181-190| 191-200| 201-210| 211-220| 221-230| 231-240| 241-250| 251-260| 261-270| 271-280| 281-290| 291-300| 301-310| 311-320| 321-330
Sample Test Questions
GRE Sample Questions
CAT Sample Questions
GMAT Sample Questions
TOEFL Sample Questions
ACT Sample Questions
SAT Sample Questions
LSAT Sample Questions
PSAT Sample Questions
MCAT Sample Questions
PMP Sample Questions
GED Sample Questions
ECDL Sample Questions
DMV Sample Questions
CCNA Sample Questions
MCSE Sample Questions
Network+ Sample Questions
A+ Sample Questions
Technical Sample Questions
WASL Sample Questions
CISA Sample Questions

Other Sample Questions
Sample Interview Questions
Sample Teacher Interview Questions
Sample Citizenship Questions
Accuplacer Sample Questions
Science Bowl sample Questions
Driving Test Sample Questions
Sample Survey Questions Sample Essay Questions
Sample Behavioral Interview Questions

Copyright © 2004-2013, Best BSQ. All Rights Reserved.