Weekly Puzzles

Java Chars

For help with these questions, please see the page on Java Chars!

What is the output of the following line of code?

System.out.println('i' + 's');

The following line of code performs widening primitive conversion:

System.out.println('B' + "ig");

What is the output of the following line of code?

System.out.println("2 + 2 = " + 2 + 2);

Which line of code runs without any problems?

char temp2 = 'i' * 'o' / 100;
char temp = 'i' - "o";
System.out.println(5 < "z");

Java Doubles and Floating Point Numbers

For help with the following questions, please see the page on Java Doubles!

What is the output of the following code?

     System.out.println(2.00 - 1.10);

How many bits are in a byte?

What is the output of the following code?

public class Elementary{
public static void main(String[] args) {
System.out.println(12345 + 5432l);
}
}

What is the difference between Double values and BigDecimal values?

What will the following code output?

public class threshold {
    public static void main(String[] args) {
        final double THRESHOLD = .0001;
        double f1 = .0;
        for (int i = 1; i <= 11; i++) {
            f1 += .1;
        }
        double f2 = .1 * 11;
        if (Math.abs(f1 - f2) < THRESHOLD) {
            System.out.println("f1 and f2 are equal using threshold");
        } else { System.out.println("f1 and f2 are not equal using threshold");
                
        }
    }
}

Java Booleans

For help with the following questions, visit the page on Java Booleans!

  • What type is a Boolean?
    • Native
    • Simple
    • Primitive
  • What does the following code output?
!false || !true && 7 >= 10
  • What will the following code output?
public class MyClass {
     public static void main(String[] args) {
          boolean isGood = false;
          for(int i = 0; i < 3; i++) {
               if(isGood) {
                    System.out.print("x");
                    isGood = false;
               } else {
                    System.out.print("y");
                    isGood = true;
               }
          }
     }
}
  • What does the following expression evaluate to?
((7 / 2) == (7.0 / 2))

November 16

The code snippet below is supposed to read in from Holidays.txt and print out the first four holidays in the file.  What is wrong with the snippet?  (Hint:  look at the scanner and at the loop condition.)

import java.util.Scanner;
public class Holiday {

    public static void main(String [] args) {
        Scanner scan = new Scanner("Holidays.txt");
        int num = 0;
        while (scan.hasNext && num > 4) {
            System.out.println(scan.nextLine());
            num ++;
        }
    }
}

Assume Holidays.txt has the following data:

New Year  Jan 1
Valentine's Day Feb 14
St. Patrick's Day Mar 16
April Fools!  April 1
Memorial Day  May 28
4th of July   July 4
Labor Day     Sept 1
Halloween     Oct 31
Veteran's Day  Nov 11
Christmas     Dec 25