Java Programming

Java Programming

Declarations and Access Control has 34 Questions
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

public
private
protected
transient
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));  
    } 
}

x = 0
x = 1
Compilation fails.
An exception is thrown at runtime.

Which statement is true?
class A 
{ 
    A( ) { } 
} 

class B extends A 
{ }


Class B'S constructor is public.
Class B'S constructor has no arguments.
Class B'S constructor includes a call to this( ).
None of these.
Which of the following code fragments inserted, will allow to compile?
public class Outer 
{ 
    public void someOuterMethod() 
    {
        //Line 5 
    } 
    public class Inner { } 
    
    public static void main(String[] argv) 
    {
        Outer ot = new Outer(); 
        //Line 10
    } 
} 



new Inner(); //At line 5
new Inner(); //At line 10
new ot.Inner(); //At line 10
new Outer.Inner(); //At line 10
What will be the output of the program?
public class Test 
{  
    public static void main(String args[])
    { 
        class Foo 
        {
            public int i = 3;
        } 
        Object o = (Object)new Foo();
        Foo foo = (Foo)o;
        System.out.println("i = " + foo.i);
    }
}

i = 3
Compilation fails.
i = 5
A ClassCastException will occur.

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


  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); }
  5. abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) { return rad * rad * 3.14; } }

1 only
2 only
3 and 5
1 and 4

which two code fragments will compile?
interface Base 
{
    boolean m1 ();
    byte m2(short s);
}
  1. interface Base2 implements Base {}
  2. abstract class Class2 extends Base
    { public boolean m1(){ return true; }}
  3. abstract class Class2 implements Base {}
  4. abstract class Class2 implements Base
    { public boolean m1(){ return (7 > 4); }}
  5. abstract class Class2 implements Base
    { protected boolean m1(){ return (5 > 7) }}

1 and 2
2 and 3
3 and 4
1 and 5
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(); 
    } 
}

Class A
Compilation fails.
An exception is thrown at line 3.
The code executes with no output.
Which two statements are true for any concrete class implementing the java.lang.Runnable interface?
  1. You can extend the Runnable interface as long as you override the public run() method.
  2. The class must contain a method called run() from which all code for that thread will be initiated.
  3. The class must contain an empty public void method named run().
  4. The class must contain a public void method named runnable().
  5. The class definition must include the words implements Threads and contain a method called run().
  6. The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.

1 and 3
2 and 4
1 and 5
2 and 6
Which three form part of correct array declarations?
  1. public int a [ ]
  2. static int [ ] a
  3. public [ ] int a
  4. private int a [3]
  5. private int [3] a [ ]
  6. public final int [ ] a

1, 3, 4
2, 4, 5
1, 2, 6
2, 5, 6
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); 
    } 
}

0
1
2
Compilation fails.
/* Missing statements ? */
public class NewTreeSet extends java.util.TreeSet
{
    public static void main(String [] args) 
    {
        java.util.TreeSet t = new java.util.TreeSet();
        t.clear();
    }
    public void clear() 
    {
        TreeMap m = new TreeMap();
        m.clear();
    }
}
which two statements, added independently at beginning of the program, allow the code to compile?
  1. No statement is required
  2. import java.util.*;
  3. import.java.util.Tree*;
  4. import java.util.TreeSet;
  5. import java.util.TreeMap;

1 only
2 and 5
3 and 4
3 and 4
What is the prototype of the default constructor?
public class Test { }


Test( )
Test(void)
public Test( )
public Test(void)
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);
    }
}

0
1
2
Compilation fails.
Which three statements are true?
  1. The default constructor initialises method variables.
  2. The default constructor has the same access as its class.
  3. The default constructor invokes the no-arg constructor of the superclass.
  4. If a class lacks a no-arg constructor, the compiler always creates a default constructor.
  5. The compiler creates a default constructor only when there are no other constructors for the class.

1, 2 and 4
2, 3 and 5
3, 4 and 5
1, 2 and 3
What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?

public
abstract
protected
default access
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);
        }
    }
}

0 1 2
1 2 3
0 1 2 3
Compilation fails

which statement is true?
package testpkg.p1;
public class ParentUtil 
{
    public int x = 420;
    protected int doStuff() { return x; }
}

package testpkg.p2;
import testpkg.p1.ParentUtil;
public class ChildUtil extends ParentUtil 
{
    public static void main(String [] args) 
    {
        new ChildUtil().callStuff();
    }
    void callStuff() 
    {
        System.out.print("this " + this.doStuff() ); /* Line 18 */
        ParentUtil p = new ParentUtil();
        System.out.print(" parent " + p.doStuff() ); /* Line 20 */
    }
}


The code compiles and runs, with output this 420 parent 420.
If line 18 is removed, the code will compile and run.
If line 20 is removed, the code will compile and run.
An exception is thrown at runtime.
Which of the following is/are legal method declarations?
  1. protected abstract void m1();
  2. static final void m1(){}
  3. synchronized public final void m1() {}
  4. private native void m1();

1 and 3
2 and 4
1 only
All of them are legal declarations.
What will be the output of the program?
class Base
{ 
    Base()
    {
        System.out.print("Base");
    }
} 
public class Alpha extends Base
{ 
    public static void main(String[] args)
    { 
        new Alpha(); /* Line 12 */
        new Base(); /* Line 13 */
    } 
}

Base
BaseBase
Compilation fails
The code runs with no output
Which cause a compiler error?

int[ ] scores = {3, 5, 7};
int [ ][ ] scores = {2,7,6}, {9,3,45};
String cats[ ] = {"Fluffy", "Spot", "Zeus"};
boolean results[ ] = new boolean [] {true, false, true};
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);
        }
    }
}

0 2 4
0 2 4 6
Compilation fails at line 2
Compilation fails at line 10
Which three are valid method signatures in an interface?
  1. private int getArea();
  2. public float getVol(float x);
  3. public void main(String [] args);
  4. public static void main(String [] args);
  5. boolean setFlag(Boolean [] test);

1 and 2
2, 3 and 5
3, 4, and 5
2 and 4
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]); 
    } 
}

It prints f2[0] = 0.0
It prints f2[0] = NaN
An error at f2 = f1; causes compile to fail.
It prints the garbage value.
You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

public
private
protected
default access
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() ); 
    } 
}

4, 4
4, 5
5, 4
Compilation fails.
What is the narrowest valid returnType for methodA in line 3?
public class ReturnIt 
{ 
    returnType methodA(byte x, double y) /* Line 3 */
    { 
        return (long)x / y * 2; 
    } 
}

int
byte
long
double
Which is valid in a class that extends class A?
class A 
{  
    protected int method1(int a, int b) 
    {
        return 0; 
    } 
}


public int method1(int a, int b) {return 0; }
private int method1(int a, int b) { return 0; }
public short method1(int a, int b) { return 0; }
static protected int method1(int a, int b) { return 0; }
Which one creates an instance of an array?

int[ ] ia = new int[15];
float fa = new float[20];
char[ ] ca = "Some String";
int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };
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 {}

1 and 4
2 and 5
3 and 6
4 and 6
Which of the following class level (nonlocal) variable declarations will not compile?

protected int a;
transient int b = 3;
public static final int c;
private synchronized int e;
Which two cause a compiler error?
  1. float[ ] f = new float(3);
  2. float f2[ ] = new float[ ];
  3. float[ ]f1 = new float[3];
  4. float f3[ ] = new float[3];
  5. float f5[ ] = {1.0f, 2.0f, 2.0f};

2, 4
3, 5
4, 5
1, 2
Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

final
static
private
protected
Which is a valid declaration within an interface?

public static short stop = 23;
protected short stop = 23;
transient short stop = 23;
final void madness(short stop);