Sample Quiz

  • Which of the following snippets of code is error-free? For any that you think will produce an error, identify what the error is and try to fix the code?
(a)
List<int> some = new MysteryListImplementation<int>();

(b)
int a = 2;  
int b = 3.0 * a;

(c) 
public static Integer someStaticMethod() {  
    return new Integer();
}

(d)
public Integer someNonStaticMethod() {  
    return someStaticMethod();
}

(e)
public static Integer secondStaticMethod() {  
    someNonStaticMethod();
}

(f)
public class SomeClass<T> {  
    ...
    public SomeClass<T>() {

    }

    public void someMethod() {

    }

    ...
}

(g)
public class MyInterface {

}
public class SecondClass<T> implements MyInterface {

}
  • Show the state of the underlying data structure at every step of the algorithm.
Stack<Integer> s = new Stack<Integer>();  
String someTest = "23*2*8*9*";  
int first, second;  
for (int i = 0; i < someTest.length(); i++) {  
    if (someTest.charAt(i) == '*') {
        first = s.pop();
        second = s.pop();
        s.push(first * second);
    } else {
        s.push(
          Character.getNumericValue(someTest.charAt(i))
        );
    }
}
System.out.println(s.pop());