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 public.
  2.    Declare the method with the keyword private.
  3.    Declare the method with the keyword protected.
  4.    Do not declare the method with any accessibility modifiers.
  5.    Declare the method with the keyword public and private.
 Discuss Question
Answer: Option D. -> Do not declare the method with any accessibility modifiers.
Question 12. Choose the correct statement
public class Circle{
private double radius;
public Circle(double radius){
radius = radius;
}
}
  1.    The program has a compilation error because it does not have a main method.
  2.    The program will compile, but we cannot create an object of Circle with a specified radius. The object will always have radius 0.
  3.    The program has a compilation error because we cannot assign radius to radius.
  4.    The program does not compile because Circle does not have a default constructor.
 Discuss Question
Answer: Option B. -> The program will compile, but we cannot create an object of Circle with a specified radius. The object will always have radius 0.
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.    Only (I)
  2.    (I) and (II)
  3.    (II) and (III)
  4.    Only (III)
  5.    (I), (II) and (III)
 Discuss Question
Answer: Option E. -> (I), (II) and (III)
Question 14. 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?
  1.    It will fail to compile.
  2.    Runtime error
  3.    Compiles and runs with no output.
  4.    Compiles and runs printing
 Discuss Question
Answer: Option D. -> Compiles and runs printing
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.    Compile with error
  2.    Runtime Exception
  3.    5
  4.    6
  5.    0
 Discuss Question
Answer: Option A. -> Compile with error
Question 16. 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
  2.    one two
  3.    one two 20
  4.    20
  5.    one 20
 Discuss Question
Answer: Option E. -> one 20
Question 17. 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.    static A
  4.    A
  5.    None of these
 Discuss Question
Answer: Option B. -> static block A
Question 18. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.
  1.    static
  2.    final
  3.    abstract
  4.    native
  5.    volatile
 Discuss Question
Answer: Option A. -> static
Question 19. 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.    abc
  2.    xyz
  3.    Compilation fails
  4.    Compilation clean but no output
  5.    None of these
 Discuss Question
Answer: Option A. -> abc
Question 20. 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.    5
  3.    0
  4.    Runtime Exception
  5.    None of these
 Discuss Question
Answer: Option A. -> Compile with error

Latest Videos

Latest Test Papers