Monday, 18 April 2016

Exception Handling Objective Question and Answers

The below blog has objective questions on Exception Handling. It concentrates on usage of keywords like throws, throw, try/catch and Exception

This blog is a extension to previous blog of Core Java Objective Question Test 1


1.
1. public class Test {
2.  public static void main(String[] args) {
3.   System.out.println("hi");
4.   try {
5.    System.out.println("Here");
6.    int j=1/0;
7.    throw new UserNotFoundException();
8.    
9.   } catch (UserNotFoundException e) {
10.    System.out.println("Caught");
11.    e.printStackTrace();
12.   } finally{
13.    System.out.println("in finally");
14.   }
15.   System.out.println("After catch");
16.  }
17.  }

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

JAVA and OOPS multiple choice questions Test with answeres

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.


Core Java Test 1 >> Core Java Test 2 >> Exception Handling Test 3


Share the post