Monday, 23 July 2012

Declarations and Access Control - Objective Questions -Part 1


1)

// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
  public void m1() {System.out.print("A.m1, ");}
  private void m2() {System.out.print("A.m2, ");}
  }
// Class D is declared in a file named D.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class D {
  public static void main(String[] args) {
    A a = new A();
    a.m1();  // 1
    a.m2();  // 2
    }}
What is the result of attempting to compile and run the program?

A. Prints: A.m1, A.m2,
B. Compile-time error at 1
C. Compile-time error at 2
D. None of the above

Answer: B

Private data members and member functions can be accessed only with in the class.So class D can't access the private member function,m2() of class A.

2)

// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
  public void m1() {System.out.print("A.m1, ");}
  void m2() {System.out.print("A.m2, ");}
}
// Class D is declared in a file named D.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class D {
  public static void main(String[] args) {
    A a = new A();
    a.m1();  // 1
    a.m2();  // 2
   }}

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

A. Prints: A.m1, A.m2,
B. Compile-time error at 1.
C. Compile-time error at 2.
D. None of the above

Answer: C

The data members and member functions with the 'default' access can be used only within the same package.So class D can't access the function m2() with default access declared in class A.

3)

// Class A is declared in a file named A.java.
package com.dan.chisholm;
public class A {
  public void m1() {System.out.print("A.m1, ");}
  protected void m2() {System.out.print("A.m2, ");}
  }
// Class D is declared in a file named D.java.
package com.dan.chisholm.other;
import com.dan.chisholm.A;
public class D {
  public static void main(String[] args) {
    A a = new A();
    a.m1();  // 1
    a.m2();  // 2
   }}

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

A. Prints: A.m1, A.m2,
B. Compile-time error at 1.
C. Compile-time error at 2
D. None of the above

Answer: C

Since class D does not extend class A, class D does not have access to the protected method, m2, of class A.Class D can access protected method of class A only through inheritance.

4)

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);

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

Answer: B

(2), (3), and (5). These are all valid interface method signatures.

(1), is incorrect because an interface method must be public; if it is not explicitly declared public it will
be made public implicitly. (4) is incorrect because interface methods cannot be static.

5)

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()
    {   S.o.p("this " + this.doStuff() ); /* Line 18 */
        ParentUtil p = new ParentUtil();
        S.o.p(" parent " + p.doStuff() ); /* Line 20 */
    }}

which statement is true?

A. The code compiles and runs, with output this 420 parent 420
B. If line 18 is removed, the code will compile and run.
C. If line 20 is removed, the code will compile and run.
D. An exception is thrown at runtime.

Answer: C

The ParentUtil instance p  cannot be used to access the doStuff() method. Because doStuff() has protected access, and the ChildUtil class is not in the same package as the ParentUtil class, doStuff() can be accessed only by instances of the ChildUtil class (a subclass of ParentUtil).

6)

class JSC105 {
  private static int x; protected static int y; public static int z;
  public static void main (String[] args) {System.out.println(x+y+z);}
}

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

A. Prints nothing.
B. Prints an undefined value.
C. Prints: null
D. Prints: 0

Answer: D

Member variables are initialized automatically. Type int variables are initialized to zero.
But usage of local variables without initialization will generate error.

7)

class Basics {
  int x = 1, y = 2;
  public static void main (String[] args) {System.out.println(x+y);}
}

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

A. Prints 3
B. Prints 12
C. Compile-time error
D. Run-time error

Answer: C

Static member functions can access only static data members and member functions.Hence the usage of x,y inside main() generate compile time error.

8)

class A
{     protected int method1(int a, int b)
    {        return 0;     }
}

Which is valid in a class that extends class A?

A. public int method1(int a, int b) {return 0; }
B. private int method1(int a, int b) { return 0; }
C. public short method1(int a, int b) { return 0; }
D. static protected int method1(int a, int b) { return 0; }

Answer: A

Option A is correct - because the class that extends A is just simply overriding method1.
Option B is wrong - because it can't override as there are less access privileges in the subclass method1.
Option C is wrong - because to override it, the return type needs to be an integer.
Option D is wrong - because you can't override a method and make it a class method i.e. using static.

9)

Which of the following class level (nonlocal) variable declarations will not compile?

A. protected int a;
B. transient int b = 3;
C. private synchronized int e;
D. volatile int d;

Answer: C

Option C will not compile; the synchronized modifier applies only to methods.

10)

class JSC204 {
 static int m1(short s)  {return s;}   // 1
 static int m2(float f) {return f;}    // 2
 public static void main(String[] args) {
   short s = 3; float f = 5.0f;
   System.out.print(""+m1(s)+m2(f));
}}

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

A. Prints 35.0
B. Prints 8.0
C. Compile-time error at line 1
D. Compile-time error at line 2

Answer: D

Type mismatch at Line 2.Can't convert from float to int.But conversion from short to int is possible.

No comments:

Post a Comment