Java Access Modifiers Interview MCQ Questions and Answers

Study and learn Interview MCQ Questions and Answers on Java Access Modifiers. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Access Modifiers before reading these objective questions.



1) Which are access modifiers below in Java?
A) public
B) private
C) protected
D) All the above
Answer [=]
D
Explanation:

Java provides 4 access modifiers namely public, private, protected, and default(not a keyword).

2) What is an access modifier in Java?
A) An access modifier controls the visibility of variables, constants and methods of a class
B) An access modifier can hide or show a variable or method to outside classes.
C) Access modifiers help in implementing the Encapsulation feature of Object-Oriented Programming (OOPs).
D) All the above
Answer [=]
D
3) Which is the keyword used to specify the DEFAULT access modifier in java?
A) default
B) bydefault
C) normal
D) There is no keyword
Answer [=]
D
Explanation:

Yes. If you do not specify any access modifier before a variable or method, it is considered to be declared under the default access modifer.

4) Which is the less restrictive access modifier in Java?
A) public
B) private
C) protected
D) default
Answer [=]
A
Explanation:

A variable or a method marked PUBLIC is available to all outside classes irrespective of package that a class lives in. So, PUBLIC is the least restrictive access modifier in Java.

5) Which is the most restrictive access modifier in Java?
A) public
B) private
C) protected
D) default
Answer [=]
B
Explanation:

A variable or method marked PRIVATE is accessible only within the class. Outside classes can not access these. So, PRIVATE is the most restrictive access modifier in Java.

6) Is it possible to combine more than one access modifier in Java?
A) No
B) Yes
C) -
D) -
Answer [=]
A
Explanation:

No. A method, variable or class can be marked with only one access modifier.

7) Choose the correct Java code snippet with access modifiers below?
A)
class ABC
{

}
B)
private protected class ABC
{

}
C)
protected public class ABC
{

}
D)
protected private class ABC
{

}
Answer [=]
A
Explanation:

Class ABC has a default access modifier. It is not mentioned as it does not have a keyword. You can not combine more than one access modifiers in Java.



8) Can you access a default member of a class from a class of outside current package in Java?
A) No
B) Yes
C) -
D) -
Answer [=]
A
Explanation:

No. You can not access a default member from outside the package.

9) Choose the correct statement about access modifiers in Java.
A) public, protected and default variables and methods can be accessed outside the package somehow.
B) public and protected variables and methods can be accessed outside the package somehow.
C) Only public variables and methods can be accessed outside the package somehow.
D) None of the above
Answer [=]
B
10) To access a protected variable or method of a Class outside the package, you need to ____ in Java.
A) Create an instance and call the protected variable or method
B) Create a Subclass and call the protected variable or method
C) A and B
D) None of the above
Answer [=]
B
Explanation:

Protected variables and methods must be called from inside subclasses or subclass objects only.

11) What is the output of the Java program with access modifiers?
package apartment1;

public class Class101
{
  public String colour = "BLUE";
}


package apartment2;
import apartment1;

public class Apartment2Flat102
{
  public static void main(String[] args)
  {
    Class101 flat = new Class101();
    System.out.println(flat.colour);
  }
}
A) BLUE
B) empty
C) null
D) Compiler error
Answer [=]
D
Explanation:
You should import either a specific Class or all Classes of a Java Package.
So, use
import apartment1.*; or import apartment1.Class101;

error: Class101 cannot be resolved to a type
12) What is the output of the below Java Code Snippet with access modifiers?
package package1;

public class Forest {
int area = 10;//sq. km
}


package package1;

public class SmallForest
{
  public static void main(String[] args)
  {
    Forest fr = new Forest();
    System.out.println("Area = " + fr.area);
  }
}
A) Area = 0
B) Area =10
C) No ouput
D) Compiler error
Answer [=]
B
Explanation:

The DEFAULT variables or methods of class can be called inside any class of the same Package.

13) What is the output of the Java code snippet with default access modifier?
package package1;

public class Bug {
int legs = 4;
}


package package2;
import package1.Bug;

public class BugTest extends Bug
{
  public static void main(String[] args)
  {
    Bug bug = new Bug();
    System.out.println("Bug Legs = " + bug.legs);
  }
}
A) Bug Legs = 4
B) Bug Legs = 0
C) Bug Legs = 
D) Compiler error
Answer [=]
D
Explanation:

You can not access a DEFAULT variable or method of a class from outside package in any means even through inheritance. So, the compiler throws error.

Unresolved compilation problem: The field Bug.legs is not visible
14) What is the output of the Java code snippet with PROTECTED access modifier?
package package1;

public class Parent
{
  protected void showProperty()
  {
    System.out.println("10 million");
  }
}


package package2;
import package1.*;

public class Children extends Parent
{
  void show()
  {
    showProperty();
  }
  public static void main(String[] args)
  {
    Children chil = new Children();
    chil.show();
  }
}
A) No output
B) 10 million
C) Compiler error
D) None of the above
Answer [=]
B
Explanation:

Through inheritance, you can access a PROTECTED variable or method of a class even from outside the package. Here, we accessed Parent Class of Package1 from Children Class of Package2.



15) What is the output of the Java code with PRIVATE access modifier?
package package1;
public class Factory
{
  private void run()
  {
    System.out.println("Running factory");
  }
}


package package1;
public class FactoryTest
{
  void callFactory()
  {
    Factory fac2 = new Factory();
    fac2.run();
  }
  public static void main(String[] args)
  {
    Factory fac1 = new Factory();
    fac.run();
  }
}
a
A)
Running factory
Running factory
B)
Running factory
C) Compile error
D) None of the above
Answer [=]
C
Explanation:

PRIVATE variables or methods of a Class are visible only within that Class. No other class can refer to these PRIVATE things even through inheritance. So, the compiler produces error.

Unresolved compilation problem: The method run() from the type Factory is not visible
16) In Java, a PROTECTED variable or method of a Super class can not be accessed from a STATIC method of Subclass of different PACKAGE. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
17) What is the output of the Java program with PROTECTED access modifer?
package package1;

public class Milk
{
  protected int volume = 1000; //Liters
}

package package2;
import package1.Milk;
public class RoseMilk extends Milk
{
  public static void main(String[] args)
  {
    Milk object = new Milk();
    System.out.println(object.volume);
  }
}
A) 0
B) 1000
C) null
D) Compiler error
Answer [=]
D
Explanation:

You can access PROTECTED things from within the same package in either STATIC or NON-STATIC methods. But, if you want to call PROTECTED things from outside PACKAGE, you should move the code to a non-static method.

18) What is the output of the Java code with PROTECTED access modifier?
package package1;
public class Plant
{
  protected void showHeight()
  {
    System.out.println("50 meters");
  }
}


package package2;
import package1.Plant;

public class Tree extends Plant
{
  public static void main(String[] args)
  {
    Tree tree = new Tree();
    tree.showHeight();
  }
}
A) No output
B) 50 meters
C) Compiler error
D) No output
Answer [=]
B
Explanation:

Here, a PROTECTED method of super class has been accessed using Subclass reference. So, the compiler does not produce error even though, it has been called inside STATIC method. If you use Superclass reference or object to access PROTECTED things, you will get error.