Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

JAVA NOTES

The document contains a question paper from Bangalore City University focusing on Java programming concepts, including platform independence, keywords, exceptions, event handling, visibility modifiers, packages, generics, and threading. It includes explanations, example programs, and comparisons of various Java features such as constructors, strings, interfaces, and applet lifecycle. Additionally, it covers Java collections and socket programming with example code for both server and client applications.

Uploaded by

DeezNutsss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA NOTES

The document contains a question paper from Bangalore City University focusing on Java programming concepts, including platform independence, keywords, exceptions, event handling, visibility modifiers, packages, generics, and threading. It includes explanations, example programs, and comparisons of various Java features such as constructors, strings, interfaces, and applet lifecycle. Additionally, it covers Java collections and socket programming with example code for both server and client applications.

Uploaded by

DeezNutsss
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Bangalore City University Questions paper with solution

SECTION – A

1. Java is platform independent language. Justify.

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

2. What is the use of ‘super’ and ‘this’ keywords?

- `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.

3. What is finalization in Java?

- Finalization is a process in Java that allows an object to clean up resources before it is


garbage collected. The `finalize` method can be overridden to release resources such as
closing files or freeing up network connections.

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.

5. What is an event? State any four event classes.

- An event is an occurrence or action recognized by software that may be handled by the


software. Four event classes in Java include `ActionEvent`, `MouseEvent`, `KeyEvent`,
and `WindowEvent`.
6. What are JavaBeans? State any two conventions that should be followed when they
are implemented.

- JavaBeans are reusable software components that follow certain conventions. Two
conventions are:

- The class should have a public no-argument constructor.

- Properties should be accessed using getter and setter methods.

SECTION – B

7. Explain the different visibility modifiers in Java.

- Java provides four visibility modifiers:

- `public`: The member is accessible from any other class.

- `protected`: The member is accessible within its own package and by subclasses.

- `default` (no modifier): The member is accessible only within its own package.

- `private`: The member is accessible only within its own class.

8. What is a package? How are packages created and accessed in Java?

- A package is a namespace that organizes a set of related classes and interfaces.


Packages are created using the `package` keyword and accessed using the `import`
statement.

9. Discuss Generics and illustrate with a program.

- Generics enable types (classes and interfaces) to be parameters when defining classes,
interfaces, and methods. Example program:

Import java.util.ArrayList;

Public class GenericExample {

Public static void main(String[] args) {

ArrayList<String> list = new ArrayList<>();


List.add(“Hello”);

List.add(“World”);

For (String s : list) {

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;

Class NegativeNumberException extends Exception {

Public NegativeNumberException(String message) {

Super(message);

Public class NegativeExceptionTest {

Public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter a number: “);

Int number = scanner.nextInt();

Try {

If (number < 0) {

Throw new NegativeNumberException(“Negative number entered”);

System.out.println(“You entered: “ + number);


} catch (NegativeNumberException e) {

System.out.println(e.getMessage());

11. Write a program that demonstrates any two mouse events.

Import java.awt.*;

Import java.awt.event.*;

Import javax.swing.*;

Public class MouseEventDemo extends JFrame implements MouseListener,


MouseMotionListener {

Public MouseEventDemo() {

addMouseListener(this);

addMouseMotionListener(this);

setSize(300, 200);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

Public void mouseClicked(MouseEvent e) {

System.out.println(“Mouse Clicked”);

Public void mouseMoved(MouseEvent e) {

System.out.println(“Mouse Moved”);

// Other mouse event methods need to be overridden


Public void mousePressed(MouseEvent e) {}

Public void mouseReleased(MouseEvent e) {}

Public void mouseEntered(MouseEvent e) {}

Public void mouseExited(MouseEvent e) {}

Public void mouseDragged(MouseEvent e) {}

Public static void main(String[] args) {

New MouseEventDemo();

12. Explain the life cycle of a thread with a neat diagram.

- The life cycle of a thread in Java includes the following states: `New`, `Runnable`,
`Blocked`, `Waiting`, `Timed Waiting`, and `Terminated`.

- A diagram would illustrate the transitions between these states.

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.

Public class Student {

Int vvcmsNo;

String studName;

Public Student(int vvcmsNo, String studName) {

This.vvcmsNo = vvcmsNo;

This.studName = studName;

(b) State any two differences between string and string Buffer class.

- `String` is immutable, while `StringBuffer` is mutable.


- `String` methods are synchronized, while `StringBuffer` methods are not.

14. Explain the following Java concepts with example programs.

- Dynamic Binding

Class Animal {

Void sound() {

System.out.println(“Animal makes a sound”);

Class Dog extends Animal {

Void sound() {

System.out.println(“Dog barks”);

Public class Test {

Public static void main(String[] args) {

Animal a = new Dog();

a.sound();

- Abstract classes

Abstract class Animal {

Abstract void sound();

Class Dog extends Animal {

Void sound() {
System.out.println(“Dog barks”);

Public class Test {

Public static void main(String[] args) {

Animal a = new Dog();

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 {

Public void methodA() {

System.out.println(“Method A”);

Public void methodB() {

System.out.println(“Method B”);

}
Public class Test {

Public static void main(String[] args) {

C obj = new C();

Obj.methodA();

Obj.methodB();

(b) Differentiate between method overloading and method overriding.

- Method Overloading: Multiple methods in the same class with the same name but
different parameters.

- Method Overriding: A subclass provides a specific implementation of a method that is


already defined in its superclass.

(include few more Points for 4- 8 marks)

16. (a) Explain the lifecycle of an applet.

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

Class AscendingThread extends Thread {

Public void run() {

For (int I = 1; I <= 10; i++) {

System.out.println(i);

Try { Thread.sleep(500); } catch (InterruptedException e) { }

}
}

Class DescendingThread extends Thread {

Public void run() {

For (int I = 10; I >= 1; i--) {

System.out.println(i);

Try { Thread.sleep(500); } catch (InterruptedException e) { }

Public class TestThreads {

Public static void main(String[] args) {

New AscendingThread().start();

New DescendingThread().start();

17. (a) Write Java code to create any four GUI components.

Import javax.swing.*;

Public class GUIComponents {

Public static void main(String[] args) {

JFrame frame = new JFrame(“GUI Components”);

JButton button = new JButton(“Click Me”);

JLabel label = new JLabel(“Label”);

JTextField textField = new JTextField(20);

JCheckBox checkBox = new JCheckBox(“Check Box”);


Frame.setLayout(new FlowLayout());

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);

18. Write short notes on:

(a) Java Collections

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

Socket programming in Java is used for communication between applications running on


different JVMs. Java provides the java.net package for socket programming. A Socket in
Java is an endpoint for communication between two machines. For example, a
ServerSocket is used on the server side to listen for client requests, and a Socket on the
client side is used to send requests to the server.

Here's a simple example of a server and client:

Server:

Import java.io.*;
Import java.net.*;

Public class Server {

Public static void main(String[] args) {

Try {

ServerSocket serverSocket = new ServerSocket(6666);

Socket socket = serverSocket.accept();

DataInputStream dis = new DataInputStream(socket.getInputStream());

String str = (String) dis.readUTF();

System.out.println(“Message from client: “ + str);

serverSocket.close();

} catch (Exception e) {

System.out.println€;

Client:

Import java.io.*;

Import java.net.*;

Public class Client {

Public static void main(String[] args) {

Try {

Socket socket = new Socket(“localhost”, 6666);

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

Dos.writeUTF(“Hello Server”);

Dos.flush();

Dos.close();
Socket.close();

} catch (Exception e) {

System.out.println€;

You might also like