Declaration And Access Control - Study Mode

[#41] Consider the following two classes declared and defined in two different packages, what can be added in class B to form what considered a correct access to class A from main() method of class B? package subPackage
public class A { }
package anotherPackage
// line 1
public class B{
public static void main(String[] args){
// line 2
}
} 1. At line1 add noting
At line2 add: new A()
2. At line 1 add: import package.*
at line 2 add : new subPackage.A()
3. At line 1 add: import subPackage.*
at line 2 add : new A()
4. At line 1 add: import subPackage.A
at line 2 add : new A()
Correct Answer

(C) 3 and 4

[#42] Determine output: public class InitDemo{
static int i = demo()
static{
System.out.print(i)
}
InitDemo(){
System.out.print("hello1")
}
public static void main(String... args){
System.out.print("Hello2")
}
static int demo(){
System.out.print("InsideDemo")
return 10
}
}
Correct Answer

(C) InsideDemo 10 Hello2

Explanation

Solution: As soon as the class are loaded static variables are initialized first. To initialize it demo must be called first then then static block executes and then main method is called.

[#43] Which statements are most accurate regarding the following classes? class A{
private int i
protected int j
}
class B extends A{
private int k
protected int m
}
Correct Answer

(B) An object of B contains data fields j, k, m.

[#44] Choose the correct statement public class Circle{
private double radius
public Circle(double radius){
radius = radius
}
}
Correct Answer

(B) The program will compile, but we cannot create an object of Circle with a specified radius. The object will always have radius 0.

[#45] You have the following code in a file called Test.java class Base{
public static void main(String[] args){
System.out.println("Hello")
}
}
public class Test extends Base{} What will happen if you try to compile and run this?
Correct Answer

(D) Compiles and runs printing

Explanation

Solution: This will compile and print "Hello". The entry point for a standalone java program is the main method of the class that is being run. The java run-time system will look for that method in class Test and find that it should have such a method. It does not matter whether it is defined in the class itself or is inherited from a parent class.