Tag: java 3rd Sem
JAVA 2019 Old Papers Solution
year 2019 Q.no 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class BIM { public static void main(String args[]) { int a=0, b=1, c; System.out.println(a); System.out.println(b); for(int i=3;i<=10;i++) { c=a+b; a=b; b=c; System.out.println(c); } } } |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
String aar[]={"January","February","March","April","May","June","July","August","September","October","November","December"}; for(int i=0; i<aar.length; i++) { System.out.println("month"+aar[i]); try{ Thread.sleep(1000); } catch(Exception e) { System.out.println(e); } }}} class days implements Runnable { public void run() { String aa[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; for(int i=0; i<aa.length; i++) { System.out.println("month"+aa[i]); try{ Thread.sleep(1500); } catch(Exception e) { System.out.println(e); } }}} class den { public static void main(String args[]) { month m=new month(); Thread t=new Thread(m); t.start(); days d=new days(); Thread t1=new Thread(d); t1.start(); } } |
Q.no 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Number { int x; int y; int z; Number(inta,intb,int c) { x=a; y=b; z=c; } intgetMax(){ if(x>y&&y>x) return(x); else if(y>z) return(y); else return(z); } } classNumberDemo { public static void main(String args[]) { Number n=new Number(3,7,5); System.out.println("largest number is"+n.getMax()); } } |
Q.no 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class Box { int length; int breadth; int height; Box(inta,intb,int c) { length=a; breadth=b; height=c; } voidgetVolume(){ int v=length*breadth*height; System.out.println("Volume is"+v); }} classBoxWeight extends Box{ int weight; BoxWeight(inta,int b, int c) { super(a,b,c); } voidsetweight(int w) { weight=w; } voidgetWeight(){ System.out.println("weight"+weight); } void display(){ super.getVolume(); } } classBoxDemo { public static void main(String args[]) { BoxWeight n=new BoxWeight(0,0,0); n.setweight(200); n.getWeight(); BoxWeightob=new BoxWeight(2,3,4); ob.display(); } } |
Q.no 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
importjava.util.Scanner; import java.io.*; public class JavaProgram { public static void main(String[] input) { String fname; Scanner scan = new Scanner(System.in); /* enter filename with extension to open and read its content */ System.out.print("Enter File Name to Open (with extension like file.txt) : "); fname = scan.nextLine(); /* this will reference only one line at a time */ String line = null; try { /* FileReader reads text files in the default encoding */ FileReaderfileReader = new FileReader(fname); /* always wrap the FileReader in BufferedReader */ BufferedReaderbufferedReader = new BufferedReader(fileReader); while((line = bufferedReader.readLine()) != null) { System.out.println(line); } /* always close the file after use */ bufferedReader.close(); } catch(IOException ex) { System.out.println("Error reading file named '" + fname + "'"); } } } |
JAVA 2017 Old Paper Solutions || BIM STUDY NOTES
Year 2017 Q.no 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class tu17Q11 { public static void main(String[] args) { int n,i; System.out.println("Enter the number of numbers to enter"); Scanner sn=new Scanner(System.in); n=sn.nextInt(); System.out.println("Enter the numbers"); int num[]; for(i=0;i<n;i++){ num[i]=sn.nextInt(); if(num[i]%2==0&&num[i]%3==0){ System.out.println(num[i]); } } } } |
Q.no 12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/** * * @author bim study notes */ class Student { int roll_no; public void readRoll(int r){ roll_no=r; } public void displayRoll(){ System.out.println(roll_no); } } class Test extends Student{ int markJava,markWeb; public void readMarks(int mj,int mw){ markJava=mj; markWeb=mw; } public void displayMarks(){ System.out.println("Java:"+markJava+" "+"Web:"+markWeb); } } class Results extends Test{ int total; public void calculate(){ total=markJava+markWeb; } public void display(){ System.out.println("The total marks is :"+total); } public static void main(String[] args) { Results obj=new Results(); obj.readRoll(1); obj.readMarks(49,40); obj.calculate(); obj.displayRoll(); obj.displayMarks(); obj.display(); } } |
Q.no 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
interface Shape{ public void get_data(int l,int b); public void display_area(); } class Rectangle implements Shape{ int length,breadth,area; public void get_data(int l,int b){ length=l; breadth=b; } public void display_area(){ area=length*breadth; System.out.println("The area of rectangle is:"+area); } } class Square implements Shape{ int length,breadth,area; public void get_data(int l,int b) { length=l; } public void display_area(){ area=length*length; System.out.println("The area of Square is:"+area); } } public class tu17Q13 { public static void main(String[] args) { Rectangle obj=new Rectangle(); obj.get_data(10, 20); obj.display_area(); Square obj1=new Square(); obj1.get_data(10, 0); obj1.display_area(); } } |
Q.no 14
1 2 3 4 5 6 7 8 9 10 11 |
public class tu17Q14 { public static void main(String[] args) { String []country={"Nepal","USA","Australia","Brazil","Paragua","Canada"}; int i; for(i=0;i<6;i++){ if(country[i].endsWith("a")){ System.out.println(country[i]); } } } } |
1 |
17.Explain user defined exception in java with a suitable example. In java we can create our own exception class and throw that exception using throw keyword. These exceptions are known as user-defined or custom exceptions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class JavaException{ public static void main(String args[]){ try{ throw new MyException(2); // throw is used to create a new exception and throw it. } catch(MyException e){ System.out.println(e) ; } } } class MyException extends Exception{ int a; MyException(int b) { a=b; } public String toString(){ return ("Exception Number = "+a) ; } } |
JAVA 2016 Old Papers Solutions
Year 2016 Q.no 11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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 […]
Continue Reading