JAVA NOTES
JAVA NOTES
SECTION – A
- Java is considered platform independent due to its ability to compile code into an
intermediate form called bytecode. The bytecode is executed by the Java Virtual Machine
(JVM), which is available on various platforms. This allows the same Java program to run on
different operating systems without modification.
- `super` keyword is used to refer to the immediate parent class object. It is used to call
parent class methods and constructors.
- `this` keyword is used to refer to the current instance of the class. It is used to access
class variables and methods.
4. What are exceptions? Which keywords are used for exception handling?
- Exceptions are unexpected events that occur during the execution of a program,
disrupting its normal flow. Java uses `try`, `catch`, `finally`, `throw`, and `throws`
keywords for exception handling.
- JavaBeans are reusable software components that follow certain conventions. Two
conventions are:
SECTION – B
- `protected`: The member is accessible within its own package and by subclasses.
- `default` (no modifier): The member is accessible only within its own package.
- Generics enable types (classes and interfaces) to be parameters when defining classes,
interfaces, and methods. Example program:
Import java.util.ArrayList;
List.add(“World”);
System.out.println(s);
10. Write a program that catches negative exception (user defined exception). This is
caused when a negative number is entered by a user.
Import java.util.Scanner;
Super(message);
Try {
If (number < 0) {
System.out.println(e.getMessage());
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Public MouseEventDemo() {
addMouseListener(this);
addMouseMotionListener(this);
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
System.out.println(“Mouse Clicked”);
System.out.println(“Mouse Moved”);
New MouseEventDemo();
- The life cycle of a thread in Java includes the following states: `New`, `Runnable`,
`Blocked`, `Waiting`, `Timed Waiting`, and `Terminated`.
SECTION – C
13. (a) What is Constructor? Define ‘student’ class with a parameterized constructor
used to initialize two instance variables – vvcms.no and stud – name.
Int vvcmsNo;
String studName;
This.vvcmsNo = vvcmsNo;
This.studName = studName;
(b) State any two differences between string and string Buffer class.
- Dynamic Binding
Class Animal {
Void sound() {
Void sound() {
System.out.println(“Dog barks”);
a.sound();
- Abstract classes
Void sound() {
System.out.println(“Dog barks”);
a.sound();
15. (a) What are interfaces? Illustrate how interfaces can be used for implementing
multiple inheritance.
Interface A {
Void methodA();
Interface B {
Void methodB();
Class C implements A, B {
System.out.println(“Method A”);
System.out.println(“Method B”);
}
Public class Test {
Obj.methodA();
Obj.methodB();
- Method Overloading: Multiple methods in the same class with the same name but
different parameters.
- The life cycle of an applet includes the following methods: `init()`, `start()`, `stop()`,
and `destroy()`.
(Repeated questions)
(b) Write a program that creates two threads; one thread displays numbers from 1 to
10, and the other thread displays numbers from 10 to 1.
System.out.println(i);
}
}
System.out.println(i);
New AscendingThread().start();
New DescendingThread().start();
17. (a) Write Java code to create any four GUI components.
Import javax.swing.*;
Frame.add(button);
Frame.add(label);
Frame.add(textField);
Frame.add(checkBox);
Frame.setSize(300, 200);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
Java Collections Framework provides a set of classes and interfaces that implement
commonly reusable collection data structures. It includes interfaces like List, Set, and
Map, and classes like ArrayList, HashSet, HashMap, etc. It offers algorithms to manipulate
data, such as sorting and searching, and provides utility classes like Collections and
Arrays.
(b).Socket Programming
Server:
Import java.io.*;
Import java.net.*;
Try {
serverSocket.close();
} catch (Exception e) {
System.out.println€;
Client:
Import java.io.*;
Import java.net.*;
Try {
Dos.writeUTF(“Hello Server”);
Dos.flush();
Dos.close();
Socket.close();
} catch (Exception e) {
System.out.println€;