Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
May 13, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning


I recently took Sun's Certified Java Programmers Exam and I ran into a few questions from the exam I'd like to ask you.

What is the difference between >> and >>>? For example let's say you have:

int x = 64;
     what is x >> 4?
     what is x >>> 4?
I get 4 for both.

What can be said about garbage collection? Select all true statements:

a. It will be done so that your program will never run out of memory (within reason).
b.It happens at predictable times.
c.You can flag an object for collection.
d.You can call a method to have an object explicitly collected.

Under which circumstance would a thread stop execution? Select all true statements:

a.A thread with a higher priority becomes run-able.
b.The priority of the running thread is changed.
c.Thread objects of the same class terminates.

If you have an object and then you set it to null:

SomeObject ob = new SomeObject();
      ob = null;
after you set ob to null, is the object available for garbage collection?

Thanks for your questions. It's interesting for us to see the types of questions on Sun's own exam.

First question: 64 >> 4 = 64 >>> 4 = 4


 x >> y is arithmetic shift right of x by y bits
   x >>> y is logical shift right of x by y bits
The difference is logical: Right shift uses 0 as a fill bit, while arithmetic shift right uses the most significant bit of x as the fill bit. Thus, logical shift right corresponds to unsigned integer division by two, while arithmetic shift right corresponds to signed integer division by two.

Since 64 > 0, the answer is the same, but in the eight-bit 2's complement representation, -64 = 11000000. Hence -64 >> 4 = 11111100 = -4, while -64 >>> 4 = 00001111 = 15.

What about left shift? Fortunately, the same fill bit, 0, is used for unsigned and signed multiplication by two. Hence x << y corresponds to x * 2^y no matter what the sign of x is.

Garbage Collection

a. I'm going to answer true to a. It would be hard to write a program that gobbled up the entire heap without releasing any of it. Reading a giant file into a hash table could do the trick.

b. No. The Java garbage collector runs in a low priority thread. In some systems the thread is scheduled when System.gc() is explicitly called by a program, or when the heap runs low. On other systems gc() runs whenever the system is idle, and is interrupted whenever a higher priority thread becomes run-able.

c. An object is implicitly flagged for collection as soon as there are no more active references to the object. Explicitly flagging an object for collection would imply there was an active reference to the object — otherwise, how would one explicitly flag it? — hence, when the object is collected, we are left with a dangling reference.

d.Programs can directly call the garbage collector:

But asking for a specific object to be collected implies one has an active reference to the object, which would leave a dangling reference after collection.

Thread scheduling

a. The Java thread scheduler is preemptive in the sense that it will preempt a running thread if a higher priority thread becomes run-able. But don't depend on this; the Java scheduler may run a lower priority thread to prevent starvation.

b. Yes. I tried the following simple experiment:

class MyThread extends Thread {
   int id;
   MyThread(int i) { id = i; }
   public void run() {
      for(int j = 0; j < 3; j++) {
         System.out.println("Thread " + id + " running");
         setPriority(1);  // 5 = normal

public class Test {
   public static void main(String[] args) {
      MyThread t1 = new MyThread(1);
      MyThread t2 = new MyThread(2);
On SunOS 5.5.1, I saw:
Thread 1 running
   Thread 2 running
   Thread 2 running
   Thread 2 running
   Thread 1 running
   Thread 1 running
(I guess they didn't let you use computers during the test. Too bad!)

c. My example shows the answer to c is no. Thread 2 stopped running, but that didn't stop thread 1. Of course threads in the same ThreadGroup can be terminated simultaneously.

Breaking an active reference to an object:

ob = null;
doesn't imply the object becomes garbage unless this was the last active reference to the object.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date