Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
rayjcwu / WalkMatrix
Created January 14, 2014 08:31
platform 9 interview question: given a M x N matrix, you put your robot at coordinate (0, 0) (upper-left corner) and the destination is (m - 1, n - 1). Robot can only go down or right. Print all possible route from (0, 0) to (m - 1, n - 1).
public class Solution {
public void entry(int m, int n) {
Step [] steps = new Step[(m - 1) + (n - 1) + 1];
steps[0] = new Step(0, 0);
recur(m, n, 0, 0, steps);
}
public recur(int m, int n, int y, int x, Step [] steps) {
if (y == m - 1 && x == n - 1) {
@rayjcwu
rayjcwu / Barasheet 1
Created January 20, 2014 06:45
You are teaching Java by way of geometry problems. Your work requires Math.PI, Math.cos, Math.sin and other Math package functions. You would prefer to simply use the simple names (e.g. PI, cos, sin) without the Math. or java.lang.Math. prefixes. How do you accomplish that? 1. Declare that GeometryLesson aliases java.lang.Math. 2. Declare that G…
class GeometryLesson {
static double degreesToRadians (double d) {
return (Math.PI / 360) * d;
}
...
}
@rayjcwu
rayjcwu / Barasheet 2
Created January 20, 2014 06:47
The class snippet above is marked public. Which statement about this class is true? 1. All methods of this class are publicly available. 2. Because the class is not protected, it cannot be extended through inheritance. 3. The source file containing this class definition is name MyClass.java 4. This class must implement everything in the java.lan…
public class MyClass {
...
}
@rayjcwu
rayjcwu / Barasheet 3
Created January 20, 2014 06:49
The code above expects a group of objects to print. Which of the following is a valid replacement type of XXX shown. 1. java.io.Reader 2. java.lang.Enum 3. java.lang.Iterable 4. java.util.Map
void printEach (XXX items) {
for (Object o: items) {
System.out.println(o);
}
}
@rayjcwu
rayjcwu / Barasheet 4
Created January 20, 2014 06:51
With reference to the wheelCount() method, the three classes shown above demonstrate which aspect of object-oriented programming? 1. AOP - (Aspect Oriented Programming) 2. Method overload 3. Multiple inheritance 4. Polymorphism
abstract class Vehicle {
abstract int wheelCount();
}
class Automobile extends Vehicle {
int wheelCount() {
return 4;
}
}
@rayjcwu
rayjcwu / Barasheet 5
Created January 20, 2014 06:52
Which one of the following statements concerning the type SomeType is true? 1. (exception instanceof Throwable) is true. 2. exception is a checked exception instance. 3. SomeType is java.lang.Exception. 4. SomeType is java.lang.RuntimeException
try {
... some code ...
} catch (SomeType exception) {
... some code ...
}
@rayjcwu
rayjcwu / Barasheet 6
Created January 20, 2014 06:55
Which one of the following is a legal statement? 1. InventoryItem inv = new Perishable(); 2. Perishable p = new PRoduce(); 3. Produce p = new Fruit(); 4. Produce p = new Produce();
interface Perishable {
int daysRemaining();
}
class InventoryItem {}
abstract class Produce extends InventoryItem implements Perishable {}
class Fruit extends Produce {
@Override
@rayjcwu
rayjcwu / Barasheet 7
Created January 20, 2014 06:57
This utitlity class includes MD5 hash generation from either String, byte[] or InputStream sources. Which Java feature is demostrated by these three static methods? 1. Method overloading 2. Method overriding 3. Polymorphism 4. Reflection
class Checksums {
static String md5 (String input) {
return ...
}
static String md5 (byte[] array) {
return ...
}
static String md5 (InputStream is) {
@rayjcwu
rayjcwu / Barasheet 8
Created January 20, 2014 07:00
You are creating some functionality intended to be private to the com.mycompany.marketing package. You have defined ian interface for which you plan on having a few package-speciic implementions. Your IDE insists that your implementation of doSomething must be public, yet the interface method is not public. Why is there a discrepancy? 1. All int…
package com.mycompany.marketing;
interface PackageLevelInterface {
void doSomthing();
}
public class MyClass implements PackageLevelInterface {
@Override
/// IDE insists on this being public (?)
public void doSomthing() {
@rayjcwu
rayjcwu / Barasheet 9
Created January 20, 2014 07:02
Your manager has dictated that you log all exceptions in an effort to diagnose some problems with a daily process. What statement is true about this effort to comply? 1. Except for the logged message, all the information about the original exception is lost. 2. One cannot throw an exception from within a catch block. 3. The catch specification o…
void dailyProcessingSetup() {
try {
...
} catch (Exception e) {
log(e.getMessage());
throw new RuntimeException("exception logged in dailyProcessingSetup");
}
}