Sei sulla pagina 1di 10

Ans. 1.1 1.2 1.3 1.

4 (c) (a) (d) (a)

1.

MULTIPLE CHOICE QUESTIONS

1.5 (a) 1.6 (d) 1.7 (b) 1.8 (b) 1.9 (?) 1.10 Ans.

Throw is a Java keyword used to raise exception. The program does not compile, as i, the local variable is not initialized. There is no such restriction on the last statement in a finalize method. The output will be 4486 as ++i is pre-increment operator and j++ is post increment operator. Byte code is the code generated by Java compiler when a java program is compiled. For a variable defined in a class accessible only to methods defined in the class in same package should be a default variable/friendly variable which is not preceded by any modifier. The method parseInt( ) converts String to an integer, assuming that the string represents an integer. Default priority of a thread is NORM_PRIORITY with value 5. The value of (string) method is found with all java built in types and their wrapper classes. (c) Constructors are used to initialize instance variable of a class. 2. TRUE OR FALSE

2.1 (T) Multithreaded programs can be created using thread class. 2.2 (F) Two threads should not access the same variable or the same method of same object at same time. 2.3 (F) A class that inherits from another class is called a subclass and the inherited class is called super-class. 2.4 (T) The size( ) method returns number of elements in Vector. 2.5 (F) Final classes cannot be inherited. It cannot have any sub-class. 2.6 (T) Object class is the super class of all classes in the Java class hierarchy. 2.7 (T) Initialization can happen only once in the applets lifetime through the init( ) method. When the applet is reloaded start( ) method is called. 2.8 (T) setFont( ) method is used to set the Font of most of window component like Label, Button etc. 2.9 (T) A component can have multiple listeners for an event. For example, a component can have MouseListener, and MouseMotionListener. 2.10 (T) Key events are generated when a user presses or releases a key on the keyboard. Ans. 3.1 3.2 3.3 3.4 3.5 (C) (F) (G) (H) (D) 3. MATCHING THE COLUMNS

3.6 (I) 3.7 (B) 3.8 (E) 3.9 (A) 3.10 Ans.

equals( ) method is used to compare two strings to each other. Finally block is executed even whether or not an exception is thrown. finalize( ) method is used for garbage collection. addElement( ) is a method of vector class. Do while loop is always executed once at least, since the condition is checked after the statements in loop is executed. Synchronized keyword is used to ensure that resource will be executed by only one thread at a time. Static method can be invoked even before a single instance of class is created. Example main( ). Java applets does not have main( ) method. Integer class is a Wrapper class, used for int data type. (J) Constructors are used to instantiate objects. 4. FILL IN THE BLANKS

4.1 (A) Java Applets are Java Programs, which are specifically made to run in Java enabled web browser. 4.2 (H) Byte code file created by Java compiler has .class extension. 4.3 (E) The constructor methods are used to instantiate the object from a class. 4.4 (D) Inheritance is a mechanism to inherit behaviour and attributes of another class. 4.5 (E) A class can use an interface by using the implements clause. 4.6 (C) Radio Buttons are implemented in swing through the JRadioButton class. 4.7 (F) Throws clause is used in method definition to indicate that the method may possibly throw an exception. 4.8 (?) The sleep( ) method is used to temporarily stop the execution of thread. 4.9 (J) The join( ) method suspends the current thread until that thread object dies. 4.10 (O) After the start( ) method is called, the thread is in Runnable state.

5.

(a) Interface are similar to classes, but they lack instance variables, and their methods are declared without any body. We can say that interface is a collection of final variables and method declaration. We cannot instantiate an interface. Interface must be implemented by a class to define the methods declared in the interface. With the help of interface we can implement multiple inheritance in Java. Interfaces help in development of large programs. 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.

(b) Write once and run anywhere This is an important feature of Java programs which means that a Java program written once can turn anywhere. Most of the programs written in other languages is written for a particular computer and operating system. If we try to run the same program in a different platform it does not run. But this is not the case with Java. Java programs once written will run even if operating system upgrades, processor upgrades or there is some change in the core system resources. The java designers made several hard decisions to add this feature of Write once and run anywhere. (c) Final Keyword: Final keyword of Java can be used with variables, methods and class. A variable declared with the key word final cannot be modified. It is similar to constant in C/C++. A final variable must be initialized at the time of declaration. Example: final double PI=3.14; Using final keyword in front of method prevent overriding. When it is required that a particular method should not be overriden in the subclass, this method is declared with keyword final. Example: class C1 { final void f(C) { System.out.println (f1 of C1); } } class C2 extends C1 { void f1( ) // This is not allowed { System.out.println (f1 of C2); } } Declaring a class with keyword final prevents inheritance, That means we cannot create child class of a final class. Many times due to security reasons or other reasons we want to stop subclassing of a class, we declare it with keyword final. Example: final class C1 { } /* This is not allowed. class B1 extends C1 { } */ (d) A method or variable declared with the keywords static is called static variable or static method. A variable which is declared static is common to all objects of the class. It is used for object to object communication. The static variable is also called class variable. Similarly a method declared as static can be called without object reference. It can be called by its class

name. A static method cannot access instance variable. Example: class C1 { //static variable static int bonus =500; //static method static void net salary (int sal) { int netsal; netsal=sal+bonus: } System.out.println (Net Salary=+netsal); // static method public static void main (String s[ ] ) { netsalary (5000); } } (e) JDBC (Java Database Connectivity) is the first standard API that allows users to develop database front ends with Java. JDBC provides the ability to create robust platform independent applications which can access any database. The interaction between front ends and database is done through a set of classes and methods defined in the JDBC API. The set of classes that implement the JDBC interface for a particular database is called JDBC driver. The following figure shows the JDBC architecture:

The commonly used drivers are: JDBC ODBC bridge driver Nature API driver Generic API driver Third Party driver Ans. 6. import javax.swing.*; import java.awt.*; public class DigitalClock extends JFrame implements Runnable { JLabel l1; int hr, min, sec; Thread t1;

DigitalClock (int h, int m, int s) { hr=h; min=m; sec=s; l1=new JLabel( ); l1.setText (hr+ : + min+ : +sec); Container c=getcontentPane( ); c.add (l1); setTitle (Digital Clock); setSize (200, 200); setVisible (true); t1=new Thread (this); t1.start( ); } public void run( ) { while (true) { try { if (sec==60) { sec=0; min++; } if (min==60) { min=0; hour++; } if (hr==24) { hr=0; } l1.setText (hr+ : + min+ : + sec); sleep (1000); } catch (Exception e) { } } } public static void main(String s[ ]) { DigitalClock dc=new DigitalClock(0, 0, 0); } } 7. (a) Dynamic Method Dispatch: class c1 implements Runnable { public void run( ) { for (int i=1; i<=5; i++) { System.out.println(i); } } }

class RunnableDemo { public static void main(String s[ ]) { C1 obj=new C1( ); Thread t1=new Thread (obj); t1.start( ); System.out println (Main Ends); } } (b) Packages and Interfaces: Package is a group of related classes and interfaces. A package is a folder which contains similar classes and interfaces in the form of Library. These classes and interfaces in the package can be used by any Java program, by importing them. For example: package pack1; public class c1 { public void f(c) { System.out.println (f1 of c1); } } This class must be saved in a folder pack1. An interface on the other hand is a prototype for a class. It provides a method to define protocols for a class. An interface can contain only method declarations and final fields. The interface have to be implemented by a class to define the method and use them. Package (i) It is a group of similar classes and interfaces. (ii) It is used as a library by other Java (i) Interface It is a group of similar abstract methods and final fields. (ii) It is useful from Logical design programs. perspective. (iii) package keyword is used to declare a (iii) interface keyword is used to declare package. interface. (iv) A package can contain many interfaces. (iv) An interface cannot contain a package. 8. (a) Inter Thread Communication

(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 applet.

(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 small booting utility

such as Jview.exe or Java.exe. 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. 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 4. Application support in HTML for downloading. 5. have no special or

embedding

Applications have security restrictions.

no

inherent

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.

(b) class Calculate


{ int factorial (int n) if (f <=0) return (1); else return (n*factorial (n1)); } {

} class Test { public static void main(String s[ ]) { Calculate obj=new Calculate( ); int n, f; try { n=Integer.parseInt (s[0]); f=obj.factorial (n); System.out.println (f); { catch (Exception e) { System.out.println (Error: + e); } } } 9. (a) Factory Classes: Factory classes provides an interface for creating families of related objects. They are useful when the decision of which class to use

must be done at run time. Generally they do not have constructors. Their objects are instantiated using methods 0s different class. Some examples of Factory classes are borders contained in Javax.swing.border package. The package consists of border interface, an abstract border class that serves as the parent 0s the eight border classes. Eg1: Border b1=BorderFactory.createLineBorder (color.red); Eg2: Border b2=BorderFactory.createBevelBorder (BevelBorder.RAISED); (b) Advantages of exception handling: Exceptions are run time errors. Exception handling is a method by which these run time errors can be caught and corrected or proper messages are displayed. Generally exceptions occur because the program is not being run in the way it is supposed to by the user. A user can enter some value which can create an exception. In such situations if the program abnormally terminates, the user will not be able to know the exact cause and the same error may be repeated again. By using try and catch blocks such error can be caught and properly dealt which will make using friendly programme. To handle exceptions in Java, we use try and catch block with an optional finally block. The syntax is as follows: try { statements; } catch (exception e) } exception handling codes } finally { final message } The statement which can raise exceptions are written in the try block. If there are no exception during execution the statement in the catch block are skipped. If during execution an exception occurs the control is transferred to the correspond catch block. The catch block contains exception handling codes. One try block can have multiple catch blocks, each catch block can handle one type of exception. The final block is optional which is exception in both the case of exception and no exception. (c) 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.

(ii) Flowlayout Manager: User interface components such as text fields radio buttons etc are arranged inside a frame by layout manager. There are different types of layout manager. Flowlayout manager is the default layout manager for Applet and Panel. This layout manager adds the component line by line. Components are added in one line until there is no more space in this line and then starts new line of components. The default alignment of this layout is centered. Other possible alignments are : left and right. As for example left alignment can be implemented by following statement :Setlayout (new Flowlayout (Flowlayout. LEFT)); By default there is no horizontal and vertical spacing between components but the horizontal and vertical spacing between components could be set. eg. The statemnt :Setlayout (new Flowlayout (Flowlayout. LEFT, 10,15)); Constructs a new flowlayout with left alignment and specified horizontal (10 unit) and vertical (15 unit) gaps between components (iii) 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( ) {.....} 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. 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. (iv) Interfaces in Java 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 { } (d) Applets are java programs which can be embedded in a web page. The <APPLET> tag is used to add a java compiled code in a web page. These programs can be executed by any java compatible web browser. They are executed on the clients machine when the web page is downloaded at the client machine. These programs do not have main( ) method. The java applet programs must inherit the class java.applet.Applet. Some of the commonly used methods in Applets are init( ), start( ), paint( ), run( ), update( ) and stop. Applets are window programs i.e., they are event driven. They make the web page more attractive and interactive. (e) Constructors: Constructors are methods of a class, used to initialize the instance variable. The name of the constructor should be same as that of the class. There can be more than one constructor in a class. Each class must contain at least one constructor. A constructor without any parameter is a default constructor. A constructor with parameters is called parameterised constructor. Constructors do not have any return type (not even void). They are called automatically when object of the class is created. They cannot be called explicitly like other member functions. class C1 { int x, y; C1( ) { //default constructor x=y=0; } C1 (int a) { //parameterised x=y=a; // constructor } C1 (int a, int b) { // parameterised constructor x=a; y=b; } }

Potrebbero piacerti anche