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.

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.

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as