Wednesday, 13 April 2016

JAVA and OOPS multiple choice questions part 2

2.
package inter;

public interface Shape {

}

package inter;

public class TestInter {
 public static void main(String[] args) {

  Shape shape = new Shape();
 }
}
Output
1. Compilation Error. Object of interface cannot be created.
2. No error


3.
package inter;

public interface Shape {

 public int area;
}

package inter;

public class TestInter {
 public static void main(String[] args) {

  System.out.println(Shape.area);
 }
}

Output
1. Compilation Error. Class variable in interface should be initialized.
2. Compilation Error. Class variable in interface should be static.
3. No error


4.
package inter;

public interface Shape {

 public int area=100;
}

package inter;

public class TestInter {
 public static void main(String[] args) {

  System.out.println(Shape.area);
 }
}

1. No Error
2. Compilation error : Class variables should be static and final


5.
package inter;

public interface Shape {

 public double pie=3.14;
 
 double getArea();
}

package inter;

public class Circle implements Shape {

 double radius;
 
 public Circle(double radius){
  this.radius=radius;
 }
 
 public double getArea() {
  
  return pie * (radius*radius);
 }

}

1. Compilation Error. getArea() in Interface Shape should be public and abstract.
2. No error



6.
package Collections;

import java.util.List;

public class TestCollection {

 public static void main(String[] args) {
  
  List l = new List();
 }
}


1. Compilation Error. List is an Interface. You cannot instantiate an interface.
2. No Error



7.
package Collections;


public class TestCollection {
 
 int instanceVariable;
 
 public int doSomething(){
 
  System.out.println(instanceVariable);
  int localVariable;
  return instanceVariable+localVariable;
 }

 public static void main(String[] args) {
  
  TestCollection testCollection  = new TestCollection();
  testCollection.doSomething();
  
 }
}


1. 0
2. Compilation Error: localvariable not initialized.




8.
package annot;

class Animal{
 public void eat(){
  System.out.println("Call Aniaml");
 }
}

public class TestClass {
 public static void main(String[] args) {
  callEat(new Animal(){
   public void eat(){
    System.out.println("Call Aniaml of Test");
   }
   public void talk(){
    System.out.println("Calling talk");
   }
  });
 }

 private static void callEat(Animal a) {
  a.talk();
 }
}
Options:
1. Calling talk
2. Compilation error while calling a.talk()
3. Compilation error while calling callEat() method


9.
package annot;

interface Animal{
 public void eat();
}
public class TestClass {

 public static void main(String[] args) {
  callEat(new Animal(){
   public void eat(){
    System.out.println("Call Aniaml of Test");
   }
   public void talk(){
    System.out.println("Calling talk");
   }
  });
 }
 
 private static void callEat(Animal a) {
  a.eat();
 }
}
1. Cannot instantiate an interface
2. Call Animal of Test

Share the post