Java Code Snippets I

Java Code Snippets I

Java Code Snippets Questions & Answers

public class Test {
    public static void main(String[] args) {
        args[1] = "Day!";
        System.out.println(args[0] + " " + args[1]);
    }
}

/*
Question:
>javac Test.java
>java Test Good

What is the result after running the two following commands?

Answer:
Runtime error / JVM would create a problem during execution
*/
public class Test {
    public static void main(String[] args) {
        System.out.println("Welcome " + args[0] + "!");
    }
}

/*
Question:
>javac Test.java
>java Test "James Gosling" "Bill Joy"

What is the result?

Answer:
Welcome James Gosling!
*/
public class Test {
    public static void main(String[] args) {
        boolean b1 = 0;
        boolean b2 = 1;
        System.out.println(b1 + b2);
    }
}

/*
Question:
What is the result of compiling and executing Test class?

Answer:
Compilation Error
*/
public class Test {
    public static void main(String[] args) {
        byte b1 = (byte) (127 +21); // byte b1 = (byte)(148);
        System.out.println(b1);
    }
}

/*
Queation:
What is the result of compling and executing Test class?

Answer:
-108

How!:
    minrange + (result - maxrange - 1)
    Here this formula will be applied 
    (byte minrange=-128 & maxrange=127)
*/
public class Test {
    public static void main(String[] args) {
        char c1 = 'a';
        int i1 = c1;
        System.out.pringln(i1);
    }
}

/*
Question:
What is the result of compling and executing Test class?

Answer:
97
*/
public class Test {
    public static void main(String[] args) {
        byte b1 = 10;
        int i1 = b1;
        byte b2 = i1;
        System.out.println(b1 + i1 + b2);
    }
}

/*
Question:
What is the result of compling and executing Test class?

Answer:
Compilation Error
*/
public class Test {
    public static void main(String[] args) {
        int x = 100;
        int a = x++;
        int b = ++x;
        int c = x++;
        int d = (a < b) ? ((a < c) ? a : (b < c) ? b : c) : x;
        System.out.println(d);
    }
}

/*
Question:
What is the output of this program?

Answer:
100
*/
public class Test {
    public static void main(String[] args) {
        System.out.println(args[1]);
    }
}

/*
Question:
>javac Test.java
>java Test Good Morning Guys

What is the result?

Answer:
Morning
*/
public class Test {
    public static void main(String[] args) {
        byte ab = 10;
        byte ac = 2;
        byte result = ab * ac;
    }
}

/*
Question:
What is the result of compling and executing Test class?

Answer:
Compilation Error
*/
public class Test {
    public static void main(String[] args) {
        int x = 10, y = 15;
        if(++x < 10 & ++y > 15) {
            x++;
        }
        else {
            y++;
        }
        System.out.println(x + "____" + y);
    }
}

/*
Question:
What is the result of compling and executing Test class?

Answer:
11____17
*/
public class Test {
    public static void main(String[] args) {
        int x = 10;
        if(++x < 10 && ((x/0) > 10) ) {
            System.out.println("Hello!");
        }
        else {
            System.out.println("Hi");
        }
    }
}

/*
Question:
What is the result of compling and executing Test class?

Answer:
Hi
*/