Methods and Constructors

Quiz Guidelines

 

Multiple Choice Question

QID: 
454

Consider the following code:

public abstract class TestClass{
public abstract void m1();
public abstract void m2(){
System.out.println("hello");
}
}

Which of the following corrections can be applied to the above code (independently) so that it compiles without any error?

Select 2 options

A. Replace the method body of m2() with a ; (semi-colon).
B. Replace the ; at the end of m1() with a method body.
C. Remove abstract from m2().
D. Remove abstract from the class declaration.

Multiple Choice Question

QID: 
453

Which line of code will not be acceptable to the compiler?

public class XBox{
  volatile int root = 20; //1
  private XBox() //2
  {
    volatile int i = 30; //3
  }
  private void XBox() //4
  {
    int local = 30;
  }
}

Select 1 option

A. 1
B. 2
C. 3 
D. 4 
E. The code will compile fine.

Multiple Choice Question

QID: 
452

Which of the statements regarding the following code are correct?

public class TestClass{
static int a;
int b;
public TestClass(){
int c;
c = a;
a++;
b += c;
}
public static void main(String args[]) { new TestClass(); }
}

Select 1 option

A. The code will fail to compile because the constructor is trying to access static members.
B. The code will fail to compile because the constructor is trying to use static member variable a before it has been initialized.
C. The code will fail to compile because the constructor is trying to use member variable b before it has been initialized.
D. The code will fail to compile because the constructor is trying to use local variable c before it has been initialized.
E. The code will compile and run without any problem.

Multiple Choice Question

QID: 
451

Which line contains a valid constructor in the following class definition?

public class TestClass{  
 int i, j;  
public TestClass getInstance() {  
return new TestClass();    }  //1  
 public void TestClass(int x, int y) {  
 i = x;   j = y;   }     //2   
public TestClass TestClass() {   
return new TestClass();   }    //3   
public ~TestClass() {     }                     //4
 }

Select 1 option

A. Line 1 
B. Line 2 
C. Line 3 
D. Line 4 
E. None of the above. 

Multiple Choice Question

QID: 
450

Under what situations does a class get a default constructor?

Select 1 option

A. All classes in Java get a default constructor.
B. You have to define at least one constructor to get the default constructor.
C. If the class does not define any constructors explicitly.
D. All classes get default constructor from Object class.
E. None of the above.

Pages