Declaration And Access Control(Java Program ) Questions and Answers
Explanation:-
Answer: Option D. -> This method is accessible from within the class itself and from within all classes defined in the same package as the class itself.
Question 2. What is the result of compiling and running the following code?
public class Tester{
static int x = 4;
int y = 9;
public Tester(){
System.out.print(this.x); // line 1
printVariables();
}
public static void printVariables(){
System.out.print(x); // line 2
System.out.print(y); // line 3
}
public static void main(String... args) { // line 4
new Tester();
}
}
public class Tester{
static int x = 4;
int y = 9;
public Tester(){
System.out.print(this.x); // line 1
printVariables();
}
public static void printVariables(){
System.out.print(x); // line 2
System.out.print(y); // line 3
}
public static void main(String... args) { // line 4
new Tester();
}
}
Explanation:-
Answer: Option B. -> Compile error at line 3 (static methods can't make reference to non-static variables)static methods cannot make references to non-static variables.
Explanation:-
Answer: Option E. -> static void add(int...args){}var-args = variable number of arguments = 0 or many
void add(Integer... args){} is correct IF made "static" as it's called from a static context: main().
Var-args can be of both object(eg Integer) and primitive(eg int) types.
static void add(int... args, int y){} is correct IF its parameters' order is reversed.If a method has both var-arg(0 or MAX 1) + non-var-args(0 or more) parameters then the var-arg parameter MUST come LAST!
static void add(int args...){} : "..." must come after the type of the var-arg parameter, not after its name
static void add(int[]... args){} : for this to be a correct declaration then add() should have been called something like this: "add(arr);" or "add(arr, arr);" where "arr" could be defined as "int[] arr = new int[5];"
static void add(int...args){} is a valid way to define var-args (there is no need to have any space between "..." and the type and name of the var-arg param)
Explanation:-
Answer: Option D. -> Compilation ErrorImplicit super constructor Base is not visible, must explicitly invoke another constructor.
Question 5. What is the result of compiling and running the following code?
public class Tester{
static int x = 4;
public Tester(){
System.out.print(this.x); // line 1
Tester();
}
public static void Tester(){ // line 2
System.out.print(this.x); // line 3
}
public static void main(String... args){ // line 4
new Tester();
}
}
public class Tester{
static int x = 4;
public Tester(){
System.out.print(this.x); // line 1
Tester();
}
public static void Tester(){ // line 2
System.out.print(this.x); // line 3
}
public static void main(String... args){ // line 4
new Tester();
}
}
Explanation:-
Answer: Option C. -> Compile error at line 3 (static methods can't invoke this)static methods can't invoke 'this'.
Explanation:-
Answer: Option C. -> InsideDemo 10 Hello2As 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.
Question 10. 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();
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();