MCQs_for_Access_Modifiers
MCQs_for_Access_Modifiers
class access
{
public int x;
private int y;
void cal(int a, int b)
{
x = a + 1;
y = b;
}
}
public class access_specifier
{
public static void main(String args[])
{
access obj = new access();
obj.cal(5,7);
System.out.println(obj.x + " " + obj.y);
}
}
private A(int i)
{
//Second Constructor
}
}
a. yes b. No c. Depends on the main() method d. None
Answer: B
12. Does field ‘i’ of Class A be inherited to Class B in the below code?
class A
{
protected int i;
} class B extends A{
}
a. Yes b. No c. Can not be determined d. None of the above
Answer : A
package pack1;
class X
{
protected int i = 3993;
void methodOfX()
{
System.out.println(i);
}
}
System.out.println(x.i);
x.methodOfX();
}
}
Answer: A
14. Why we can’t instantiate Class-A in the below code outside the package even though
it has public constructor?
package pack1;
class A
{
public A()
{
//public constructor
}
}
package pack2;
import pack1.*;
class B
{
Public static void main()
{
A a = new A();
}
}
a. Both A and B classes are in different packages
b. Class B is not inherited from class A
c. Class A has been defined as default access modifier
d. Class B is not public
Answer C
XX. A method within a class is only accessible by classes that are defined within the
same package as the class of the method. Which one of the following is used to enforce
such restriction?
a. declare the method with the keyword public
b. declare the method with the keyword private
c. declare the method with the keyword protected
d. do not declare the method with any of the access modifiers
Answer: C
XX. Choose the correct statement
public class Circle{
private double radius;
public Circle(double radius)
{
Radius=radius;
}
}
a. The program will have a compilation error because it does not have a main method
b. the program will compile but we can not create an object of Circle with a specified
radius. The object will always have radius 0.
c. the program has a compilation error because we can not assign radius to radius
d. the program does not compile because Circle does not have default Constructor
Answer: B
15. 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
9. Choose the correct statement.
Restrictions on static methods are
I. They can only call other static methods
II. They must only access static data.
III. They can not refer this or super in anyway
a. only {I}
b. {I} and {II}
c. {II} and (III)
d. (I) (II) and (III)
Answer : D
16. What is the result of Compiling and Running the Following Code
class Base{
private Base(){
System.out.println(“Base”);
}
}
public class test extends base{
public test(){
System.out.println(“Derived”);
}
public static void main(String args[]){
New test();
}
}
a. BaseDerived
b. Derived
c. Exception is thrown at run time
d. Compilation Error
Answer: D
17. What will be the output of the following program?
Public class Test{
Public static void main(String args[]) {
Int x=10;
X=myMethod(x--);
System.out.printlln(x);
}
Static int myMethod(final int x){
return x--;
}
}
a. the programs compiles successfully and displays 9 as the output
b. the program will lead to compilation error
c. the program will lead to runtime error
d. the program runs successfully and displays 10 as output
Answer: B
private class A
{ private class B
{ private class C
{ }
Answer : C
b. RunTime Error
c. VIT UNIVERSITY
Answer: C