Sunteți pe pagina 1din 14

SANS 

   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;

2. What is the value of a?

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) {

int m = (a >b ) ? a:b;

return m;

}
}

4. Write a constructor that makes sense for the LongAndWideObj class.

class LongObj {
private int m_length;

LongObj( int length) {


m_length = length;
}
}

class LongAndWideObj extends LongObj {


private int m_width;

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 divide( int num, int den) {


if( den == 0)
throw new Exception(“can’t be divided by zero”);
return num/den;

void fun() {

try{
divide( 4, 2);

}catch(Exception ex){

System.out.println( “Divide” + ex.getMessage());

}
}

6. Fix the compile bug.

class SomeClass {

void fun( int[] data) {


for( int a = 0; a < data.length; a++) {
process( data[a]);
}
}
}
Void process(int d){

}
7. Write the code to add a node to the beginning of the list.

class Node {
public int value;
public Node next;
}

class LinkedList {

private Node first;

public void addNodeToBeginningOfList( Node node) {

node->first = new List;

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) {

for(int i=0; i < v.size(); i++)

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:

Public abstract class Car {


public Car(int numDoors) {
myNumDoors = numDoors;
}

public abstract int maxNumPassengers();

// private int myNumDoors;


public int myNumDoors;

public class Sedan extends Car {


public Sedan(int numDoors) {
super(numDoors);
}

public int maxNumPassengers() {


return myNumDoors * 1;
}
}

11. Re-write this code to run more efficiently:

public static String concatStrings(Vector strings) {


//String returnValue = "";
StringBuffer returnValue = new StringBuffer();

Iterator iter = strings.iterator();


while( iter.hasNext() ) {
// returnValue += (String)iter.next();
returnValue.append((String)iter.next());

return returnValue;
}

Answer:

public static String concatStrings(Vector strings) {


StringBuffer returnValue = new StringBuffer();

Iterator iter = strings.iterator();


while( iter.hasNext() ) {
returnValue.append((String)iter.next());
}

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 class StatusFrame extends JFrame {

public StatusFrame() {
// init frame
setVisible(true);
}

public void addLabel() {


myPanel.add(new JLabel("90%"));
}
}

public class Application {

public void showStatusFrame() {


StatusFrame frame = new StatusFrame();

frame.addLabel();
}
}

Answer:
public synchronized void addLabel() {
myPanel.add(new JLabel("90%"));
}

13. What is the difference between protected and package visibility?


Answer:
For protected any class in the same package as this one, as well as
any subclass of this class, can access this field.

14. What is the advantage of setting bigObject = null in the following code?

public void someMethod() {


BigObject bigObject = new BigObject();

// 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.

15. How can the following code be improved?

String foo = new String("Foo");

Answer:

Stringbuffer foo = new Stringbuffer();


Foo.append(“Foo”);

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 Key extends Main{


int m_keyVal;

Key( int val) {


m_keyVal = val;
}

class Main {
static HashMap table = new HashMap();

static public void addToMap( int val, Object obj) {


Key key = new Key( val);
table.put( key, obj);
}

static public Object getObj( int val) {


Key key = new Key( val);
return table.get( key);
}
}
17. Create a class called Families which creates a one-to-many mapping of parent
name (String) to Child objects. Create any needed member variables and write the
two specified methods. (You do not need to write the Child class.)

class Families {
public String parent;
public String childName;
public void addToFamily( String parent, Child child) {

public List getChildren( String parent) {

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;

public Person (int firstName, String lastName) {


// set values on attributes
}

public int compareTo(Person o) {


return this. firstName - o. lastName;
}

public static void main(String[] args) {


List coll = Util.getPerson ();
Collections.sort(coll); // sort method
printList(coll);
}
}
General Programming

1. What are the advantages of using composition as opposed to inheritance?


composition is not only more resistant to changes in super classes but enhances
encapsulation instead of diminishing it.

2. Which of the following circumstances should throw Exceptions? Explain your reasoning.

a. Someone tries to set the capacity of a PackingBox to a negative value.


Yes, capacity of a PackingBox can’t be negative value

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.

c. A method that searches for a programmer-specified word in a string array cannot


find any occurrence of the word.
No, There is no any problem if you get nothing.

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.

Give a chance to do something before garbage collection.

2. What does the final keyword do and why would you use it.

Sometimes we don’t want change value of variable


Final keyword cannot be changed.

GUI Specific

1. How can one make radio buttons be mutually exclusive?

You can group by array. Use same name.


2. What is the difference between a JScrollbar and a JScrollPane?
With a JScrollPane, the primary differences between the look and
feel types are related to the scrollbar’s appearance and border
around the viewport.

3. What is the difference between a Window and a JFrame?


The Frame class extends Window to define a main application window
that can have a menu bar.

S-ar putea să vă placă și