Sail E0 Webinar

MCQs

Total Questions : 51 | Page 2 of 6 pages
Question 11.


What will be the output of the program?


public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}
  1.    0
  2.    1
  3.    2
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.

Compilation failed because static was an illegal start of expression - method variables 

do not have a modifier (they are always considered local).


Question 12.


What will be the output of the program?


public class Test
{
public static void main(String args[])
{
class Foo
{
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}
  1.    i = 3
  2.    Compilation fails.
  3.    i = 5
  4.    A ClassCastException will occur.
 Discuss Question
Answer: Option A. -> i = 3


Question 13.


What will be the output of the program?


class Super
{
public int i = 0;
public Super(String text) /* Line 4 */
{
i = 1;
}
}
class Sub extends Super
{
public Sub(String text)
{
i = 2;
}
public static void main(String args[])
{
Sub sub = new Sub("Hello");
System.out.println(sub.i);
}
}
  1.    0
  2.    1
  3.    2
  4.    Compilation fails.
 Discuss Question
Answer: Option D. -> Compilation fails.


A default no-args constructor is not created because there is a constructor



supplied that has an argument, line 4. Therefore the sub-class constructor



must explicitly make a call to the super class constructor:


public Sub(String text)
{
super(text); // this must be the first line constructor
i = 2;
}


Question 14.


What will be the output of the program?


public class A
{
void A() /* Line 3 */
{
System.out.println("Class A");
}
public static void main(String[] args)
{
new A();
}
}
  1.    Class A
  2.    Compilation fails.
  3.    An exception is thrown at line 3.
  4.    The code executes with no output.
 Discuss Question
Answer: Option D. -> The code executes with no output.

Option D is correct. The specification at line 3 is for a method and not a constructor 

and this method is never called therefore there is no output. The constructor that is 

called is the default constructor.


Question 15.


What will be the output of the program?


class A
{
final public int GetResult(int a, int b) { return 0; }
}
class B extends A
{
public int GetResult(int a, int b) {return 1; }
}
public class Test
{
public static void main(String args[])
{
B b = new B();
System.out.println("x = " + b.GetResult(0, 1));
}
}
  1.    x = 0
  2.    x = 1
  3.    Compilation fails.
  4.    An exception is thrown at runtime.
 Discuss Question
Answer: Option C. -> Compilation fails.

The code doesn't compile because the method GetResult() in class A is final and

 so cannot be overridden.

Question 16.

Which is a valid declaration within an interface?


  1.    public static SHORT stop = 23;
  2.    protected short stop = 23;
  3.    transient short stop = 23;
  4.    final void madness(short stop);
 Discuss Question
Answer: Option A. -> public static SHORT stop = 23;

(A) is valid interface declarations.

(B) and (C) are incorrect because interface variables cannot be either protected or

 transient. (D) is incorrect because interface methods cannot be final or static.

Question 17.

Given a method in a protected class, what access modifier do you use to restrict

 access to that method to only the other members of the same class?


  1.    final
  2.    static
  3.    private
  4.    protected
  5.    volatile
 Discuss Question
Answer: Option C. -> private

The private access modifier limits access to members of the same class.

Option A, B, D, and E are wrong because protected are the wrong access modifiers,

 and final, static, and volatile are modifiers but not access modifiers.


Question 18.

Which two cause a compiler error?

      1. float[ ] f = new float(3);

      2. float f2[ ] = new float[ ];

      3. float[ ]f1 = new float[3];

      4. float f3[ ] = new float[3];

      5. float f5[ ] = {1.0f, 2.0f, 2.0f};



  1.    2, 4
  2.    3, 5
  3.    4, 5
  4.    1, 2
 Discuss Question
Answer: Option D. -> 1, 2

(1) causes two compiler errors ( '[' expected and illegal start of expression) because

 the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax:

float[ ] f = new float[3];

(2) causes a compiler error ( '{' expected ) because the array constructor does not

specify the number of elements in the array. The following is the correct syntax: float

 f2[ ] = new float[3];

(3), (4), and (5) compile without error.

Question 19.

Which two of the following are legal declarations for nonnested classes and interfaces?

   1. final abstract class Test {}

   2. public static interface Test {}

   3. final public class Test {}

   4. protected abstract class Test {}

   5. protected interface Test {}

   6. abstract public class Test {}



  1.    1 and 4
  2.    2 and 5
  3.    3 and 6
  4.    4 and 6
 Discuss Question
Answer: Option C. -> 3 and 6

(3), (6). Both are legal class declarations.

(1) is wrong because a class cannot be abstract and final”there would be no way to use such a class. (2) is wrong because interfaces and classes cannot be marked as static. (4) and (5) are wrong because classes and interfaces cannot be marked as protected.


Question 20.

Which of the following class level (nonlocal) variable declarations will not compile?


  1.    protected int a;
  2.    transient int b = 3;
  3.    private synchronized int e;
  4.    volatile int d;
 Discuss Question
Answer: Option C. -> private synchronized int e;

Option C will not compile; the synchronized modifier applies only to methods.

Option A and B will compile because protected and transient are legal variable

modifiers. Option D will compile because volatile is a proper variable modifier.

Latest Videos

Latest Test Papers