Sail E0 Webinar

MCQs

Total Questions : 25 | Page 2 of 3 pages
Question 11. A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?
  1.    Declare the method with the keyword protected.
  2.    Declare the method with the keyword public.
  3.    Declare the method with the keyword public and private.
  4.    Declare the method with the keyword private.
  5.    Do not declare the method with any accessibility modifiers.
 Discuss Question
Answer: Option E. -> Do not declare the method with any accessibility modifiers.
Question 12. You have the following code in a file called Test.javaclass 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?
  1.    Runtime error
  2.    Compiles and runs with no output.
  3.    Compiles and runs printing
  4.    It will fail to compile.
 Discuss Question
Answer: Option C. -> Compiles and runs printing
Question 13. Choose the correct statement. Restriction on static methods are: I.   They can only call other static methods.II.   They must only access static data.III. They cannot refer this or super in any way.
  1.    (II) and (III)
  2.    (I) and (II)
  3.    Only (I)
  4.    (I), (II) and (III)
  5.    Only (III)
 Discuss Question
Answer: Option D. -> (I), (II) and (III)
Question 14. Choose the correct statementpublic class Circle{ private double radius; public Circle(double radius){ radius = radius; }}
  1.    The program has a compilation error because we cannot assign radius to radius.
  2.    The program does not compile because Circle does not have a default constructor.
  3.    The program will compile, but we cannot create an object of Circle with a specified radius. The object will always have radius 0.
  4.    The program has a compilation error because it does not have a main method.
 Discuss Question
Answer: Option C. -> The program will compile, but we cannot create an object of Circle with a specified radius. The object will always have radius 0.
Question 15. What will be the output?public class Test{static{int a = 5;}public static void main(String args[]){new Test().call();}void call(){this.a++;System.out.print(this.a);}}
  1.    0
  2.    5
  3.    Runtime Exception
  4.    6
  5.    Compile with error
 Discuss Question
Answer: Option E. -> Compile with error
Question 16. What is the output for the below code ?public class A{ static{ System.out.println("static"); } { System.out.println("block"); } public A(){ System.out.println("A"); } public static void main(String[] args){ A a = new A(); }}
  1.    A block static
  2.    static block A
  3.    None of these
  4.    A
  5.    static A
 Discuss Question
Answer: Option B. -> static block A
Question 17. Determine Output:class MyClass{ static final int a = 20; static final void call(){ System.out.println("two"); } static{ System.out.println("one"); }}public class Test{ public static void main(String args[]){ System.out.println(MyClass.a); }}
  1.    one two 20
  2.    one
  3.    one two
  4.    20
  5.    one 20
 Discuss Question
Answer: Option E. -> one 20
Question 18. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
  1.    abstract
  2.    static
  3.    final
  4.    native
  5.    volatile
 Discuss Question
Answer: Option B. -> static
Question 19. What will be the output for the below code?public class Test{ static{ int a = 5; } public static void main(String[] args){ System.out.println(a); }}
  1.    Compile with error
  2.    Runtime Exception
  3.    5
  4.    0
  5.    None of these
 Discuss Question
Answer: Option A. -> Compile with error
Question 20. What will be the output?public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.println(value); } public static void changeValue(String a){ a = "xyz"; }}
  1.    Compilation fails
  2.    None of these
  3.    abc
  4.    Compilation clean but no output
  5.    xyz
 Discuss Question
Answer: Option C. -> abc

Latest Videos

Latest Test Papers