Explore topic-wise MCQs in Declarations and Access Control Finding the output.

This section includes 9 Mcqs, each offering curated multiple-choice questions to sharpen your Declarations and Access Control Finding the output knowledge and support exam preparation. Choose a topic below to get started.

1.

What will be the output of the program? 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() ); } }

A. 4, 4
B. 4, 5
C. 5, 4
D. Compilation fails.
Answer» E.
2.

What will be the output of the program? 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]); } }

A. It prints f2[0] = 0.0
B. It prints f2[0] = NaN
C. An error at f2 = f1; causes compile to fail.
D. It prints the garbage value.
Answer» B. It prints f2[0] = NaN
3.

What will be the output of the program? import java.util.*; public class NewTreeSet2 extends NewTreeSet { public static void main(String [] args) { NewTreeSet2 t = new NewTreeSet2(); t.count(); } } protected class NewTreeSet { void count() { for (int x = 0; x < 7>

A. 0 2 4
B. 0 2 4 6
C. Compilation fails at line 2
D. Compilation fails at line 10
Answer» E.
4.

What will be the output of the program? interface Count { short counter = 0; 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) /* Line 14 */ { System.out.print(" " + counter); } } }

A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. 1 2 3 4
E. Compilation fails
Answer» F.
5.

What will be the output of the program? class Super { public int i = 0; public Super(String text) /* Line 4 */ { 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); } }

A. 1
B. 2
C. 00
D. Compilation fails
Answer» E.
6.

What will be the output of the program? public class Test { public int aMethod() { static int i = 0; i++; return i; } public static void main(String args[]) { Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); } }

A. 00
B. 1
C. 2
D. Compilation fails
Answer» E.
7.

Which two of the following are legal declarations for nonnested classes and interfaces? 1.final abstract class Test {} 2.public static interface Test {} 3.final public class Test {} 4.protected abstract class Test {} 5.protected interface Test {} 6.abstract public class Test {}

A. 1 and 4
B. 2 and 5
C. 3 and 6
D. 4 and 6
Answer» D. 4 and 6
8.

What is the widest valid returnType for methodA in line 3? public class ReturnIt { returnType methodA(byte x, double y) /* Line 3 */ { return (long)x / y * 2; } }

A. int
B. byte
C. long
D. double
Answer» B. byte
9.

What will be the output of the program? 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 */ } }

A. Base
B. BaseBase
C. Compilation fails
D. The code runs with no output
Answer» C. Compilation fails