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 typesof 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 bitof x as the fill bit. Thus, logical shift right correspondsto unsigned integer division by two, while arithmetic shiftright corresponds to signed integer division by two.
See also  Does It Make Sense to Splurge on a Laptop?

Since 64 > 0, the answer is the same, but in the eight-bit 2’scomplement 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. Hencex << y corresponds to x * 2^y no matter what the sign ofx is.

Garbage Collection

a. I’m going to answer true to a. It would be hard to writea program that gobbled up the entire heap without releasingany of it. Reading a giant file into a hash table coulddo the trick.

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

c. An object is implicitly flagged for collection as soon asthere are no more active references to the object. Explicitlyflagging an object for collection would imply there was anactive reference to the object ? otherwise, how would one explicitlyflag it? ? hence, when the object is collected, we are left witha dangling reference.

d.Programs can directly call the garbage collector:

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

Thread scheduling

a. The Java thread scheduler is preemptive in the sense that itwill preempt a running thread if a higher priority thread becomesrun-able. But don’t depend on this; the Java scheduler may runa lower priority thread to prevent starvation.

See also  Redefining Energy Management: Advanced Solutions for Modern Buildings

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);      t1.start();      t2.start();   }}
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 stoppedrunning, but that didn’t stop thread 1. Of course threads inthe 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 lastactive reference to the object.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist