Sail E0 Webinar

MCQs

Total Questions : 25 | Page 1 of 3 pages
Question 1. Suppose a class has public visibility. In this class we define a protected method. Which of the following statements is correct?
  1.    This method is only accessible from inside the class itself and from inside all subclasses.
  2.    In a class, you cannot declare methods with a lower visibility than the visibility of the class in which it is defined.
  3.    From within protected methods you do not have access to public methods.
  4.    This method is accessible from within the class itself and from within all classes defined in the same package as the class itself.
 Discuss Question
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. Choose all the lines which if inserted independently instead of "//insert code here" will allow the following code to compile:
public class Test{
public static void main(String args[]){
add();
add(1);
add(1, 2);
}
// insert code here
}
  1.    void add(Integer... args){}
  2.    static void add(int... args, int y){}
  3.    static void add(int args...){}
  4.    static void add(int[]... args){}
  5.    static void add(int...args){}
 Discuss Question
Answer: Option E. -> static void add(int...args){}
Question 3. What is the result of compiling and running the following code?
class Base{
private Base(){
System.out.print("Base");
}
}
public class test extends Base{
public test(){
System.out.print("Derived");
}
public static void main(String[] args){
new test();
}
}
  1.    BaseDerived
  2.    Derived
  3.    Exception is thrown at runtime
  4.    Compilation Error
 Discuss Question
Answer: Option D. -> Compilation Error
Question 4. 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();
}
}
  1.    Compile error at line 1 (static x must be only accessed inside static methods)
  2.    Compile error at line 2 (constructors can't be static)
  3.    Compile error at line 3 (static methods can't invoke this)
  4.    Compile error at line 4 (invalid argument type for method main )
  5.    44
 Discuss Question
Answer: Option C. -> Compile error at line 3 (static methods can't invoke this)
Question 5. 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();
}
}
  1.    Compile error at line 1 (static x must be only accessed inside static methods)
  2.    Compile error at line 3 (static methods can't make reference to non-static variables)
  3.    Compile error at line 4 (invalid argument type for method main)
  4.    49
  5.    Compile error at line 2 (must access x by writing Tester.x)
 Discuss Question
Answer: Option B. -> Compile error at line 3 (static methods can't make reference to non-static variables)
Question 6. The object is created with new keyword
  1.    At Compile-time
  2.    At run-time
  3.    Depends on the code
  4.    None of these
 Discuss Question
Answer: Option B. -> At run-time
Question 7. 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();
  1.    1 and 2
  2.    2 and 4
  3.    3 and 4
  4.    1 and 3
 Discuss Question
Answer: Option C. -> 3 and 4
Question 8. 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;
}
}
  1.    Compilation error.
  2.    IllegalArgumentException is thrown at runtime.
  3.    InsideDemo 10 Hello2
  4.    Hello2 InsideDemo 10
  5.    InsideDemo 10 Hello2 hello1
 Discuss Question
Answer: Option C. -> InsideDemo 10 Hello2
Question 9. 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;
}
  1.    An object of B contains data fields i, j, k, m.
  2.    An object of B contains data fields j, k, m.
  3.    An object of B contains data fields j, m.
  4.    An object of B contains data fields k, m.
 Discuss Question
Answer: Option B. -> An object of B contains data fields j, k, m.
Question 10. A package is a collection of
  1.    Classes
  2.    Interfaces
  3.    Editing tools
  4.    Classes and Interfaces
  5.    Editing tools and Interfaces
 Discuss Question
Answer: Option D. -> Classes and Interfaces

Latest Videos

Latest Test Papers