Packages And Access Modifiers
21.What does this print?
package a;
public class Base {
private int data = 10;
public int getData() {
return data;
}
}
package b;
import a.Base;
public class Derived {
public static void main(String[] args) {
Base b = new Base();
System.out.println(b.getData());
}
}
-
A. Compile Error
-
B. 10
-
C. 0
-
D. Runtime Exception
10
10
22.Output?
public class Test {
private Test() {
System.out.println("Private Constructor");
}
public static void main(String[] args) {
Test t = new Test();
}
}
-
A. Compile Error
-
B. Private Constructor
-
C. Runtime Error
-
D. Nothing
Private Constructor
Private Constructor
23.What is printed?
public class Outer {
private class Inner {
void msg() {
System.out.println("Hello from Inner");
}
}
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
i.msg();
}
}
-
A. Compile Error
-
B. Hello from Inner
-
C. Runtime Error
-
D. Nothing
Hello from Inner
Hello from Inner
24.Output of the following?
package ex;
public class Alpha {
protected int num = 42;
}
package demo;
import ex.Alpha;
public class Beta extends Alpha {
public static void main(String[] args) {
Beta b = new Beta();
System.out.println(b.num);
}
}
-
A. 42
-
B. Compile Error
-
C. 0
-
D. Runtime Exception
42
42
25. What will this print?
public class Sample {
static {
System.out.println("Static Block");
}
public static void main(String[] args) {
System.out.println("Main Method");
}
}
-
A. Static Block Main Method
-
B. Main Method Static Block
-
C. Static Block only
-
D. Compile Error
Static Block Main Method
Static Block Main Method