IB Java GUI 20080415
IB Java GUI 20080415
IB Java GUI 20080415
C o n s u l t i n g S e r v i c e s, I
n c .
Name: Date:
1. What are the values of a, b, and c after all three lines have executed?
int a = 1;
int b = a++;
int c = ++b;
a=2;b=2;c=2;
int a = 1 << 2;
a=4;
3. Fill in the code to return the max value using the ternary (question mark) operator.
class Util {
static int max( int a, int b) {
return m;
}
}
class LongObj {
private int m_length;
LongAndWideObj(int width) {
m_width = width;
}
}
5. Change the function divide() to throw an exception if den is zero, and change the
function fun() to catch the exception.
class SomeClass {
void fun() {
try{
divide( 4, 2);
}catch(Exception ex){
}
}
class SomeClass {
}
7. Write the code to add a node to the beginning of the list.
class Node {
public int value;
public Node next;
}
class LinkedList {
node = node->first;
}
}
8. v is an ArrayList of Strings. Write code to remove all strings from the vector that are
equal to “bad”.
class Main {
public void filter( ArrayList v) {
if(v.get(i).toString().equals("bad")){
arrayList.remove( new Integer(i) )
}
}
}
9. Implement the following class model:
Answer:
You can use (1 to n) interface.
O to 1 or 1 to 1 use abstract.
10. The following code will not compile; fix it:
return returnValue;
}
Answer:
return returnValue;
}
12. Since Swing is not thread safe, this code could cause problems. How can it be
fixed? (Assume Application is running in it's own thread)?
public StatusFrame() {
// init frame
setVisible(true);
}
frame.addLabel();
}
}
Answer:
public synchronized void addLabel() {
myPanel.add(new JLabel("90%"));
}
14. What is the advantage of setting bigObject = null in the following code?
// Use bigObject
// .
// .
// Done with bigObject
bigObject = null;
// Rest of method
}
Answer:
A null object is very predictable and has no side effects; it does
nothing.
Answer:
Or String foo;
Foo=”Foo”;
16. If I have a class Key, implement methods so I can use it as a key into a HashMap as
shown below?
class Main {
static HashMap table = new HashMap();
class Families {
public String parent;
public String childName;
public void addToFamily( String parent, Child child) {
public
18. Let the Person class implement the java.lang.Comparable interface so that it
sorts first by last name and then by first name.
class Person {
String m_firstName;
String m_lastName;
2. Which of the following circumstances should throw Exceptions? Explain your reasoning.
b. A syntax error is found in a configuration file that an object uses to set its initial state
Yes, we need to find this problem and to fix it. Because it will cause system probem.
3. What are some situations where you would make constructors private?
When I try to use this class to init data. But I don’t want any other class to access this init.
Java Specific
1. When is an object’s finalize method called? What should finalize methods be used for?
What shouldn’t they be used for?
finalize method is called after an object becomes inaccessible, unless the object has been
exempted from finalization by a call to SuppressFinalize.
Fibalize allows an Object to attempt to free resources and perform other cleanup operations
before the Object reclaimed by garbage collection.
2. What does the final keyword do and why would you use it.
GUI Specific