Skip to content

Instantly share code, notes, and snippets.

@hasinur1997
Last active July 21, 2018 17:42
Show Gist options
  • Save hasinur1997/7fb074d80020ae947ccb7e5db9960100 to your computer and use it in GitHub Desktop.
Save hasinur1997/7fb074d80020ae947ccb7e5db9960100 to your computer and use it in GitHub Desktop.

Answer to question no 1.(b)

public class A {

  public static void method(int x){
    System.out.println("x = " + x);
  }
}

public class B extends A {

  public static void method(int y){
  	System.out.println("y = "+ y);
  
  }
  
  public static void method(String s){
  	System.out.println("s = "+s);
  }
  
  public static void main(String[] args){
  	A a1 = new A();
    B a2 = new B();
    
    a1.method(10);
    a2.method(20);
  }
}

Output

 x = 10
 y = 20

Answer to the question no 2.(a)

in question paper no acces modifier is used. If don't use access modifier the program will not execute. If use access modifire such as public Exceptions

public class Exceptions{
  
  public static void main(String[] args){
  
    String languages[] = {"C", "C++", "Java", "Perl", "Python"};
    
    try{
      for(int c= 1; c<=5; c++){
      	System.out.println(languages[c]);
      }
    }catch(Exception e){
    	System.out.println(e);
    }
  }

}

Output

C++
Java
Perl
Python
java.lang.ArrayIndexOutOfBoundsException: 5

Answer to the question no 2.(b)

There is an error in the question. Then we can handle the errors in the following

public class Exam {

  public static void main(String[] args) {
  
  	int num1 = 30, num2 = 0, a[] = new int[10];
    
    try{
    	int output1 = num1 / num2;
    }catch(Exception e){
    	System.out.println("Cought exception while divide 30 by 0");
    };
    
    
    a[1] = 9;
    
    int output2 = a[1] + num1;
    
    System.out.println("Sum of array values = " + output2);
  }
}

Output

  Cought exception while divide 30 by 0
  Sum of array values = 39

Answer to the question no 5.(a)

public abstract class A {
  
  public abstract void firstMethod();
  
  public void secondMethod(){
    
    System.out.println("SECOND");
    firstMethod();
  }
  
}

public abstract class B extends A {
	
  public void abstract thirdMethod();
  
  public void firstMethod(){
    
  	System.out.println("First");
  	thirdMethod();  
  }
  
} 

public class C extends B {
  
  public void thirdMethod(){
  	System.out.println("THIRD");
  }
}

public class MainClass {
  
  public static void main( String[] args ){
  	C c = new C();
  	c.firstMethod();
  	c.secondMethod();
  	c.thirdMethod();
  }
}

Output

FIRST
SECOND
THIRD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment