Monday 23 July 2012

Java Language Fundamentals- Objective Questions -Part 4

1)

Which three are legal array declarations?


   1. int [] myScores [];
   2. char [] myChars;
   3. int [6] myScores;
   4. Dog myDogs [];
   5. Dog myDogs [7];


A. 1, 2, 4
B. 2, 4, 5
C. 2, 3, 4
D. All are correct.


Answer: A

(1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a multidimensional array, and place them on both sides of the identifier.(3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the array is actually instantiated .


2)

Which is the valid declarations within an interface definition?


A. public double methoda();
B. public final double methoda();
C. static void methoda(double d1);
D. protected void methoda(double d1);


Answer: A


Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract.Option C is wrong. static is concerned with the class and not an instance.


3)

Which is a valid declarations of a String?


A. String s1 = null;
B. String s2 = 'null';
C. String s3 = (String) 'abc';
D. String s4 = (String) '\ufeed';


Answer: A


Option A sets the String reference to null.
Option B is wrong because null cannot be in single quotes.
Option C is wrong because there are multiple characters between the single quotes ('abc').
Option D is wrong because you can't cast a char (primitive) to a String (object).


4)


What will be the output of the program?


public class CommandArgsThree
{ public static void main(String [] args)
    {  String [][] argCopy = new String[2][2];
        int x;
        argCopy[0] = args;
        x = argCopy[0].length;
        for (int y = 0; y < x; y++)
        {
            System.out.print(" " + argCopy[0][y]);
        }  }}
and the command-line invocation is


> java CommandArgsThree 1 2 3
A. 0 0
B. 1 2
C. 0 0 0
D. 1 2 3


Answer: D


In argCopy[0] = args;, the reference variable argCopy[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.


5)

Which one of the following will declare an array and initialize it with five numbers?


A. Array a = new Array(5);
B. int [] a = {23,22,21,20,19};
C. int a [] = new int(5);
D. int [5] array;


Answer: B


Option B is the legal way to declare and initialize an array with five elements.Option A is wrong because it shows an example of instantiating a class named Array, passing the integer value 5 to the object's constructor.Option C is wrong because it shows a legal array declaration, but with no initialization.Option D is wrong (and will not compile) because it declares an array with a size. Arrays must never be given a size when declared.


6)


In the given program, how many lines of output will be produced?


public class Test
{
    public static void main(String [] args)
    {
    int [] [] [] x = new int [3] [] [];
    int i, j;
    x[0] = new int[4][];
    x[1] = new int[2][];
    x[2] = new int[5][];
    for (i = 0; i < x.length; i++)
    {
        for (j = 0; j < x[i].length; j++)
        {
            x[i][j] = new int [i + j + 1];
            System.out.println("size = " + x[i][j].length);
        }
    }
    }
}


The output of the above code is:


A. 7
B. 9
C. 11
D. 13


Answer: C


The loops use the array sizes (length).
It produces 11 lines of output as given below.
D:\Java>javac Test.java
D:\Java>java Test
size = 1
size = 2
  .
  .
size = 6
size = 7


Therefore, 11 is the answer.


7)

What will be the output of the program?


public class X
{
    public static void main(String [] args)
    {
        String names [] = new String[5];
        for (int x=0; x < args.length; x++)
            names[x] = args[x];
        System.out.println(names[2]);
    }
}


and the command line invocation is


> java X a b
A. names
B. null
C. Compilation fails
D. An exception is thrown at runtime


Answer: B


The names array is initialized with five null elements. Then elements 0 and 1 are assigned the String  values "a" and "b" respectively (the command-line arguments passed to main). Elements of names array 2, 3, and 4 remain unassigned, so they have a value of null.


8)


What will be the output of the program?


public class CommandArgsTwo
{
    public static void main(String [] argh)
    {
        String [] args;
        int x;
        x = argh.length;
        for (int y = 1; y <= x; y++)
        {
            System.out.print(" " + argh[y]);
        }
    }
}


and the command-line invocation is


> java CommandArgsTwo 1 2 3
A. 0 1 2
B. 1 2 3
C. 0 0 0
D. An exception is thrown at runtime


Answer: D


An exception is thrown because at some point in (System.out.print(" " + argh[y]);), the value of x will be equal to y, resulting in an attempt to access an index out of bounds for the array. Remember that you can access only as far as length - 1, so loop logical tests should use x < someArray.length as opposed to x < = someArray.length.


9)

/* Missing Statement ? */
public class foo
{
    public static void main(String[]args)throws Exception
    {
        java.io.PrintWriter out = new java.io.PrintWriter();
        new java.io.OutputStreamWriter(System.out,true);
        out.println("Hello");
    }
}


What line of code should replace the missing statement to make this program compile?


A. No statement required.
B. import java.io.*;
C. include java.io.*;
D. import java.io.PrintWriter;


Answer: A


The usual method for using/importing the java packages/classes is by using an import statement at the top of your code. However it is possible to explicitly import the specific class that you want to use as you use it which is shown in the code above. The disadvantage of this however is that every time you create a new object you will have to use the class path in the case "java.io" then the class name in the long run leading to a lot more typing.


10)

What will be the output of the program?


public class Test
{
    private static float[] f = new float[2];
    public static void main (String[] args)
    {
        System.out.println("f[0] = " + f[0]);
    }
}


The output of the above code:


A. f[0] = 0
B. f[0] = 0.0
C. Compile Error
D. Runtime Error


Answer:B


The choices are between Option A and B, what this question is really testing is your knowledge of default values of an initialized array. This is an array type float i.e. it is a type that uses decimal point numbers therefore its initial value will be 0.0 and not 0

No comments:

Post a Comment