OBJECT ORIENTED PROGRAMMING MCQ's SET-4





1. Which among the following best describes the protected specifier?

 a) Members are most secure and can’t be used outside class 

b) Members are secure but can be used outside the class 

c) Members are secure as private, but can be inherited 

d) Members are secure like private, but can’t be inherited 

 Answer: c 

2. If a constructor is defined in protected access, then? 

a) It’s instance can be created inside the subclasses 

b) It’s instance can be created anywhere in the program 

c) It’s instance can be created inside the subclasses and main() function 

d) It’s instance can be created inside the parent class only 

Answer: a 


3. For the following code, choose the correct option.

 class A {

         int marks; 

                protected : A() { 

                            marks=100; } 

                public : A( int x) { marks=x; } }; 

a) The instances can be created only in subclasses 

b) The instances can be created only in main() function 

c) The instances can be created only in parent class 

d) The instances can be created anywhere in the program 

 Answer: d 

 

4. If the protected members are to be made accessible only to the nearest subclass and no further subclasses, which access specifier should be used in inheritance? 

a) The sub class should inherit the parent class privately 

b) The sub class should inherit the parent class as protected  

c) The sub class should inherit the parent class as public 

d) The sub class can use any access modifier 

 Answer: a 


5. What will be the output of the following code (all header files and required things are included)?

class A { 

    int marks; 

    protected : A(int x) { marks=x; } 

    public : A() { marks=100; }

             } 

class B { 

            A a; A b=100; 

            }; 

    main() { A a, b=100; B c; } 

a) Program runs fine 

b) Program gives runtime error 

c) Program gives compile time error 

d) Program gives logical error 

 Answer: c 

 

6. Which among the following is true for the given code below? 

class A { protected : int marks; public : A() { marks=100; } disp() { cout<<”marks=”<<marks;  } }; class B: protected A { }; B b; b.disp(); 

a) Object b can’t access disp() function 

b) Object b can access disp() function inside its body 

c) Object b can’t access members of class A 

d) Program runs fine 

Answer: a 


7. Protected members differ from default members as _______ 

a) Protected members can be accessed outside package using inheritance, but default can’t 

b) Default members can be accessed outside package using inheritance, but protected can’t 

c) Protected members are allowed for inheritance but Default members are not allowed 

d) Both are same 

 Answer: a 


8. If all the members are defined in protected specifier then? (Constructors not considered) 

a) Instance of class can’t be created 

b) Instance of class can be created anywhere 

c) Instance of class can be created only in subclasses 

d) Instance of class can be created only in main() function 

 Answer: b 


9. Which among the following is correct for the code given? 

class A { 

     private: int marks; 

     A() { } 

     Public : disp() 

         { cout<< marks; }

         }; 

class B: public A {  } B b; 

a) Instance of B will not be created 

b) Instance of B will be created 

c) Program gives compile time error 

d) Program gives runtime error 

 Answer: a 


10. If protected inheritance is used then _____ 

a) Public members become public in subclass 

b) Protected members become public in subclass 

c) Protected members become protected in subclass 

d) Protected and Public members become protected in subclass 

 Answer: d 

11. If protected members are to be accessed from outside the class then__________ 

a) Members must be inherited publicly in subclass 

b) Members must accessed using class pointers 

c) Members must be accessed as usual 

d) Members must be made public 

 Answer: d 


12. Which among the following can use protected access specifier? 

a) Members which may be used in other packages 

b) Members which have to be secure and should be used by other packages/subclass 

c) Members which have to be accessed from anywhere in the program 

d) Members which have to be as secure as private but can be used by main() function 

Answer: b 

 

13. Protected access is same as default access that is given implicitly in java if no specifier is mentioned. 

a) True  

b) False 

 Answer: b 


14. If a class have default constructor defined in private access, and one parameter constructor in protected mode, how will it be possible to create instance of object? 

a) Define a constructor in public access with different signature 

b) Directly create the object in the subclass 

c) Directly create the object in main() function 

d) Not possible 

 Answer: a


15. What will be the output of the program given below? 

class A { 

    Public : A(int a=0) 

            { cout<<”new A”; } 

               }; 

                A a; A b; A c; 

a) new A new A new A 

b) newAnewAnewA 

c) new Anew Anew A 

d) new A new Anew A 

Answer: c 


16. If the members have to be accessed from anywhere in the program and other packages also, which access specifier should be used?

 a) Public 

b) Private 

c) Protected 

d) Default 

 Answer: a 

 

17. Which among the following have least security according to the access permissions allowed? 

a) Private 

b) Default 

c) Protected 

d) Public 

 Answer: d 


18. Which among the following can be used for outermost class access specifier in java? 

a) Private 

b) Public 

c) Default 

d) Default or Public 

 Answer: d


19.  We can reduce the visibility of inherited methods. 

a) True 

b) False 

 Answer: b


20. If members of a super class are public, then________

a) All those will be available in subclasses 

b) None of those will be available in subclasses 

c) Only data members will be available in subclass 

d) Only member functions will be available in subclass 

 Answer: a 










Popular Posts