JAVA 2016 Old Papers Solutions

3rd Semester

Year 2016

 
Q.no 11

public class tu16Q11 {
    public static void main(String[] args) {
        int [] a={1,22,23,3,4,67,87,23,56,35,47,5,61,7,80,9,23,23,45,456,345,67,0,23,45,67,87,1,3,4};
        for(int i=0;i<30;i++){
            if(a[i]>16 &&a[i]<47){
                System.out.println(a[i]);
            }
        }
    }
    }
or
/** * * @author bim study notes */
import java.util.*;
public class ArrayDemo {
 public static void main(String[] args) {
 int arr[] = new int[30];
 Random r = new Random();
 int i;
 for(i=0;i<arr.length;i++){
 int num = r.nextInt()%50;
 arr[i] = num;
 }
 for(i=0;i<arr.length;i++){
 if(arr[i]>16&&arr[i]<47)
 System.out.print(arr[i]+" ");
 }
}
}





Q.no 12 Create two classes ThreadA and ThreadB which implement Runnable interface. ThreadA displays all even numbers from 50 to 100 and ThreadB displays all odd numbers from 100 to 200. Define a main class which creates the objects of both the classes and displays the numbers as per the above mentioned specifications .

class ThreadA implements Runnable{
    public void run(){
        int i;
        for(i=50;i<100;i++){
            if(i%2==0){
                System.out.println(i);
            }
        }
    }
}
class ThreadB implements Runnable{
    public void run(){
        int j;
        for(j=100;j<200;j++){
            if(j%2==1){
                System.out.println(j);
            }
        }
    }
}
public class tu16Q12 {
    public static void main(String[] args) {
        ThreadA t1=new ThreadA();
        Thread objA=new Thread(t1);
        objA.start();
        ThreadB t2=new ThreadB();
        Thread objB=new Thread(t2);
        objB.start();
     /** * * @author bim study notes */
    }
 }




13. Create an interface called Calculate which has methods int add(int x, int y) and diff(int x, int y) to perform addition and subtraction of numbers passed as arguments.Then define a class that implements interface calculate .

interface Calculate{
    int add(int x,int y);
    int diff(int x,int y);
}
class Mathematics implements Calculate{
    @Override
    public int add(int x,int y)
    {
       int sum=x+y;
       System.out.println("The sum is:"+sum);
       return sum;
    }
    @Override
    public int diff(int x, int y) {
       int sub=x-y;
       System.out.println("The difference is:"+sub);
        return sub;
    }
    public static void main(String[] args) {
        Mathematics mth=new Mathematics();
        mth.add(10, 30);
        mth.diff(10, 5);
    }
}
or
interface Calculate{
int add(int x, int y);
int diff(int x, int y);
}
class Calculator implements Calculate{
public int add(int x,int y){
return x+y;
}
public int diff(int x,int y){
return x-y;
/** * * @author bim study notes */
}
}
public class CalculatorDemo {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println("The sum of 4 and 5 is ="+c.add(4, 5));
System.out.println("The difference of 10 and 5 ="+c.diff(10, 5));
 

14. Define String array of size 4 and store name of 4 students. Then display the names of students whose name has character ‘t

/** * * @author bim study notes */
public class tu16Q14 {
    public static void main(String[] args) {
        String []name={"abc","ukutyy","ags","yasty"};
        for(int i=0;i<4;i++){
            if(name[i].contains("t")){
                System.out.println(name[i]);
            }
        }
    }
  }
or
public class NameDemo {
 public static void main(String[] args) {
 String name[] = {"Sita","Gita","Ram","Kamala"};
 for(int i=0;i<name.length;i++){
 if(name[i].contains("t"))
 System.out.println(name[i]);
 }
}
}




15. Write a program that displays content of folder database stored in d: drive. 

import java.io.File;
public class DirList {
public static void main(String[] args) {
String dirname = “D:/database";
File f1 = new File(dirname);
if(f1.isDirectory()){
String s[] = f1.list();
for(int i=0;i<s.length;i++){
File f = new File(dirname+ "/"+s[i]);
if(f.isDirectory()){
System.out.println(s[i]+" is a directory");
}
else
System.out.println(s[i]+" is a file");
}
}
}
}

Group C

16: Explain dynamic polymorphism with example.
Runtime polymorphismor Dynamic Method Dispatchis a process in which a call to an overridden method is resolved at runtime rather than compile-time.In this process, an overridden method is called through the reference variable of a super class.The determination of the method to be called is based on the object being referred to by the reference variable.

class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run(){System.out.println("running safely with 60km");
}
}
public class DymamicPolymorphismDemo {
public static void main(String[] args) {
Bike b;
Splender s = new Splender();
Bike b1 = new Bike();
b = b1;
b.run();
b = s;
b.run();
}
}
/** * * @author bim study notes */

17. What is exception? Write a program to catch the ArrayIndexOutOfBoundsException.






An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run time error.When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.Some reasons that may cause exception to occur

  • A user has entered invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.
public class ArrayIndexOutOfBoundsExceptionDemo {
public static void main(String[] args) {
int arr[] = {3,4,5,6,1};
try{
for(int i=0;i<=arr.length;i++)
System.out.print(arr[i]+" ");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception occurred "+e);
}

}
}

Leave a Reply

Your email address will not be published.