Monday 23 July 2012

Java Language Fundamentals- Objective Questions -Part 3

1)


What will be the output of the program ?


public class Test
{
    public static void main(String [] args)
    {
        signed int x = 10;
        for (int y=0; y<5; y++, x--)
            System.out.print(x + ", ");
    }
}


A. 10, 9, 8, 7, 6,
B. 9, 8, 7, 6, 5,
C. Compilation fails.
D. An exception is thrown at runtime.


Answer: C


The word "signed" is not a valid modifier keyword in the Java language. All number primitives in Java are signed. Hence the Compilation will fails.


2)

What will be the output of the program?


public class Test
{
    public static void main (String[] args)
    {
        String foo = args[1];
        String bar = args[2];
        String baz = args[3];
        System.out.println("baz = " + baz); /* Line 8 */
    }
}


And the command line invocation:


> java Test red green blue
A. baz =
B. baz = null
C. baz = blue
D. Runtime-error


Answer: D

When running the program you entered 3 arguments "red", "green" and "blue". When dealing with arrays in java you must remember ALL ARRAYS IN JAVA ARE ZERO BASED therefore args[0] becomes "red", args[1] becomes "green" and args[2] becomes "blue".
When the program encounters line 8 above at runtime it looks for args[3] which has never been created therefore you get an ArrayIndexOutOfBoundsException at runtime.


3)


What will be the output of the program?


public class Test
{
    private static int[] x;
    public static void main(String[] args)
    {
        System.out.println(x[0]);
    }
}


A. 0
B. null
C. Compile Error
D. NullPointException at runtime


Answer: D

n the above code the array reference variable x  has been declared but it has not been instantiated i.e. the new statement is missing, for example:
private static int[]x = new int[5];
private static int[x] declares a static i.e. class level array.
the "new" keyword is the word that actually creates said array.


4)

Which is a valid keyword in java?


A. interface
B. string
C. Float
D. unsigned


Answer: A

interface is a valid keyword.Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java.


5)

class JSC205 {
  static int m1(int i) {return i;}     // 1
  static void m2(int i) {return i;}    // 2
  static int m3(int i) {return;}       // 3
  public static void main(String[] args) {
    System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
}}


What is the result of attempting to compile and run the program?


A. Prints 123
B. Prints 6
C. Prints 100
D. Compile-time error


Answer: D


At line 2 compile-time error because void function can't return any value.At line 3 compile-time error because m3() should return a result of type integer.


6)

Which will legally declare, construct, and initialize an array?


int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};


Answer: D

The only legal array declaration and assignment statement is Option D
Option A is wrong because it initializes an int array with String literals.
Option B is wrong because it use something other than curly braces for the initialization.
Option C is wrong because it provides initial values for only one dimension, although the declared array is a
two-dimensional array.


7)

What will be the output of the program?


public class TestDogs
{
    public static void main(String [] args)
    {
        Dog [][] theDogs = new Dog[3][];
        System.out.println(theDogs[2][0].toString());
    }
}
class Dog { }


The output of the above code is:


A. null
B. theDogs
C. Compilation fails
D. An exception is thrown at runtime


Answer: D


The second dimension of the array referenced by theDogs  has not been initialized. Attempting to access an uninitialized object element (System.out.println(theDogs[2][0].toString());) raises a NullPointerException.


8)

Which is a reserved word in the Java programming language?


A. method
B. native
C. reference
D. array


Answer: B


The word "native" is a valid keyword, used to modify a method declaration.native keyword is used to access non java methods within java.


9)

Which one is a valid declaration of a boolean?


boolean b1 = 0;
boolean b2 = 'false';
boolean b3 = false;
boolean b4 = Boolean.false();


Answer: C


A boolean can only be assigned the literal true  or false.


10)

What will be the output of the program?


public class CommandArgs
{
    public static void main(String [] args)
    {
        String s1 = args[1];
        String s2 = args[2];
        String s3 = args[3];
        String s4 = args[4];
        System.out.print(" args[2] = " + s2);
    }
}


and the command-line invocation is


> java CommandArgs 1 2 3 4
A. args[2] = 2
B. args[2] = 3
C. args[2] = null
D. An exception is thrown at runtime.


Answer: D


An exception is thrown because in the code String s4 = args[4];, the array index (the fifth element) is out of bounds. The exception thrown is the cleverly named ArrayIndexOutOfBoundsException.

No comments:

Post a Comment