0% found this document useful (0 votes)
3 views

MCQs_for_Access_Modifiers

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MCQs_for_Access_Modifiers

Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

10. What will be output of the following program ?

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

a. 6, 7 b. 7, 7 c. Compile Time Error d. Run Time Error


Answer: C
11. Can you create a sub class to the following class?
class A
{
private A()
{
//First Constructor
}

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

13. What is the Output of the following Program?

package pack1;
class X
{
protected int i = 3993;

void methodOfX()
{
System.out.println(i);
}
}

public class MainClass


{
public static void main(String[] args)
{
X x = new X();

System.out.println(x.i);

x.methodOfX();
}
}

a. 3993 b. 0 c. Compile Time Error d. Run Time Error


3993 3993

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

18. What will be the output of the following code


package pack1;
public class A
{
private int methodOne(int i)
{
return ++i;
}

public int methodTwo(int i)


{
return methodOne(++i);
}
}
package pack2;
import pack1.A;
class B extends A
{
int methodOne(int i)
{
return methodTwo(++i);
}
}
public class MainClass
{
public static void main(String[] args)
{
System.out.println(new B().methodOne(101));
}
}
a. 101
b. 102
c. 103
d. 104
Answer: D
13. How many public classes a .java file can have?
a. 1
b. 2
c. 3
4. No Restriction
14. what is the error in the following code

private class A

{ private class B

{ private class C

{ }

a. class C can not be private

b. class B can not be private as it is an inner class


c. class A can not be private as it is an outer class

d. The code Compiles successfully

Answer : C

15. Predict the output of following Java program

import static java.lang.System.*;


class StaticImportDemo
{
public static void main(String args[])
{ out.println("VIT UNIVERSITY");
}
}
a. CompileTime Error

b. RunTime Error

c. VIT UNIVERSITY

d. Exception is thrown at run time

Answer: C

You might also like