Java Multiple Choice Questions & Answers on Access Control for Freshers

1. Which of these access specifiers must be used for main() method?

a) private
b) public
c) protected
d) None of the mentioned
Answer: b

Explanation: main() method must be specified public as it called by Java run time system, outside of the program. If no access specifier is used then by default member is public within its own package & cannot be accessed by Java run time system.
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
Answer: c
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
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
Answer: c
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.
Answer: c

Explanation: private members of a class can not be inherited by a sub class.
6. What is the output of this program?
  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
Answer: c
7. What is the output of this program?
  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
Answer: b
8. What is the output of this program?
  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
Answer: c
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
Answer: a
10. What is the output of this program?
  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
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.

Related

Computer Fundamentals Multiple choice Questions and Answers on Network Security and Encryption for Freshers

1. The process of transforming plain text into unreadable text. a) Decryptionb) Encryptionc) Network Securityd) Information Hiding Answer: b Explanation: Encryption is the process of transforming...

Computer Fundamentals Multiple choice Questions and Answers on Network Security for Freshers

1. The field that covers a variety of computer networks, both public and private, that are used in everyday jobs. a) Artificial Intelligenceb) MLc) Network Securityd) IT Answer: c Explanation: Ne...

Computer Fundamentals Multiple choice Questions and Answers on Artificial Intelligence for Freshers

1. The technology that has the ability to interact with the world. a) AIb) MLc) IOTd) IT Answer: a Explanation: AI which is artificial intelligence is the ability to interact with the world. It i...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item