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
Answer: 1
This is a simple one. A instance/object of Interface can not be created. It will not be of any help actually. Since all the methods are abstract.
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
Answer : 1
In a interface the variables should be public static and final. Java internally applies these attributes to a variable in an interface. So no need to define them.
A final variable needs to be initialized before it is used. That is the 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
Answer : 1.
As mentioned earlier no error.
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
Answer : 2.
Methods in interface are always public and abstract. No need to define public and abstract. Java does it for you.
So 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
Answer : 1.
Again List is a interface here so can not be instantiated.
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.
Answer : 2.
In java the instance variable are by default given some default values. But not local variables.
They have to be initialized before used.
So intanceVariable is assigned 0 as it is a intance variable but localVariable is not initialized, hence it is mandatory to initialize it.
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
Answer : 2.
The reference is of Animal. so only methods present in Animal can be called.
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
Answer : 2.
Here actually we are creating a implementing class of the Interface. Which is also called as anonymous class. And we have overriden the eat() method
Since now a days the number of candidates appearing for a job has increased, companies have started keeping screening process. This screening process consist of objective questions which can be answered online. Only those candidates which score good marks in Objective questions are called for personal interviews.
This blog has multiple choice OOPS and Java questions. It is a mix of topics like Inheritance, overriding, static, constructor etc.
Read the question and try to solve it yourself. Then you can click on the Answer button to verify your answer.