Declarations and Access Control Questions

Practice Declarations and Access Control MCQs with answers and explanations. Page 1 of 2.

Category
Java Programming
Topic
Declarations and Access Control
Page
1 / 2
Mode
Practice

Questions

Open any question to view the answer and explanation.

Java — Identify which class-level (non-local) variable declarations will not compile
Open
View answer
Java — Restrict method access to only other members of the same class
Open
View answer
Java — Which is a valid declaration within an interface?
Open
View answer
Java — Which code fragment allows successful compilation with inner class instantiation? public class Outer { public void someOuterMethod() { //Line 5 } public class Inner { } public static void main(String[] argv) { Outer ot = new Outer(); //Line 10 } }
Open
View answer
Java — Which overriding declarations are valid in a subclass of class A? class A { protected int method1(int a, int b) { return 0; } }
Open
View answer
Java — Most restrictive access modifier that still allows class members in the same package to access each other
Open
View answer
Java — Which of the following are legal method declarations?
Open
View answer
Java — Which two are legal declarations for non-nested classes and interfaces?
Open
View answer
Java — Which three are valid method signatures in an interface?
Open
View answer
Java — Which three form part of correct array declarations?
Open
View answer
In Java, which two of the following float-array declarations cause a compiler error? Choose all that apply. float[] f = new float(3); float f2[] = new float[]; float[] f1 = new float[3]; float f3[] = new float[3]; float f5[] = {1.0f, 2.0f, 2.0f};
Open
View answer
Java access control: You want a class to access members of another class in the same package. What is the most restrictive access modifier that still allows access?
Open
View answer
For the empty class below, what is the exact prototype of its compiler-provided default constructor? public class Test { }
Open
View answer
Java arrays: Which one of the following declarations causes a compiler error?
Open
View answer
Interfaces and abstract classes in Java: Given the interface below, which two class/interface fragments will compile? interface Base { boolean m1(); byte m2(short s); } Fragments: 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); } }
Open
View answer
Java access control across packages: You want subclasses in any package to have access to a superclass member. What is the most restrictive modifier that satisfies this?
Open
View answer
Which one of the following statements actually creates (allocates) a new array instance in Java?
Open
View answer
Given the method below, what is the widest valid return type for methodA on line 3? public class ReturnIt { returnType methodA(byte x, double y) // Line 3 { return (long)x / y * 2; } }
Open
View answer
Java interfaces and constants: what happens when code attempts to modify an interface field inside a loop? interface Count { short counter = 0; // implicitly public static final void countUp(); } public class TestCount implements Count { public static void main(String[] args) { TestCount t = new TestCount(); t.countUp(); } public void countUp() { for (int x = 6; x > counter; x--, ++counter) { System.out.print(" " + counter); } } }
Open
View answer
Java constructors vs. methods with the class name: will a method declared as "void A()" act as a constructor? public class A { void A() { // not a constructor due to return type System.out.println("Class A"); } public static void main(String[] args) { new A(); } }
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.