20

Click here to load reader

Java solution

Embed Size (px)

Citation preview

Page 1: Java solution

JAVA Solution

2010

Page 2: Java solution

Short Answer Question

1. What is thread• Is a sequential flow of control in a process

2. What is the use of finally keyword• Contains code to free the resource like file

handling, database connection etc

3. Which classes are used to read and write bytes to file• PrintStream (Write)

• Scanner (Read)

Page 3: Java solution

Short Answer Question…

4. Define interface

5. What is skeleton in RMI

ProductImpl_Skel (A skeleton class that is located on the server (needed prior to JDK 1.2))

Server side stub is refered to as a skeleton

6.Why is java called architecturally neutral language

Platform Independent

Page 4: Java solution

Short Answer Question…

7. Define package

8. Why is main function defined as static

To call by class name i.e. ClassName.main()

9. List the access specifier in java

Private, Public, Protected

10. Which function is used in servlet when data is using get method

doGet()

Page 5: Java solution

Long Answer Question

1. What is nested class? Explain with suitable example.• Class inside Class (Inner Class)

class A{

public int temp;public static class B{

public int b;

}public void test(){

B b1 = new B();b1.b = 10;System.out.println(b1.b);

}}class TestFirstInnerClass{

public static void main(String arg[]){

A a1 = new A();A.B binner = new A.B();binner.b = 20;System.out.println(binner.b);a1.test();

}}

Page 6: Java solution

Long Answer Question…

2. Java code to connect database

class DataBase{

public static void main(String agr[]) throws Exception{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection conn = DriverManager.getConnection("jdbc:odbc:Library10","","");String str="insert into titles values(1,'java','comuter',3434)";Statement stat = conn.createStatement();stat.executeUpdate(str);conn.close();

}}

Page 7: Java solution

Long Answer Question…

3. RMI and Its Steps

Creating Remote Interface

Implementing the Remote Interface

Creating main server class which register the remote object

Client consuming the server’s method

Page 8: Java solution

Long Answer Question…

5. AWT (Abstract Windows Toolkit).

Page 9: Java solution

CODE…public class AWTTest extends Frame{

public Label lbl1 = new Label("Enter a number:");

public Label lbl2 = new Label("Enter a number:");

public TextField fld1 = new TextField(10);

public TextField fld2 = new TextField(10);

public Button btn = new Button("OK");

public AWTTest()

{

setLayout(new BorderLayout());

add(lbl1);

add(fld1);

add(lbl2);

add(fld2);

add(btn);

setSize(250, 250);

setVisible(true);

}

public static void main(String arg[])

{

new AWTTest();

}

}

Page 10: Java solution

Long Answer Question…

6. Event Handling Example

Page 11: Java solution

public class EventTest extends JFrame implements ActionListener{

public JLabel lbl1 = new JLabel("Enter a number:");

public JLabel lbl2 = new JLabel("Enter a number:");

public JTextField fld1 = new JTextField(10);

public JTextField fld2 = new JTextField(10);

public JTextField fld3 = new JTextField(10);

public JButton btn = new JButton("OK");

public EventTest()

{

setLayout(new FlowLayout());

add(lbl1);

add(fld1);

add(lbl2);

add(fld2);

add(btn);

add(fld3);

btn.addActionListener(this);

setSize(250, 250);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e)

{

int a = Integer.parseInt(fld1.getText());

int b = Integer.parseInt(fld2.getText());

int c = a + b;

fld3.setText(String.valueOf(c));

}

public static void main(String arg[])

{

new EventTest();

}

}

Page 12: Java solution

Long Answer Question…

7. Java Beans• A reusable software component that can be manipulated visually in a

‘builder tool’. (from JavaBean Specification)

• The JavaBeans API provides a framework for defining reusable, embeddable, modular software components

• Software components written in Java

• Event Handling

Page 13: Java solution

Long Answer Question…

8. Applet A Java applet is an applet delivered to users in the form of Java bytecode

Java applets can be part of a web page and executed by the Java Virtual

Machine (JVM) in a process separate from the web browser, or run

in Sun's AppletViewer

Give Example

9. Final Keyword To make the constant

To stop the class from being inherited

Give Example

Page 14: Java solution

2011Short Answer Question

1. What is bytecode? Java bytecode is the form of instructions that the Java virtual

machine executes

2. What is parameter marshalling? Transfer of parameters (or marshalling) is done by the RMI

3. What is synchronization? Synchronization is a feature by which only one thread can access a resource in a

particular time of instance.

No other thread can interrupt for that resource

Page 15: Java solution

Short Answer Question…

4. What is an exception? An exception is an event, which occurs during the execution of a program, that

disrupts the normal flow of the program's instructions

Example :- NullPointerException, DivideByZeroException, dereference of a null

pointer, out-of-bounds array access, attempt to open a non-existent file for reading

5. How do you create a new package in java?

package <package_name>

6. Differentiate between AWT and SwingHeavyweight vs Lightweight

Page 16: Java solution

Short Answer Question…

8. How does java support runtime polymorphism?

Function Overriding

9. Why are character streams more convenient for handling in java?

Text-based I/O works with streams of human-readable characters, while data-based I/O works with streams of binary data

10. Interface and abstract class All methods of interface are by default abstract

Only abstract method of abstract class are abstract

Examples

Page 17: Java solution

Long Answer Questions

1. Event handling with addition and subtractionExample

2. Thread priority

Every Java thread has a priority that helps the operating system determine the

order in which threads are scheduled

ThreadObject.setPriority(num)

MIN (1) and MAX(10) NORMAL(5)

3. Socket Programs manage each client connection with a Socket object

Socket allows the server to interact with the client

Example

Page 18: Java solution

Long Answer Questions…

4. Prepared Statement

Page 19: Java solution

2012Short Answer Question

1. What is SQLExceptionIf the DriverManager cannot connect to the database

2. Are the java always platform independent?No if java has called the native file

3. States of a thread in javaNew, Blocked, Running, Wait, Runnable

4. Name the event generated by the buttonActionEvent, ItemEvent (by list items)

5. Use of import

To import the API of java

Page 20: Java solution

Short Answer Question…

6. rebind() methodRegister the server object in RMI

7. read() method

To read the buffer string