Sie sind auf Seite 1von 7

Ans.

1.

MULTIPLE CHOICE QUESTIONS

1.1 (c) Package should always be the first element, then we can use import if other classes are required and then class. 1.2 (a) Int is of 4 bytes i.e., 8 4 = 32 bits. 1 bit is reserved to store sign. 1.3 (c) An identifier should contain digits letter, $ or underscore. It cannot contain +, &. It cannot start with a digit. 1.4 (a) A native method cannot be static. 1.5 (c) Friendly is not a java modifier. 1.6 (b) start( ) calls the run( ) and thus it schedules a thread for execution. 1.7 (d) The last color will be set. 1.8 (d) The g(c) of the System Class initiates garbage collection. 1.9 (a) Flow layout is the default layout manager. 1.10 (a) Shifting 1 bit to the right divides the number by 2. Here 16/2 = 8 and 17/2 = 8. Ans. 2.1 2.2 2.3 2.4 2.5 2.6 (T) (T) (T) (F) (F) (T) 2. TRUE OR FALSE

Yes, Java is a strongly typed language. Variables can be initialized dynamically with the help of constructors. Yes, switch is one of the java keywords. Final methods cannot be overriden. Abstract methods cannot be static. wait( ) used in thread must be used in try catch block as it throws Interrupted Exception. 2.7 (F) The finalize method releases resources and do other necessary cleanup that Java does not handle during garbage collection. 2.8 (F) Readers have methods that can read characters or strings. 2.9 (T) A single try block can catch multiple exception. One catch is required for one exception. 2.10 (F) Component is a superclass of container. Ans. 3. MATCHING THE COLUMNS

3.1 (F) Unicode is a 16-bit character code. It has code for letters used in different languages. ASCII code of 7 bits supports only English letters. 3.2 (N) & is also called short circuit logical operator. 3.3 (L) goto is Java reserved word, reserved for future use. 3.4 (A) List and Set extends collection interface. 3.5 (H) Stack is a subclass of vector, that implements standard LIFO stack. 3.6 (B) String tokenizer implements the enumeration interface. 3.7 (J) The start( ) calls the run( ) and move to ready state. 3.8 (C) The ActionListener interface has methods to receive action event. 3.9 (D) Panel is a sub-class of component. 3.10 (I) Reader and writer are designed for character streams. Ans. 4.1 4.2 4.3 4.4 4.5 (L) (B) (C) (P) (E) 4. FILL IN THE BLANKS

Media tracker is an object to handle images. Proxy server is used to speak client side of a protocol to another server. Applets are displayed in a web browser and support window programming. The paint( ) is called each time, applets output is to be redrawn. Datagrams are bundles of information used to pass information in a client/server program. 4.6 (K) Cloneable interface defines no members. It is used to indicate that a class allows bitwise copy of an object.

4.7 (N) 4.8 (G) 4.9 (J) 4.10

Update( ) is called when a change in object takes place. Serialization is the process of writing state of an object to byte stream. MIME types are type descriptor for media (multimedia) contents. (H) Item Listener interface is used to handle checkbox events.

Qs. 5.

Write a program to print current date, year and time using Gregorian calendar class and also check whether the year is a leap year or not. Ans. import java.util.*; class GCDemo { public static void main (String args [ ]{ String months [ ]={ Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; int year; // Create a Gregorian calendar initialized // with the current date and time in the // default locate and timezone. GregorianCalendar gcalendar=new GregorianCalendar) // Display current time and date information. System.out.print (Date:); System.out.print (months [gcalendar.get (calendar.MONTH)]); System.out.print ( +gcalendar.get (calendar.DATE+ ); System.out.print In (year=gcalendar.get (calendar.YEAR)); System.out.print (Time:); System.out.print (gcalendar.get (calendar.HOUR)+ :); System.out.print (gcalendar.get (calendar.MINUTE)+ :); System.out.println (gcalendar.get (calendar.SECOND)); // Test if the current year is a leap year. if (gcalender.isLeapYear(year): System.out-println (Current Year is Leap Year); } else { System.out.println (Current Year is not a Leap Year); } } }

Ans. 6. class StringFunctions { public static void main(String S[ ]) { String s1; char chars[ ]={a, b, c, d}; s1=new String (chars); int l=s1.length( ); System.out.println (Length= +l); Sring s2=Java; // Concatenation String s3=s1+s2;

System.out.println (s3); // Replace String s4=Hello. replace (l, w); // s4=Hewwo System.out.println (s4= +s4); // Compare two strings String s5=Hello; String s6=Hello; if (s5.equals (s6)) Sstem.out.println (Equal); else Sytem.out.println (Different); } } 7. (a) Access modifiers: Access modifiers are used to restrict the access to certain variables and methods from outside the class. Java provides three types of access modifiers : public, private and protected. They provide different levels of protection as described below: Public: A variable or method declared as public has the widest possible visibility and is accessible everywhere. The syntax is as follows: public int total; public void sum( ) {.....} Protected: A variable or method declared as protected can be accessed from all classes and subclasses of same package and other packages. It cannot be accessed from Non-subclasses in other packages. Private: A variable or method declared as private can only be accessed in the class in which it is declared. It is not visible from outside the class and so it can not be accessed from outside the class. (b) Inner class: A class that is declared and defined inside some other class is nested class or inner class. Inner classes in program makes it more clear. The basic construct of the inner class is as follows: public class Outer { int x=1; public class Inner { int y=2; public void inner Fn( ){ System.out.println (y = +y); } } public void outerFn( ){ System.out.println(x = + x); } public static void main (String s[ ]){ Outer out=new outer( ); out.outerFn( ); } } Some important points on Inner class. Inner class can be static. Inner class can be abstract.

7.

An inner class can not have any static variable declared inside it. (c) Methods of the class constructor: The constructor class has a method getConstructors( ) which returns an array on constructors available in the class. It can be used as follows: import java.lang.reflect.*; public class C1 { public static void main (String s[ ]) { try{ class c=class.forName (java.awt.Dimension); Constructor construct[ ]=c.getConstructors( ); System.out.println (Constructors); for (int i=0; i<construct.length, i++) { System.out.println (construct[i]); } } catch (Exception e) { System.out.println (Error : +e); } } } (d) Stack is a subclass of vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own. Method Description boolean empty( ) Returns true if the stack is empty, and returns false if the stack contains elements. object peek( ) Returns the element on the top of the stack, but does not remove it. object pop( ) Returns the element on the top of the stack, removing it in the process. Object push (Object element) Pushes element onto the stack. Element is also returned. int search (Object element) Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, 1 is returned. Ans. 8. /* <APPLET CODE=CALCULATION WIDTH=300 HEIGHT=200> </APPLET> */ import java.awt.*; import java.applet.*; import java. awt.event.*; public class CALCULATION extend Applet implements ActionListener { Label l1, l2, l3; TextField num1, num2, result;

Button addb, subb, mulb, divb; public void init( ) { l1=new Label (Enter number 1); l2=new Label (Enter number 2); l3=new Label (Result); num1=new TextField( ); num2=new TextField( ); result=new TextField( ); addb=new Button (Add); subb=new Button (Substract); mulb=new Button (Multiply); divb=new Button (Divide); addb=addActionListener (this); subb.addActionListener (thir); mulb.addActionListener (this); divb.addActionListener (this); setlayout (new Gridlayout(5, 2)); add(l1)); add(num1); add(l2); add(num2); add(addb); add(subb); add(mulb); add (divb); } public void actionPerformed (ActionEvent are) { float n1, n2, r=Of; n1=Float.parseFloat (num1.getText( )); n2=Float.parseFloat(num2.getText( ); if (ac.getSource( )==addb) r=n1+n2; else if (ac.getSource( )==subb) r=n1n2; else if (ac.getSource( )==mulb) r=n1*n2; else if (ac.getSource( )==divb) r=n1/n2; result.setText(Float.toString(r)); } }

9.(a). (1) Applets 1.Applets are those Java programs which work on web pages. 2.it does not contain main() method. 3.we need Java enabled web browser or Applet viewer to execute an (2) Applications 1.Applications are also Java programs but it does not work on web pages. 2.It contains main() method. 3.To execute it requires booting utility small

applet. 4. Applets can be embedded in HTML pages and down loaded over the Internet or Intranet. 5. Applets execute under strict security limitations that disallow certain operations, such as accessing files or systems services on the users computer.

such as Jview.exe or Java.exe. 4. Application support in HTML for downloading. 5. have no special or

embedding

Applications have security restrictions.

no inherent

6. Applets are the programs written specially for distribution over a network. These programs contain information to be delivered to the world and involve user interaction, for example, order entry form, registration form mailing etc

6.

Applications are system level programs i.e., these programs run in the background and dont involve user interaction, for example server administration, security manager etc.

9.

(b) The ways in which things interact with one another is known as interface. In the context of object oriented programming(OOP) interface denotes the ways of communication of objects with one another. A java interface describes a set of methods that can be called on an object to tell the object to perform some operations or task. The interface specifies what operations a object have to perform but does not specify how the operations are performed. This simple benefit can make large projects much easier to manage, once interfaces have been designed the class development can take place without worrying about communication among classes. With the help of interface we can implement multiple inheritance in Java In java an interface is declared with keyword interface and it contains only constants and abstract methods that is static final variables and function declaration. A class can extend only one class but implements many interfaces. The syntax can be class c1 { } interface i1 { } interface i2 { } class c extends c1 implements i1,i2 {

} Difference with abstract class: (i) An abstract class can have method body i.e., they can include as much implementation as is required, some of the methods may not contain body in an abstract class and some methods may have body. In an interface there can only be method declaration, they cannot have method declaration, they cannot have method body in it. (ii) An abstract class can have final variables as well as normal variables but an interface can have only final variables. (iii) A class cannot extend more than one abstract class but a class can implement more than one interface. Components of an interface access interface name { return_type method1 (parameter list); return_type method2 (parameter list); ..... ..... return_type method n (parameter list); type final variable1=value ; type final variable2=value; ..... ..... type final variablen=value; } access: Access is the access modifier, which can be public or not used. name: It is the name of the interface, which can be any valid identifier. methods: method declaration. variables: final variables.

Das könnte Ihnen auch gefallen