Monday 23 July 2012

Declarations and Access Control - Objective Questions -Part 3

1)

Which two of the following are legal declarations for nonnested classes and interfaces?

   1. final abstract class Test {}
   2. public static interface Test {}
   3. final public class Test {}
   4. protected abstract class Test {}
   5. protected interface Test {}
   6. abstract public class Test {}

A. 1 and 4
B. 2 and 5
C. 3 and 6
D. 4 and 6

Answer: C

(1) is wrong because a class cannot be abstract  and final—there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.

2)

 What will be the output of the program?

class Super
{   public int i = 0;
    public Super(String text) /* Line 4 */
    { i = 1; }
}
class Sub extends Super
{
    public Sub(String text)
    { i = 2; }
    public static void main(String args[])
    {   Sub sub = new Sub("Hello");
        System.out.println(sub.i);
    }
}

A. 0
B. 1
C. 2
D. compile-time error

Answer: D

A default no-args constructor is not created because there is a constructor supplied that has an argument, line 4. Therefore the sub-class constructor must explicitly make a call to the super class constructor:

public Sub(String text)
{
    super(text); // this must be the first line constructor
    i = 2;
}

3)

What will be the output of the program?

class A
{
    final public int methodl(int a, int b) { return 0; }
}
class B extends A
{
    public int method1 (int a, int b) {return 1; }
}
public class Test
{
    public static void main(String args[])
    {
        B b;
        System.out.println("x = " + b.method1(0, 1));
    }
}

A. x =0
B. x=1
C. Compilation-error
D. Run-time error

Answer: C

The code doesn't compile because the method in class A is final and so cannot be overridden.

4)

What will be the output of the program?

interface Count
{   short counter = 0;
    void countUp();
}
public class TestCount implements Count
{
    public static void main(String [] args)
    {
        TestCount t = new TestCount();
        t.countUp();
    }
    public void countUp()
    {
        for (int x = 6; x>counter; x--, ++counter) /* Line 14 */
        {
            System.out.print(" " + counter);
        }
    }
}

A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. compile-time error

Answer: D

The code will not compile because the variable counter is an interface variable that is by default final
static. The compiler will complain at line 14 when the code attempts to increment counter.

5)

What will be the output of the program?

import java.util.*;
public class NewTreeSet2 extends NewTreeSet
{
    public static void main(String [] args)
    {
        NewTreeSet2 t = new NewTreeSet2();
        t.count();
    }
}
protected class NewTreeSet
{
    void count()
    {
        for (int x = 0; x < 7; x++,x++ )
        {
            System.out.print(" " + x);
        }
    }
}

A. 0 2 4
B. 0 2 4 6
C. compile-time error
D. run-time error

Answer: C

Nonnested classes cannot be marked protected ie only public or default access is possible.

6)

What will be the output of the program?

class Super
{   public Integer getLength()
    { return new Integer(4); }
}
public class Sub extends Super
{   public Long getLength()
    { return new Long(5); }
    public static void main(String[] args)
    {   Super sooper = new Super();
        Sub sub = new Sub();
        System.out.println(
        sooper.getLength().toString() + "," + sub.getLength().toString() );
    }
}

A. 4, 4
B. 4, 5
C. 5, 4
D. compile-time error

Answer: D

Option D is correct, compilation fails - The return type of getLength( ) in the super class is an object of
reference type Integer and the return type in the sub class is an object of reference type Long. In other
words, it is not an override because of the change in the return type and it is also not an overload because the argument list has not changed.

7)

interface DoMath {double getArea(int rad); }
interface MathPlus { double getVol(int b, int h); }
which two code fragments inserted at end of the program, will allow to compile?

   1. class AllMath extends DoMath { double getArea(int r); }
   2. interface AllMath implements MathPlus { double getVol(int x, int y); }
   3. interface AllMath extends DoMath { float getAvg(int h, int l); }
   4. class AllMath implements MathPlus { double getArea(int rad); }

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

Answer: C

One class can 'implements' one or more interfaces.One interface can 'extends' one or more interfaces.If a class implements an interface ,either it should implement all the functions in the interface or declare the class itself as abstract.

8)

What will be the output of the program?

public class A
{
    void A() /* Line 3 */
    {
        System.out.println("Class A");
    }
    public static void main(String[] args)
    {
        new A();
    }
}

A. Class A
B. compile-time error
C. run-time error
D. The code executes with no output

Answer: D

Option D is correct. The specification at line 3 is for a method and not a constructor and this method is never called therefore there is no output. The constructor that is called is the default constructor.

9)

What will be the output of the program?

public class Test
{
    public int aMethod()
    {
        static int i = 0;
        i++;
        return i;
    }
    public static void main(String args[])
    {
        Test test = new Test();
        test.aMethod();
        int j = test.aMethod();
        System.out.println(j);
    }
}

A. 0
B. 1
C. 2
D. compile-time error

Answer: D

Compilation failed because static was an illegal start of expression - method variables do not have a modifier (they are always considered local).

10)

What will be the output of the program?

public class ArrayTest
{    public static void main(String[ ] args)
    {   float f1[ ], f2[ ];
        f1 = new float[10];
        f2 = f1;
        System.out.println("f2[0] = " + f2[0]);
    }
}

A. It prints f2[0] = 0.0
B. It prints f2[0] = NaN
C. An error at f2 = f1; causes compile to fail.
D. It prints the garbage value.

Answer: A

Option A is correct. When you create an array (f1 = new float[10];) the elements are initialises to the default values for the primitive data type (float in this case - 0.0).The   statement f2 = f1; copies the reference (pointer/memory address) of f1  into f2 so now f2 points at the array pointed to by f1.

No comments:

Post a Comment