Java Programming - Declarations and Access Control
Why should I learn to solve Java Programming questions and answers section on "Declarations and Access Control"?
Learn and practise solving Java Programming questions and answers section on "Declarations and Access Control" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.
Where can I get the Java Programming questions and answers section on "Declarations and Access Control"?
IndiaBIX provides you with numerous Java Programming questions and answers based on "Declarations and Access Control" along with fully solved examples and detailed explanations that will be easy to understand.
Where can I get the Java Programming section on "Declarations and Access Control" MCQ-type interview questions and answers (objective type, multiple choice)?
Here you can find multiple-choice Java Programming questions and answers based on "Declarations and Access Control" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.
How do I download the Java Programming questions and answers section on "Declarations and Access Control" in PDF format?
You can download the Java Programming quiz questions and answers section on "Declarations and Access Control" as PDF files or eBooks.
How do I solve Java Programming quiz problems based on "Declarations and Access Control"?
You can easily solve Java Programming quiz problems based on "Declarations and Access Control" by practising the given exercises, including shortcuts and tricks.
- Declarations and Access Control - General Questions
- Declarations and Access Control - Finding the output
- Declarations and Access Control - Pointing out the correct statements
Access modifiers dictate which classes, not which instances, may access features.
Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way.
private makes a member accessible only from within its own class
protected makes a member accessible only to classes in the same package or subclass of the class
default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.
public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible)
final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised
abstract declares a method that has not been implemented.
transient indicates that a variable is not part of the persistent state of an object.
volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable.
After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C - protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.
public class Outer
{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }
public static void main(String[] argv)
{
Outer ot = new Outer();
//Line 10
}
}
Which of the following code fragments inserted, will allow to compile?
Option A compiles without problem.
Option B gives error - non-static variable cannot be referenced from a static context.
Option C package ot does not exist.
Option D gives error - non-static variable cannot be referenced from a static context.
interface Base
{
boolean m1 ();
byte m2(short s);
}
which two code fragments will compile?
- interface Base2 implements Base {}
-
abstract class Class2 extends Base
{ public boolean m1(){ return true; }} - abstract class Class2 implements Base {}
- abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }} - abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}
(3) is correct because an abstract class doesn't have to implement any or all of its interface's methods. (4) is correct because the method is correctly implemented ((7 > 4) is a boolean).
(1) is incorrect because interfaces don't implement anything. (2) is incorrect because classes don't extend interfaces. (5) is incorrect because interface methods are implicitly public, so the methods being implemented must be public.
- public int a [ ]
- static int [ ] a
- public [ ] int a
- private int a [3]
- private int [3] a [ ]
- public final int [ ] a
(1), (2) and (6) are valid array declarations.
Option (3) is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a
Option (4) is not a correct array declaration. The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a []
Option (5) is not a correct array declaration. The compiler complains with 2 errors:
']' expected. A closing bracket is expected in place of the 3 and
<identifier> expected A variable name is expected after a[ ] .
public class Test { }
What is the prototype of the default constructor?Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).
Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.