2. Question #0
Which browser does this code open?
a) Internet Explorer
b) Opera
c) FireFox
d) Safari
class Google {
public static void main(String []args) {
http://www.google.com
System.out.println("Hello world");
}
}
3. Question #1
class LongVal {
public static void main(String []s) {
long l = 0x22l;
System.out.printf("%ld", l);
}
}
4. Question #2
class NaNTest {
public static void main(String []s) {
double d = getVal();
if(d == Double.NaN)
System.out.println("d is NaN”);
}
private static double getVal() {
return Double.NaN;
}
}
5. ❖ FindBugs names this bug as: “Doomed test for
equality to NaN”
❖ This code checks to see if a floating point value is
equal to the special Not A Number value (d ==
Double.NaN).
❖ special semantics of NaN: no value is equal to NaN,
including NaN.
❖ d == Double.NaN is always false
❖ Correct check: Use Double.isNaN(x)
Bug: (NaN == NaN) is false!
6. “95% of the folks out there
are completely clueless
about floating-point”
-James Gosling (in 1998
JavaOne keynote address)
Floating-point Numbers are Tricky!
7. Question #3
class Base {
public Base() {
foo();
}
public void foo() {
System.out.println("In Base's foo ");
}
}
class Derived extends Base {
public Derived() {
i = new Integer(10);
}
public void foo() {
System.out.println("In Derived's foo " + i.toString());
}
private Integer i;
}
class Test {
public static void main(String [] s) {
new Derived().foo();
}
}
8. Question #4
abstract class Printer {
private Integer portNumber = getPortNumber();
abstract Integer getPortNumber();
public static void main(String[]s) {
Printer p = new LPDPrinter();
System.out.println(p.portNumber);
}
}
class LPDPrinter extends Printer {
/* Line Printer Deamon port no is 515 */
private Integer defaultPortNumber = 515;
Integer getPortNumber() {
return defaultPortNumber;
}
}
9. Question #5
class Color {
int red, green, blue;
void Color() {
red = 10; green = 10; blue = 10;
}
void printColor() {
System.out.println("red: " + red + " green: " + green + " blue: " + blue);
}
public static void main(String [] args) {
Color color = new Color();
color.printColor();
}
}
10. Question #6
Supplier<String> s = () -> "hello world";
System.out.println(s.get());
What bytecode
instruction would
s.get() generate?
11. Question #7
class Vehicle {
public static void run() {
System.out.println("In Vehicle.run()");
}
public static void main(String[] args) {
Vehicle vehicle = null;
vehicle.run();
}
}
class Car {
public static void run() {
System.out.println("In Car.run()");
}
}
12. Question #8
class Base {
public void aMethod(int args) {
System.out.println("Called Base.method with value " + args);
}
}
class Derived extends Base {
public void aMethod(long args) {
System.out.println("Called Derived.method with value " + args);
}
}
class Overriding1 {
public static void main(String []args) {
Base b = new Derived();
b.aMethod(10);
}
}