Sie sind auf Seite 1von 10

Answers for 2010 Paper Collected by - H. A. S.

Nishantha Question One Java Basics, Java Structure

A. Machine Language is. Binary instructions.. (Machine language is the only language that use binary digits) B. i. ii. No, Im not agree Because, Java language has compiler and interpreter both. Before run the application its compiling by creating java byte code. Then start to run. But C language has only a compiler to create machine code. Therefore C program is faster. But, IF you run compiled Java application, its may be faster than the C program. Because the Java byte code already created. C. JDK is Java Development Kit. Is containing all the Libraries that wants to create java applications. We have to install this package to the machine if we are doing programming in Java. Output is 0 ii. Error. Operator && can not be applied to int ,int Error. Possible loss of precision. Found: double, Required: float Output is false v. Output is 11 Output is false vii. Output is true Output is false

D. i. iii. iv. vi. viii. E.

class questione // same question in the 2009 paper also { public static void main(String [] args) { String str=args[0]; int low=0; int upp=0; int not=0; for(int i=0;i<str.length();i++) { if(Character.isLowerCase(str.charAt(i))) { low++; } else if(Character.isUpperCase(str.charAt(i))) { upp++; } Else { not++; // Additional } } System.out.println(" Number of Lower Cases \t = "+low); System.out.println(" Number of upper cases \t = "+upp); System.out.println(" Number of Other Charactors \t ="+not); // Additional } }

Collected By: H. A. S. Nishantha

F.

import java.util.*;

// same question in the 2009 paper also

class questionf { public static void main(String [] args) { Scanner sc=new Scanner(System.in); String str=sc.nextLine(); StringTokenizer tokens=new StringTokenizer(str," "); while(tokens.hasMoreTokens()) { System.out.println(tokens.nextToken()); } } } G. class eachloop { public static void main(String [] args) { int[][] int2DArray={{1,23,4,5},{2,3,4,5,6},{1,3,8}}; for(int[] row:int2DArray) { for(int colomn:row) { System.out.print(colomn+, ); } System.out.println(); } } }

Collected By: H. A. S. Nishantha

Question Two

OOP

A. Object Oriented Programming is, OOP is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. B. - Object Oriented Programming has re-usability but in Structured Programming - Save Time, Money and Space (memory). Difficult to attain C. Static method is common to the whole class. A static method dose not operate on an object D. Polymorphism is, Polymorphism means the ability to take more than one form. Ex. Number+Number=Number (take sum by addition) String+String=String (String Concatenation) E. Abstract Class and Interface Similarity - Abstract class containing at least one abstract method, interface is a fully abstract class Differences -

F. Advantages of Inner Class - Outer class can access inner class without importing - Inner class can only be accessed by the Outer Class not by any other class

G. i.

Doctor class is a abstract class, Because the Doctor class is containing a member function (abstract member) call totalPayment(). This method must implement in the GeneralPractitioner() class as well as in the Specialist() class.

Collected By: H. A. S. Nishantha

ii. abstract class Doctor { private String name; private String address; private String phoneNumber; private String RegNumber; public Doctor() { //default constractor } public Doctor(String r) { this.RegNumber=r; } public Doctor(String n,String a,String p,String r) { this.name=n; this.address=a; this.phoneNumber=p; this.RegNumber=r; } abstract public double totalPayment(int param); } iii. class GeneralPractitioner extends Doctor { int HourlyRate; public GeneralPractitioner(String n,String a,String p,String r,int rate) { super(n,a,p,r); this.HourlyRate=rate; } public double totalPayment(int Hours) { return HourlyRate*Hours; } }

Collected By: H. A. S. Nishantha

iv. class Specialist extends Doctor { int ChargePerPatient; public Specialist(String n,String a,String p,String r,int cpp) { super(n,a,p,r); this.ChargePerPatient=cpp; } public double totalPayment(int nPatient) { return ChargePerPatient*nPatient; } }

Collected By: H. A. S. Nishantha

Question Three Exceptions, Applet, Swing A. i. Syntax errors i. Missing semicolons ii. Missing double quotes in strings iii. Bad reference to object ii. Errors that are related to directory paths i. javac: command not found ii. Compiling successfully but not running (wrong logic/stack overflow) B. Class Error. Error class is a subclass of Throwable. That indicates serious problems. E.g.: LinkageError VirtualMachineError outofMemoryError C. i. - NumberFormatException - ArrayIndexOutOfBoundsException public class Excep { public static void main(String [] args) { try { int n1=Integer.parseInt(args[0]); int n2=Integer.parseInt(args[1]); int sum=n1+n2; System.out.println("Result of addition : "+sum); } catch(NumberFormatException e) { System.out.println("Enter numbers "); } catch(ArrayIndexOutOfBoundsException x) { System.out.println("Enter 2 arguments"); } catch(Exception e) { System.out.println(" Last Exception "); //for any other exceptions } } }

ii.

D. Applets can not run independently because they have no main method. Therefore the applet is running under control of a web browser.

Collected By: H. A. S. Nishantha

E. Applet Life Cycle Initialization

F. import java.awt.*; import java.applet.*; public class Ovel extends Applet { public void paint(Graphics a) { a.setColor(Color.BLACK); a.fillOval(500,100,50,250); a.fillOval(400,200,250,50); a.setColor(Color.WHITE); a.fillOval(500,200,50,50); } } G. - mouseClicked - mouseEntered - mouseExited - mousePressed - mouseReleased H. - FlowLayout - BorderLayout - GridLayout - BoxLayout - CardLayout

Collected By: H. A. S. Nishantha

Question Four

Threads, J2EE

A. Switching between processes is relatively expensive compared to switching between threads inside a process. Therefore thread based multitasking is faster than the process based multitasking. And communication between processes is much complex. B. public class questionb extends Thread { public static void main(String[] args) throws Exception { questionb p=new questionb(); p.start(); p.yield(); p.join(); System.out.println("Main thread Finishes"); } public void run() { for(int i=0;i<100;i++) { System.out.println("child's message "+(i+1)); } } } C. If the two threads got to read and write at the same time. The real information is not reading by the thread. This is not a good fact. Therefore it must prevent that accessing to the same piece of data at the same time by synchronizing threads. D. J2EE is a technology that provides a components based approach to the - Design - Development - Assembly - Development of enterprise applications E. J2EE container is, J2EE architecture makes J2EE applications easy to write because business logic is organized into usable components, in addition the J2EE server provides underlying services in the form of container. E.g. J2EE server Enterprise Java Beans (EJB) Container Web Container F. - XML is case sensitive but HTML is not case sensitive - HTML is intended for presentation of information as web pages. But XML representing Meta data - HTML has fixed set of markup tags but in XML if we want we can define our own tags too. - HTML cant structure the information but the XML creates for structure the information

Collected By: H. A. S. Nishantha

G. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. H. i. ii.

HttpServlet HttpServletRequest HttpServletResponse response.getWriter() request.getParameter(surname); request.getParameter(firstname); request.getParameter(age); lname fname age <%! TextBook tbean=new TextBook(); %> <%= tbean.getSubject() %>

Collected By: H. A. S. Nishantha

Question Six A. class qsix { public static void main(String[] args) { for(int i=9;i>0;i--) { for(int j=i;j>0;j--) { System.out.print(i); } System.out.println(); } } } B. class Rectangle { private int length; private int width; public Rectangle(int l,int w) { this.length=l; this.width=w; } public double getArea() { return length*width; } } C. i. we can use try and catch Blocks ii. we can put the throws Exception to the main metho D. - paint() - start() - init() - stop() - destroy() E. Well formed XML document <AddressList> <Address ID=100> <Name>Nishantha</name> <Street>Main Road</street> <City>Rajagiriya</city> </Address> <Address ID=101> <Name>Shashi</name> <Street>Main Road</street> <City>Malabe</city> </Address> </AddressList>

Collected By: H. A. S. Nishantha

Das könnte Ihnen auch gefallen