Packages And Access Modifiers
16.What will be the output?
package pkg;
class MyClass {
void greet() {
System.out.println("Hello");
}
}
public class Main {
public static void main(String[] args) {
MyClass m = new MyClass();
m.greet();
}
}
-
A. Hello
-
B. Compile Error: MyClass is not public
-
C. Runtime Error
-
D. No output
Hello
Hello
17.What is the output?
package one;
public class A {
private void show() {
System.out.println("Private A");
}
public static void main(String[] args) {
A a = new A();
a.show();
}
}
-
A. Private A
-
B. Compile Error
-
C. Runtime Exception
-
D. No output
Private A
Private A
18.What happens when this code runs?
package one;
class X {
protected void test() {
System.out.println("X");
}
}
package two;
import one.X;
public class Y {
public static void main(String[] args) {
X obj = new X();
obj.test();
}
}
-
A. X
-
B. Compile Error
-
C. Runtime Error
-
D. Nothing
Compile Error
Compile Error
19.Output of the code?
package mypack;
public class Example {
public static void main(String[] args) {
System.out.println("From Example");
}
}
Run from terminal:
> javac Example.java
> java Example
-
A. From Example
-
B. Compile Error
-
C. ClassNotFoundException
-
D. Runtime Error
ClassNotFoundException
ClassNotFoundException
20.Result of execution?
class A {
protected static void show() {
System.out.println("Protected Static");
}
}
public class Main extends A {
public static void main(String[] args) {
show();
}
}
-
A. Compile Error
-
B. Protected Static
-
C. Nothing
-
D. Runtime Error
Protected Static
Protected Static