Model Questions
- Write a Java program to display all the numbers between -300 to -1 which when divided by 17 gives 7 as remainder and also displays sum of those numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Sum { public static void main(String[] args) { int i,s=0; for(i=-300;i<-1;i++) { if(i%17==-7){ System.out.print(i+ " "); s = s+i; } } System.out.println("The sum = "+s); } } |
3.Make two threads, one displays odd number after one second and another thread display even
number after half second between -200 and -10.
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 |
/** * * @author bim study notes */ class Odd extends Thread{ public void run(){ for(int i=-200;i<=-10;i++){ try{ Thread.sleep(1000); }catch(InterruptedException e){ System.out.println("Thread Interrupted "); } if(i%2==-1) System.out.print(" "+i); } } } class Even extends Thread{ public void run(){ for(int i=-200;i<=-10;i++){ try{ Thread.sleep(500); }catch(InterruptedException e){ System.out.println("Thread Interrupted "); } if(i%2==0) System.out.println (" "+i); } } } public class EvenOddDemo { public static void main(String[] args) { Odd o = new Odd(); Even e = new Even(); o.start(); e.start(); } } |
4. Write a program that displays all the read-only files of a given folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.*; public class FileList { public static void main(String[] args) { File dir = new File("e:/java"); if(dir.isDirectory()){ String[] s = dir.list(); for(int i=0;i<s.length;i++){ File f = new File(dir+"/"+s[i]); if(f.canRead()) System.out.println(s[i]); } } else System.out.println("Its is not directory"); } } /** * * @author bim study notes */ |
5.Make a class named student with private member variables name, age, and public functions to
set, display and return values of member variables. Then create ten objects of the student class,
set them and display the name of youngest student in the main function of another class named
student_demo.
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 44 45 |
/** * * @author bim study notes */ class Student{ private String name; private int age; public void setData(String n, int a ){ name = n; age =a; } public void display(){ System.out.println("Name : "+name); System.out.println("Age : "+age); } public int getAge(){ return age; } public String getName(){ return name; } } class StudentDemo { public static void main(String[] args) { int minage,j=0; Student []s = new Student[10]; for(int i=0;i<s.length;i++) s[i] = new Student(); s[0].setData("Kamal", 22); s[1].setData("Suresh", 21); s[2].setData("Binita", 20); s[3].setData("Kamala", 29); s[4].setData("keshav", 50); s[5].setData("Raju", 11); s[6].setData("Hari", 88); s[7].setData("Krishna", 1); s[8].setData("Ram", 40); s[9].setData("Bimala", 30); minage = s[0].getAge(); for(int i=1;i<s.length;i++){ if(minage>s[i].getAge()){ minage = s[i].getAge(); j = i; } } System.out.println("The name of the student with minimum age is "+s[j].getName()); } } |
6. Write a super class named furniture with member variables weight and price. From this furniture
class derive class named chair. These both super and sub classes have functions to set values of
their member variables. In another class, named furn_demo make objects of class chair and
display values of member variables of those objects.
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 |
class Furniture{ private int weight; private int price; public void setData(int w, int p){ weight = w; price = p; } /** * * @author bim study notes */ public void display(){ System.out.println("Weight:"+weight); System.out.println("Price : "+price); } } class Chair extends Furniture{ private String color; public void setData(int w, int p, String c){ super.setData(w, p); color = c; } public void display(){ super.display(); System.out.println("Color :"+color); } } public class Furn_Demo { public static void main(String[] args) { Chair ch = new Chair(); ch.setData(20, 400, "Red"); ch.display(); } } |
Group “C”
7.What is exception? Explain ArrayIndexOutOfBoundsException with an example.
Ans: An exception is an event which occurs during the execution of a program that disrupts the normal flow of the program’s instructions. Java terminology, creating object and handling it to the runtime system called throwing an exception. The exception handler choosen is said to catch the exception.
ArrayIndexOutOfBoundsException: This is runtime exception. This exception is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater or equal to the size of the array.
Let us consider the following program:
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * * @author bim study notes */ public class ExcpeptionDemo { public static void main(String[] args) { int arr[] = {1,2,3,4,5,6}; try{ for(int i=0;i<=arr.length;i++) System.out.println (arr[i]+ " "); }catch(ArrayIndexOutOfBoundsException e){ System.out.println("Exception Occurred "+e); } } } |
8.Write the steps required to create package with an example.
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 |
<strong>Ans</strong>: Creating a package in Java is quite easy. Simply include a package command followed by name of the package as the first statement in Java source file. package mypack Public class employee { ……statement; } The above statement creates a package called <strong>mypack Let us consider the following example: </strong> /** * * @author bim study notes */ Import mypack; Class Book { String bookname; String author; Book(String b, String c) { this.bookname = b; this. Author = c; } Public void show() { System.out.println(bookname+” “+author); } } Class Test{ Public static void main(String []agrs) { B Book bk = new Book(“java”,”Herbert”); Bk.show(); } } |