Sunday, June 21, 2009

Puzzle 35 – Memory Management

Language – Java | Type – Concept | Last date 24-Jun-2009 09:00 p.m. IST | Points 3

The other day I was trying out some memory conversions. I vaguely recalled a piece of code that I wrote in my college days.

package com.twisters;
public class MemoryManager {
public static void main(String[] args) {
int oneKB = 1024; /* No change to this line! */
int oneMB = oneKB;
System.out.println(
"1 MB of memory is :"+oneMB+" bytes.");/* No change to this line! */
}
}

Obviously I think I have missed something. And I am sure I have missed it in line 5. Help me complete this code!!

Rules – pretty simple ones really!
No additional semicolons.
No changes to the lines marked with the comment /* No change to this line! */
No multiplication or division sign to be used in the program.
No additional packages except java.lang – import(ing) is a costly affair!
Make minimum changes to the code – a maximum of 8 characters change would suffice.

Link for the day – http://mindcipher.com/ - A must view site for any puzzle lover.

Got an answer? Do leave it here.

12 comments:

  1. public class MemoryManager {

    public static void main(String[] args) {
    int oneKB = 1024; /* No change to this line! */
    int oneMB = oneKB+1047552;
    System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* No change to this line! */
    }
    }

    ReplyDelete
  2. Hope bitwise is accepted:

    Line 5:

    int oneMB = oneKB << 10;

    ReplyDelete
  3. Line 5:
    int oneMB = oneKB<<10;

    ReplyDelete
  4. The obvious simple change is:
    int oneMB = oneKB+1047552;

    which is kind of stupid but does the job (and goes inline with my previous statement that performance can be improved using hard coded precalculated values.)

    I like the following variant much better, however:
    int oneMB = oneKB<<10;

    (four changes and it still makes sense)

    ReplyDelete
  5. public class MemoryManager {
    public static void main(String[] args) {
    int oneKB = 1024; /* Никаких изменений в этой строке! */
    int oneMB = oneKB<<10;
    System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* Никаких изменений в этой строке! */
    }
    }

    ReplyDelete
  6. public static void main(String[] args) {
    int oneKB = 1024; /* No change to this line! */
    int oneMB = oneKB<<10;
    System.out.println("1 MB of memory is :" + oneMB + " bytes.");/* No change to this line! */
    }

    :)

    ReplyDelete
  7. public class MemoryManager {
    public static void main(String[] args) {
    int oneKB = 1024; /* No change to this line! */
    int oneMB = oneKB<<10;
    System.out.println("1 MB of memory is :"+oneMB+" bytes.");/* No change to this line! */
    }
    }

    ReplyDelete
  8. int oneMB = 1048576;

    ReplyDelete

Solution for this question?