Declarations and Access Control Questions
Practice Declarations and Access Control MCQs with answers and explanations. Page 2 of 2.
Category
Java Programming
Topic
Declarations and Access Control
Page
2 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
Java top-level class access modifiers and loop stepping: does this code compile, and if so what prints?
import java.util.*;
public class NewTreeSet2 extends NewTreeSet {
public static void main(String[] args) {
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet { // illegal: top-level classes cannot be protected
void count() {
for (int x = 0; x < 7; x++, x++) {
System.out.print(" " + x);
}
}
}
Open
View answer
Java local classes, casting through Object, and type safety at runtime: what is printed?
public class Test {
public static void main(String[] args) {
class Foo { public int i = 3; }
Object o = (Object) new Foo();
Foo foo = (Foo) o;
System.out.println("i = " + foo.i);
}
}
Open
View answer
Java array references and default initialization: determine f2[0] after assigning f2 = f1.
public class ArrayTest {
public static void main(String[] args) {
float f1[], f2[];
f1 = new float[10];
f2 = f1;
System.out.println("f2[0] = " + f2[0]);
}
}
Open
View answer
Java method-local static variables are not allowed: what happens when a method declares "static int i" locally?
public class Test {
public int aMethod() {
static int i = 0; // illegal: static not allowed in local context
i++;
return i;
}
public static void main(String args[]) {
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
Open
View answer
Java method overriding and covariant returns: can a subclass change Integer to Long in an override?
class Super {
public Integer getLength() { return new Integer(4); }
}
public class Sub extends Super {
public Long getLength() { return new Long(5); }
public static void main(String[] args) {
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(sooper.getLength().toString() + "," + sub.getLength().toString());
}
}
Open
View answer
Java constructor chaining: subclass must invoke a superclass constructor; what is the outcome here?
class Super {
public int i = 0;
public Super(String text) { i = 1; }
}
class Sub extends Super {
public Sub(String text) { i = 2; }
public static void main(String args[]) {
Sub sub = new Sub("Hello");
System.out.println(sub.i);
}
}
Open
View answer
Java inheritance and constructors — what exact output is printed?
class Base
{
Base()
{
System.out.print("Base");
}
}
public class Alpha extends Base
{
public static void main(String[] args)
{
new Alpha(); // Line 12
new Base(); // Line 13
}
}
Predict the combined standard output produced by running main().
Open
View answer
Java methods and overriding — what happens when a subclass attempts to override a final method?
class A
{
final public int GetResult(int a, int b) { return 0; }
}
class B extends A
{
public int GetResult(int a, int b) { return 1; }
}
public class Test
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.GetResult(0, 1));
}
}
Choose the correct outcome.
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.