1. Question: 
    public class MyRunnable implements Runnable 
    {
        public void run() 
        {
            // some code here
        }
    }
    which of these will create and start this thread?

    A
    new Runnable(MyRunnable).start();

    B
    new Thread(MyRunnable).run();

    C
    new Thread(new MyRunnable()).start();

    D
    new MyRunnable().start();

    Note: Because the class implements Runnable, an instance of it has to be passed to the Thread constructor, and then the instance of the Thread has to be started. A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor. B is incorrect for the same reason; you can't pass a class or interface name to any constructor. D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.
    1. Report
  2. Question: What is the value of "d" after this line of code has been executed? double d = Math.round ( 2.5 + Math.random() );

    A
    2

    B
    3

    C
    4

    D
    2.5

    Note: The Math.random() method returns a number greater than or equal to 0 and less than 1 . Since we can then be sure that the sum of that number and 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that Math.round() will round that number to 3. So Option B is the answer.
    1. Report
  3. Question: Which of the following would compile without error?

    A
    int a = Math.abs(-5);

    B
    int b = Math.abs(5.0);

    C
    int c = Math.abs(5.5F);

    D
    int d = Math.abs(5L);

    Note: The return value of the Math.abs() method is always the same as the type of the parameter passed into that method. In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a". The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".
    1. Report
  4. Question: Which of the following are valid calls to Math.max? Math.max(1,4) Math.max(2.3, 5) Math.max(1, 3, 5, 7) Math.max(-1.5, -2.8f)

    A
    1, 2 and 4

    B
    2, 3 and 4

    C
    1, 2 and 3

    D
    3 and 4

    Note: (1), (2), and (4) are correct. The max() method is overloaded to take two arguments of type int, long, float, or double. (3) is incorrect because the max() method only takes two arguments.
    1. Report
  5. Question: 
    public class Myfile 
    { 
        public static void main (String[] args) 
        {
            String biz = args[1]; 
            String baz = args[2]; 
            String rip = args[3]; 
            System.out.println("Arg is " + rip); 
        } 
    }
    Select how you would start the program to cause it to print: Arg is 2

    A
    java Myfile 222

    B
    java Myfile 1 2 2 3 4

    C
    java Myfile 1 3 2 2

    D
    java Myfile 0 1 2 3

    Note: Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output.
    1. Report
  6. Question: What will be the output of the program?
    class PassA 
    {
        public static void main(String [] args) 
        {
            PassA p = new PassA();
            p.start();
        }
    
        void start() 
        {
            long [] a1 = {3,4,5};
            long [] a2 = fix(a1);
            System.out.print(a1[0] + a1[1] + a1[2] + " ");
            System.out.println(a2[0] + a2[1] + a2[2]);
        }
    
        long [] fix(long [] a3) 
        {
            a3[1] = 7;
            return a3;
        }
    }

    A
    12 15

    B
    15 15

    C
    3 4 5 3 7 5

    D
    3 7 5 3 7 5

    Note: The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object. So Output: 3+7+5+" "3+7+5 Output: 15 15 Because Numeric values will be added
    1. Report
  7. Question: What will be the output of the program?
    class Test 
    {
        public static void main(String [] args) 
        {
            Test p = new Test();
            p.start();
        }
    
        void start() 
        {
            boolean b1 = false;
            boolean b2 = fix(b1);
            System.out.println(b1 + " " + b2);
        }
    
        boolean fix(boolean b1) 
        {
            b1 = true;
            return b1;
        }
    }

    A
    true true

    B
    false true

    C
    true false

    D
    false false

    Note: The boolean b1 in the fix() method is a different boolean than the b1 in the start() method. The b1 in the start() method is not updated by the fix() method.
    1. Report
  8. Question: What will be the output of the program?
    class PassS 
    {
        public static void main(String [] args) 
        {
            PassS p = new PassS();
            p.start();
        }
    
        void start() 
        {
            String s1 = "slip";
            String s2 = fix(s1);
            System.out.println(s1 + " " + s2);
        }
    
        String fix(String s1) 
        {
            s1 = s1 + "stream";
            System.out.print(s1 + " ");
            return "stream";
        }
    }

    A
    slip stream

    B
    slipstream stream

    C
    stream slip stream

    D
    slipstream slip stream

    Note: When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".
    1. Report
  9. Question: What will be the output of the program?
    class BitShift 
    {
        public static void main(String [] args) 
        {
            int x = 0x80000000;
            System.out.print(x + " and  ");
            x = x >>> 31;
            System.out.println(x);
        }
    }

    A
    -2147483648 and 1

    B
    0x80000000 and 0x00000001

    C
    -2147483648 and -1

    D
    1 and -2147483648

    Note: Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this: Before: 1000 0000 0000 0000 0000 0000 0000 0000 After: 0000 0000 0000 0000 0000 0000 0000 0001 Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown. Option B is incorrect because the output method print() always displays integers in base 10. Option D is incorrect because this is the reverse order of the two output numbers.
    1. Report
  10. Question: What will be the output of the program?
    class Equals 
    {
        public static void main(String [] args) 
        {
            int x = 100;
            double y = 100.1;
            boolean b = (x = y); /* Line 7 */
            System.out.println(b);
        }
    }

    A
    true

    B
    false

    C
    Compilation fails

    D
    An exception is thrown at runtime

    Note: The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables. Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.
    1. Report
Copyright © 2025. Powered by Intellect Software Ltd