Professional Documents
Culture Documents
This section of our 1000+ Java MCQs focuses on access control of Java Programming Language.
2. Which of these is used to access member of class before object of that class is created?
a) public
b) private
c) static
d) protected
View Answer
Answer: c
Explanation: None.
3. Which of these is used as default for a member of a class if no access specifier is used for it?
a) private
b) public
c) public, within its own package
d) protected
View Answer
Answer: a
Explanation: When we pass an argument by call-by-value a copy of argument is made into the
formal parameter of the subroutine and changes made on parameters of subroutine have no effect
on original argument, they remain the same.
4. What is the process by which we can control what parts of a program can access the members
of a class?
a) Polymorphism
b) Abstraction
c) Encapsulation
d) Recursion
View Answer
Answer: c
Explanation: None.
5. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program.
b) private members of class can only be accessed by other members of the class.
c) private members of class can be inherited by a sub class, and become protected members in
sub class.
d) protected members of a class can be inherited by a sub class, and become private members of
the sub class.
View Answer
Answer: c
Explanation: private members of a class can not be inherited by a sub class.
1. class access{
2. public int x;
3. private int y;
4. void cal(int a, int b){
5. x = a + 1;
6. y = b;
7. }
8. }
9. class access_specifier {
10. public static void main(String args[])
11. {
12. access obj = new access();
13. obj.cal(2, 3);
14. System.out.println(obj.x + " " + obj.y);
15. }
16. }
a) 3 3
b) 2 3
c) Runtime Error
d) Compilation Error
View Answer
Answer: c
Explanation: None.
output:
$ javac access_specifier.java
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field access.y is not visible
1. class access{
2. public int x;
3. private int y;
4. void cal(int a, int b){
5. x = a + 1;
6. y = b;
7. }
8. void print() {
9. system.out.println(" " + y);
10. }
11. }
12. class access_specifier {
13. public static void main(String args[])
14. {
15. access obj = new access();
16. obj.cal(2, 3);
17. System.out.println(obj.x);
18. obj.print();
19. }
20. }
a) 2 3
b) 3 3
c) Runtime Error
d) Compilation Error
View Answer
Answer: b
Explanation: None.
output:
$ javac access_specifier.java
$ java access_specifier
33
1. class static_out {
2. static int x;
3. static int y;
4. void add(int a, int b){
5. x = a + b;
6. y = x + b;
7. }
8. }
9. class static_use {
10. public static void main(String args[])
11. {
12. static_out obj1 = new static_out();
13. static_out obj2 = new static_out();
14. int a = 2;
15. obj1.add(a, a + 1);
16. obj2.add(5, a);
17. System.out.println(obj1.x + " " + obj2.y);
18. }
19. }
a) 7 7
b) 6 6
c) 7 9
d) 9 7
View Answer
Answer: c
Explanation: None.
output:
$ javac static_use.java
$ java static_use
6 6.4
9. Which of these access specifier must be used for class so that it can be inherited by another
sub class?
a) public
b) private
c) protected
d) None of the mentioned
View Answer
Answer: a
Explanation: None.
1. class test {
2. int a;
3. int b;
4. test(int i, int j) {
5. a = i;
6. b = j;
7. }
8. void meth(test o) {
9. o.a *= 2;
10. O.b /= 2;
11. }
12. }
13. class Output {
14. public static void main(String args[])
15. {
16. test obj = new test(10 , 20);
17. obj.meth(obj);
18. System.out.println(obj.a + " " + obj.b); }
19. }
a) 10 20
b) 20 10
c) 20 40
d) 40 20
View Answer
Answer: b
Explanation: class objects are always passed by reference, therefore changes done are reflected
back on original arguments. obj.meth(obj) sends object obj as parameter whose variables a & b
are multiplied and divided by 2 respectively by meth() function of class test. a & b becomes 20
& 10 respectively.
output:
$ javac Output.java
$ java Output
20 10