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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
4) Which is the less restrictive access modifier in Java?
A) public
B) private
C) protected
D) default
Answer [=]
Answer[=]
5) Which is the most restrictive access modifier in Java?
A) public
B) private
C) protected
D) default
Answer [=]
Answer[=]
6) Is it possible to combine more than one access modifier in Java?
A) No
B) Yes
C) -
D) -
Answer [=]
Answer[=]
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 [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]


Like or Share

Show some care. Like or Subscribe. [FB]...[Youtube]

C MCQ App by ExamTray 

Android APP

Java MCQ App by ExamTray 

Android APP
ALL MCQ App by ExamTray Android APP

Ad

 

Try Some Java Books

Book Price
1. Java - The Complete Reference  Check Price
2. Core Java: An Integrated Approach, Black Book Check Price
3. Java All-in-One for Dummies Check Price
4. OCP Java SE 8: Programmer II Check Price
5. Programming with Java Check Price
6. Head First Java Check Price

We may get some affiliate commission for the above purchases.