Monday 23 July 2012

Java Language Fundamentals- Objective Questions -Part 1

1)

class GFM13 {
  static byte a; static short b; static char c;
  static int d; static long e; static String s;
  public static void main(String[] args) {
    System.out.println(a+b+c+d+e+s);
}}


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


A. Prints: 00000null
B. Compile-time error
C. Prints: 0null
D. Prints: 00000


Answer:Prints: C

The numeric sum of variables a, b, c,d and e is zero.The zero is converted to String and concatenated with s.
 
2)

class M{
  static boolean a, b, c;
  public static void main (String[] args) {
 int[] a1[],a2[];    // 1
   int []a3,[]a4;    // 2
   int []a5,a6[];    // 3
   int[] a7,a8[];    // 4




}}


A compile time error is generated at which line ?


A. 1
B. 2
C. 3
D. 4


Answer: B

An array variable is declared by placing brackets after the identifier or after the type name. A compile-time error occurs at the line marked 2, because the brackets appearing before the identifier for array variable a4 are not associated with the type or the identifier


3)

class SC2
{
    public static void main(String [] args)
    {
        SC2 s = new SC2();
        s.start();
    }


    void start()
    {
        int a = 3;
        int b = 4;
        System.out.print(" " + 7 + 2 + " ");
        System.out.print(a + b);
        System.out.print(" " + a + b + " ");
        System.out.print(foo() + a + b + " ");
        System.out.println(a + b + foo());
    }


    String foo()
    {
        return "foo";
    }
}


The output of the above code is


A. 9 7 7 foo 7 7foo
B. 72 34 34 foo34 34foo
C. 9 7 7 foo34 34foo
D. 72 7 34 foo34 7foo


Answer: D


Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.


4)

class GFM12 {
static int x;
int y;


public static void main(String[] args) {
System.out.println("x=" + x);
System.out.print("y=" + y);
}
}
What is the output ?


A. x=0 y=0
B. x=0 y=garbage
C. x= y=
D. Compile-time error


Answer: D

We can't use non static instance variables inside static methods.Here usage of y within main() will create compile time error.


5)

class EBH202 {
  static boolean a, b, c;
  public static void main (String[] args) {
    boolean x = (a = true) || (b = true) && (c = true);
    System.out.print(a + "," + b + "," + c);
}}


The output of the above code is:


A. Prints: true,false,false
B. Prints: false,false,false
C. Prints: true,false,true
D. Prints: true,true,true


Answer: A

The conditional and expression is not evaluated, because the left hand operand of the conditional or expression  is true. The original expression is as follows: x = (a = true) || (b = true) && (c = true). The left hand operand of the conditional or expression is the result of the assignment expression, (a = true). Since the left hand operand of the conditional or expression is true, the right hand operand will not be evaluated.

6)

class GRC4 {public static void main(String[] args) {}} // 1
class GRC5 {public static void main(String []args) {}} // 2
class GRC6 {public static void main(String args[]) {}} // 3


What is the result of attempting to compile and run the above programs?


A. Compile-time error at line 1.
B. An attempt to run GRC5 from the command line fails.
C. Compile-time error at line 3.
D. None of the above


Answer: D


Section 12.1.4 of the Java Language Specification requires that the main method must accept a single argument that is an array of components of type String. In each of the three class declarations, the single argument is indeed an array of components of type String. Please note that the square brackets within an array declaration may appear as part of the type or part of the declarator (i.e. array name).


7)

class JJF1 {
  public static void main (String args[]) {
    System.out.print(Byte.MIN_VALUE+",");
    System.out.print(Byte.MAX_VALUE);
}}


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


A. Prints: 0,255
B. Prints: -128,127
C. Prints: -127,128
D. Prints: 0,256


Answer: B

A byte is an 8 bit signed value; so the minimum byte value is -(2^7) and the maximum value is (2^7 - 1).
 
8)

class MWC102 {
  public static void main (String[] args) {
    byte[] a = new byte[1]; long[] b = new long[1];
    float[] c = new float[1]; Object[] d = new Object[1];
    System.out.print(a[0]+","+b[0]+","+c[0]+","+d[0]);
}}


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


A. Prints: 0,0,0,null
B. Prints: 0,0,0.0,null
C. Compile-time error
D. Run-time error


Answer: B


Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null.


9)

What will be the output of the program?


class BoolArray
{   boolean [] b = new boolean[3];int count = 0;
    void set(boolean [] x, int i)
    { x[i] = true;++count;}
    public static void main(String [] args)
    {   BoolArray ba = new BoolArray();
        ba.set(ba.b, 0);ba.set(ba.b, 2);ba.test();
    }
    void test()
    {
        if ( b[0] && b[1] | b[2] ) count++;
        if ( b[1] && b[(++count - 2)] )count += 7;
        System.out.println("count = " + count);
    } }


The output of the above code is:


A. count = 0
B. count = 2
C. count = 3
D. count = 4


Answer: C


The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test.


10)


class MCZ11 {
  public static void main (String[] args) {
    char a = '\c';  // 1
    char b = '\r';  // 2
    char c = '\"';  // 3
    char d = '\b';  // 4
}}


A compile-time error is generated at which line?


A. 1
B. 2
C. 3
D. 4


Answer: A

The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors".

No comments:

Post a Comment